Skip to content

Commit 1ab7ce9

Browse files
committed
Update Sync policy schema
1 parent aebf2aa commit 1ab7ce9

File tree

6 files changed

+103
-119
lines changed

6 files changed

+103
-119
lines changed

browser/components/enterprisepolicies/Policies.sys.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ChromeUtils.defineESModuleGetters(lazy, {
3333
WebsiteFilter: "resource:///modules/policies/WebsiteFilter.sys.mjs",
3434
});
3535

36-
const PREF_LOGLEVEL = "browser.policies.loglevel";
36+
export const PREF_LOGLEVEL = "browser.policies.loglevel";
3737
const BROWSER_DOCUMENT_URL = AppConstants.BROWSER_CHROME_URL;
3838
const ABOUT_CONTRACT = "@mozilla.org/network/protocol/about;1?what=";
3939

browser/components/enterprisepolicies/helpers/SyncPolicy.sys.mjs

Lines changed: 69 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
33
* You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
const lazy = {};
5+
import {
6+
PREF_LOGLEVEL,
7+
setAndLockPref,
8+
unsetAndUnlockPref,
9+
PoliciesUtils,
10+
} from "resource:///modules/policies/Policies.sys.mjs";
611

12+
const lazy = {};
713
ChromeUtils.defineESModuleGetters(lazy, {
8-
PREF_LOGLEVEL: "resource:///modules/policies/Policies.sys.mjs",
9-
setAndLockPref: "resource:///modules/policies/Policies.sys.mjs",
1014
STATUS_OK: "resource://services-sync/constants.sys.mjs",
11-
unsetAndUnlockPref: "resource:///modules/policies/Policies.sys.mjs",
1215
Weave: "resource://services-sync/main.sys.mjs",
1316
});
1417

1518
ChromeUtils.defineLazyGetter(lazy, "log", () => {
1619
return console.createInstance({
17-
prefix: "SyncSettingsPolicy",
18-
maxLogLevelPref: lazy.PREF_LOGLEVEL,
20+
prefix: "SyncPolicy",
21+
maxLogLevelPref: PREF_LOGLEVEL,
1922
});
2023
});
2124

@@ -30,22 +33,16 @@ const ENGINE_PREFS = {
3033
settings: "services.sync.engine.prefs",
3134
};
3235

33-
const STATE = {
34-
DEFAULT: "default",
35-
SYNC_ENABLED: "enabled",
36-
SYNC_DISABLED: "disabled",
37-
POLICY_NOT_APPLIED: "policy-not-applied",
38-
};
36+
const SYNC_FEATURE = "change-sync-state";
3937

