b2g/components/FxAccountsMgmtService.jsm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /**
michael@0 6 * Some specific (certified) apps need to get access to certain Firefox Accounts
michael@0 7 * functionality that allows them to manage accounts (this is mostly sign up,
michael@0 8 * sign in, logout and delete) and get information about the currently existing
michael@0 9 * ones.
michael@0 10 *
michael@0 11 * This service listens for requests coming from these apps, triggers the
michael@0 12 * appropriate Fx Accounts flows and send reponses back to the UI.
michael@0 13 *
michael@0 14 * The communication mechanism is based in mozFxAccountsContentEvent (for
michael@0 15 * messages coming from the UI) and mozFxAccountsChromeEvent (for messages
michael@0 16 * sent from the chrome side) custom events.
michael@0 17 */
michael@0 18
michael@0 19 "use strict";
michael@0 20
michael@0 21 this.EXPORTED_SYMBOLS = ["FxAccountsMgmtService"];
michael@0 22
michael@0 23 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
michael@0 24
michael@0 25 Cu.import("resource://gre/modules/Services.jsm");
michael@0 26 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 27 Cu.import("resource://gre/modules/FxAccountsCommon.js");
michael@0 28
michael@0 29 XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsManager",
michael@0 30 "resource://gre/modules/FxAccountsManager.jsm");
michael@0 31
michael@0 32 XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
michael@0 33 "resource://gre/modules/SystemAppProxy.jsm");
michael@0 34
michael@0 35 this.FxAccountsMgmtService = {
michael@0 36 _onFulfill: function(aMsgId, aData) {
michael@0 37 SystemAppProxy._sendCustomEvent("mozFxAccountsChromeEvent", {
michael@0 38 id: aMsgId,
michael@0 39 data: aData ? aData : null
michael@0 40 });
michael@0 41 },
michael@0 42
michael@0 43 _onReject: function(aMsgId, aReason) {
michael@0 44 SystemAppProxy._sendCustomEvent("mozFxAccountsChromeEvent", {
michael@0 45 id: aMsgId,
michael@0 46 error: aReason ? aReason : null
michael@0 47 });
michael@0 48 },
michael@0 49
michael@0 50 init: function() {
michael@0 51 Services.obs.addObserver(this, "content-start", false);
michael@0 52 Services.obs.addObserver(this, ONLOGIN_NOTIFICATION, false);
michael@0 53 Services.obs.addObserver(this, ONVERIFIED_NOTIFICATION, false);
michael@0 54 Services.obs.addObserver(this, ONLOGOUT_NOTIFICATION, false);
michael@0 55 },
michael@0 56
michael@0 57 observe: function(aSubject, aTopic, aData) {
michael@0 58 log.debug("Observed " + aTopic);
michael@0 59 switch (aTopic) {
michael@0 60 case "content-start":
michael@0 61 SystemAppProxy.addEventListener("mozFxAccountsContentEvent",
michael@0 62 FxAccountsMgmtService);
michael@0 63 Services.obs.removeObserver(this, "content-start");
michael@0 64 break;
michael@0 65 case ONLOGIN_NOTIFICATION:
michael@0 66 case ONVERIFIED_NOTIFICATION:
michael@0 67 case ONLOGOUT_NOTIFICATION:
michael@0 68 // FxAccounts notifications have the form of fxaccounts:*
michael@0 69 SystemAppProxy._sendCustomEvent("mozFxAccountsUnsolChromeEvent", {
michael@0 70 eventName: aTopic.substring(aTopic.indexOf(":") + 1)
michael@0 71 });
michael@0 72 break;
michael@0 73 }
michael@0 74 },
michael@0 75
michael@0 76 handleEvent: function(aEvent) {
michael@0 77 let msg = aEvent.detail;
michael@0 78 log.debug("Got content msg " + JSON.stringify(msg));
michael@0 79 let self = FxAccountsMgmtService;
michael@0 80
michael@0 81 if (!msg.id) {
michael@0 82 return;
michael@0 83 }
michael@0 84
michael@0 85 let data = msg.data;
michael@0 86 if (!data) {
michael@0 87 return;
michael@0 88 }
michael@0 89
michael@0 90 switch(data.method) {
michael@0 91 case "getAccounts":
michael@0 92 FxAccountsManager.getAccount().then(
michael@0 93 account => {
michael@0 94 // We only expose the email and verification status so far.
michael@0 95 self._onFulfill(msg.id, account);
michael@0 96 },
michael@0 97 reason => {
michael@0 98 self._onReject(msg.id, reason);
michael@0 99 }
michael@0 100 ).then(null, Components.utils.reportError);
michael@0 101 break;
michael@0 102 case "logout":
michael@0 103 FxAccountsManager.signOut().then(
michael@0 104 () => {
michael@0 105 self._onFulfill(msg.id);
michael@0 106 },
michael@0 107 reason => {
michael@0 108 self._onReject(msg.id, reason);
michael@0 109 }
michael@0 110 ).then(null, Components.utils.reportError);
michael@0 111 break;
michael@0 112 case "queryAccount":
michael@0 113 FxAccountsManager.queryAccount(data.accountId).then(
michael@0 114 result => {
michael@0 115 self._onFulfill(msg.id, result);
michael@0 116 },
michael@0 117 reason => {
michael@0 118 self._onReject(msg.id, reason);
michael@0 119 }
michael@0 120 ).then(null, Components.utils.reportError);
michael@0 121 break;
michael@0 122 case "signIn":
michael@0 123 case "signUp":
michael@0 124 case "refreshAuthentication":
michael@0 125 FxAccountsManager[data.method](data.accountId, data.password).then(
michael@0 126 user => {
michael@0 127 self._onFulfill(msg.id, user);
michael@0 128 },
michael@0 129 reason => {
michael@0 130 self._onReject(msg.id, reason);
michael@0 131 }
michael@0 132 ).then(null, Components.utils.reportError);
michael@0 133 break;
michael@0 134 }
michael@0 135 }
michael@0 136 };
michael@0 137
michael@0 138 FxAccountsMgmtService.init();

mercurial