michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: // gSyncUI handles updating the tools menu and displaying notifications. michael@0: let gSyncUI = { michael@0: DEFAULT_EOL_URL: "https://www.mozilla.org/firefox/?utm_source=synceol", michael@0: michael@0: _obs: ["weave:service:sync:start", michael@0: "weave:service:quota:remaining", michael@0: "weave:service:setup-complete", michael@0: "weave:service:login:start", michael@0: "weave:service:login:finish", michael@0: "weave:service:logout:finish", michael@0: "weave:service:start-over", michael@0: "weave:service:start-over:finish", michael@0: "weave:ui:login:error", michael@0: "weave:ui:sync:error", michael@0: "weave:ui:sync:finish", michael@0: "weave:ui:clear-error", michael@0: "weave:eol", michael@0: ], michael@0: michael@0: _unloaded: false, michael@0: michael@0: init: function () { michael@0: Cu.import("resource://services-common/stringbundle.js"); michael@0: michael@0: // Proceed to set up the UI if Sync has already started up. michael@0: // Otherwise we'll do it when Sync is firing up. michael@0: let xps = Components.classes["@mozilla.org/weave/service;1"] michael@0: .getService(Components.interfaces.nsISupports) michael@0: .wrappedJSObject; michael@0: if (xps.ready) { michael@0: this.initUI(); michael@0: return; michael@0: } michael@0: michael@0: Services.obs.addObserver(this, "weave:service:ready", true); michael@0: michael@0: // Remove the observer if the window is closed before the observer michael@0: // was triggered. michael@0: window.addEventListener("unload", function onUnload() { michael@0: gSyncUI._unloaded = true; michael@0: window.removeEventListener("unload", onUnload, false); michael@0: Services.obs.removeObserver(gSyncUI, "weave:service:ready"); michael@0: michael@0: if (Weave.Status.ready) { michael@0: gSyncUI._obs.forEach(function(topic) { michael@0: Services.obs.removeObserver(gSyncUI, topic); michael@0: }); michael@0: } michael@0: }, false); michael@0: }, michael@0: michael@0: initUI: function SUI_initUI() { michael@0: // If this is a browser window? michael@0: if (gBrowser) { michael@0: this._obs.push("weave:notification:added"); michael@0: } michael@0: michael@0: this._obs.forEach(function(topic) { michael@0: Services.obs.addObserver(this, topic, true); michael@0: }, this); michael@0: michael@0: if (gBrowser && Weave.Notifications.notifications.length) { michael@0: this.initNotifications(); michael@0: } michael@0: this.updateUI(); michael@0: }, michael@0: michael@0: initNotifications: function SUI_initNotifications() { michael@0: const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; michael@0: let notificationbox = document.createElementNS(XULNS, "notificationbox"); michael@0: notificationbox.id = "sync-notifications"; michael@0: notificationbox.setAttribute("flex", "1"); michael@0: michael@0: let bottombox = document.getElementById("browser-bottombox"); michael@0: bottombox.insertBefore(notificationbox, bottombox.firstChild); michael@0: michael@0: // Force a style flush to ensure that our binding is attached. michael@0: notificationbox.clientTop; michael@0: michael@0: // notificationbox will listen to observers from now on. michael@0: Services.obs.removeObserver(this, "weave:notification:added"); michael@0: }, michael@0: michael@0: _needsSetup: function SUI__needsSetup() { michael@0: // We want to treat "account needs verification" as "needs setup". So michael@0: // "reach in" to Weave.Status._authManager to check whether we the signed-in michael@0: // user is verified. michael@0: // Referencing Weave.Status spins a nested event loop to initialize the michael@0: // authManager, so this should always return a value directly. michael@0: // This only applies to fxAccounts-based Sync. michael@0: if (Weave.Status._authManager._signedInUser) { michael@0: // If we have a signed in user already, and that user is not verified, michael@0: // revert to the "needs setup" state. michael@0: if (!Weave.Status._authManager._signedInUser.verified) { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: let firstSync = ""; michael@0: try { michael@0: firstSync = Services.prefs.getCharPref("services.sync.firstSync"); michael@0: } catch (e) { } michael@0: michael@0: return Weave.Status.checkSetup() == Weave.CLIENT_NOT_CONFIGURED || michael@0: firstSync == "notReady"; michael@0: }, michael@0: michael@0: _loginFailed: function () { michael@0: return Weave.Status.login == Weave.LOGIN_FAILED_LOGIN_REJECTED; michael@0: }, michael@0: michael@0: updateUI: function SUI_updateUI() { michael@0: let needsSetup = this._needsSetup(); michael@0: let loginFailed = this._loginFailed(); michael@0: michael@0: // Start off with a clean slate michael@0: document.getElementById("sync-reauth-state").hidden = true; michael@0: document.getElementById("sync-setup-state").hidden = true; michael@0: document.getElementById("sync-syncnow-state").hidden = true; michael@0: michael@0: if (loginFailed) { michael@0: document.getElementById("sync-reauth-state").hidden = false; michael@0: } else if (needsSetup) { michael@0: document.getElementById("sync-setup-state").hidden = false; michael@0: } else { michael@0: document.getElementById("sync-syncnow-state").hidden = false; michael@0: } michael@0: michael@0: if (!gBrowser) michael@0: return; michael@0: michael@0: let syncButton = document.getElementById("sync-button"); michael@0: let panelHorizontalButton = document.getElementById("PanelUI-fxa-status"); michael@0: [syncButton, panelHorizontalButton].forEach(function(button) { michael@0: if (!button) michael@0: return; michael@0: button.removeAttribute("status"); michael@0: }); michael@0: michael@0: if (needsSetup && syncButton) michael@0: syncButton.removeAttribute("tooltiptext"); michael@0: michael@0: this._updateLastSyncTime(); michael@0: }, michael@0: michael@0: michael@0: // Functions called by observers michael@0: onActivityStart: function SUI_onActivityStart() { michael@0: if (!gBrowser) michael@0: return; michael@0: michael@0: ["sync-button", "PanelUI-fxa-status"].forEach(function(id) { michael@0: let button = document.getElementById(id); michael@0: if (!button) michael@0: return; michael@0: button.setAttribute("status", "active"); michael@0: }); michael@0: }, michael@0: michael@0: onLoginFinish: function SUI_onLoginFinish() { michael@0: // Clear out any login failure notifications michael@0: let title = this._stringBundle.GetStringFromName("error.login.title"); michael@0: this.clearError(title); michael@0: }, michael@0: michael@0: onSetupComplete: function SUI_onSetupComplete() { michael@0: this.onLoginFinish(); michael@0: }, michael@0: michael@0: onLoginError: function SUI_onLoginError() { michael@0: // if login fails, any other notifications are essentially moot michael@0: Weave.Notifications.removeAll(); michael@0: michael@0: // if we haven't set up the client, don't show errors michael@0: if (this._needsSetup()) { michael@0: this.updateUI(); michael@0: return; michael@0: } michael@0: // if we are still waiting for the identity manager to initialize, don't show errors michael@0: if (Weave.Status.login == Weave.LOGIN_FAILED_NOT_READY) { michael@0: this.updateUI(); michael@0: return; michael@0: } michael@0: michael@0: let title = this._stringBundle.GetStringFromName("error.login.title"); michael@0: michael@0: let description; michael@0: if (Weave.Status.sync == Weave.PROLONGED_SYNC_FAILURE) { michael@0: // Convert to days michael@0: let lastSync = michael@0: Services.prefs.getIntPref("services.sync.errorhandler.networkFailureReportTimeout") / 86400; michael@0: description = michael@0: this._stringBundle.formatStringFromName("error.sync.prolonged_failure", [lastSync], 1); michael@0: } else { michael@0: let reason = Weave.Utils.getErrorString(Weave.Status.login); michael@0: description = michael@0: this._stringBundle.formatStringFromName("error.sync.description", [reason], 1); michael@0: } michael@0: michael@0: let buttons = []; michael@0: buttons.push(new Weave.NotificationButton( michael@0: this._stringBundle.GetStringFromName("error.login.prefs.label"), michael@0: this._stringBundle.GetStringFromName("error.login.prefs.accesskey"), michael@0: function() { gSyncUI.openPrefs(); return true; } michael@0: )); michael@0: michael@0: let notification = new Weave.Notification(title, description, null, michael@0: Weave.Notifications.PRIORITY_WARNING, buttons); michael@0: Weave.Notifications.replaceTitle(notification); michael@0: this.updateUI(); michael@0: }, michael@0: michael@0: onLogout: function SUI_onLogout() { michael@0: this.updateUI(); michael@0: }, michael@0: michael@0: onStartOver: function SUI_onStartOver() { michael@0: this.clearError(); michael@0: }, michael@0: michael@0: onQuotaNotice: function onQuotaNotice(subject, data) { michael@0: let title = this._stringBundle.GetStringFromName("warning.sync.quota.label"); michael@0: let description = this._stringBundle.GetStringFromName("warning.sync.quota.description"); michael@0: let buttons = []; michael@0: buttons.push(new Weave.NotificationButton( michael@0: this._stringBundle.GetStringFromName("error.sync.viewQuotaButton.label"), michael@0: this._stringBundle.GetStringFromName("error.sync.viewQuotaButton.accesskey"), michael@0: function() { gSyncUI.openQuotaDialog(); return true; } michael@0: )); michael@0: michael@0: let notification = new Weave.Notification( michael@0: title, description, null, Weave.Notifications.PRIORITY_WARNING, buttons); michael@0: Weave.Notifications.replaceTitle(notification); michael@0: }, michael@0: michael@0: _getAppName: function () { michael@0: let brand = new StringBundle("chrome://branding/locale/brand.properties"); michael@0: return brand.get("brandShortName"); michael@0: }, michael@0: michael@0: onEOLNotice: function (data) { michael@0: let code = data.code; michael@0: let kind = (code == "hard-eol") ? "error" : "warning"; michael@0: let url = data.url || gSyncUI.DEFAULT_EOL_URL; michael@0: michael@0: let title = this._stringBundle.GetStringFromName(kind + ".sync.eol.label"); michael@0: let description = this._stringBundle.formatStringFromName(kind + ".sync.eol.description", michael@0: [this._getAppName()], michael@0: 1); michael@0: michael@0: let buttons = []; michael@0: buttons.push(new Weave.NotificationButton( michael@0: this._stringBundle.GetStringFromName("sync.eol.learnMore.label"), michael@0: this._stringBundle.GetStringFromName("sync.eol.learnMore.accesskey"), michael@0: function() { michael@0: window.openUILinkIn(url, "tab"); michael@0: return true; michael@0: } michael@0: )); michael@0: michael@0: let priority = (kind == "error") ? Weave.Notifications.PRIORITY_WARNING : michael@0: Weave.Notifications.PRIORITY_INFO; michael@0: let notification = new Weave.Notification(title, description, null, priority, buttons); michael@0: Weave.Notifications.replaceTitle(notification); michael@0: }, michael@0: michael@0: openServerStatus: function () { michael@0: let statusURL = Services.prefs.getCharPref("services.sync.statusURL"); michael@0: window.openUILinkIn(statusURL, "tab"); michael@0: }, michael@0: michael@0: // Commands michael@0: doSync: function SUI_doSync() { michael@0: setTimeout(function() Weave.Service.errorHandler.syncAndReportErrors(), 0); michael@0: }, michael@0: michael@0: handleToolbarButton: function SUI_handleStatusbarButton() { michael@0: if (this._needsSetup()) michael@0: this.openSetup(); michael@0: else michael@0: this.doSync(); michael@0: }, michael@0: michael@0: //XXXzpao should be part of syncCommon.js - which we might want to make a module... michael@0: // To be fixed in a followup (bug 583366) michael@0: michael@0: /** michael@0: * Invoke the Sync setup wizard. michael@0: * michael@0: * @param wizardType michael@0: * Indicates type of wizard to launch: michael@0: * null -- regular set up wizard michael@0: * "pair" -- pair a device first michael@0: * "reset" -- reset sync michael@0: */ michael@0: michael@0: openSetup: function SUI_openSetup(wizardType) { michael@0: let xps = Components.classes["@mozilla.org/weave/service;1"] michael@0: .getService(Components.interfaces.nsISupports) michael@0: .wrappedJSObject; michael@0: if (xps.fxAccountsEnabled) { michael@0: fxAccounts.getSignedInUser().then(userData => { michael@0: if (userData) { michael@0: this.openPrefs(); michael@0: } else { michael@0: switchToTabHavingURI("about:accounts", true); michael@0: } michael@0: }); michael@0: } else { michael@0: let win = Services.wm.getMostRecentWindow("Weave:AccountSetup"); michael@0: if (win) michael@0: win.focus(); michael@0: else { michael@0: window.openDialog("chrome://browser/content/sync/setup.xul", michael@0: "weaveSetup", "centerscreen,chrome,resizable=no", michael@0: wizardType); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: openAddDevice: function () { michael@0: if (!Weave.Utils.ensureMPUnlocked()) michael@0: return; michael@0: michael@0: let win = Services.wm.getMostRecentWindow("Sync:AddDevice"); michael@0: if (win) michael@0: win.focus(); michael@0: else michael@0: window.openDialog("chrome://browser/content/sync/addDevice.xul", michael@0: "syncAddDevice", "centerscreen,chrome,resizable=no"); michael@0: }, michael@0: michael@0: openQuotaDialog: function SUI_openQuotaDialog() { michael@0: let win = Services.wm.getMostRecentWindow("Sync:ViewQuota"); michael@0: if (win) michael@0: win.focus(); michael@0: else michael@0: Services.ww.activeWindow.openDialog( michael@0: "chrome://browser/content/sync/quota.xul", "", michael@0: "centerscreen,chrome,dialog,modal"); michael@0: }, michael@0: michael@0: openPrefs: function SUI_openPrefs() { michael@0: openPreferences("paneSync"); michael@0: }, michael@0: michael@0: openSignInAgainPage: function () { michael@0: switchToTabHavingURI("about:accounts?action=reauth", true); michael@0: }, michael@0: michael@0: // Helpers michael@0: _updateLastSyncTime: function SUI__updateLastSyncTime() { michael@0: if (!gBrowser) michael@0: return; michael@0: michael@0: let syncButton = document.getElementById("sync-button"); michael@0: if (!syncButton) michael@0: return; michael@0: michael@0: let lastSync; michael@0: try { michael@0: lastSync = Services.prefs.getCharPref("services.sync.lastSync"); michael@0: } michael@0: catch (e) { }; michael@0: if (!lastSync || this._needsSetup()) { michael@0: syncButton.removeAttribute("tooltiptext"); michael@0: return; michael@0: } michael@0: michael@0: // Show the day-of-week and time (HH:MM) of last sync michael@0: let lastSyncDate = new Date(lastSync).toLocaleFormat("%a %H:%M"); michael@0: let lastSyncLabel = michael@0: this._stringBundle.formatStringFromName("lastSync2.label", [lastSyncDate], 1); michael@0: michael@0: syncButton.setAttribute("tooltiptext", lastSyncLabel); michael@0: }, michael@0: michael@0: clearError: function SUI_clearError(errorString) { michael@0: Weave.Notifications.removeAll(errorString); michael@0: this.updateUI(); michael@0: }, michael@0: michael@0: onSyncFinish: function SUI_onSyncFinish() { michael@0: let title = this._stringBundle.GetStringFromName("error.sync.title"); michael@0: michael@0: // Clear out sync failures on a successful sync michael@0: this.clearError(title); michael@0: }, michael@0: michael@0: onSyncError: function SUI_onSyncError() { michael@0: let title = this._stringBundle.GetStringFromName("error.sync.title"); michael@0: michael@0: if (Weave.Status.login != Weave.LOGIN_SUCCEEDED) { michael@0: this.onLoginError(); michael@0: return; michael@0: } michael@0: michael@0: let description; michael@0: if (Weave.Status.sync == Weave.PROLONGED_SYNC_FAILURE) { michael@0: // Convert to days michael@0: let lastSync = michael@0: Services.prefs.getIntPref("services.sync.errorhandler.networkFailureReportTimeout") / 86400; michael@0: description = michael@0: this._stringBundle.formatStringFromName("error.sync.prolonged_failure", [lastSync], 1); michael@0: } else { michael@0: let error = Weave.Utils.getErrorString(Weave.Status.sync); michael@0: description = michael@0: this._stringBundle.formatStringFromName("error.sync.description", [error], 1); michael@0: } michael@0: let priority = Weave.Notifications.PRIORITY_WARNING; michael@0: let buttons = []; michael@0: michael@0: // Check if the client is outdated in some way michael@0: let outdated = Weave.Status.sync == Weave.VERSION_OUT_OF_DATE; michael@0: for (let [engine, reason] in Iterator(Weave.Status.engines)) michael@0: outdated = outdated || reason == Weave.VERSION_OUT_OF_DATE; michael@0: michael@0: if (outdated) { michael@0: description = this._stringBundle.GetStringFromName( michael@0: "error.sync.needUpdate.description"); michael@0: buttons.push(new Weave.NotificationButton( michael@0: this._stringBundle.GetStringFromName("error.sync.needUpdate.label"), michael@0: this._stringBundle.GetStringFromName("error.sync.needUpdate.accesskey"), michael@0: function() { window.openUILinkIn("https://services.mozilla.com/update/", "tab"); return true; } michael@0: )); michael@0: } michael@0: else if (Weave.Status.sync == Weave.OVER_QUOTA) { michael@0: description = this._stringBundle.GetStringFromName( michael@0: "error.sync.quota.description"); michael@0: buttons.push(new Weave.NotificationButton( michael@0: this._stringBundle.GetStringFromName( michael@0: "error.sync.viewQuotaButton.label"), michael@0: this._stringBundle.GetStringFromName( michael@0: "error.sync.viewQuotaButton.accesskey"), michael@0: function() { gSyncUI.openQuotaDialog(); return true; } ) michael@0: ); michael@0: } michael@0: else if (Weave.Status.enforceBackoff) { michael@0: priority = Weave.Notifications.PRIORITY_INFO; michael@0: buttons.push(new Weave.NotificationButton( michael@0: this._stringBundle.GetStringFromName("error.sync.serverStatusButton.label"), michael@0: this._stringBundle.GetStringFromName("error.sync.serverStatusButton.accesskey"), michael@0: function() { gSyncUI.openServerStatus(); return true; } michael@0: )); michael@0: } michael@0: else { michael@0: priority = Weave.Notifications.PRIORITY_INFO; michael@0: buttons.push(new Weave.NotificationButton( michael@0: this._stringBundle.GetStringFromName("error.sync.tryAgainButton.label"), michael@0: this._stringBundle.GetStringFromName("error.sync.tryAgainButton.accesskey"), michael@0: function() { gSyncUI.doSync(); return true; } michael@0: )); michael@0: } michael@0: michael@0: let notification = michael@0: new Weave.Notification(title, description, null, priority, buttons); michael@0: Weave.Notifications.replaceTitle(notification); michael@0: michael@0: this.updateUI(); michael@0: }, michael@0: michael@0: observe: function SUI_observe(subject, topic, data) { michael@0: if (this._unloaded) { michael@0: Cu.reportError("SyncUI observer called after unload: " + topic); michael@0: return; michael@0: } michael@0: michael@0: // Unwrap, just like Svc.Obs, but without pulling in that dependency. michael@0: if (subject && typeof subject == "object" && michael@0: ("wrappedJSObject" in subject) && michael@0: ("observersModuleSubjectWrapper" in subject.wrappedJSObject)) { michael@0: subject = subject.wrappedJSObject.object; michael@0: } michael@0: michael@0: switch (topic) { michael@0: case "weave:service:sync:start": michael@0: this.onActivityStart(); michael@0: break; michael@0: case "weave:ui:sync:finish": michael@0: this.onSyncFinish(); michael@0: break; michael@0: case "weave:ui:sync:error": michael@0: this.onSyncError(); michael@0: break; michael@0: case "weave:service:quota:remaining": michael@0: this.onQuotaNotice(); michael@0: break; michael@0: case "weave:service:setup-complete": michael@0: this.onSetupComplete(); michael@0: break; michael@0: case "weave:service:login:start": michael@0: this.onActivityStart(); michael@0: break; michael@0: case "weave:service:login:finish": michael@0: this.onLoginFinish(); michael@0: break; michael@0: case "weave:ui:login:error": michael@0: this.onLoginError(); michael@0: break; michael@0: case "weave:service:logout:finish": michael@0: this.onLogout(); michael@0: break; michael@0: case "weave:service:start-over": michael@0: this.onStartOver(); michael@0: break; michael@0: case "weave:service:start-over:finish": michael@0: this.updateUI(); michael@0: break; michael@0: case "weave:service:ready": michael@0: this.initUI(); michael@0: break; michael@0: case "weave:notification:added": michael@0: this.initNotifications(); michael@0: break; michael@0: case "weave:ui:clear-error": michael@0: this.clearError(); michael@0: break; michael@0: case "weave:eol": michael@0: this.onEOLNotice(subject); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsIObserver, michael@0: Ci.nsISupportsWeakReference michael@0: ]) michael@0: }; michael@0: michael@0: XPCOMUtils.defineLazyGetter(gSyncUI, "_stringBundle", function() { michael@0: //XXXzpao these strings should probably be moved from /services to /browser... (bug 583381) michael@0: // but for now just make it work michael@0: return Cc["@mozilla.org/intl/stringbundle;1"]. michael@0: getService(Ci.nsIStringBundleService). michael@0: createBundle("chrome://weave/locale/services/sync.properties"); michael@0: }); michael@0: