Bug 1147270 - extract Sync's device name logic so it can be used by reading list. r=rnewman a=readinglist
authorMark Hammond <mhammond@skippinet.com.au>
Wed, 25 Mar 2015 16:28:19 +1100
changeset 248479 bbad6053a2592634fc78029551546f71224540dc
parent 248478 20d4b2b21a8f9e24f470388d61b362b6ce6bbb62
child 248480 cdda1e9c607daea0717287db445484bf9593fca5
push id7837
push userjwein@mozilla.com
push dateFri, 27 Mar 2015 00:27:16 +0000
treeherdermozilla-aurora@cb0db44ce60e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman, readinglist
bugs1147270
milestone38.0a2
Bug 1147270 - extract Sync's device name logic so it can be used by reading list. r=rnewman a=readinglist
browser/components/readinglist/ReadingList.jsm
services/sync/modules/engines/clients.js
services/sync/modules/util.js
--- a/browser/components/readinglist/ReadingList.jsm
+++ b/browser/components/readinglist/ReadingList.jsm
@@ -14,16 +14,22 @@ Cu.import("resource://gre/modules/Servic
 Cu.import("resource://gre/modules/Task.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Preferences.jsm");
 Cu.import("resource://gre/modules/Log.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "SQLiteStore",
   "resource:///modules/readinglist/SQLiteStore.jsm");
 
+// We use Sync's "Utils" module for the device name, which is unfortunate,
+// but let's give it a better name here.
+XPCOMUtils.defineLazyGetter(this, 'SyncUtils', function() {
+  const {Utils} = Cu.import('resource://services-sync/util.js"', {});
+  return Utils;
+});
 
 { // Prevent the parent log setup from leaking into the global scope.
   let parentLog = Log.repository.getLogger("readinglist");
   parentLog.level = Preferences.get("browser.readinglist.logLevel", Log.Level.Warn);
   Preferences.observe("browser.readinglist.logLevel", value => {
     parentLog.level = value;
   });
   let formatter = new Log.BasicFormatter();
@@ -307,22 +313,20 @@ ReadingListImpl.prototype = {
     record = normalizeRecord(record);
     if (!record.url) {
       throw new Error("The item must have a url");
     }
     if (!("addedOn" in record)) {
       record.addedOn = Date.now();
     }
     if (!("addedBy" in record)) {
-      let pref = "services.sync.client.name";
-      if (Services.prefs.prefHasUserValue(pref)) {
-        record.addedBy = Services.prefs.getCharPref(pref);
-      }
-      if (!record.addedBy) {
-        record.addedBy = "Firefox";
+      try {
+        record.addedBy = Services.prefs.getCharPref("services.sync.client.name");
+      } catch (ex) {
+        record.addedBy = SyncUtils.getDefaultDeviceName();
       }
     }
     if (!("syncStatus" in record)) {
       record.syncStatus = SYNC_STATUS_NEW;
     }
 
     yield this._store.addItem(record);
     this._invalidateIterators();
--- a/services/sync/modules/engines/clients.js
+++ b/services/sync/modules/engines/clients.js
@@ -109,39 +109,17 @@ ClientEngine.prototype = {
     return brand.get("brandShortName");
   },
 
   get localName() {
     let localName = Svc.Prefs.get("client.name", "");
     if (localName != "")
       return localName;
 
-    // Generate a client name if we don't have a useful one yet
-    let env = Cc["@mozilla.org/process/environment;1"]
-                .getService(Ci.nsIEnvironment);
-    let user = env.get("USER") || env.get("USERNAME") ||
-               Svc.Prefs.get("account") || Svc.Prefs.get("username");
-
-    let brandName = this.brandName;
-    let appName;
-    try {
-      let syncStrings = new StringBundle("chrome://browser/locale/sync.properties");
-      appName = syncStrings.getFormattedString("sync.defaultAccountApplication", [brandName]);
-    } catch (ex) {}
-    appName = appName || brandName;
-
-    let system =
-      // 'device' is defined on unix systems
-      Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2).get("device") ||
-      // hostname of the system, usually assigned by the user or admin
-      Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2).get("host") ||
-      // fall back on ua info string
-      Cc["@mozilla.org/network/protocol;1?name=http"].getService(Ci.nsIHttpProtocolHandler).oscpu;
-
-    return this.localName = Str.sync.get("client.name2", [user, appName, system]);
+    return this.localName = Utils.getDefaultDeviceName();
   },
   set localName(value) Svc.Prefs.set("client.name", value),
 
   get localType() Svc.Prefs.get("client.type", "desktop"),
   set localType(value) Svc.Prefs.set("client.type", value),
 
   isMobile: function isMobile(id) {
     if (this._store._remoteClients[id])
--- a/services/sync/modules/util.js
+++ b/services/sync/modules/util.js
@@ -635,16 +635,49 @@ this.Utils = {
       } catch (_) {
         continue;
       }
       let uri = Services.io.newURI(prefVal, null, null);
       result.add(uri.prePath);
     }
     return this._syncCredentialsHostsFxA = result;
   },
+
+  getDefaultDeviceName() {
+    // Generate a client name if we don't have a useful one yet
+    let env = Cc["@mozilla.org/process/environment;1"]
+                .getService(Ci.nsIEnvironment);
+    let user = env.get("USER") || env.get("USERNAME") ||
+               Svc.Prefs.get("account") || Svc.Prefs.get("username");
+    // A little hack for people using the the moz-build environment on Windows
+    // which sets USER to the literal "%USERNAME%" (yes, really)
+    if (user == "%USERNAME%" && env.get("USERNAME")) {
+      user = env.get("USERNAME");
+    }
+
+    let brand = new StringBundle("chrome://branding/locale/brand.properties");
+    let brandName = brand.get("brandShortName");
+
+    let appName;
+    try {
+      let syncStrings = new StringBundle("chrome://browser/locale/sync.properties");
+      appName = syncStrings.getFormattedString("sync.defaultAccountApplication", [brandName]);
+    } catch (ex) {}
+    appName = appName || brandName;
+
+    let system =
+      // 'device' is defined on unix systems
+      Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2).get("device") ||
+      // hostname of the system, usually assigned by the user or admin
+      Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2).get("host") ||
+      // fall back on ua info string
+      Cc["@mozilla.org/network/protocol;1?name=http"].getService(Ci.nsIHttpProtocolHandler).oscpu;
+
+    return Str.sync.get("client.name2", [user, appName, system]);
+  }
 };
 
 XPCOMUtils.defineLazyGetter(Utils, "_utf8Converter", function() {
   let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]
                     .createInstance(Ci.nsIScriptableUnicodeConverter);
   converter.charset = "UTF-8";
   return converter;
 });