Bug 1239606 - Implement UserContextUI.jsm, r=paolo
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 14 Jan 2016 14:23:54 +0000
changeset 280059 ac467630a2d6a2c41d3148bbf41bbfbbd9dd92e4
parent 280058 40da34cef5721d59761b0bbd16d366a3c9ee49cc
child 280060 10cf3da0f57b9e19903e56d0fa958fdaa9d5bf65
push id29900
push usercbook@mozilla.com
push dateFri, 15 Jan 2016 10:47:20 +0000
treeherdermozilla-central@e1486d83107f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspaolo
bugs1239606
milestone46.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1239606 - Implement UserContextUI.jsm, r=paolo
browser/base/content/browser.js
browser/components/preferences/cookies.js
browser/components/preferences/cookies.xul
browser/modules/UserContextUI.jsm
browser/modules/moz.build
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -5,16 +5,17 @@
 
 var Ci = Components.interfaces;
 var Cu = Components.utils;
 var Cc = Components.classes;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/NotificationDB.jsm");
 Cu.import("resource:///modules/RecentWindow.jsm");
+Cu.import("resource:///modules/UserContextUI.jsm");
 
 
 XPCOMUtils.defineLazyModuleGetter(this, "Preferences",
                                   "resource://gre/modules/Preferences.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "Deprecated",
                                   "resource://gre/modules/Deprecated.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "BrowserUITelemetry",
                                   "resource:///modules/BrowserUITelemetry.jsm");
