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 file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Some specific (certified) apps need to get access to certain Firefox Accounts michael@0: * functionality that allows them to manage accounts (this is mostly sign up, michael@0: * sign in, logout and delete) and get information about the currently existing michael@0: * ones. michael@0: * michael@0: * This service listens for requests coming from these apps, triggers the michael@0: * appropriate Fx Accounts flows and send reponses back to the UI. michael@0: * michael@0: * The communication mechanism is based in mozFxAccountsContentEvent (for michael@0: * messages coming from the UI) and mozFxAccountsChromeEvent (for messages michael@0: * sent from the chrome side) custom events. michael@0: */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["FxAccountsMgmtService"]; michael@0: michael@0: const { classes: Cc, interfaces: Ci, utils: Cu } = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/FxAccountsCommon.js"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsManager", michael@0: "resource://gre/modules/FxAccountsManager.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", michael@0: "resource://gre/modules/SystemAppProxy.jsm"); michael@0: michael@0: this.FxAccountsMgmtService = { michael@0: _onFulfill: function(aMsgId, aData) { michael@0: SystemAppProxy._sendCustomEvent("mozFxAccountsChromeEvent", { michael@0: id: aMsgId, michael@0: data: aData ? aData : null michael@0: }); michael@0: }, michael@0: michael@0: _onReject: function(aMsgId, aReason) { michael@0: SystemAppProxy._sendCustomEvent("mozFxAccountsChromeEvent", { michael@0: id: aMsgId, michael@0: error: aReason ? aReason : null michael@0: }); michael@0: }, michael@0: michael@0: init: function() { michael@0: Services.obs.addObserver(this, "content-start", false); michael@0: Services.obs.addObserver(this, ONLOGIN_NOTIFICATION, false); michael@0: Services.obs.addObserver(this, ONVERIFIED_NOTIFICATION, false); michael@0: Services.obs.addObserver(this, ONLOGOUT_NOTIFICATION, false); michael@0: }, michael@0: michael@0: observe: function(aSubject, aTopic, aData) { michael@0: log.debug("Observed " + aTopic); michael@0: switch (aTopic) { michael@0: case "content-start": michael@0: SystemAppProxy.addEventListener("mozFxAccountsContentEvent", michael@0: FxAccountsMgmtService); michael@0: Services.obs.removeObserver(this, "content-start"); michael@0: break; michael@0: case ONLOGIN_NOTIFICATION: michael@0: case ONVERIFIED_NOTIFICATION: michael@0: case ONLOGOUT_NOTIFICATION: michael@0: // FxAccounts notifications have the form of fxaccounts:* michael@0: SystemAppProxy._sendCustomEvent("mozFxAccountsUnsolChromeEvent", { michael@0: eventName: aTopic.substring(aTopic.indexOf(":") + 1) michael@0: }); michael@0: break; michael@0: } michael@0: }, michael@0: michael@0: handleEvent: function(aEvent) { michael@0: let msg = aEvent.detail; michael@0: log.debug("Got content msg " + JSON.stringify(msg)); michael@0: let self = FxAccountsMgmtService; michael@0: michael@0: if (!msg.id) { michael@0: return; michael@0: } michael@0: michael@0: let data = msg.data; michael@0: if (!data) { michael@0: return; michael@0: } michael@0: michael@0: switch(data.method) { michael@0: case "getAccounts": michael@0: FxAccountsManager.getAccount().then( michael@0: account => { michael@0: // We only expose the email and verification status so far. michael@0: self._onFulfill(msg.id, account); michael@0: }, michael@0: reason => { michael@0: self._onReject(msg.id, reason); michael@0: } michael@0: ).then(null, Components.utils.reportError); michael@0: break; michael@0: case "logout": michael@0: FxAccountsManager.signOut().then( michael@0: () => { michael@0: self._onFulfill(msg.id); michael@0: }, michael@0: reason => { michael@0: self._onReject(msg.id, reason); michael@0: } michael@0: ).then(null, Components.utils.reportError); michael@0: break; michael@0: case "queryAccount": michael@0: FxAccountsManager.queryAccount(data.accountId).then( michael@0: result => { michael@0: self._onFulfill(msg.id, result); michael@0: }, michael@0: reason => { michael@0: self._onReject(msg.id, reason); michael@0: } michael@0: ).then(null, Components.utils.reportError); michael@0: break; michael@0: case "signIn": michael@0: case "signUp": michael@0: case "refreshAuthentication": michael@0: FxAccountsManager[data.method](data.accountId, data.password).then( michael@0: user => { michael@0: self._onFulfill(msg.id, user); michael@0: }, michael@0: reason => { michael@0: self._onReject(msg.id, reason); michael@0: } michael@0: ).then(null, Components.utils.reportError); michael@0: break; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: FxAccountsMgmtService.init();