Bug 1081061: switch to a different database if a userProfile is active during the first mozLoop.contacts access to always be in sync with the correct state. r=MattN
authorMike de Boer <mdeboer@mozilla.com>
Wed, 15 Oct 2014 12:47:54 +0200
changeset 210431 30af3afe667e53e9b135b18cc525477598563447
parent 210430 d369649bb2657762c504a0c8a6678051ce7b1f3f
child 210432 87136846a6c86091da3cf0f78158500ba20ed243
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersMattN
bugs1081061
milestone36.0a1
Bug 1081061: switch to a different database if a userProfile is active during the first mozLoop.contacts access to always be in sync with the correct state. r=MattN
browser/components/loop/MozLoopAPI.jsm
--- a/browser/components/loop/MozLoopAPI.jsm
+++ b/browser/components/loop/MozLoopAPI.jsm
@@ -5,18 +5,21 @@
 "use strict";
 
 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
 
 Cu.import("resource://services-common/utils.js");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource:///modules/loop/MozLoopService.jsm");
-Cu.import("resource:///modules/loop/LoopContacts.jsm");
 
+XPCOMUtils.defineLazyModuleGetter(this, "LoopContacts",
+                                        "resource:///modules/loop/LoopContacts.jsm");
+XPCOMUtils.defineLazyModuleGetter(this, "LoopStorage",
+                                        "resource:///modules/loop/LoopStorage.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "hookWindowCloseForPanelClose",
                                         "resource://gre/modules/MozSocialAPI.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "PluralForm",
                                         "resource://gre/modules/PluralForm.jsm");
 XPCOMUtils.defineLazyGetter(this, "appInfo", function() {
   return Cc["@mozilla.org/xre/app-info;1"]
            .getService(Ci.nsIXULAppInfo)
            .QueryInterface(Ci.nsIXULRuntime);
@@ -226,16 +229,22 @@ function injectLoopAPI(targetWindow) {
      * @returns {Object} The contacts API object
      */
     contacts: {
       enumerable: true,
       get: function() {
         if (contactsAPI) {
           return contactsAPI;
         }
+
+        // Make a database switch when a userProfile is active already.
+        let profile = MozLoopService.userProfile;
+        if (profile) {
+          LoopStorage.switchDatabase(profile.uid);
+        }
         return contactsAPI = injectObjectAPI(LoopContacts, targetWindow);
       }
     },
 
     /**
      * Import a list of (new) contacts from an external data source.
      *
      * @param {Object}   options  Property bag of options for the importer