add engine registration service; use global id service in service.js
authorDan Mills <thunder@mozilla.com>
Mon, 14 Apr 2008 18:53:35 -0700
changeset 44428 d26b6158de4527672576a3b62d47218f694d27b2
parent 44427 c8d5c3e546099e7649a276c943af6b2dec1a6ae2
child 44429 3551897234ddf559cb47a771bd9cced01706ddc2
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
add engine registration service; use global id service in service.js
services/sync/modules/engines.js
services/sync/modules/service.js
--- a/services/sync/modules/engines.js
+++ b/services/sync/modules/engines.js
@@ -29,17 +29,18 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
-const EXPORTED_SYMBOLS = ['Engine', 'BookmarksEngine', 'HistoryEngine', 'CookieEngine'];
+const EXPORTED_SYMBOLS = ['Engines', 'Engine',
+                          'BookmarksEngine', 'HistoryEngine', 'CookieEngine'];
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cr = Components.results;
 const Cu = Components.utils;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://weave/log4moz.js");
@@ -49,19 +50,46 @@ Cu.import("resource://weave/crypto.js");
 Cu.import("resource://weave/dav.js");
 Cu.import("resource://weave/identity.js");
 Cu.import("resource://weave/stores.js");
 Cu.import("resource://weave/syncCores.js");
 Cu.import("resource://weave/async.js");
 
 Function.prototype.async = Async.sugar;
 
