1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-07-08 12:57:57 +02:00

Firefox: implement vAPI.storage via SQLite

This commit is contained in:
Deathamns 2014-11-27 20:45:54 +01:00
parent 98e5645ab5
commit 8297014700
7 changed files with 237 additions and 68 deletions

View File

@ -4,7 +4,7 @@
'use strict';
var
let
appName = 'ublock',
contentBaseURI = 'chrome://' + appName + '/content/js/',
listeners = {},
@ -25,7 +25,7 @@ var
addMessageListener('µBlock:broadcast', function(msg) {
for (var id in listeners) {
listeners[id](msg.data);
listeners[id](msg);
}
});

View File

@ -1,34 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<r:RDF xmlns:r="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://www.mozilla.org/2004/em-rdf#">
<r:Description about="urn:mozilla:install-manifest">
<id>{2b10c1c8-a11f-4bad-fe9c-1c11e82cac42}</id>
<version>0.7.0.11</version>
<name>µBlock</name>
<description>Finally, an efficient blocker. Easy on CPU and memory.</description>
<homepageURL>https://github.com/gorhill/uBlock</homepageURL>
<creator>Raymond Hill</creator>
<type>2</type>
<bootstrap>true</bootstrap>
<multiprocessCompatible>true</multiprocessCompatible>
<optionsType>3</optionsType>
<optionsURL>chrome://ublock/content/dashboard.html</optionsURL>
<r:Description about="urn:mozilla:install-manifest">
<id>{2b10c1c8-a11f-4bad-fe9c-1c11e82cac42}</id>
<version>0.7.0.11</version>
<name>µBlock</name>
<description>Finally, an efficient blocker. Easy on CPU and memory.</description>
<homepageURL>https://github.com/gorhill/uBlock</homepageURL>
<creator>Raymond Hill</creator>
<type>2</type>
<bootstrap>true</bootstrap>
<multiprocessCompatible>true</multiprocessCompatible>
<optionsType>3</optionsType>
<optionsURL>chrome://ublock/content/dashboard.html</optionsURL>
<!-- Firefox -->
<targetApplication>
<r:Description>
<id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</id>
<minVersion>24.0</minVersion>
<maxVersion>37.0</maxVersion>
</r:Description>
</targetApplication>
<!-- Firefox -->
<targetApplication>
<r:Description>
<id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</id>
<minVersion>24.0</minVersion>
<maxVersion>37.0</maxVersion>
</r:Description>
</targetApplication>
<!-- SeaMonkey -->
<targetApplication>
<r:Description>
<id>{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}</id>
<minVersion>2.21</minVersion>
<maxVersion>2.34</maxVersion>
</r:Description>
</targetApplication>
</r:Description>
<!-- SeaMonkey -->
<targetApplication>
<r:Description>
<id>{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}</id>
<minVersion>2.21</minVersion>
<maxVersion>2.34</maxVersion>
</r:Description>
</targetApplication>
</r:Description>
</r:RDF>

View File

