From 2004d7b0e79bde897a4ce311b80653db75e22136 Mon Sep 17 00:00:00 2001 From: Mark Hammond Date: Wed, 25 Mar 2015 16:28:19 +1100 Subject: [PATCH] Bug 1147270 - extract Sync's device name logic so it can be used by reading list. r=rnewman --- .../components/readinglist/ReadingList.jsm | 16 +++++---- services/sync/modules/engines/clients.js | 24 +------------- services/sync/modules/util.js | 33 +++++++++++++++++++ 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/browser/components/readinglist/ReadingList.jsm b/browser/components/readinglist/ReadingList.jsm index 38f6be04a93d..e978b641891c 100644 --- a/browser/components/readinglist/ReadingList.jsm +++ b/browser/components/readinglist/ReadingList.jsm @@ -19,6 +19,12 @@ 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"); @@ -312,12 +318,10 @@ ReadingListImpl.prototype = { 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)) { diff --git a/services/sync/modules/engines/clients.js b/services/sync/modules/engines/clients.js index 6fe8aee0265b..f423242c9ac0 100644 --- a/services/sync/modules/engines/clients.js +++ b/services/sync/modules/engines/clients.js @@ -114,29 +114,7 @@ ClientEngine.prototype = { 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), diff --git a/services/sync/modules/util.js b/services/sync/modules/util.js index a7da7419a1fb..72cab42fac14 100644 --- a/services/sync/modules/util.js +++ b/services/sync/modules/util.js @@ -640,6 +640,39 @@ this.Utils = { } 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() {