-function Engine(davCollection, pbeId) {
-  //this._init(davCollection, pbeId);
+// Singleton service, holds registered engines
+
+Utils.lazy(this, 'Engines', EngineManagerSvc);
+
+function EngineManagerSvc() {
+  this._engines = {};
 }
+EngineManagerSvc.prototype = {
+  get: function EngMgr_get(name) {
+    return this._engines[name]
+  },
+  getAll: function EngMgr_getAll() {
+    let ret = [];
+    for (key in this._engines) {
+      ret.push(this._engines[key]);
+    }
+    return ret;
+  },
+  register: function EngMgr_register(engine) {
+    this._engines[engine.name] = engine;
+  },
+  unregister: function EngMgr_unregister(val) {
+    let name = val;
+    if (val instanceof Engine)
+      name = val.name;
+    delete this._engines[name];
+  }
+};
+
+function Engine() {}
 Engine.prototype = {
   // "default-engine";
   get name() { throw "name property must be overridden in subclasses"; },
 
   // "DefaultEngine";
   get logName() { throw "logName property must be overridden in subclasses"; },
 
   // "user-data/default-engine/";
@@ -789,17 +817,17 @@ Engine.prototype = {
     return this._resetClient.async(this, onComplete);
   }
 };
 
 function BookmarksEngine(davCollection, pbeId) {
   this._init(davCollection, pbeId);
 }
 BookmarksEngine.prototype = {
-  get name() { return "bookmarks-engine"; },
+  get name() { return "bookmarks"; },
   get logName() { return "BmkEngine"; },
   get serverPrefix() { return "user-data/bookmarks/"; },
 
   __core: null,
   get _core() {
     if (!this.__core)
       this.__core = new BookmarksSyncCore();
     return this.__core;
@@ -900,17 +928,17 @@ BookmarksEngine.prototype = {
   }
 };
 BookmarksEngine.prototype.__proto__ = new Engine();
 
 function HistoryEngine(davCollection, pbeId) {
   this._init(davCollection, pbeId);
 }
 HistoryEngine.prototype = {
-  get name() { return "history-engine"; },
+  get name() { return "history"; },
   get logName() { return "HistEngine"; },
   get serverPrefix() { return "user-data/history/"; },
 
   __core: null,
   get _core() {
     if (!this.__core)
       this.__core = new HistorySyncCore();
     return this.__core;
@@ -920,22 +948,21 @@ HistoryEngine.prototype = {
   get _store() {
     if (!this.__store)
       this.__store = new HistoryStore();
     return this.__store;
   }
 };
 HistoryEngine.prototype.__proto__ = new Engine();
 
-// Jono: the following is copy-and-paste code
 function CookieEngine(davCollection, pbeId) {
   this._init(davCollection, pbeId);
 }
 CookieEngine.prototype = {
-  get name() { return "cookie-engine"; },
+  get name() { return "cookies"; },
   get logName() { return "CookieEngine"; },
   get serverPrefix() { return "user-data/cookies/"; },
 
   __core: null,
   get _core() {
     if (!this.__core)
       this.__core = new CookieSyncCore();
     return this.__core;
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -72,16 +72,27 @@ Utils.lazy(Weave, 'Service', WeaveSvc);
  * Service singleton
  * Main entry point into Weave's sync framework
  */
 
 function WeaveSvc() {
   this._initLogs();
   this._log.info("Weave Sync Service Initializing");
 
+  ID.set('WeaveID', new Identity('Mozilla Services Password', this.username));
+  ID.setAlias('WeaveID', 'DAV:default');
+
+  ID.set('WeaveCryptoID',
+         new Identity('Mozilla Services Encryption Passphrase', this.username));
+  //ID.setAlias('WeaveCryptoID', '...');
+
+  Engines.register(new BookmarksEngine(DAV, ID.get('WeaveCryptoID')));
+  Engines.register(new HistoryEngine(DAV, ID.get('WeaveCryptoID')));
+  Engines.register(new CookieEngine(DAV, ID.get('WeaveCryptoID')));
+
   Utils.prefs.addObserver("", this, false);
 
   if (!this.enabled) {
     this._log.info("Weave Sync disabled");
     return;
   }
 
   this._setSchedule(this.schedule);
@@ -104,81 +115,40 @@ WeaveSvc.prototype = {
   __dirSvc: null,
   get _dirSvc() {
     if (!this.__dirSvc)
       this.__dirSvc = Cc["@mozilla.org/file/directory_service;1"].
         getService(Ci.nsIProperties);
     return this.__dirSvc;
   },
 
-  // FIXME: engines should be loaded dynamically somehow / need API to register
-
-  __bmkEngine: null,
-  get _bmkEngine() {
-    if (!this.__bmkEngine)
-      this.__bmkEngine = new BookmarksEngine(DAV, this._cryptoId);
-    return this.__bmkEngine;
-  },
-
-  __histEngine: null,
-  get _histEngine() {
-    if (!this.__histEngine)
-      this.__histEngine = new HistoryEngine(DAV, this._cryptoId);
-    return this.__histEngine;
-  },
-
-  __cookieEngine: null,
-  get _cookieEngine() {
-    // This gets an error that "CookieEngine" is undefined.  Why?
-    // BookmarksEngine and HistoryEngine are both defined in engines.js
-    // and so is CookieEngine, but...
-    if (!this.__cookieEngine)
-      this.__cookieEngine = new CookieEngine(DAV, this._cryptoId);
-    return this.__cookieEngine;
-  },
-
   // Timer object for automagically syncing
   _scheduleTimer: null,
 
-  __mozId: null,
-  get _mozId() {
-    if (this.__mozId === null)
-      this.__mozId = new Identity('Mozilla Services Password', this.username);
-    return this.__mozId;
-  },
-
-  __cryptoId: null,
-  get _cryptoId() {
-    if (this.__cryptoId === null)
-      this.__cryptoId = new Identity('Mozilla Services Encryption Passphrase',
-				     this.username);
-    return this.__cryptoId;
-  },
-
   get username() {
     return Utils.prefs.getCharPref("username");
   },
   set username(value) {
     if (value)
       Utils.prefs.setCharPref("username", value);
     else
       Utils.prefs.clearUserPref("username");
 
     // fixme - need to loop over all Identity objects - needs some rethinking...
-    this._mozId.username = value;
-    this._cryptoId.username = value;
+    ID.get('WeaveID').username = value;
+    ID.get('WeaveCryptoID').username = value;
   },
 
-  get password() { return this._mozId.password; },
-  set password(value) { this._mozId.password = value; },
+  get password() { return ID.get('WeaveID').password; },
+  set password(value) { ID.get('WeaveID').password = value; },
 
-  get passphrase() { return this._cryptoId.password; },
-  set passphrase(value) { this._cryptoId.password = value; },
+  get passphrase() { return ID.get('WeaveCryptoID').password; },
+  set passphrase(value) { ID.get('WeaveCryptoID').password = value; },
 
-  get userPath() { return this._mozId.userHash; },
+  get userPath() { return ID.get('WeaveID').userHash; },
 
   get currentUser() {
     if (this._loggedIn)
       return this.username;
     return null;
   },
 
   get enabled() {
@@ -345,35 +315,38 @@ WeaveSvc.prototype = {
     let self = yield;
 
     DAV.GET("private/privkey", self.cb);
     let keyResp = yield;
     Utils.ensureStatus(keyResp.status,
                        "Could not get private key from server", [[200,300],404]);
 
     if (keyResp.status != 404) {
-      this._cryptoId.privkey = keyResp.responseText;
-      Crypto.RSAkeydecrypt.async(Crypto, self.cb, this._cryptoId);
-      this._cryptoId.pubkey = yield;
+      let id = ID.get('WeaveCryptoID');
+      id.privkey = keyResp.responseText;
+      Crypto.RSAkeydecrypt.async(Crypto, self.cb, id);
+      id.pubkey = yield;
 
     } else {
       this._generateKeys.async(this, self.cb);
       yield;
     }
   },
 
   _generateKeys: function WeaveSync__generateKeys() {
     let self = yield;
 
     this._log.debug("Generating new RSA key");
-    Crypto.RSAkeygen.async(Crypto, self.cb, this._cryptoId);
+
+    let id = ID.get('WeaveCryptoID');
+    Crypto.RSAkeygen.async(Crypto, self.cb, id);
     let [privkey, pubkey] = yield;
 
-    this._cryptoId.privkey = privkey;
-    this._cryptoId.pubkey = pubkey;
+    id.privkey = privkey;
+    id.pubkey = pubkey;
 
     DAV.MKCOL("private/", self.cb);
     let ret = yield;
     if (!ret)
       throw "Could not create private key directory";
 
     DAV.MKCOL("public/", self.cb);
     ret = yield;
@@ -411,21 +384,18 @@ WeaveSvc.prototype = {
     this._localLock(this._notify("login", this._login,
                                  password, passphrase)).async(this, onComplete);
   },
   _login: function WeaveSync__login(password, passphrase) {
     let self = yield;
 
     // cache password & passphrase
     // if null, we'll try to get them from the pw manager below
-    this._mozId.setTempPassword(password);
-    this._cryptoId.setTempPassword(passphrase);
-
-    ID.set('MozID', this._mozId);
-    ID.setAlias('MozID', 'DAV:default');
+    ID.get('WeaveID').setTempPassword(password);
+    ID.get('WeaveCryptoID').setTempPassword(passphrase);
 
     this._log.debug("Logging in");
 
     if (!this.username)
       throw "No username set, login failed";
     if (!this.password)
       throw "No password given or found in password manager";
 
@@ -455,18 +425,18 @@ WeaveSvc.prototype = {
     this._loggedIn = true;
 
     self.done(true);
   },
 
   logout: function WeaveSync_logout() {
     this._log.info("Logging out");
     this._loggedIn = false;
-    this._mozId.setTempPassword(null); // clear cached password
-    this._cryptoId.setTempPassword(null); // and passphrase
+    ID.get('WeaveID').setTempPassword(null); // clear cached password
+    ID.get('WeaveCryptoID').setTempPassword(null); // and passphrase
     this._os.notifyObservers(null, "weave:service:logout:success", "");
   },
 
   resetLock: function WeaveSvc_resetLock(onComplete) {
     this._notify("reset-server-lock", this._resetLock).async(this, onComplete);
   },
   _resetLock: function WeaveSvc__resetLock() {
     let self = yield;
@@ -510,30 +480,30 @@ WeaveSvc.prototype = {
 
     this._versionCheck.async(this, self.cb);
     yield;
 
     this._keyCheck.async(this, self.cb);
     yield;
 
     if (Utils.prefs.getBoolPref("bookmarks")) {
-      this._notify(this._bmkEngine.name + ":sync",
-                   this._syncEngine, this._bmkEngine).async(this, self.cb);
+      this._notify(Engines.get("bookmarks").name + "-engine:sync",
+                   this._syncEngine, Engines.get("bookmarks")).async(this, self.cb);
       yield;
-      this._bmkEngine.syncMounts(self.cb); // FIXME
+      Engines.get("bookmarks").syncMounts(self.cb); // FIXME
       yield;
     }
     if (Utils.prefs.getBoolPref("history")) {
-      this._notify(this._histEngine.name + ":sync",
-                   this._syncEngine, this._histEngine).async(this, self.cb);
+      this._notify(Engines.get("history").name + "-engine:sync",
+                   this._syncEngine, Engines.get("history")).async(this, self.cb);
       yield;
     }
     if (Utils.prefs.getBoolPref("cookies")) {
-      this._notify(this._cookieEngine.name + ":sync",
-                   this._syncEngine, this._cookieEngine).async(this, self.cb);
+      this._notify(Engines.get("cookies").name + "-engine:sync",
+                   this._syncEngine, Engines.get("cookies")).async(this, self.cb);
       yield;
     }
   },
   _syncEngine: function WeaveSvc__syncEngine(engine) {
     let self = yield;
     engine.sync(self.cb);
     yield;
   },
@@ -543,39 +513,41 @@ WeaveSvc.prototype = {
                             this._resetServer)).async(this, onComplete);
   },
   _resetServer: function WeaveSync__resetServer() {
     let self = yield;
 
     if (!this._loggedIn)
       throw "Can't reset server: Not logged in";
 
-    this._bmkEngine.resetServer(self.cb);
-    yield;
-    this._histEngine.resetServer(self.cb);
-    yield;
+    let engines = Engines.getAll();
+    for (let i = 0; i < engines.length; i++) {
+      engines[i].resetServer(self.cb);
+      yield;
+    }
   },
 
   resetClient: function WeaveSync_resetClient(onComplete) {
     this._localLock(this._notify("reset-client",
                                  this._resetClient)).async(this, onComplete);
   },
   _resetClient: function WeaveSync__resetClient() {
     let self = yield;
-    this._bmkEngine.resetClient(self.cb);
-    yield;
-    this._histEngine.resetClient(self.cb);
-    yield;
+    let engines = Engines.getAll();
+    for (let i = 0; i < engines.length; i++) {
+      engines[i].resetClient(self.cb);
+      yield;
+    }
   },
 
   shareBookmarks: function WeaveSync_shareBookmarks(onComplete, username) {
     this._lock(this._notify("share-bookmarks",
                             this._shareBookmarks,
                             username)).async(this, onComplete);
   },
   _shareBookmarks: function WeaveSync__shareBookmarks(username) {
     let self = yield;
-    this._bmkEngine.share(self.cb, username);
+    Engines.get("bookmarks").share(self.cb, username);
     let ret = yield;
     self.done(ret);
   }
 
 };