Bug 587027 - Use as little memory as possible (part 1)
authorPhilipp von Weitershausen <philipp@weitershausen.de>
Wed, 25 Aug 2010 15:49:44 -0700
changeset 52156 fccbb7bad369db106164e0ea7244a9dfbd95ee92
parent 52155 d9804a22f720f71b559e277cfaf2c57436a5a347
child 52157 7f6be7428f0f038529e1247db292f9cdb2615058
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
bugs587027
Bug 587027 - Use as little memory as possible (part 1) Move the Weave object out of service.js
services/sync/modules/main.js
services/sync/modules/service.js
new file mode 100644
--- /dev/null
+++ b/services/sync/modules/main.js
@@ -0,0 +1,74 @@
+/* ***** BEGIN LICENSE BLOCK *****
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is Sync
+ *
+ * The Initial Developer of the Original Code is Mozilla Foundation.
+ * Portions created by the Initial Developer are Copyright (C) 2007
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Philipp von Weitershausen <philipp@weitershausen.de>
+ *
+ * Alternatively, the contents of this file may be used under the terms of
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+ * in which case the provisions of the GPL or the LGPL are applicable instead
+ * of those above. If you wish to allow use of your version of this file only
+ * under the terms of either the GPL or the LGPL, and not to allow others to
+ * 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 = ['Weave'];
+
+let Weave = {};
+Components.utils.import("resource://services-sync/constants.js", Weave);
+let lazies = {
+  "auth.js":              ['Auth', 'BrokenBasicAuthenticator',
+                           'BasicAuthenticator', 'NoOpAuthenticator'],
+  "base_records/keys.js": ['PubKey', 'PrivKey', 'PubKeys', 'PrivKeys'],
+  "engines.js":           ['Engines', 'Engine', 'SyncEngine'],
+  "engines/bookmarks.js": ['BookmarksEngine', 'BookmarksSharingManager'],
+  "engines/clients.js":   ["Clients"],
+  "engines/forms.js":     ["FormEngine"],
+  "engines/history.js":   ["HistoryEngine"],
+  "engines/prefs.js":     ["PrefsEngine"],
+  "engines/passwords.js": ["PasswordEngine"],
+  "engines/tabs.js":      ["TabEngine"],
+  "identity.js":          ["Identity", "ID"],
+  "notifications.js":     ["Notifications", "Notification", "NotificationButton"],
+  "resource.js":          ["Resource"],
+  "service.js":           ["Service"],
+  "status.js":            ["Status"],
+  "stores.js":            ["Store"],
+  "util.js":              ['Utils', 'Svc', 'Str']
+};
+
+function lazyImport(module, dest, props) {
+  function getter(prop) function() {
+    let ns = {};
+    Components.utils.import(module, ns);
+    delete dest[prop];
+    return dest[prop] = ns[prop];
+  };
+  props.forEach(function(prop) dest.__defineGetter__(prop, getter(prop)));
+}
+
+for (let mod in lazies) {
+  lazyImport("resource://services-sync/" + mod, Weave, lazies[mod]);
+}
--- a/services/sync/modules/service.js
+++ b/services/sync/modules/service.js
@@ -31,17 +31,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 = ['Weave'];
+// 'Weave' continues to be exported for backwards compatibility.
+const EXPORTED_SYMBOLS = ["Service", "Weave"];
 
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 const Cr = Components.results;
 const Cu = Components.utils;
 
 // how long we should wait before actually syncing on idle
 const IDLE_TIME = 5; // xxxmpc: in seconds, should be preffable
@@ -53,44 +54,25 @@ Cu.import("resource://gre/modules/XPCOMU
 Cu.import("resource://services-sync/auth.js");
 Cu.import("resource://services-sync/base_records/crypto.js");
 Cu.import("resource://services-sync/base_records/keys.js");
 Cu.import("resource://services-sync/base_records/wbo.js");
 Cu.import("resource://services-sync/constants.js");
 Cu.import("resource://services-sync/engines.js");
 Cu.import("resource://services-sync/engines/clients.js");
 Cu.import("resource://services-sync/ext/Sync.js");
+Cu.import("resource://services-sync/ext/Preferences.js");
 Cu.import("resource://services-sync/identity.js");
 Cu.import("resource://services-sync/log4moz.js");
 Cu.import("resource://services-sync/resource.js");
 Cu.import("resource://services-sync/status.js");
 Cu.import("resource://services-sync/util.js");
+Cu.import("resource://services-sync/main.js");
 
-// for export
-let Weave = {};
-Cu.import("resource://services-sync/auth.js", Weave);
-Cu.import("resource://services-sync/constants.js", Weave);
-Cu.import("resource://services-sync/base_records/keys.js", Weave);
-Cu.import("resource://services-sync/engines.js", Weave);
-Cu.import("resource://services-sync/engines/bookmarks.js", Weave);
-Cu.import("resource://services-sync/engines/clients.js", Weave);
-Cu.import("resource://services-sync/engines/forms.js", Weave);
-Cu.import("resource://services-sync/engines/history.js", Weave);
-Cu.import("resource://services-sync/engines/prefs.js", Weave);
-Cu.import("resource://services-sync/engines/passwords.js", Weave);
-Cu.import("resource://services-sync/engines/tabs.js", Weave);
-Cu.import("resource://services-sync/ext/Preferences.js");
-Cu.import("resource://services-sync/identity.js", Weave);
-Cu.import("resource://services-sync/notifications.js", Weave);
-Cu.import("resource://services-sync/resource.js", Weave);
-Cu.import("resource://services-sync/status.js", Weave);
-Cu.import("resource://services-sync/stores.js", Weave);
-Cu.import("resource://services-sync/util.js", Weave);
-
-Utils.lazy(Weave, 'Service', WeaveSvc);
+Utils.lazy(this, 'Service', WeaveSvc);
 
 /*
  * Service singleton
  * Main entry point into Weave's sync framework
  */
 
 function WeaveSvc() {
   this._notify = Utils.notify("weave:service:");
@@ -870,17 +852,17 @@ WeaveSvc.prototype = {
       "password": Utils.encodeUTF8(password),
       "email": email,
       "captcha-challenge": captchaChallenge,
       "captcha-response": captchaResponse
     });
 
     let url = this.userAPI + username;
     let res = new Resource(url);
-    res.authenticator = new Weave.NoOpAuthenticator();
+    res.authenticator = new NoOpAuthenticator();
 
     // Hint to server to allow scripted user creation or otherwise
     // ignore captcha.
     if (Svc.Prefs.isSet("admin-secret"))
       res.setHeader("X-Weave-Secret", Svc.Prefs.get("admin-secret", ""));
 
     let error = "generic-server-error";
     try {
@@ -1759,9 +1741,9 @@ WeaveSvc.prototype = {
 
     // Send the command to all remote clients
     this._log.debug("Sending clients: " + [command, args, commandData.desc]);
     Clients.sendCommand(command, args);
   },
 };
 
 // Load Weave on the first time this file is loaded
-Weave.Service.onStartup();
+Service.onStartup();