Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / b2g / components / FxAccountsMgmtService.jsm
blobee11c53894f04e911c9b6bca0ff3f965b3d5d448
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/. */
5 /**
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
9  * ones.
10  *
11  * This service listens for requests coming from these apps, triggers the
12  * appropriate Fx Accounts flows and send reponses back to the UI.
13  *
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.
17  */
19 "use strict";
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", {
38       id: aMsgId,
39       data: aData ? aData : null
40     });
41   },
43   _onReject: function(aMsgId, aReason) {
44     SystemAppProxy._sendCustomEvent("mozFxAccountsChromeEvent", {
45       id: aMsgId,
46       error: aReason ? aReason : null
47     });
48   },
50   init: function() {
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);
56   },
58   observe: function(aSubject, aTopic, aData) {
59     log.debug("Observed " + aTopic);
60     switch (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)
67         });
68         break;
69     }
70   },
72   handleEvent: function(aEvent) {
73     let msg = aEvent.detail;
74     log.debug("MgmtService got content event: " + JSON.stringify(msg));
75     let self = FxAccountsMgmtService;
77     if (!msg.id) {
78       return;
79     }
81     if (msg.error) {
82       self._onReject(msg.id, msg.error);
83       return;
84     }
86     let data = msg.data;
87     if (!data) {
88       return;
89     }
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;
94     }
96     switch(data.method) {
97       case "getAccounts":
98         FxAccountsManager.getAccount().then(
99           account => {
100             // We only expose the email and verification status so far.
101             self._onFulfill(msg.id, account);
102           },
103           reason => {
104             self._onReject(msg.id, reason);
105           }
106         ).then(null, Components.utils.reportError);
107         break;
108       case "logout":
109         FxAccountsManager.signOut().then(
110           () => {
111             self._onFulfill(msg.id);
112           },
113           reason => {
114             self._onReject(msg.id, reason);
115           }
116         ).then(null, Components.utils.reportError);
117         break;
118       case "queryAccount":
119         FxAccountsManager.queryAccount(data.email).then(
120           result => {
121             self._onFulfill(msg.id, result);
122           },
123           reason => {
124             self._onReject(msg.id, reason);
125           }
126         ).then(null, Components.utils.reportError);
127         break;
128       case "resendVerificationEmail":
129         FxAccountsManager.resendVerificationEmail().then(
130           () => {
131             self._onFulfill(msg.id);
132           },
133           reason => {
134             self._onReject(msg.id, reason);
135           }
136         ).then(null, Components.utils.reportError);
137         break;
138       case "signIn":
139       case "signUp":
140       case "refreshAuthentication":
141         FxAccountsManager[data.method](data.email, data.password).then(
142           user => {
143             self._onFulfill(msg.id, user);
144           },
145           reason => {
146             self._onReject(msg.id, reason);
147           }
148         ).then(null, Components.utils.reportError);
149         break;
150     }
151   }
154 FxAccountsMgmtService.init();