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 = { } ;
713ChromeUtils . 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
1518ChromeUtils . 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} ;
0 commit comments