summaryrefslogtreecommitdiffstats
path: root/services/cloudsync/CloudSyncLocal.jsm
blob: 998c0c3c4d21806b27a7219b3cf2ca31d41f043c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* 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/. */

"use strict";

this.EXPORTED_SYMBOLS = ["Local"];

const Cu = Components.utils;
const Cc = Components.classes;
const Ci = Components.interfaces;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://services-common/stringbundle.js");
Cu.import("resource://services-common/utils.js");
Cu.import("resource://services-crypto/utils.js");
Cu.import("resource://gre/modules/Preferences.jsm");

function lazyStrings(name) {
  let bundle = "chrome://weave/locale/services/" + name + ".properties";
  return () => new StringBundle(bundle);
}

this.Str = {};
XPCOMUtils.defineLazyGetter(Str, "errors", lazyStrings("errors"));
XPCOMUtils.defineLazyGetter(Str, "sync", lazyStrings("sync"));

function makeGUID() {
  return CommonUtils.encodeBase64URL(CryptoUtils.generateRandomBytes(9));
}

this.Local = function () {
  let prefs = new Preferences("services.cloudsync.");
  this.__defineGetter__("prefs", function () {
    return prefs;
  });
};

Local.prototype = {
  get id() {
    let clientId = this.prefs.get("client.GUID", "");
    return clientId == "" ? this.id = makeGUID(): clientId;
  },

  set id(value) {
    this.prefs.set("client.GUID", value);
  },

  get name() {
    let clientName = this.prefs.get("client.name", "");

    if (clientName != "") {
      return clientName;
    }

    // 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");
    let appName;
    let brand = new StringBundle("chrome://branding/locale/brand.properties");
    let brandName = brand.get("brandShortName");

    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.name = Str.sync.get("client.name2", [user, appName, system]);
  },

  set name(value) {
    this.prefs.set("client.name", value);
  },
};