-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathSyncPolicy.sys.mjs
More file actions
163 lines (144 loc) · 4.76 KB
/
SyncPolicy.sys.mjs
File metadata and controls
163 lines (144 loc) · 4.76 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* 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/. */
import {
PREF_LOGLEVEL,
setAndLockPref,
unsetAndUnlockPref,
PoliciesUtils,
} from "resource:///modules/policies/Policies.sys.mjs";
import { STATUS_OK as SYNC_STATUS_OK } from "resource://services-sync/constants.sys.mjs"
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
Weave: "resource://services-sync/main.sys.mjs",
});
ChromeUtils.defineLazyGetter(lazy, "log", () => {
return console.createInstance({
prefix: "SyncPolicy",
maxLogLevelPref: PREF_LOGLEVEL,
});
});
const ENGINE_PREFS = {
addresses: "services.sync.engine.addresses",
addons: "services.sync.engine.addons",
bookmarks: "services.sync.engine.bookmarks",
history: "services.sync.engine.history",
openTabs: "services.sync.engine.tabs",
passwords: "services.sync.engine.passwords",
paymentMethods: "services.sync.engine.creditcards",
settings: "services.sync.engine.prefs",
};
const SYNC_FEATURE = "change-sync-state";
/**
* Customizes Sync settings (all settings are optional):
* - Whether sync is enabled/disabled
* - Which types of data to sync
* - Whether to lock the sync customization
* See SyncPolicyParams for details.
*/
export const SyncPolicy = {
/**
* Get current sync state.
*
* @returns {boolean} Whether sync is currently enabled.
*/
isSyncEnabled() {
return lazy.Weave.Status.checkSetup() == SYNC_STATUS_OK;
},
/**
* @typedef {object} SyncPolicyParams
* @property {boolean} [Enabled] Whether Sync should be enabled
* @property {boolean} [addresses] Whether syncing addresses should be enabled
* @property {boolean} [bookmarks] Whether syncing bookmarks should be enabled
* @property {boolean} [history] Whether syncing history should be enabled
* @property {boolean} [openTabs] Whether syncing openTabs should be enabled
* @property {boolean} [passwords] Whether syncing passwords should be enabled
* @property {boolean} [paymentMethods] Whether syncing paymentMethods should be enabled
* @property {boolean} [addons] Whether syncing addons should be enabled
* @property {boolean} [settings] Whether syncing settings should be enabled
* @property {boolean} [Locked] Whether to lock the customized sync settings
*/
/**
* Apply Sync settings
*
* @param {EnterprisePoliciesManager} manager
* @param {SyncPolicyParams} params
*
* @returns {Promise<void>} Resolves once all Sync settings have been applied.
*/
async applySettings(manager, params) {
lazy.log.debug("Apply Sync Settings");
// This might be an update to the Sync policy
// so restore previous sync settings
this.restoreSettings(manager);
const {
Enabled: shouldEnableSync,
Locked: shouldLock,
...typeSettings
} = params;
const isSyncEnabled = this.isSyncEnabled();
if (shouldEnableSync === true) {
lazy.log.debug("Enable Sync");
if (!isSyncEnabled) {
await this.connectSync(manager);
}
} else if (shouldEnableSync === false) {
lazy.log.debug("Disable Sync");
if (isSyncEnabled) {
await this.disconnectSync(manager);
}
}
for (const [type, value] of Object.entries(typeSettings)) {
const pref = ENGINE_PREFS[type];
if (shouldLock) {
lazy.log.debug(`Setting and locking ${type}: ${pref} : ${value}`);
setAndLockPref(pref, value);
continue;
}
lazy.log.debug(`Setting ${type}: ${pref} : ${value}`);
PoliciesUtils.setDefaultPref(pref, value, false);
}
// Only lock the Sync feature if 'Enabled' is configured
if (shouldLock && shouldEnableSync !== undefined) {
manager.disallowFeature(SYNC_FEATURE);
}
},
/**
* Restore initial sync state.
*
* @param {EnterprisePoliciesManager} manager
*/
async restoreSettings(manager) {
if (!Services.policies.isAllowed(SYNC_FEATURE)) {
manager.allowFeature(SYNC_FEATURE);
}
for (const pref of Object.values(ENGINE_PREFS)) {
lazy.log.debug(`Unsetting ${pref}`);
unsetAndUnlockPref(pref);
}
// We don't have a way yet to restore the pre-policy sync
// state (Bug 2017719). So for now we fallback to sync enabled.
this.connectSync()
},
/**
* Disconnect sync
*/
async disconnectSync() {
try {
await lazy.Weave.Service.promiseInitialized;
await lazy.Weave.Service.startOver();
} catch (e) {
lazy.log.error(`Failed to disconnect sync: ${e}`)
}
},
/**
* Connect sync
*/
async connectSync() {
try {
await lazy.Weave.Service.configure();
} catch (e) {
lazy.log.error(`Failed to connect sync: ${e}`)
}
},
};