@@ -11,21 +11,119 @@ ChromeUtils.defineLazyGetter(lazy, "localization", () => {
1111ChromeUtils . defineESModuleGetters ( lazy , {
1212 BrowserUtils : "resource://gre/modules/BrowserUtils.sys.mjs" ,
1313 ConsoleClient : "resource:///modules/enterprise/ConsoleClient.sys.mjs" ,
14+ EnterpriseCommon : "resource:///modules/enterprise/EnterpriseCommon.sys.mjs" ,
15+ UIState : "resource://services-sync/UIState.sys.mjs" ,
16+ Weave : "resource://services-sync/main.sys.mjs" ,
17+ } ) ;
18+
19+ ChromeUtils . defineLazyGetter ( lazy , "log" , ( ) => {
20+ return console . createInstance ( {
21+ prefix : "EnterpriseHandler" ,
22+ maxLogLevelPref : lazy . EnterpriseCommon . ENTERPRISE_LOGLEVEL_PREF ,
23+ } ) ;
1424} ) ;
1525
1626const PROMPT_ON_SIGNOUT_PREF = "enterprise.promptOnSignout" ;
27+ const ENTERPRISE_SYNC_ENABLED_PREF = "enterprise.sync.enabledByDefault" ;
1728
1829export const EnterpriseHandler = {
1930 /**
2031 * @type {{name:string, email:string, pictureUrl:string} | null }
2132 */
2233 _signedInUser : null ,
2334
24- async init ( window ) {
25- await this . initUser ( ) ;
35+ /**
36+ * Whether the handler is initialized, hence we have retrieved the
37+ * user information and initialized the sync state.
38+ */
39+ _isInitialized : false ,
2640
27- this . hideFxaToolbarButton ( window ) ;
41+ /**
42+ * Handles the enterprise state for each new browser window.
43+ * On first call:
44+ * - Make a request to the console to retrieve the user information of the signed in user.
45+ * - Configure sync to be enabled or disable (depending on ENTERPRISE_SYNC_ENABLED_PREF)
46+ * On every call:
47+ * - Hide FxA toolbar button and FxA item in app menu (hamburger menu)
48+ *
49+ * @param {Window } window chrome window
50+ */
51+ async init ( window ) {
52+ if ( ! this . _isInitialized ) {
53+ lazy . log . debug ( "Initializing..." ) ;
54+ await this . initUser ( ) ;
55+ this . setupSyncOnceInitialized ( window ) ;
56+ }
2857 this . updateBadge ( window ) ;
58+ this . restrictEnterpriseView ( window ) ;
59+ this . _isInitialized = true ;
60+ } ,
61+
62+ /**
63+ * Check if the FxA state is initialised yet.
64+ * - If the state is still undefined, listen for a state update
65+ * and set up once the state update occurs.
66+ * - If the state is initialized, set up sync immediately.
67+ *
68+ * @param {Window } window chrome window
69+ */
70+ setupSyncOnceInitialized ( window ) {
71+ const status = lazy . UIState . get ( ) . status ;
72+ if ( status === lazy . UIState . get ( ) . STATUS_NOT_CONFIGURED ) {
73+ // State not configured yet.
74+ lazy . log . debug ( "Waiting for FxA/Sync status to be updated" ) ;
75+ const syncStateObserver = ( _ , topic ) => {
76+ switch ( topic ) {
77+ case lazy . UIState . ON_UPDATE :
78+ lazy . log . debug ( "Sync state has been initialized" ) ;
79+ this . setUpSync ( window ) ;
80+ Services . obs . removeObserver (
81+ syncStateObserver ,
82+ lazy . UIState . ON_UPDATE
83+ ) ;
84+ break ;
85+ default :
86+ break ;
87+ }
88+ } ;
89+ Services . obs . addObserver ( syncStateObserver , lazy . UIState . ON_UPDATE ) ;
90+ return ;
91+ }
92+ this . setUpSync ( ) ;
93+ } ,
94+
95+ /**
96+ * Align sync state with expected state (ENTERPRISE_SYNC_ENABLED_PREF)
97+ * by enabling or disabling sync.
98+ *
99+ * @param {Window } window chrome window
100+ */
101+ setUpSync ( window ) {
102+ lazy . log . debug ( "Handling sync state." ) ;
103+ const isSyncCurrentlyEnabled = lazy . UIState . get ( ) . syncEnabled ;
104+ const isEnableSync = Services . prefs . getBoolPref (
105+ ENTERPRISE_SYNC_ENABLED_PREF ,
106+ false
107+ ) ;
108+
109+ if ( isSyncCurrentlyEnabled === isEnableSync ) {
110+ // Nothing to do
111+ lazy . log . debug (
112+ `Not changing sync state. It was already ${ isSyncCurrentlyEnabled ? "enabled" : "disabled" } `
113+ ) ;
114+ return ;
115+ }
116+
117+ if ( isEnableSync ) {
118+ lazy . log . debug ( `Connect sync.` ) ;
119+ lazy . Weave . Service . configure ( ) ;
120+ } else {
121+ lazy . log . debug ( `Disconnect sync.` ) ;
122+ window . gSync . disconnect ( {
123+ confirm : false ,
124+ disconnectAccount : false ,
125+ } ) ;
126+ }
29127 } ,
30128
31129 async initUser ( ) {
@@ -42,6 +140,11 @@ export const EnterpriseHandler = {
42140 }
43141 } ,
44142
143+ /**
144+ * Updates the user icon
145+ *
146+ * @param {Window } window chrome window
147+ */
45148 updateBadge ( window ) {
46149 const userIcon = window . document . querySelector ( "#enterprise-user-icon" ) ;
47150
@@ -99,11 +202,17 @@ export const EnterpriseHandler = {
99202 }
100203 } ,
101204
102- // TODO: FxA shows up in a lot of different areas. For now we hide the most prominent
103- // toolbar button. How to hide or integrate with Fxa and Sync to be determined.
104- hideFxaToolbarButton ( window ) {
105- const fxaBtn = window . document . getElementById ( "fxa-toolbar-menu-button" ) ;
106- fxaBtn . hidden = true ;
205+ /**
206+ * Hide away FxA appearances in the toolbar and the app menu (hamburger menu)
207+ *
208+ * @param {Window } window chrome window
209+ */
210+ restrictEnterpriseView ( window ) {
211+ // Hides fxa toolbar button
212+ Services . prefs . setBoolPref ( "identity.fxaccounts.toolbar.enabled" , false ) ;
213+
214+ // Hides fxa item and separator in main view (hamburg menu)
215+ window . PanelUI . mainView . setAttribute ( "restricted-enterprise-view" , true ) ;
107216 } ,
108217
109218 async onSignOut ( window ) {
@@ -168,4 +277,9 @@ export const EnterpriseHandler = {
168277 Services . startup . quit ( Ci . nsIAppStartup . eForceQuit ) ;
169278 }
170279 } ,
280+
281+ uninit ( ) {
282+ this . _signedInUser = { } ;
283+ this . _isInitialized = false ;
284+ } ,
171285} ;
0 commit comments