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" ;
11+
12+ const SYNC_STATUS_OK = ChromeUtils . importESModule (
13+ "resource://services-sync/constants.sys.mjs"
14+ ) . STATUS_OK
615
16+
17+ const lazy = { } ;
718ChromeUtils . defineESModuleGetters ( lazy , {
8- PREF_LOGLEVEL : "resource:///modules/policies/Policies.sys.mjs" ,
9- setAndLockPref : "resource:///modules/policies/Policies.sys.mjs" ,
10- STATUS_OK : "resource://services-sync/constants.sys.mjs" ,
11- unsetAndUnlockPref : "resource:///modules/policies/Policies.sys.mjs" ,
1219 Weave : "resource://services-sync/main.sys.mjs" ,
1320} ) ;
1421
1522ChromeUtils . defineLazyGetter ( lazy , "log" , ( ) => {
1623 return console . createInstance ( {
17- prefix : "SyncSettingsPolicy " ,
18- maxLogLevelPref : lazy . PREF_LOGLEVEL ,
24+ prefix : "SyncPolicy " ,
25+ maxLogLevelPref : PREF_LOGLEVEL ,
1926 } ) ;
2027} ) ;
2128
@@ -30,145 +37,130 @@ const ENGINE_PREFS = {
3037 settings : "services.sync.engine.prefs" ,
3138} ;
3239
33- const STATE = {
34- DEFAULT : "default" ,
35- SYNC_ENABLED : "enabled" ,
36- SYNC_DISABLED : "disabled" ,
37- POLICY_NOT_APPLIED : "policy-not-applied" ,
38- } ;
40+ const SYNC_FEATURE = "change-sync-state" ;
3941
4042/**
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.
43+ * Customizes Sync settings (all settings are optional):
44+ * - Whether sync is enabled/disabled
45+ * - Which types of data to sync
46+ * - Whether to lock the sync customization
47+ * See SyncPolicyParams for details.
4448 */
45- export const SyncSettingsPolicy = {
46- _isSyncEnabledDefaultValue : null ,
47- _currentPolicyState : null ,
48-
49+ export const SyncPolicy = {
4950 /**
5051 * Get current sync state.
5152 *
5253 * @returns {boolean } Whether sync is currently enabled.
5354 */
5455 isSyncEnabled ( ) {
55- return lazy . Weave . Status . checkSetup ( ) == lazy . STATUS_OK ;
56+ return lazy . Weave . Status . checkSetup ( ) == SYNC_STATUS_OK ;
5657 } ,
5758
5859 /**
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.
60+ * @typedef {object } SyncPolicyParams
61+ * @property {boolean } [Enabled] Whether Sync should be enabled
62+ * @property {boolean } [addresses] Whether syncing addresses should be enabled
63+ * @property {boolean } [bookmarks] Whether syncing bookmarks should be enabled
64+ * @property {boolean } [history] Whether syncing history should be enabled
65+ * @property {boolean } [openTabs] Whether syncing openTabs should be enabled
66+ * @property {boolean } [passwords] Whether syncing passwords should be enabled
67+ * @property {boolean } [paymentMethods] Whether syncing paymentMethods should be enabled
68+ * @property {boolean } [addons] Whether syncing addons should be enabled
69+ * @property {boolean } [settings] Whether syncing settings should be enabled
70+ * @property {boolean } [Locked] Whether to lock the customized sync settings
6371 */
6472
6573 /**
66- * Apply policy Sync settings to the current profile and prevent changes to
67- * the Sync state while the policy is active.
74+ * Apply Sync settings
6875 *
6976 * @param {EnterprisePoliciesManager } manager
70- * @param {SyncSettings } param
77+ * @param {SyncPolicyParams } params
7178 *
7279 * @returns {Promise<void> } Resolves once all Sync settings have been applied.
7380 */
74- async applySettings ( manager , param ) {
81+ async applySettings ( manager , params ) {
7582 lazy . log . debug ( "Apply Sync Settings" ) ;
7683
84+ // This might be an update to the Sync policy
85+ // so restore previous sync settings
86+ this . restoreSettings ( manager ) ;
87+
88+ const {
89+ Enabled : shouldEnableSync ,
90+ Locked : shouldLock ,
91+ ...typeSettings
92+ } = params ;
93+
7794 const isSyncEnabled = this . isSyncEnabled ( ) ;
78- if ( this . _isSyncEnabledDefaultValue === null ) {
79- // Cache initial sync state
80- this . _isSyncEnabledDefaultValue = isSyncEnabled ;
81- }
8295
83- if ( ! param . SyncEnabled ) {
84- lazy . log . debug ( "Force-disable Sync" ) ;
96+ if ( shouldEnableSync === true ) {
97+ lazy . log . debug ( "Enable Sync" ) ;
98+ if ( ! isSyncEnabled ) {
99+ await this . connectSync ( manager ) ;
100+ }
101+ } else if ( shouldEnableSync === false ) {
102+ lazy . log . debug ( "Disable Sync" ) ;
85103 if ( isSyncEnabled ) {
86104 await this . disconnectSync ( manager ) ;
87105 }
88- return ;
89106 }
90107
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 ) ;
108+ for ( const [ type , value ] of Object . entries ( typeSettings ) ) {
109+ const pref = ENGINE_PREFS [ type ] ;
110+ if ( shouldLock ) {
111+ lazy . log . debug ( `Setting and locking ${ type } : ${ pref } : ${ value } ` ) ;
112+ setAndLockPref ( pref , value ) ;
113+ continue ;
100114 }
115+ lazy . log . debug ( `Setting ${ type } : ${ pref } : ${ value } ` ) ;
116+ PoliciesUtils . setDefaultPref ( pref , value , false ) ;
101117 }
102118
103- await this . connectSync ( manager ) ;
104-
105- this . _currentPolicyState = STATE . SYNC_ENABLED ;
119+ // Only lock the Sync feature if 'Enabled' is configured
120+ if ( shouldLock && shouldEnableSync !== undefined ) {
121+ manager . disallowFeature ( SYNC_FEATURE ) ;
122+ }
106123 } ,
107124
108125 /**
109- * Restore Sync preferences and state to what they were before policy enforcement,
110- * and re-allow changes to the Sync state.
126+ * Restore initial sync state.
111127 *
112128 * @param {EnterprisePoliciesManager } manager
113129 */
114130 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 ) ;
131+ if ( ! Services . policies . isAllowed ( SYNC_FEATURE ) ) {
132+ manager . allowFeature ( SYNC_FEATURE ) ;
121133 }
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 ) {
132134 for ( const pref of Object . values ( ENGINE_PREFS ) ) {
133- lazy . unsetAndUnlockPref ( pref ) ;
135+ lazy . log . debug ( `Unsetting ${ pref } ` ) ;
136+ unsetAndUnlockPref ( pref ) ;
134137 }
135138
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 ) ;
146- }
147-
148- this . _isSyncEnabledDefaultValue = null ;
149- manager . allowFeature ( "change-sync-state" ) ;
139+ // We don't have a way yet to restore the pre-policy sync
140+ // state (Bug 2017719). So for now we fallback to sync enabled.
141+ this . connectSync ( )
150142 } ,
151143
152144 /**
153- * Disconnect Sync and disallow any changes to the sync state.
154- *
155- * @param {EnterprisePoliciesManager } manager
145+ * Disconnect sync
156146 */
157- async disconnectSync ( manager ) {
158- manager . allowFeature ( "change-sync-state" ) ;
159- await lazy . Weave . Service . promiseInitialized ;
160- await lazy . Weave . Service . startOver ( ) ;
161- manager . disallowFeature ( "change-sync-state" ) ;
147+ async disconnectSync ( ) {
148+ try {
149+ await lazy . Weave . Service . promiseInitialized ;
150+ await lazy . Weave . Service . startOver ( ) ;
151+ } catch ( e ) {
152+ lazy . log . error ( `Failed to disconnect sync: ${ e } ` )
153+ }
162154 } ,
163155
164156 /**
165- * Connect Sync and disallow any changes to the sync state.
166- *
167- * @param {EnterprisePoliciesManager } manager
157+ * Connect sync
168158 */
169- async connectSync ( manager ) {
170- manager . allowFeature ( "change-sync-state" ) ;
171- await lazy . Weave . Service . configure ( ) ;
172- manager . disallowFeature ( "change-sync-state" ) ;
159+ async connectSync ( ) {
160+ try {
161+ await lazy . Weave . Service . configure ( ) ;
162+ } catch ( e ) {
163+ lazy . log . error ( `Failed to connect sync: ${ e } ` )
164+ }
173165 } ,
174166} ;
0 commit comments