@ -43,6 +43,180 @@ vAPI.firefox = true;
/******************************************************************************/
var SQLite = {
open: function() {
var path = Services.dirsvc.get('ProfD', Ci.nsIFile);
path.append('extension-data');
if (!path.exists()) {
path.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0774', 8));
}
if (!path.isDirectory()) {
throw Error('Should be a directory...');
}
path.append('uBlock.sqlite');
this.db = Services.storage.openDatabase(path);
this.db.executeSimpleSQL(
'CREATE TABLE IF NOT EXISTS settings' +
'(name TEXT PRIMARY KEY NOT NULL, value TEXT);'
);
},
close: function() {
this.run('VACUUM');
this.db.asyncClose();
},
run: function(query, values, callback) {
if (!this.db) {
this.open();
}
var result = {};
query = this.db.createAsyncStatement(query);
if (Array.isArray(values) && values.length) {
var i = values.length;
while (i--) {
query.bindByIndex(i, values[i]);
}
}
query.executeAsync({
handleResult: function(rows) {
if (!rows || typeof callback !== 'function') {
return;
}
var row;
while (row = rows.getNextRow()) {
// we assume that there will be two columns, since we're
// using it only for preferences
result[row.getResultByIndex(0)] = row.getResultByIndex(1);
}
},
handleCompletion: function(reason) {
if (typeof callback === 'function' && reason === 0) {
callback(result);
}
},
handleError: function(error) {
console.error('SQLite error ', error.result, error.message);
}
});
}
};
/******************************************************************************/
vAPI.storage = {
QUOTA_BYTES: 100 * 1024 * 1024,
sqlWhere: function(col, valNum) {
if (valNum > 0) {
valNum = Array(valNum + 1).join('?, ').slice(0, -2);
return ' WHERE ' + col + ' IN (' + valNum + ')';
}
return '';
},
get: function(details, callback) {
if (typeof callback !== 'function') {
return;
}
var values = [], defaults = false;
if (details !== null) {
if (Array.isArray(details)) {
values = details;
}
else if (typeof details === 'object') {
defaults = true;
values = Object.keys(details);
}
else {
values = [details.toString()];
}
}
SQLite.run(
'SELECT * FROM settings' + this.sqlWhere('name', values.length),
values,
function(result) {
var key;
for (key in result) {
result[key] = JSON.parse(result[key]);
}
if (defaults) {
for (key in details) {
if (!result[key]) {
result[key] = details[key];
}
}
}
callback(result);
}
);
},
set: function(details, callback) {
var key, values = [], questionmarks = [];
for (key in details) {
values.push(key);
values.push(JSON.stringify(details[key]));
questionmarks.push('?, ?');
}
if (!values.length) {
return;
}
SQLite.run(
'INSERT OR REPLACE INTO settings (name, value) SELECT ' +
questionmarks.join(' UNION SELECT '),
values,
callback
);
},
remove: function(keys, callback) {
if (typeof keys === 'string') {
keys = [keys];
}
SQLite.run(
'DELETE FROM settings' + this.sqlWhere('name', keys.length),
keys,
callback
);
},
clear: function(callback) {
SQLite.run('DELETE FROM settings', null, callback);
SQLite.run('VACUUM');
},
getBytesInUse: function(keys, callback) {
if (typeof callback !== 'function') {
return;
}
SQLite.run(
"SELECT 'size' AS size, SUM(LENGTH(value)) FROM settings" +
this.sqlWhere('name', Array.isArray(keys) ? keys.length : 0),
keys,
function(result) {
callback(result.size);
}
);
}
};
/******************************************************************************/
vAPI.messaging = {
gmm: Cc['@mozilla.org/globalmessagemanager;1'].getService(Ci.nsIMessageListenerManager),
frameScript: 'chrome://ublock/content/frameScript.js',
@ -133,8 +307,11 @@ vAPI.messaging.setup = function(defaultHandler) {
/******************************************************************************/
vAPI.messaging.broadcast = function(msg) {
this.gmm.broadcastAsyncMessage(vAPI.app.name + ':broadcast', msg);
vAPI.messaging.broadcast = function(message) {
this.gmm.broadcastAsyncMessage(
vAPI.app.name + ':broadcast',
JSON.stringify({broadcast: true, msg: message})
);
};
/******************************************************************************/
@ -148,8 +325,9 @@ vAPI.lastError = function() {
// clean up when the extension is disabled
window.addEventListener('unload', function() {
SQLite.close();
vAPI.messaging.gmm.removeMessageListener(
app.name + ':background',
vAPI.app.name + ':background',
vAPI.messaging.postMessage
);
vAPI.messaging.gmm.removeDelayedFrameScript(vAPI.messaging.frameScript);

View File

@ -93,4 +93,3 @@ setScriptDirection(navigator.language);
})();
/******************************************************************************/

View File

@ -2,21 +2,21 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>DefaultValue</key>
<false/>
<key>FalseValue</key>
<false/>
<key>Key</key>
<string>open_prefs</string>
<key>Secure</key>
<false/>
<key>Title</key>
<string>Click to see the Preferences</string>
<key>TrueValue</key>
<true/>
<key>Type</key>
<string>CheckBox</string>
</dict>
<dict>
<key>DefaultValue</key>
<false/>
<key>FalseValue</key>
<false/>
<key>Key</key>
<string>open_prefs</string>
<key>Secure</key>
<false/>
<key>Title</key>
<string>Click to see the Preferences</string>
<key>TrueValue</key>
<true/>
<key>Type</key>
<string>CheckBox</string>
</dict>
</array>
</plist>

View File

@ -119,7 +119,7 @@ body[dir=rtl] #externalListsDiv {
font-size: smaller;
width: 48em;
height: 8em;
white-space: nowrap;
white-space: pre;
}
body #busyOverlay {
position: fixed;

View File

@ -3,42 +3,34 @@
import os
import json
import sys
from shutil import rmtree as rmt
from shutil import rmtree
from collections import OrderedDict
if not sys.argv[1]:
raise SystemExit('Build dir missing.')
osp = os.path
pj = osp.join
def rmtree(path):
if osp.exists(path):
rmt(path)
def mkdirs(path):
try:
os.makedirs(path)
finally:
return osp.exists(path)
return os.path.exists(path)
build_dir = osp.abspath(sys.argv[1])
source_locale_dir = pj(build_dir, '_locales')
target_locale_dir = pj(build_dir, 'locale')
build_dir = os.path.abspath(sys.argv[1])
source_locale_dir = os.path.join(build_dir, '_locales')
target_locale_dir = os.path.join(build_dir, 'locale')
for alpha2 in os.listdir(source_locale_dir):
locale_path = pj(source_locale_dir, alpha2, 'messages.json')
locale_path = os.path.join(source_locale_dir, alpha2, 'messages.json')
with open(locale_path, encoding='utf-8') as f:
string_data = json.load(f, object_pairs_hook=OrderedDict)
alpha2 = alpha2.replace('_', '-')
mkdirs(pj(target_locale_dir, alpha2))
mkdirs(os.path.join(target_locale_dir, alpha2))
locale_path = pj(target_locale_dir, alpha2, 'messages.properties')
locale_path = os.path.join(target_locale_dir, alpha2, 'messages.properties')
with open(locale_path, 'wt', encoding='utf-8', newline='\n') as f:
for string_name in string_data:
f.write(string_name)