[Pkg-mozext-commits] [greasemonkey] 21/43: Fix script storage by passing messages to chrome.

David Prévot taffit at moszumanska.debian.org
Sun Feb 22 21:56:11 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository greasemonkey.

commit 086e2e35f46afbf35f14ef436e0652ff9fcfef74
Author: Anthony Lieuallen <arantius at gmail.com>
Date:   Fri Nov 7 12:30:48 2014 -0500

    Fix script storage by passing messages to chrome.
    
    TODO: listValues() returns inaccessible object.
---
 components/greasemonkey.js |  72 +++++++++++++++---
 content/config.js          |  21 -----
 modules/contentObserver.js |  27 ++++---
 modules/miscapis.js        | 185 +--------------------------------------------
 modules/sandbox.js         |   5 +-
 modules/storageBack.js     | 156 ++++++++++++++++++++++++++++++++++++++
 modules/storageFront.js    |  94 +++++++++++++++++++++++
 7 files changed, 333 insertions(+), 227 deletions(-)

diff --git a/components/greasemonkey.js b/components/greasemonkey.js
index a2a0620..436209a 100644
--- a/components/greasemonkey.js
+++ b/components/greasemonkey.js
@@ -11,8 +11,10 @@ var Cu = Components.utils;
 Cu.import("resource://greasemonkey/ipcscript.js");
 Cu.import("resource://greasemonkey/menucommand.js");
 Cu.import("resource://greasemonkey/prefmanager.js");
+Cu.import("resource://greasemonkey/storageBack.js");
 Cu.import("resource://greasemonkey/sync.js");
 Cu.import("resource://greasemonkey/util.js");
+Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 
@@ -34,6 +36,10 @@ function isTempScript(uri) {
   return gTmpDir.contains(file, true);
 }
 
