services/sync/modules/engines/prefs.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 this.EXPORTED_SYMBOLS = ['PrefsEngine', 'PrefRec'];
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cu = Components.utils;
michael@0 10
michael@0 11 const WEAVE_SYNC_PREFS = "services.sync.prefs.sync.";
michael@0 12
michael@0 13 Cu.import("resource://services-sync/engines.js");
michael@0 14 Cu.import("resource://services-sync/record.js");
michael@0 15 Cu.import("resource://services-sync/util.js");
michael@0 16 Cu.import("resource://services-sync/constants.js");
michael@0 17 Cu.import("resource://services-common/utils.js");
michael@0 18 Cu.import("resource://gre/modules/LightweightThemeManager.jsm");
michael@0 19 Cu.import("resource://gre/modules/Preferences.jsm");
michael@0 20
michael@0 21 const PREFS_GUID = CommonUtils.encodeBase64URL(Services.appinfo.ID);
michael@0 22
michael@0 23 this.PrefRec = function PrefRec(collection, id) {
michael@0 24 CryptoWrapper.call(this, collection, id);
michael@0 25 }
michael@0 26 PrefRec.prototype = {
michael@0 27 __proto__: CryptoWrapper.prototype,
michael@0 28 _logName: "Sync.Record.Pref",
michael@0 29 };
michael@0 30
michael@0 31 Utils.deferGetSet(PrefRec, "cleartext", ["value"]);
michael@0 32
michael@0 33
michael@0 34 this.PrefsEngine = function PrefsEngine(service) {
michael@0 35 SyncEngine.call(this, "Prefs", service);
michael@0 36 }
michael@0 37 PrefsEngine.prototype = {
michael@0 38 __proto__: SyncEngine.prototype,
michael@0 39 _storeObj: PrefStore,
michael@0 40 _trackerObj: PrefTracker,
michael@0 41 _recordObj: PrefRec,
michael@0 42 version: 2,
michael@0 43
michael@0 44 getChangedIDs: function getChangedIDs() {
michael@0 45 // No need for a proper timestamp (no conflict resolution needed).
michael@0 46 let changedIDs = {};
michael@0 47 if (this._tracker.modified)
michael@0 48 changedIDs[PREFS_GUID] = 0;
michael@0 49 return changedIDs;
michael@0 50 },
michael@0 51
michael@0 52 _wipeClient: function _wipeClient() {
michael@0 53 SyncEngine.prototype._wipeClient.call(this);
michael@0 54 this.justWiped = true;
michael@0 55 },
michael@0 56
michael@0 57 _reconcile: function _reconcile(item) {
michael@0 58 // Apply the incoming item if we don't care about the local data
michael@0 59 if (this.justWiped) {
michael@0 60 this.justWiped = false;
michael@0 61 return true;
michael@0 62 }
michael@0 63 return SyncEngine.prototype._reconcile.call(this, item);
michael@0 64 }
michael@0 65 };
michael@0 66
michael@0 67
michael@0 68 function PrefStore(name, engine) {
michael@0 69 Store.call(this, name, engine);
michael@0 70 Svc.Obs.add("profile-before-change", function () {
michael@0 71 this.__prefs = null;
michael@0 72 }, this);
michael@0 73 }
michael@0 74 PrefStore.prototype = {
michael@0 75 __proto__: Store.prototype,
michael@0 76
michael@0 77 __prefs: null,
michael@0 78 get _prefs() {
michael@0 79 if (!this.__prefs)
michael@0 80 this.__prefs = new Preferences();
michael@0 81 return this.__prefs;
michael@0 82 },
michael@0 83
michael@0 84 _getSyncPrefs: function _getSyncPrefs() {
michael@0 85 let syncPrefs = Cc["@mozilla.org/preferences-service;1"]
michael@0 86 .getService(Ci.nsIPrefService)
michael@0 87 .getBranch(WEAVE_SYNC_PREFS)
michael@0 88 .getChildList("", {});
michael@0 89 // Also sync preferences that determine which prefs get synced.
michael@0 90 return syncPrefs.concat(
michael@0 91 syncPrefs.map(function (pref) { return WEAVE_SYNC_PREFS + pref; }));
michael@0 92 },
michael@0 93
michael@0 94 _isSynced: function _isSyncedPref(pref) {
michael@0 95 return (pref.indexOf(WEAVE_SYNC_PREFS) == 0)
michael@0 96 || this._prefs.get(WEAVE_SYNC_PREFS + pref, false);
michael@0 97 },
michael@0 98
michael@0 99 _getAllPrefs: function () {
michael@0 100 let values = {};
michael@0 101 for each (let pref in this._getSyncPrefs()) {
michael@0 102 if (this._isSynced(pref)) {
michael@0 103 // Missing prefs get the null value.
michael@0 104 values[pref] = this._prefs.get(pref, null);
michael@0 105 }
michael@0 106 }
michael@0 107 return values;
michael@0 108 },
michael@0 109
michael@0 110 _setAllPrefs: function PrefStore__setAllPrefs(values) {
michael@0 111 let enabledPref = "lightweightThemes.isThemeSelected";
michael@0 112 let enabledBefore = this._prefs.get(enabledPref, false);
michael@0 113 let prevTheme = LightweightThemeManager.currentTheme;
michael@0 114
michael@0 115 for (let [pref, value] in Iterator(values)) {
michael@0 116 if (!this._isSynced(pref))
michael@0 117 continue;
michael@0 118
michael@0 119 // Pref has gone missing, best we can do is reset it.
michael@0 120 if (value == null) {
michael@0 121 this._prefs.reset(pref);
michael@0 122 continue;
michael@0 123 }
michael@0 124
michael@0 125 try {
michael@0 126 this._prefs.set(pref, value);
michael@0 127 } catch(ex) {
michael@0 128 this._log.trace("Failed to set pref: " + pref + ": " + ex);
michael@0 129 }
michael@0 130 }
michael@0 131
michael@0 132 // Notify the lightweight theme manager of all the new values
michael@0 133 let enabledNow = this._prefs.get(enabledPref, false);
michael@0 134 if (enabledBefore && !enabledNow) {
michael@0 135 LightweightThemeManager.currentTheme = null;
michael@0 136 } else if (enabledNow && LightweightThemeManager.usedThemes[0] != prevTheme) {
michael@0 137 LightweightThemeManager.currentTheme = null;
michael@0 138 LightweightThemeManager.currentTheme = LightweightThemeManager.usedThemes[0];
michael@0 139 }
michael@0 140 },
michael@0 141
michael@0 142 getAllIDs: function PrefStore_getAllIDs() {
michael@0 143 /* We store all prefs in just one WBO, with just one GUID */
michael@0 144 let allprefs = {};
michael@0 145 allprefs[PREFS_GUID] = true;
michael@0 146 return allprefs;
michael@0 147 },
michael@0 148
michael@0 149 changeItemID: function PrefStore_changeItemID(oldID, newID) {
michael@0 150 this._log.trace("PrefStore GUID is constant!");
michael@0 151 },
michael@0 152
michael@0 153 itemExists: function FormStore_itemExists(id) {
michael@0 154 return (id === PREFS_GUID);
michael@0 155 },
michael@0 156
michael@0 157 createRecord: function createRecord(id, collection) {
michael@0 158 let record = new PrefRec(collection, id);
michael@0 159
michael@0 160 if (id == PREFS_GUID) {
michael@0 161 record.value = this._getAllPrefs();
michael@0 162 } else {
michael@0 163 record.deleted = true;
michael@0 164 }
michael@0 165
michael@0 166 return record;
michael@0 167 },
michael@0 168
michael@0 169 create: function PrefStore_create(record) {
michael@0 170 this._log.trace("Ignoring create request");
michael@0 171 },
michael@0 172
michael@0 173 remove: function PrefStore_remove(record) {
michael@0 174 this._log.trace("Ignoring remove request");
michael@0 175 },
michael@0 176
michael@0 177 update: function PrefStore_update(record) {
michael@0 178 // Silently ignore pref updates that are for other apps.
michael@0 179 if (record.id != PREFS_GUID)
michael@0 180 return;
michael@0 181
michael@0 182 this._log.trace("Received pref updates, applying...");
michael@0 183 this._setAllPrefs(record.value);
michael@0 184 },
michael@0 185
michael@0 186 wipe: function PrefStore_wipe() {
michael@0 187 this._log.trace("Ignoring wipe request");
michael@0 188 }
michael@0 189 };
michael@0 190
michael@0 191 function PrefTracker(name, engine) {
michael@0 192 Tracker.call(this, name, engine);
michael@0 193 Svc.Obs.add("profile-before-change", this);
michael@0 194 Svc.Obs.add("weave:engine:start-tracking", this);
michael@0 195 Svc.Obs.add("weave:engine:stop-tracking", this);
michael@0 196 }
michael@0 197 PrefTracker.prototype = {
michael@0 198 __proto__: Tracker.prototype,
michael@0 199
michael@0 200 get modified() {
michael@0 201 return Svc.Prefs.get("engine.prefs.modified", false);
michael@0 202 },
michael@0 203 set modified(value) {
michael@0 204 Svc.Prefs.set("engine.prefs.modified", value);
michael@0 205 },
michael@0 206
michael@0 207 loadChangedIDs: function loadChangedIDs() {
michael@0 208 // Don't read changed IDs from disk at start up.
michael@0 209 },
michael@0 210
michael@0 211 clearChangedIDs: function clearChangedIDs() {
michael@0 212 this.modified = false;
michael@0 213 },
michael@0 214
michael@0 215 __prefs: null,
michael@0 216 get _prefs() {
michael@0 217 if (!this.__prefs) {
michael@0 218 this.__prefs = new Preferences();
michael@0 219 }
michael@0 220 return this.__prefs;
michael@0 221 },
michael@0 222
michael@0 223 startTracking: function () {
michael@0 224 Services.prefs.addObserver("", this, false);
michael@0 225 },
michael@0 226
michael@0 227 stopTracking: function () {
michael@0 228 this.__prefs = null;
michael@0 229 Services.prefs.removeObserver("", this);
michael@0 230 },
michael@0 231
michael@0 232 observe: function (subject, topic, data) {
michael@0 233 Tracker.prototype.observe.call(this, subject, topic, data);
michael@0 234
michael@0 235 switch (topic) {
michael@0 236 case "profile-before-change":
michael@0 237 this.stopTracking();
michael@0 238 break;
michael@0 239 case "nsPref:changed":
michael@0 240 // Trigger a sync for MULTI-DEVICE for a change that determines
michael@0 241 // which prefs are synced or a regular pref change.
michael@0 242 if (data.indexOf(WEAVE_SYNC_PREFS) == 0 ||
michael@0 243 this._prefs.get(WEAVE_SYNC_PREFS + data, false)) {
michael@0 244 this.score += SCORE_INCREMENT_XLARGE;
michael@0 245 this.modified = true;
michael@0 246 this._log.trace("Preference " + data + " changed");
michael@0 247 }
michael@0 248 break;
michael@0 249 }
michael@0 250 }
michael@0 251 };

mercurial