4038
/**
41-
* Policy to control the Sync state (force-enable or force-disable Sync)
42-
* and to control which data types are synced. The user is not able to
43-
* customize Sync settings when this policy is active.
39+
* Customizes Sync settings (all settings are optional):
40+
* - Whether sync is enabled/disabled
41+
* - Which types of data to sync
42+
* - Whether to lock the sync customization
43+
* See SyncPolicyParams for details.
4444
*/
45-
export const SyncSettingsPolicy = {
46-
_isSyncEnabledDefaultValue: null,
47-
_currentPolicyState: null,
48-
45+
export const SyncPolicy = {
4946
/**
5047
* Get current sync state.
5148
*
@@ -56,119 +53,99 @@ export const SyncSettingsPolicy = {
5653
},
5754

5855
/**
59-
* @typedef {object} SyncSettings
60-
* @property {boolean} SyncEnabled Whether Sync should be force-enabled or force-disabled.
61-
* @property {Array<"addresses"|"bookmarks"|"history"|"openTabs"|"passwords"|"paymentMethods"|"addons"|"settings">} TypesEnabled
62-
* Which data types should be synced when Sync is force-enabled.
56+
* @typedef {object} SyncPolicyParams
57+
* @property {boolean} [Enabled] Whether Sync should be enabled
58+
* @property {boolean} [addresses] Whether syncing addresses should be enabled
59+
* @property {boolean} [bookmarks] Whether syncing bookmarks should be enabled
60+
* @property {boolean} [history] Whether syncing history should be enabled
61+
* @property {boolean} [openTabs] Whether syncing openTabs should be enabled
62+
* @property {boolean} [passwords] Whether syncing passwords should be enabled
63+
* @property {boolean} [paymentMethods] Whether syncing paymentMethods should be enabled
64+
* @property {boolean} [addons] Whether syncing addons should be enabled
65+
* @property {boolean} [settings] Whether syncing settings should be enabled
66+
* @property {boolean} [Locked] Whether to lock the sync customization
6367
*/
6468

6569
/**
66-
* Apply policy Sync settings to the current profile and prevent changes to
67-
* the Sync state while the policy is active.
70+
* Apply Sync settings
6871
*
6972
* @param {EnterprisePoliciesManager} manager
70-
* @param {SyncSettings} param
73+
* @param {SyncPolicyParams} params
7174
*
7275
* @returns {Promise<void>} Resolves once all Sync settings have been applied.
7376
*/
74-
async applySettings(manager, param) {
77+
async applySettings(manager, params) {
7578
lazy.log.debug("Apply Sync Settings");
7679

80+
// This might be an update to the Sync policy
81+
// so restore previous sync settings
82+
this.restoreSettings(manager);
83+
84+
const {
85+
Enabled: isEnableSync,
86+
Locked: isLockSettings,
87+
...typeSettings
88+
} = params;
89+
7790
const isSyncEnabled = this.isSyncEnabled();
78-
if (this._isSyncEnabledDefaultValue === null) {
79-
// Cache initial sync state
80-
this._isSyncEnabledDefaultValue = isSyncEnabled;
81-
}
8291

83-
if (!param.SyncEnabled) {
84-
lazy.log.debug("Force-disable Sync");
92+
if (isEnableSync === true) {
93+
lazy.log.debug("Enable Sync");
94+
if (!isSyncEnabled) {
95+
await this.connectSync(manager);
96+
}
97+
} else if (isEnableSync === false) {
98+
lazy.log.debug("Disable Sync");
8599
if (isSyncEnabled) {
86100
await this.disconnectSync(manager);
87101
}
88-
return;
89102
}
90103

91-
lazy.log.debug("Force-enable Sync");
92-
93-
for (const [type, pref] of Object.entries(ENGINE_PREFS)) {
94-
if (param.TypesEnabled.includes(type)) {
95-
lazy.log.debug(`Enabling type: ${type}`);
96-
lazy.setAndLockPref(pref, true);
97-
} else {
98-
lazy.log.debug(`Disabling type: ${type}`);
99-
lazy.setAndLockPref(pref, false);
104+
for (const [type, value] of Object.entries(typeSettings)) {
105+
const pref = ENGINE_PREFS[type];
106+
if (isLockSettings) {
107+
lazy.log.debug(`Setting and locking ${type}: ${pref} : ${value}`);
108+
setAndLockPref(pref, value);
109+
continue;
100110
}
111+
lazy.log.debug(`Setting ${type}: ${pref} : ${value}`);
112+
PoliciesUtils.setDefaultPref(pref, value, false);
101113
}
102114

103-
await this.connectSync(manager);
104-
105-
this._currentPolicyState = STATE.SYNC_ENABLED;
115+
// Only lock the Sync feature if 'Enabled' is configured
116+
if (isLockSettings && isEnableSync !== undefined) {
117+
manager.disallowFeature(SYNC_FEATURE);
118+
}
106119
},
107120

108121
/**
109-
* Restore Sync preferences and state to what they were before policy enforcement,
110-
* and re-allow changes to the Sync state.
122+
* Restore initial sync state.
111123
*
112124
* @param {EnterprisePoliciesManager} manager
113125
*/
114126
async restoreSettings(manager) {
115-
lazy.log.debug("Restore Sync Settings");
116-
117-
if (this._currentPolicyState !== STATE.DEFAULT) {
118-
// Only restore the default state if the current state
119-
// isn't already the default state.
120-
this.restoreDefault(manager);
127+
if (!Services.policies.isAllowed(SYNC_FEATURE)) {
128+
manager.allowFeature(SYNC_FEATURE);
121129
}
122-
123-
this._currentPolicyState = STATE.POLICY_NOT_APPLIED;
124-
},
125-
126-
/**
127-
* Restore default state
128-
*
129-
* @param {EnterprisePoliciesManager} manager
130-
*/
131-
async restoreDefault(manager) {
132130
for (const pref of Object.values(ENGINE_PREFS)) {
133-
lazy.unsetAndUnlockPref(pref);
134-
}
135-
136-
const isSyncEnabled = this.isSyncEnabled();
137-
138-
if (this._isSyncEnabledDefaultValue) {
139-
// Re-connecting to re-trigger a Sync action with the
140-
// restored default enabled types.
141-
lazy.log.debug("Restoring Sync state by enabling it.");
142-
await this.connectSync(manager);
143-
} else if (!this._isSyncEnabledDefaultValue && isSyncEnabled) {
144-
lazy.log.debug("Restoring Sync state by disabling it.");
145-
await this.disconnectSync(manager);
131+
lazy.log.debug(`Unsetting ${pref}`);
132+
unsetAndUnlockPref(pref);
146133
}
147-
148-
this._isSyncEnabledDefaultValue = null;
149-
manager.allowFeature("change-sync-state");
134+
// TODO: Restoring to sync being enabled or disabled?
150135
},
151136

152137
/**
153-
* Disconnect Sync and disallow any changes to the sync state.
154-
*
155-
* @param {EnterprisePoliciesManager} manager
138+
* Disconnect sync
156139
*/
157-
async disconnectSync(manager) {
158-
manager.allowFeature("change-sync-state");
140+
async disconnectSync() {
159141
await lazy.Weave.Service.promiseInitialized;
160142
await lazy.Weave.Service.startOver();
161-
manager.disallowFeature("change-sync-state");
162143
},
163144

164145
/**
165-
* Connect Sync and disallow any changes to the sync state.
166-
*
167-
* @param {EnterprisePoliciesManager} manager
146+
* Connect sync
168147
*/
169-
async connectSync(manager) {
170-
manager.allowFeature("change-sync-state");
148+
async connectSync() {
171149
await lazy.Weave.Service.configure();
172-
manager.disallowFeature("change-sync-state");
173150
},
174151
};

browser/components/enterprisepolicies/schemas/policies-schema.json

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,27 +1700,36 @@
17001700
"Sync": {
17011701
"type": "object",
17021702
"properties": {
1703-
"State": {
1704-
"type": "string",
1705-
"enum": ["enabled", "disabled", "default"]
1703+
"Enabled": {
1704+
"type": "boolean"
17061705
},
1707-
"TypesEnabled": {
1708-
"type": "array",
1709-
"items": {
1710-
"type": "string",
1711-
"enum": [
1712-
"addresses",
1713-
"bookmarks",
1714-
"history",
1715-
"openTabs",
1716-
"passwords",
1717-
"paymentMethods",
1718-
"addons",
1719-
"settings"
1720-
]
1721-
}
1706+
"addresses": {
1707+
"type": "boolean"
1708+
},
1709+
"bookmarks": {
1710+
"type": "boolean"
17221711
},
1723-
"required": ["State"]
1712+
"history": {
1713+
"type": "boolean"
1714+
},
1715+
"openTabs": {
1716+
"type": "boolean"
1717+
},
1718+
"passwords": {
1719+
"type": "boolean"
1720+
},
1721+
"paymentMethods": {
1722+
"type": "boolean"
1723+
},
1724+
"addons": {
1725+
"type": "boolean"
1726+
},
1727+
"settings": {
1728+
"type": "boolean"
1729+
},
1730+
"Locked": {
1731+
"type": "boolean"
1732+
}
17241733
}
17251734
},
17261735

browser/components/preferences/dialogs/syncChooseWhatToSync.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ let gSyncChooseWhatToSync = {
2828
this._setupEventListeners();
2929
this._adjustForPrefs();
3030
let options = window.arguments[0];
31-
if (options.disconnectFun) {
31+
if (
32+
options.disconnectFun &&
33+
Services.policies.isAllowed("change-sync-state")
34+
) {
3235
// Offer 'Disconnect' functionality if it was provided
3336
document.addEventListener("dialogextra2", function () {
3437
options.disconnectFun().then(disconnected => {

browser/components/preferences/sync.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -911,11 +911,7 @@ var gSyncPane = {
911911
);
912912
connectAnotherDeviceLink.setAttribute("restricted-enterprise-view", true);
913913

914-
if ("SyncSettings" in (Services.policies.getActivePolicies() || {})) {
915-
// Hide "Manage Sync" button (visible when Sync is enabled)
916-
const manageSyncButton = document.getElementById("syncChangeOptions");
917-
manageSyncButton.setAttribute("restricted-enterprise-view", true);
918-
914+
if (!Services.policies.isAllowed("change-sync-state")) {
919915
// Hide info box and "Turn on syncing..."" button (visible when Sync is disabled)
920916
const syncOffBox = document.getElementById("syncNotConfigured");
921917
syncOffBox.setAttribute("restricted-enterprise-view", true);

browser/themes/shared/preferences/preferences.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,6 @@ setting-group[groupid="aiStatesDescription"] {
16071607
#fxaUnlinkButton[restricted-enterprise-view],
16081608
#verifiedManage[restricted-enterprise-view],
16091609
#connect-another-device[restricted-enterprise-view],
1610-
#syncChangeOptions[restricted-enterprise-view],
16111610
#syncNotConfigured[restricted-enterprise-view] {
16121611
display: none;
16131612
}

0 commit comments

Comments
 (0)