1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 * Some specific (certified) apps need to get access to certain Firefox Accounts
7 * functionality that allows them to manage accounts (this is mostly sign up,
8 * sign in, logout and delete) and get information about the currently existing
11 * This service listens for requests coming from these apps, triggers the
12 * appropriate Fx Accounts flows and send reponses back to the UI.
14 * The communication mechanism is based in mozFxAccountsContentEvent (for
15 * messages coming from the UI) and mozFxAccountsChromeEvent (for messages
16 * sent from the chrome side) custom events.
21 this.EXPORTED_SYMBOLS = ["FxAccountsMgmtService"];
23 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
25 Cu.import("resource://gre/modules/Services.jsm");
26 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
27 Cu.import("resource://gre/modules/FxAccountsCommon.js");
29 XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsManager",
30 "resource://gre/modules/FxAccountsManager.jsm");
32 XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
33 "resource://gre/modules/SystemAppProxy.jsm");
35 this.FxAccountsMgmtService = {
36 _onFulfill: function(aMsgId, aData) {
37 SystemAppProxy._sendCustomEvent("mozFxAccountsChromeEvent", {
39 data: aData ? aData : null
43 _onReject: function(aMsgId, aReason) {
44 SystemAppProxy._sendCustomEvent("mozFxAccountsChromeEvent", {
46 error: aReason ? aReason : null
51 Services.obs.addObserver(this, ONLOGIN_NOTIFICATION, false);
52 Services.obs.addObserver(this, ONVERIFIED_NOTIFICATION, false);
53 Services.obs.addObserver(this, ONLOGOUT_NOTIFICATION, false);
54 SystemAppProxy.addEventListener("mozFxAccountsContentEvent",
55 FxAccountsMgmtService);
58 observe: function(aSubject, aTopic, aData) {
59 log.debug("Observed " + aTopic);
61 case ONLOGIN_NOTIFICATION:
62 case ONVERIFIED_NOTIFICATION:
63 case ONLOGOUT_NOTIFICATION:
64 // FxAccounts notifications have the form of fxaccounts:*
65 SystemAppProxy._sendCustomEvent("mozFxAccountsUnsolChromeEvent", {
66 eventName: aTopic.substring(aTopic.indexOf(":") + 1)
72 handleEvent: function(aEvent) {
73 let msg = aEvent.detail;
74 log.debug("MgmtService got content event: " + JSON.stringify(msg));
75 let self = FxAccountsMgmtService;
82 self._onReject(msg.id, msg.error);
90 // Backwards compatibility: handle accountId coming from Gaia
91 if (data.accountId && typeof(data.email === "undefined")) {
92 data.email = data.accountId;
93 delete data.accountId;
98 FxAccountsManager.getAccount().then(
100 // We only expose the email and verification status so far.
101 self._onFulfill(msg.id, account);
104 self._onReject(msg.id, reason);
106 ).then(null, Components.utils.reportError);
109 FxAccountsManager.signOut().then(
111 self._onFulfill(msg.id);
114 self._onReject(msg.id, reason);
116 ).then(null, Components.utils.reportError);
119 FxAccountsManager.queryAccount(data.email).then(
121 self._onFulfill(msg.id, result);
124 self._onReject(msg.id, reason);
126 ).then(null, Components.utils.reportError);
128 case "resendVerificationEmail":
129 FxAccountsManager.resendVerificationEmail().then(
131 self._onFulfill(msg.id);
134 self._onReject(msg.id, reason);
136 ).then(null, Components.utils.reportError);
140 case "refreshAuthentication":
141 FxAccountsManager[data.method](data.email, data.password).then(
143 self._onFulfill(msg.id, user);
146 self._onReject(msg.id, reason);
148 ).then(null, Components.utils.reportError);
154 FxAccountsMgmtService.init();