Bug 1147270 - extract Sync's device name logic so it can be used by reading list. r=rnewman

This commit is contained in:
Mark Hammond 2015-03-25 16:28:19 +11:00
Родитель a558a2ad93
Коммит 2004d7b0e7
3 изменённых файлов: 44 добавлений и 29 удалений

Просмотреть файл

@ -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)) {

Просмотреть файл

@ -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),

Просмотреть файл

@ -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() {