-
Notifications
You must be signed in to change notification settings - Fork 26
Introduce policy Sync to customize sync state #313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
806ddba
924aa8d
a7f58fc
56e5d77
e5c16b1
ae753f0
d4596cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can throw from here: https://searchfox.org/firefox-main/rev/150ca809c7243bd6e994f28cc4a3750430783364/services/sync/modules/service.sys.mjs#977 Not sure if it can reasonably happen in our setup, but it would mean all the other policy/settings enforcement code below is skipped. We might want to eat the error and rethrow after we did that?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you suggest doing anything in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a first step I wrapped |
||
| } | ||
| } 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}`) | ||
| } | ||
| }, | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An alternative here is to define this as normal and do the
MOZ_ENTERPRISEcheck within the policy to make it non-functional in the non-enterprise case. That might make it easier to find this as it then appears in the normal alphabetical list of policy declarations. But I don't have a strong opinion, leave this as it is if you likeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The stretch goal is land this in m-c (Bug 2017722), so this should be temporary anyway.