@@ -4054,34 +4055,17 @@ function updateUserContextUIIndicator(br
   if (!browser.hasAttribute("usercontextid")) {
     hbox.removeAttribute("usercontextid");
     return;
   }
 
   let label = document.getElementById("userContext-label");
   let userContextId = browser.getAttribute("usercontextid");
   hbox.setAttribute("usercontextid", userContextId);
-  switch (userContextId) {
-    case "1":
-      label.value = gBrowserBundle.GetStringFromName("usercontext.personal.label");
-      break;
-    case "2":
-      label.value = gBrowserBundle.GetStringFromName("usercontext.work.label");
-      break;
-    case "3":
-      label.value = gBrowserBundle.GetStringFromName("usercontext.banking.label");
-      break;
-    case "4":
-      label.value = gBrowserBundle.GetStringFromName("usercontext.shopping.label");
-      break;
-    // Display the context IDs for values outside the pre-defined range.
-    // Used for debugging, no localization necessary.
-    default:
-      label.value = "Context " + userContextId;
-  }
+  label.value = UserContextUI.getUserContextLabel(userContextId);
 }
 
 /**
  * Makes the Character Encoding menu enabled or disabled as appropriate.
  * To be called when the View menu or the app menu is opened.
  */
 function updateCharacterEncodingMenuState()
 {
--- a/browser/components/preferences/cookies.js
+++ b/browser/components/preferences/cookies.js
@@ -2,36 +2,35 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 const nsICookie = Components.interfaces.nsICookie;
 
 Components.utils.import("resource://gre/modules/PluralForm.jsm");
 Components.utils.import("resource://gre/modules/Services.jsm")
+Components.utils.import("resource:///modules/UserContextUI.jsm");
 
 var gCookiesWindow = {
   _cm               : Components.classes["@mozilla.org/cookiemanager;1"]
                                 .getService(Components.interfaces.nsICookieManager),
   _ds               : Components.classes["@mozilla.org/intl/scriptabledateformat;1"]
                                 .getService(Components.interfaces.nsIScriptableDateFormat),
   _hosts            : {},
   _hostOrder        : [],
   _tree             : null,
   _bundle           : null,
-  _bundleBrowser    : null,
 
   init: function () {
     var os = Components.classes["@mozilla.org/observer-service;1"]
                        .getService(Components.interfaces.nsIObserverService);
     os.addObserver(this, "cookie-changed", false);
     os.addObserver(this, "perm-changed", false);
 
     this._bundle = document.getElementById("bundlePreferences");
-    this._bundleBrowser = document.getElementById("bundleBrowser");
     this._tree = document.getElementById("cookiesList");
 
     this._populateList(true);
 
     document.getElementById("filter").focus();
 
     if (!Services.prefs.getBoolPref("privacy.userContext.enabled")) {
       document.getElementById("userContextRow").hidden = true;
@@ -513,17 +512,17 @@ var gCookiesWindow = {
 
     if (aItem && !aItem.container && seln.count > 0) {
       properties = { name: aItem.name, value: aItem.value, host: aItem.host,
                      path: aItem.path, expires: this.formatExpiresString(aItem.expires),
                      isDomain: aItem.isDomain ? this._bundle.getString("domainColon")
                                               : this._bundle.getString("hostColon"),
                      isSecure: aItem.isSecure ? this._bundle.getString("forSecureOnly")
                                               : this._bundle.getString("forAnyConnection"),
-                     userContext: this._convertUserContextIdToContainerName(aItem.originAttributes.userContextId) };
+                     userContext: UserContextUI.getUserContextLabel(aItem.originAttributes.userContextId) };
       for (var i = 0; i < ids.length; ++i) {
         document.getElementById(ids[i]).disabled = false;
       }
     }
     else {
       var noneSelected = this._bundle.getString("noCookieSelected");
       properties = { name: noneSelected, value: noneSelected, host: noneSelected,
                      path: noneSelected, expires: noneSelected,
@@ -531,32 +530,16 @@ var gCookiesWindow = {
       for (i = 0; i < ids.length; ++i) {
         document.getElementById(ids[i]).disabled = true;
       }
     }
     for (var property in properties)
       document.getElementById(property).value = properties[property];
   },
 
-  _convertUserContextIdToContainerName: function(aUserContextId) {
-    // TODO: this code should be moved in a separate module - bug 1239606
-    switch (aUserContextId) {
-      // No UserContext:
-      case 0: return "";
-
-      case 1: return this._bundleBrowser.getString("usercontext.personal.label");
-      case 2: return this._bundleBrowser.getString("usercontext.work.label");
-      case 3: return this._bundleBrowser.getString("usercontext.shopping.label");
-      case 4: return this._bundleBrowser.getString("usercontext.banking.label");
-
-      // This should not happen.
-      default: return "Context " + aUserContextId;
-    }
-  },
-
   onCookieSelected: function () {
     var item;
     var seln = this._tree.view.selection;
     if (!this._view._filtered)
       item = this._view._getItemAtIndex(seln.currentIndex);
     else
       item = this._view._filterSet[seln.currentIndex];
 
--- a/browser/components/preferences/cookies.xul
+++ b/browser/components/preferences/cookies.xul
@@ -19,19 +19,16 @@
         persist="screenX screenY width height"
         onkeypress="gCookiesWindow.onWindowKeyPress(event);">
 
   <script src="chrome://browser/content/preferences/cookies.js"/>
 
   <stringbundle id="bundlePreferences"
                 src="chrome://browser/locale/preferences/preferences.properties"/>
 
-  <stringbundle id="bundleBrowser"
-                src="chrome://browser/locale/browser.properties"/>
-
   <keyset>
     <key key="&windowClose.key;" modifiers="accel" oncommand="window.close();"/>
     <key key="&focusSearch1.key;" modifiers="accel" oncommand="gCookiesWindow.focusFilterBox();"/>
     <key key="&focusSearch2.key;" modifiers="accel" oncommand="gCookiesWindow.focusFilterBox();"/>
   </keyset>
 
   <vbox flex="1" class="contentPane largeDialogContainer">
     <hbox align="center">
new file mode 100644
--- /dev/null
+++ b/browser/modules/UserContextUI.jsm
@@ -0,0 +1,32 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+this.EXPORTED_SYMBOLS = ["UserContextUI"];
+
+const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
+
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm")
+
+XPCOMUtils.defineLazyGetter(this, "gBrowserBundle", function() {
+  return Services.strings.createBundle("chrome://browser/locale/browser.properties");
+});
+
+this.UserContextUI = {
+  getUserContextLabel(userContextId) {
+    switch (userContextId) {
+      // No UserContext:
+      case 0: return "";
+
+      case 1: return gBrowserBundle.GetStringFromName("usercontext.personal.label");
+      case 2: return gBrowserBundle.GetStringFromName("usercontext.work.label");
+      case 3: return gBrowserBundle.GetStringFromName("usercontext.shopping.label");
+      case 4: return gBrowserBundle.GetStringFromName("usercontext.banking.label");
+
+      // Display the context IDs for values outside the pre-defined range.
+      // Used for debugging, no localization necessary.
+      default: return "Context " + userContextId;
+    }
+  }
+}
--- a/browser/modules/moz.build
+++ b/browser/modules/moz.build
@@ -38,16 +38,17 @@ EXTRA_JS_MODULES += [
     'RecentWindow.jsm',
     'RemotePrompt.jsm',
     'Sanitizer.jsm',
     'SelfSupportBackend.jsm',
     'SitePermissions.jsm',
     'Social.jsm',
     'TabGroupsMigrator.jsm',
     'TransientPrefs.jsm',
+    'UserContextUI.jsm',
     'WebappManager.jsm',
     'webrtcUI.jsm',
 ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'windows':
     EXTRA_JS_MODULES += [
         'Windows8WindowFrameColor.jsm',
         'WindowsJumpLists.jsm',