2014-01-25 07:19:07 -08:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "FxAccountsCommon", function () {
|
|
|
|
return Cu.import("resource://gre/modules/FxAccountsCommon.js", {});
|
|
|
|
});
|
|
|
|
|
2014-01-29 12:41:07 -08:00
|
|
|
const PREF_SYNC_START_DOORHANGER = "services.sync.ui.showSyncStartDoorhanger";
|
|
|
|
|
2014-01-25 07:19:07 -08:00
|
|
|
let gFxAccounts = {
|
|
|
|
|
2014-01-25 13:02:18 -08:00
|
|
|
_initialized: false,
|
2014-01-10 10:32:11 -08:00
|
|
|
_inCustomizationMode: false,
|
|
|
|
|
|
|
|
get weave() {
|
|
|
|
delete this.weave;
|
|
|
|
return this.weave = Cc["@mozilla.org/weave/service;1"]
|
|
|
|
.getService(Ci.nsISupports)
|
|
|
|
.wrappedJSObject;
|
|
|
|
},
|
2014-01-25 13:02:18 -08:00
|
|
|
|
2014-01-25 07:19:07 -08:00
|
|
|
get topics() {
|
2014-01-30 11:15:46 -08:00
|
|
|
// Do all this dance to lazy-load FxAccountsCommon.
|
2014-01-25 07:19:07 -08:00
|
|
|
delete this.topics;
|
|
|
|
return this.topics = [
|
2014-01-29 19:06:50 -08:00
|
|
|
"weave:service:sync:start",
|
|
|
|
"weave:service:login:error",
|
2014-01-10 10:32:11 -08:00
|
|
|
FxAccountsCommon.ONLOGIN_NOTIFICATION,
|
|
|
|
FxAccountsCommon.ONVERIFIED_NOTIFICATION,
|
2014-01-29 19:06:50 -08:00
|
|
|
FxAccountsCommon.ONLOGOUT_NOTIFICATION
|
2014-01-25 07:19:07 -08:00
|
|
|
];
|
|
|
|
},
|
|
|
|
|
2014-01-30 11:15:46 -08:00
|
|
|
// The set of topics that only the active window should handle.
|
|
|
|
get activeWindowTopics() {
|
|
|
|
// Do all this dance to lazy-load FxAccountsCommon.
|
|
|
|
delete this.activeWindowTopics;
|
|
|
|
return this.activeWindowTopics = new Set([
|
|
|
|
"weave:service:sync:start",
|
|
|
|
FxAccountsCommon.ONVERIFIED_NOTIFICATION
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
|
2014-01-10 10:32:11 -08:00
|
|
|
get button() {
|
|
|
|
delete this.button;
|
|
|
|
return this.button = document.getElementById("PanelUI-fxa-status");
|
|
|
|
},
|
|
|
|
|
2014-01-29 19:06:50 -08:00
|
|
|
get loginFailed() {
|
|
|
|
return Weave.Service.identity.readyToAuthenticate &&
|
|
|
|
Weave.Status.login != Weave.LOGIN_SUCCEEDED;
|
|
|
|
},
|
|
|
|
|
2014-01-30 11:15:46 -08:00
|
|
|
get isActiveWindow() {
|
|
|
|
let mostRecentNonPopupWindow =
|
|
|
|
RecentWindow.getMostRecentBrowserWindow({allowPopups: false});
|
|
|
|
return window == mostRecentNonPopupWindow;
|
|
|
|
},
|
|
|
|
|
2014-01-25 07:19:07 -08:00
|
|
|
init: function () {
|
2014-01-30 11:15:46 -08:00
|
|
|
// Bail out if we're already initialized and for pop-up windows.
|
|
|
|
if (this._initialized || !window.toolbar.visible) {
|
2014-01-25 13:02:18 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-25 07:19:07 -08:00
|
|
|
for (let topic of this.topics) {
|
|
|
|
Services.obs.addObserver(this, topic, false);
|
|
|
|
}
|
2014-01-25 13:02:18 -08:00
|
|
|
|
2014-01-10 10:32:11 -08:00
|
|
|
gNavToolbox.addEventListener("customizationstarting", this);
|
|
|
|
gNavToolbox.addEventListener("customizationending", this);
|
|
|
|
|
2014-01-25 13:02:18 -08:00
|
|
|
this._initialized = true;
|
2014-01-10 10:32:11 -08:00
|
|
|
|
|
|
|
this.updateUI();
|
2014-01-25 07:19:07 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
uninit: function () {
|
2014-01-25 13:02:18 -08:00
|
|
|
if (!this._initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-25 07:19:07 -08:00
|
|
|
for (let topic of this.topics) {
|
|
|
|
Services.obs.removeObserver(this, topic);
|
|
|
|
}
|
2014-01-25 13:02:18 -08:00
|
|
|
|
|
|
|
this._initialized = false;
|
2014-01-25 07:19:07 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
observe: function (subject, topic) {
|
2014-01-30 11:15:46 -08:00
|
|
|
// Ignore certain topics if we're not the active window.
|
|
|
|
if (this.activeWindowTopics.has(topic) && !this.isActiveWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-29 12:41:07 -08:00
|
|
|
switch (topic) {
|
|
|
|
case FxAccountsCommon.ONVERIFIED_NOTIFICATION:
|
|
|
|
Services.prefs.setBoolPref(PREF_SYNC_START_DOORHANGER, true);
|
|
|
|
break;
|
|
|
|
case "weave:service:sync:start":
|
|
|
|
this.onSyncStart();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.updateUI();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onSyncStart: function () {
|
|
|
|
let showDoorhanger = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
showDoorhanger = Services.prefs.getBoolPref(PREF_SYNC_START_DOORHANGER);
|
|
|
|
} catch (e) { /* The pref might not exist. */ }
|
|
|
|
|
|
|
|
if (showDoorhanger) {
|
|
|
|
Services.prefs.clearUserPref(PREF_SYNC_START_DOORHANGER);
|
2014-01-28 17:54:36 -08:00
|
|
|
this.showSyncStartedDoorhanger();
|
2014-01-10 10:32:11 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
handleEvent: function (event) {
|
|
|
|
this._inCustomizationMode = event.type == "customizationstarting";
|
|
|
|
this.updateUI();
|
2014-01-25 07:19:07 -08:00
|
|
|
},
|
|
|
|
|
2014-01-28 17:54:36 -08:00
|
|
|
showDoorhanger: function (id) {
|
|
|
|
let panel = document.getElementById(id);
|
2014-01-25 07:19:07 -08:00
|
|
|
let anchor = document.getElementById("PanelUI-menu-button");
|
|
|
|
|
|
|
|
let iconAnchor =
|
|
|
|
document.getAnonymousElementByAttribute(anchor, "class",
|
|
|
|
"toolbarbutton-icon");
|
|
|
|
|
|
|
|
panel.hidden = false;
|
|
|
|
panel.openPopup(iconAnchor || anchor, "bottomcenter topright");
|
2014-01-10 10:32:11 -08:00
|
|
|
},
|
|
|
|
|
2014-01-28 17:54:36 -08:00
|
|
|
showSyncStartedDoorhanger: function () {
|
|
|
|
this.showDoorhanger("sync-start-panel");
|
|
|
|
},
|
|
|
|
|
|
|
|
showSyncFailedDoorhanger: function () {
|
|
|
|
this.showDoorhanger("sync-error-panel");
|
|
|
|
},
|
|
|
|
|
2014-01-10 10:32:11 -08:00
|
|
|
updateUI: function () {
|
|
|
|
// Bail out if FxA is disabled.
|
|
|
|
if (!this.weave.fxAccountsEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FxA is enabled, show the widget.
|
|
|
|
this.button.removeAttribute("hidden");
|
|
|
|
|
|
|
|
// Make sure the button is disabled in customization mode.
|
|
|
|
if (this._inCustomizationMode) {
|
|
|
|
this.button.setAttribute("disabled", "true");
|
|
|
|
} else {
|
|
|
|
this.button.removeAttribute("disabled");
|
|
|
|
}
|
|
|
|
|
2014-01-29 19:06:50 -08:00
|
|
|
let defaultLabel = this.button.getAttribute("defaultlabel");
|
|
|
|
let errorLabel = this.button.getAttribute("errorlabel");
|
|
|
|
|
2014-01-10 10:32:11 -08:00
|
|
|
// If the user is signed into their Firefox account and we are not
|
|
|
|
// currently in customization mode, show their email address.
|
|
|
|
fxAccounts.getSignedInUser().then(userData => {
|
2014-01-29 19:06:50 -08:00
|
|
|
// Reset the button to its original state.
|
|
|
|
this.button.setAttribute("label", defaultLabel);
|
|
|
|
this.button.removeAttribute("tooltiptext");
|
|
|
|
this.button.removeAttribute("signedin");
|
|
|
|
this.button.removeAttribute("failed");
|
|
|
|
|
|
|
|
if (!this._inCustomizationMode) {
|
|
|
|
if (this.loginFailed) {
|
|
|
|
this.button.setAttribute("failed", "true");
|
|
|
|
this.button.setAttribute("label", errorLabel);
|
|
|
|
} else if (userData) {
|
|
|
|
this.button.setAttribute("signedin", "true");
|
|
|
|
this.button.setAttribute("label", userData.email);
|
|
|
|
this.button.setAttribute("tooltiptext", userData.email);
|
|
|
|
}
|
2014-01-10 10:32:11 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
toggle: function (event) {
|
|
|
|
if (event.originalTarget.hasAttribute("signedin")) {
|
2014-01-28 17:54:36 -08:00
|
|
|
this.openPreferences();
|
2014-01-10 10:32:11 -08:00
|
|
|
} else {
|
2014-01-28 17:54:36 -08:00
|
|
|
this.openSignInPage();
|
2014-01-10 10:32:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
PanelUI.hide();
|
2014-01-28 17:54:36 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
openPreferences: function () {
|
|
|
|
openPreferences("paneSync");
|
|
|
|
},
|
|
|
|
|
|
|
|
openSignInPage: function () {
|
|
|
|
switchToTabHavingURI("about:accounts", true);
|
2014-01-25 07:19:07 -08:00
|
|
|
}
|
|
|
|
};
|