+function shutdown(aService) {
+  aService.closeAllScriptValStores();
+}
+
 function startup(aService) {
   if (gStartupHasRun) return;
   gStartupHasRun = true;
@@ -47,9 +53,23 @@ function startup(aService) {
   var messageManager = Cc["@mozilla.org/globalmessagemanager;1"]
       .getService(Ci.nsIMessageListenerManager);
 
-  messageManager.addMessageListener('greasemonkey:scripts-for-url',
-      aService.getScriptsForUrl.bind(aService));
-  messageManager.loadFrameScript("chrome://greasemonkey/content/framescript.js", true);
+  messageManager.addMessageListener(
+      'greasemonkey:scripts-for-url', aService.getScriptsForUrl.bind(aService));
+
+  var scriptValHandler = aService.handleScriptValMsg.bind(aService);
+  messageManager.addMessageListener(
+    'greasemonkey:scriptVal-delete', scriptValHandler);
+  messageManager.addMessageListener(
+    'greasemonkey:scriptVal-get', scriptValHandler);
+  messageManager.addMessageListener(
+    'greasemonkey:scriptVal-list', scriptValHandler);
+  messageManager.addMessageListener(
+    'greasemonkey:scriptVal-set', scriptValHandler);
+
+  messageManager.loadFrameScript(
+      "chrome://greasemonkey/content/framescript.js", true);
+
+  Services.obs.addObserver(aService, 'quit-application', false);
 
   // Import this once, early, so that enqueued deletes can happen.
   Cu.import("resource://greasemonkey/util/enqueueRemoveFile.js");
@@ -59,6 +79,7 @@ function startup(aService) {
 
 function service() {
   this.filename = Components.stack.filename;
+  this.scriptValStores = {};
   this.wrappedJSObject = this;
 }
 
@@ -68,11 +89,7 @@ service.prototype.classDescription = DESCRIPTION;
 service.prototype.classID = CLASSID;
 service.prototype.contractID = CONTRACTID;
 service.prototype._xpcom_categories = [{
-      category: "app-startup",
-      entry: DESCRIPTION,
-      value: CONTRACTID,
-      service: true
-    },{
+      // TODO: Fix.  Move to frame script?!
       category: "content-policy",
       entry: CONTRACTID,
       value: CONTRACTID,
@@ -130,10 +147,12 @@ service.prototype.shouldProcess = function(ct, cl, org, ctx, mt, ext) {
 
 service.prototype.observe = function(aSubject, aTopic, aData) {
   switch (aTopic) {
-    case 'app-startup':
     case 'profile-after-change':
       startup(this);
       break;
+    case 'quit-application':
+      shutdown(this);
+      break;
   }
 };
 
@@ -152,6 +171,13 @@ service.prototype.__defineGetter__('config', function() {
   return this._config;
 });
 
+service.prototype.closeAllScriptValStores = function() {
+  for (var scriptId in this.scriptValStores) {
+    var scriptValStore = this.scriptValStores[scriptId];
+    scriptValStore.close();
+  }
+};
+
 service.prototype.getScriptsForUrl = function(aMessage) {
   var url = aMessage.data.url;
   var when = aMessage.data.when;
@@ -181,6 +207,34 @@ service.prototype.getScriptsForUrl = function(aMessage) {
   return scripts;
 };
 
+service.prototype.getStoreByScriptId = function(aScriptId) {
+  if ('undefined' == typeof this.scriptValStores[aScriptId]) {
+    var script = this.config.getScriptById(aScriptId);
+    this.scriptValStores[aScriptId] = new GM_ScriptStorageBack(script);
+  }
+  return this.scriptValStores[aScriptId];
+};
+
+service.prototype.handleScriptValMsg = function(aMessage) {
+  var d = aMessage.data;
+  var scriptStore = this.getStoreByScriptId(d.scriptId);
+  switch (aMessage.name) {
+  case 'greasemonkey:scriptVal-delete':
+    return scriptStore.deleteValue(d.name);
+  case 'greasemonkey:scriptVal-get':
+    return scriptStore.getValue(d.name);
+  case 'greasemonkey:scriptVal-list':
+    return scriptStore.listValues();
+  case 'greasemonkey:scriptVal-set':
+    return scriptStore.setValue(d.name, d.val);
+  default:
+    dump(
+        'Greasemonkey service handleScriptValMsg(): '
+        + 'Unknown message name "' + aMessage.name + '".\n');
+    break;
+  }
+};
+
 service.prototype.ignoreNextScript = function() {
   this._ignoreNextScript = true;
 };
diff --git a/content/config.js b/content/config.js
index cfdf93e..3a8c554 100644
--- a/content/config.js
+++ b/content/config.js
@@ -271,15 +271,6 @@ Config.prototype._updateVersion = function() {
     var oldVersion = GM_prefRoot.getValue("version");
     var newVersion = addon.version;
 
-    var versionChecker = Components
-        .classes["@mozilla.org/xpcom/version-comparator;1"]
-        .getService(Components.interfaces.nsIVersionComparator);
-    if (oldVersion != '0.0'
-      && (versionChecker.compare(oldVersion, '1.13') < 0)
-    ) {
-      this._migrateScriptValsToStorage();
-    }
-
     // Update the stored current version so we don't do this work again.
     GM_prefRoot.setValue("version", newVersion);
 
@@ -298,15 +289,3 @@ Config.prototype._updateVersion = function() {
     }
   }));
 };
-
-Config.prototype._migrateScriptValsToStorage = function() {
-  for (var i = 0, script; script = this._scripts[i]; i++) {
-    var prefsVals = new GM_ScriptStoragePrefs(script);
-    var storageVals = new GM_ScriptStorage(script);
-    var names = prefsVals.listValues();
-    for (var j = 0, name = null; name = names[j]; j++) {
-      storageVals.setValue(name, prefsVals.getValue(name));
-      prefsVals.deleteValue(name);
-    }
-  };
-};
diff --git a/modules/contentObserver.js b/modules/contentObserver.js
index 36d5849..ab32e88 100644
--- a/modules/contentObserver.js
+++ b/modules/contentObserver.js
@@ -29,7 +29,7 @@ function ScriptRunner(aWindow, aUrl) {
   this.url = aUrl;
 }
 
-ScriptRunner.prototype.injectScripts = function(aScripts) {
+ScriptRunner.prototype.injectScripts = function(aScripts, aMessageManager) {
   try {
     this.window.QueryInterface(Ci.nsIDOMChromeWindow);
     // Never ever inject scripts into a chrome context window.
@@ -42,7 +42,7 @@ ScriptRunner.prototype.injectScripts = function(aScripts) {
 
   for (var i = 0, script = null; script = aScripts[i]; i++) {
     if (script.noframes && !winIsTop) continue;
-    var sandbox = createSandbox(script, this);
+    var sandbox = createSandbox(script, this, aMessageManager);
     runScriptInSandbox(script, sandbox);
   }
 };
@@ -153,14 +153,13 @@ ContentObserver.prototype.pagehide = function(aEvent) {
   // for this window.
   if (!gScriptRunners[windowId].menuCommands.length) return;
 
+  var mm = GM_util.messageManagerForWin(contentWin);
   if (aEvent.persisted) {
-    var mm = GM_util.messageManagerForWin(contentWin);
     mm.sendAsyncMessage("greasemonkey:toggle-menu-commands", {
       frozen: true,
       windowId: windowId
     });
   } else {
-    var mm = GM_util.messageManagerForWin(contentWin);
     mm.sendAsyncMessage("greasemonkey:clear-menu-commands", {
       windowId: windowId
     });
@@ -186,10 +185,13 @@ ContentObserver.prototype.pageshow = function(aEvent) {
 
 ContentObserver.prototype.runDelayedScript = function(aMessage) {
   var windowId = aMessage.data.windowId;
-  if (!gScriptRunners[windowId]) return;
+  var scriptRunner = gScriptRunners[windowId];
+  if (!scriptRunner) return;
 
   var script = this.createScriptFromObject(aMessage.data.script);
-  gScriptRunners[windowId].injectScripts([script]);
+  scriptRunner.injectScripts(
+      [script],
+      GM_util.messageManagerForWin(scriptRunner.window));
 };
 
 
@@ -219,14 +221,16 @@ ContentObserver.prototype.runScripts = function(aRunWhen, aContentWin) {
 
   if (!GM_util.isGreasemonkeyable(url)) return;
 
+  var scriptRunner = gScriptRunners[windowId];
   var windowId = GM_util.windowId(aContentWin);
-  if (!gScriptRunners[windowId]) {
-    gScriptRunners[windowId] = new ScriptRunner(aContentWin, url);
-  } else if (gScriptRunners[windowId].window !== aContentWin) {
+  if (!scriptRunner) {
+    scriptRunner = new ScriptRunner(aContentWin, url);
+    gScriptRunners[windowId] = scriptRunner;
+  } else if (scriptRunner.window !== aContentWin) {
     // Sanity check, shouldn't be necessary.
     // TODO: remove
     dump("Script runner window changed for " + url + " at " + aRunWhen + "\n");
-    gScriptRunners[windowId].window = aContentWin;
+    scriptRunner.window = aContentWin;
   }
 
   var mm = GM_util.messageManagerForWin(aContentWin);
@@ -239,7 +243,8 @@ ContentObserver.prototype.runScripts = function(aRunWhen, aContentWin) {
   if (!response || !response[0]) return;
 
   var scripts = response[0].map(this.createScriptFromObject);
-  gScriptRunners[windowId].injectScripts(scripts);
+  scriptRunner.injectScripts(
+      scripts, GM_util.messageManagerForWin(aContentWin));
 };
 
 // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
diff --git a/modules/miscapis.js b/modules/miscapis.js
index 63b634a..8e07851 100644
--- a/modules/miscapis.js
+++ b/modules/miscapis.js
@@ -9,190 +9,7 @@ Cu.import("resource://greasemonkey/util.js");
 
 var EXPORTED_SYMBOLS = [
     'GM_addStyle', 'GM_console', 'GM_openInTab', 'GM_Resources',
-    'GM_ScriptLogger', 'GM_ScriptStorage', 'GM_ScriptStoragePrefs'];
-
-
-function GM_ScriptStorage(script) {
-  this._db = null;
-  this._script = script;
-  this.stringBundle = Components
-    .classes["@mozilla.org/intl/stringbundle;1"]
-    .getService(Components.interfaces.nsIStringBundleService)
-    .createBundle("chrome://greasemonkey/locale/greasemonkey.properties");
-}
-
-
-GM_ScriptStorage.prototype.__defineGetter__('dbFile',
-function GM_ScriptStorage_getDbFile() {
-  var file = GM_util.scriptDir();
-  file.append(this._script.baseDirName + '.db');
-  return file;
-});
-
-
-GM_ScriptStorage.prototype.__defineGetter__('db',
-function GM_ScriptStorage_getDb() {
-  if (null == this._db) {
-    this._db = Services.storage.openDatabase(this.dbFile);
-
-    // The auto_vacuum pragma has to be set before the table is created.
-    this._db.executeSimpleSQL('PRAGMA auto_vacuum = INCREMENTAL;');
-    this._db.executeSimpleSQL('PRAGMA incremental_vacuum(10);');
-    this._db.executeSimpleSQL('PRAGMA journal_mode = WAL;');
-    this._db.executeSimpleSQL('PRAGMA wal_autocheckpoint = 10;');
-
-    this._db.executeSimpleSQL(
-        'CREATE TABLE IF NOT EXISTS scriptvals ('
-        + 'name TEXT PRIMARY KEY NOT NULL, '
-        + 'value TEXT '
-        + ')'
-        );
-
-    // Run vacuum once manually to switch to the correct auto_vacuum mode for
-    // databases that were created with incorrect auto_vacuum. See #1879.
-    this._db.executeSimpleSQL('VACUUM;');
-  }
-  return this._db;
-});
-
-
-GM_ScriptStorage.prototype.setValue = function(name, val) {
-  if (2 !== arguments.length) {
-    throw new Error(this.stringBundle.GetStringFromName('error.args.setValue'));
-  }
-
-  var stmt = this.db.createStatement(
-      'INSERT OR REPLACE INTO scriptvals (name, value) VALUES (:name, :value)');
-  try {
-    stmt.params.name = name;
-    stmt.params.value = JSON.stringify(val);
-    stmt.execute();
-  } finally {
-    stmt.reset();
-  }
-
-  this._script.changed('val-set', name);
-};
-
-
-GM_ScriptStorage.prototype.getValue = function(name, defVal) {
-  var value = null;
-  var stmt = this.db.createStatement(
-      'SELECT value FROM scriptvals WHERE name = :name');
-  try {
-    stmt.params.name = name;
-    while (stmt.step()) {
-      value = stmt.row.value;
-    }
-  } catch (e) {
-    dump('getValue err: ' + uneval(e) + '\n');
-  } finally {
-    stmt.reset();
-  }
-
-  if (value == null) return defVal;
-  try {
-    return JSON.parse(value);
-  } catch (e) {
-    dump('JSON parse error? ' + uneval(e) + '\n');
-    return defVal;
-  }
-};
-
-
-GM_ScriptStorage.prototype.deleteValue = function(name) {
-  var stmt = this.db.createStatement(
-      'DELETE FROM scriptvals WHERE name = :name');
-  try {
-    stmt.params.name = name;
-    stmt.execute();
-  } finally {
-    stmt.reset();
-  }
-
-  this._script.changed('val-del', name);
-};
-
-
-GM_ScriptStorage.prototype.listValues = function() {
-  var valueNames = [];
-
-  var stmt = this.db.createStatement('SELECT name FROM scriptvals');
-  try {
-    while (stmt.executeStep()) {
-      valueNames.push(stmt.row.name);
-    }
-  } finally {
-    stmt.reset();
-  }
-
-  // See #1637.
-  var vals = Array.prototype.slice.call(valueNames);
-  vals.__exposedProps__ = {'length': 'r'};
-  return vals;
-};
-
-
-GM_ScriptStorage.prototype.getStats = function() {
-  var stats = {
-      count: undefined,
-      size: undefined,
-      };
-  var stmt = this.db.createStatement(
-      'SELECT COUNT(0) AS count, SUM(LENGTH(value)) AS size FROM scriptvals');
-  try {
-    while (stmt.step()) {
-      stats.count = stmt.row.count;
-      stats.size = stmt.row.size || 0;
-    }
-  } catch (e) {
-    dump('getStats err: ' + uneval(e) + '\n');
-  } finally {
-    stmt.reset();
-  }
-
-  return stats;
-};
-
-
-// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
-
-
-// TODO: Remove this when we're confident enough users have updated from
-// prefs-base to Storage based script values.
-function GM_ScriptStoragePrefs(script) {
-  this._script = script;
-  this.prefMan = new GM_PrefManager(script.prefroot);
-  this.stringBundle = Components
-    .classes["@mozilla.org/intl/stringbundle;1"]
-    .getService(Components.interfaces.nsIStringBundleService)
-    .createBundle("chrome://greasemonkey/locale/greasemonkey.properties");
-}
-
-GM_ScriptStoragePrefs.prototype.setValue = function(name, val) {
-  if (2 !== arguments.length) {
-    throw new Error(this.stringBundle.GetStringFromName('error.args.setValue'));
-  }
-
-  this.prefMan.setValue(name, val);
-  this._script.changed('val-set', name);
-};
-
-GM_ScriptStoragePrefs.prototype.getValue = function(name, defVal) {
-  return this.prefMan.getValue(name, defVal);
-};
-
-GM_ScriptStoragePrefs.prototype.deleteValue = function(name) {
-  return this.prefMan.remove(name);
-  this._script.changed('val-del', name);
-};
-
-GM_ScriptStoragePrefs.prototype.listValues = function() {
-  // See #1637.
-  var vals = Array.prototype.slice.call(this.prefMan.listValues());
-  vals.__exposedProps__ = {'length': 'r'};
-  return vals;
-};
+    'GM_ScriptLogger'];
 
 // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
 
diff --git a/modules/sandbox.js b/modules/sandbox.js
index 8e6440a..c932a37 100644
--- a/modules/sandbox.js
+++ b/modules/sandbox.js
@@ -7,6 +7,7 @@ var Cc = Components.classes;
 Cu.import('resource://greasemonkey/GM_setClipboard.js');
 Cu.import("resource://greasemonkey/menucommand.js");
 Cu.import("resource://greasemonkey/miscapis.js");
+Cu.import("resource://greasemonkey/storageFront.js");
 Cu.import("resource://greasemonkey/util.js");
 Cu.import("resource://greasemonkey/xmlhttprequester.js");
 
@@ -24,7 +25,7 @@ function alert(msg) {
       .alert(null, "Greasemonkey alert", msg);
 }
 
-function createSandbox(aScript, aScriptRunner) {
+function createSandbox(aScript, aScriptRunner, aMessageManager) {
   var contentWin = aScriptRunner.window;
   var url = aScriptRunner.url;
 
@@ -86,7 +87,7 @@ function createSandbox(aScript, aScriptRunner) {
    sandbox.GM_registerMenuCommand = gmrmc;
   }
 
-  var scriptStorage = new GM_ScriptStorage(aScript);
+  var scriptStorage = new GM_ScriptStorageFront(aScript, aMessageManager);
   if (GM_util.inArray(aScript.grants, 'GM_deleteValue')) {
     sandbox.GM_deleteValue = GM_util.hitch(scriptStorage, 'deleteValue');
   }
diff --git a/modules/storageBack.js b/modules/storageBack.js
new file mode 100644
index 0000000..729b335
--- /dev/null
+++ b/modules/storageBack.js
@@ -0,0 +1,156 @@
+// The "back end" implementation of GM_ScriptStorageBack().  This is loaded into
+// the component scope and is capable of accessing the file based SQL store.
+
+var Cu = Components.utils;
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+Cu.import("resource://greasemonkey/third-party/getChromeWinForContentWin.js");
+Cu.import('resource://greasemonkey/prefmanager.js');
+Cu.import("resource://greasemonkey/util.js");
+
+
+var EXPORTED_SYMBOLS = ['GM_ScriptStorageBack'];
+
+// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
+
+function GM_ScriptStorageBack(script) {
+  this._db = null;
+  this._script = script;
+  this.stringBundle = Components
+    .classes["@mozilla.org/intl/stringbundle;1"]
+    .getService(Components.interfaces.nsIStringBundleService)
+    .createBundle("chrome://greasemonkey/locale/greasemonkey.properties");
+}
+
+
+GM_ScriptStorageBack.prototype.__defineGetter__('dbFile',
+function GM_ScriptStorageBack_getDbFile() {
+  var file = GM_util.scriptDir();
+  file.append(this._script.baseDirName + '.db');
+  return file;
+});
+
+
+GM_ScriptStorageBack.prototype.__defineGetter__('db',
+function GM_ScriptStorageBack_getDb() {
+  if (null == this._db) {
+    this._db = Services.storage.openDatabase(this.dbFile);
+
+    // The auto_vacuum pragma has to be set before the table is created.
+    this._db.executeSimpleSQL('PRAGMA auto_vacuum = INCREMENTAL;');
+    this._db.executeSimpleSQL('PRAGMA incremental_vacuum(10);');
+    this._db.executeSimpleSQL('PRAGMA journal_mode = WAL;');
+    this._db.executeSimpleSQL('PRAGMA wal_autocheckpoint = 10;');
+
+    this._db.executeSimpleSQL(
+        'CREATE TABLE IF NOT EXISTS scriptvals ('
+        + 'name TEXT PRIMARY KEY NOT NULL, '
+        + 'value TEXT '
+        + ')'
+        );
+
+    // Run vacuum once manually to switch to the correct auto_vacuum mode for
+    // databases that were created with incorrect auto_vacuum. See #1879.
+    this._db.executeSimpleSQL('VACUUM;');
+  }
+  return this._db;
+});
+
+
+GM_ScriptStorageBack.prototype.close = function() {
+  this._db.close();
+};
+
+
+GM_ScriptStorageBack.prototype.setValue = function(name, val) {
+  if (2 !== arguments.length) {
+    throw new Error(this.stringBundle.GetStringFromName('error.args.setValue'));
+  }
+
+  var stmt = this.db.createStatement(
+      'INSERT OR REPLACE INTO scriptvals (name, value) VALUES (:name, :value)');
+  try {
+    stmt.params.name = name;
+    stmt.params.value = JSON.stringify(val);
+    stmt.execute();
+  } finally {
+    stmt.reset();
+  }
+
+  this._script.changed('val-set', name);
+};
+
+
+GM_ScriptStorageBack.prototype.getValue = function(name) {
+  var value = null;
+  var stmt = this.db.createStatement(
+      'SELECT value FROM scriptvals WHERE name = :name');
+  try {
+    stmt.params.name = name;
+    while (stmt.step()) {
+      value = stmt.row.value;
+    }
+  } catch (e) {
+    dump('getValue err: ' + uneval(e) + '\n');
+  } finally {
+    stmt.reset();
+  }
+
+  return value;
+};
+
+
+GM_ScriptStorageBack.prototype.deleteValue = function(name) {
+  var stmt = this.db.createStatement(
+      'DELETE FROM scriptvals WHERE name = :name');
+  try {
+    stmt.params.name = name;
+    stmt.execute();
+  } finally {
+    stmt.reset();
+  }
+
+  this._script.changed('val-del', name);
+};
+
+
+GM_ScriptStorageBack.prototype.listValues = function() {
+  var valueNames = [];
+
+  var stmt = this.db.createStatement('SELECT name FROM scriptvals');
+  try {
+    while (stmt.executeStep()) {
+      valueNames.push(stmt.row.name);
+    }
+  } finally {
+    stmt.reset();
+  }
+
+  // See #1637.
+  var vals = Array.prototype.slice.call(valueNames);
+  vals.__exposedProps__ = {'length': 'r'};
+  return vals;
+};
+
+
+GM_ScriptStorageBack.prototype.getStats = function() {
+  var stats = {
+      count: undefined,
+      size: undefined,
+      };
+  var stmt = this.db.createStatement(
+      'SELECT COUNT(0) AS count, SUM(LENGTH(value)) AS size FROM scriptvals');
+  try {
+    while (stmt.step()) {
+      stats.count = stmt.row.count;
+      stats.size = stmt.row.size || 0;
+    }
+  } catch (e) {
+    dump('getStats err: ' + uneval(e) + '\n');
+  } finally {
+    stmt.reset();
+  }
+
+  return stats;
+};
diff --git a/modules/storageFront.js b/modules/storageFront.js
new file mode 100644
index 0000000..03fd414
--- /dev/null
+++ b/modules/storageFront.js
@@ -0,0 +1,94 @@
+// The "front end" implementation of GM_ScriptStorageFront().  This is loaded into
+// the content process scope and simply delegates to the back end..
+
+var Cu = Components.utils;
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+Cu.import("resource://greasemonkey/third-party/getChromeWinForContentWin.js");
+Cu.import('resource://greasemonkey/prefmanager.js');
+Cu.import("resource://greasemonkey/util.js");
+
+
+var EXPORTED_SYMBOLS = ['GM_ScriptStorageFront'];
+
+// \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ // \\ //
+
+function GM_ScriptStorageFront(aScript, aMessageManager) {
+  this._db = null;
+  this._messageManager = aMessageManager;
+  this._script = aScript;
+  this.stringBundle = Components
+    .classes["@mozilla.org/intl/stringbundle;1"]
+    .getService(Components.interfaces.nsIStringBundleService)
+    .createBundle("chrome://greasemonkey/locale/greasemonkey.properties");
+}
+
+
+GM_ScriptStorageFront.prototype.__defineGetter__('dbFile',
+function GM_ScriptStorageFront_getDbFile() {
+  throw 'Script storage front end has no DB file.';
+});
+
+
+GM_ScriptStorageFront.prototype.__defineGetter__('db',
+function GM_ScriptStorageFront_getDb() {
+  throw 'Script storage front end has no DB connection.';
+});
+
+
+GM_ScriptStorageFront.prototype.close = function() {
+  throw 'Script storage front end has no DB connection.';
+};
+
+
+GM_ScriptStorageFront.prototype.setValue = function(name, val) {
+  if (2 !== arguments.length) {
+    throw new Error(this.stringBundle.GetStringFromName('error.args.setValue'));
+  }
+
+  this._messageManager.sendSyncMessage(
+      'greasemonkey:scriptVal-set',
+      {scriptId: this._script.id, name: name, val: val});
+};
+
+
+GM_ScriptStorageFront.prototype.getValue = function(name, defVal) {
+  var value = this._messageManager.sendSyncMessage(
+      'greasemonkey:scriptVal-get',
+      {scriptId: this._script.id, name: name});
+  value = value.length && value[0];
+
+  if (value === undefined || value === null) return defVal;
+
+  try {
+    return JSON.parse(value);
+  } catch (e) {
+    dump('JSON parse error? ' + uneval(e) + '\n');
+    return defVal;
+  }
+};
+
+
+GM_ScriptStorageFront.prototype.deleteValue = function(name) {
+  this._messageManager.sendSyncMessage(
+      'greasemonkey:scriptVal-delete',
+      {scriptId: this._script.id, name: name});
+};
+
+
+GM_ScriptStorageFront.prototype.listValues = function() {
+  var value = this._messageManager.sendSyncMessage(
+      'greasemonkey:scriptVal-list',
+      {scriptId: this._script.id});
+  value = value.length && value[0] || [];
+  // See #1637.
+  var vals = Array.prototype.slice.call(value);
+  vals.__exposedProps__ = {'length': 'r'};
+  return vals;
+};
+
+
+GM_ScriptStorageFront.prototype.getStats = function() {
+  throw 'Script storage front end does not expose stats.';
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/greasemonkey.git



More information about the Pkg-mozext-commits mailing list