bug 492392: on first run, set the persona to the one the user was browsing when they installed the extension, if any; r=myk
authorJose E. Bolanos <jose@glaxstar.com>
Wed, 03 Jun 2009 16:38:59 -0700
changeset 996 9d858533de494098b633b63d07d840992fd81285
parent 995 2bed70efcdffacfa4277940f9f2005eb6d6d6ed5
child 997 607d0eb2c5cdd25c0b0a16509e7134c7a3284354
push id630
push usermyk@mozilla.com
push dateWed, 03 Jun 2009 23:39:32 +0000
reviewersmyk
bugs492392
bug 492392: on first run, set the persona to the one the user was browsing when they installed the extension, if any; r=myk
client/modules/service.js
--- a/client/modules/service.js
+++ b/client/modules/service.js
@@ -83,16 +83,18 @@ let PersonaService = {
     Observers.add("quit-application", this.onQuitApplication, this);
 
     this._prefs.observe("useTextColor",   this.onUseColorChanged, this);
     this._prefs.observe("useAccentColor", this.onUseColorChanged, this);
 
     let timerManager = Cc["@mozilla.org/updates/timer-manager;1"].
                        getService(Ci.nsIUpdateTimerManager);
 
+    this._setFirstRunPersona();
+
     // Refresh data, then set a timer to refresh it periodically.
     // This isn't quite right, since we always load data on startup, even if
     // we've recently refreshed it.  And the timer that refreshes data ignores
     // the data load on startup, so if it's been more than the timer interval
     // since a user last started her browser, we load the data twice:
     // once because the browser starts and once because the refresh timer fires.
     // FIXME: save the data to disk when we retrieve it and then retrieve it
     // from there on startup instead of loading it over the network.
@@ -494,16 +496,56 @@ let PersonaService = {
   /**
    * Stop previewing a persona.
    */
   resetPersona: function() {
     this.previewingPersona = null;
     Observers.notify("personas:persona:changed");
   },
 
+  /**
+   * Sets the persona specified by the initial_persona cookie during first run.
+   */
+  _setFirstRunPersona : function() {
+    let application =
+      Cc["@mozilla.org/fuel/application;1"].getService(Ci.fuelIApplication);
+    let extension = application.extensions.get(PERSONAS_EXTENSION_ID);
+
+    if (extension.firstRun) {
+
+      let authorizedHosts = this._prefs.get("authorizedHosts").split(/[, ]+/);
+      let cookieManager =
+        Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager);
+      let cookieEnu = cookieManager.enumerator;
+      let selectedCookie = null;
+
+      while (cookieEnu.hasMoreElements()) {
+        let cookie = cookieEnu.getNext().QueryInterface(Ci.nsICookie);
+
+        if (cookie.name == "initial_persona" &&
+            authorizedHosts.some(function(v) v == cookie.host)) {
+
+          // There could be more than one "initial_persona" cookie. The cookie
+          // with latest expiration time is selected.
+          if (null == selectedCookie ||
+              cookie.expires > selectedCookie.expires) {
+            selectedCookie = cookie;
+          }
+
+          cookieManager.remove(cookie.host, cookie.name, cookie.path, false);
+        }
+      }
+
+      if (null != selectedCookie) {
+        let personaJSON = decodeURIComponent(selectedCookie.value);
+        this.changeToPersona(JSON.parse(personaJSON));
+      }
+    }
+  },
+
   onQuitApplication: function() {
     Observers.remove("quit-application", this.onQuitApplication, this);
     this._destroy();
   }
 
 };
 
 let DateUtils = {