b2g/components/FxAccountsMgmtService.jsm

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     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, "content-start", false);
    52     Services.obs.addObserver(this, ONLOGIN_NOTIFICATION, false);
    53     Services.obs.addObserver(this, ONVERIFIED_NOTIFICATION, false);
    54     Services.obs.addObserver(this, ONLOGOUT_NOTIFICATION, false);
    55   },
    57   observe: function(aSubject, aTopic, aData) {
    58     log.debug("Observed " + aTopic);
    59     switch (aTopic) {
    60       case "content-start":
    61         SystemAppProxy.addEventListener("mozFxAccountsContentEvent",
    62                                         FxAccountsMgmtService);
    63         Services.obs.removeObserver(this, "content-start");
    64         break;
    65       case ONLOGIN_NOTIFICATION:
    66       case ONVERIFIED_NOTIFICATION:
    67       case ONLOGOUT_NOTIFICATION:
    68         // FxAccounts notifications have the form of fxaccounts:*
    69         SystemAppProxy._sendCustomEvent("mozFxAccountsUnsolChromeEvent", {
    70           eventName: aTopic.substring(aTopic.indexOf(":") + 1)
    71         });
    72         break;
    73     }
    74   },
    76   handleEvent: function(aEvent) {
    77     let msg = aEvent.detail;
    78     log.debug("Got content msg " + JSON.stringify(msg));
    79     let self = FxAccountsMgmtService;
    81     if (!msg.id) {
    82       return;
    83     }
    85     let data = msg.data;
    86     if (!data) {
    87       return;
    88     }
    90     switch(data.method) {
    91       case "getAccounts":
    92         FxAccountsManager.getAccount().then(
    93           account => {
    94             // We only expose the email and verification status so far.
    95             self._onFulfill(msg.id, account);
    96           },
    97           reason => {
    98             self._onReject(msg.id, reason);
    99           }
   100         ).then(null, Components.utils.reportError);
   101         break;
   102       case "logout":
   103         FxAccountsManager.signOut().then(
   104           () => {
   105             self._onFulfill(msg.id);
   106           },
   107           reason => {
   108             self._onReject(msg.id, reason);
   109           }
   110         ).then(null, Components.utils.reportError);
   111         break;
   112       case "queryAccount":
   113         FxAccountsManager.queryAccount(data.accountId).then(
   114           result => {
   115             self._onFulfill(msg.id, result);
   116           },
   117           reason => {
   118             self._onReject(msg.id, reason);
   119           }
   120         ).then(null, Components.utils.reportError);
   121         break;
   122       case "signIn":
   123       case "signUp":
   124       case "refreshAuthentication":
   125         FxAccountsManager[data.method](data.accountId, data.password).then(
   126           user => {
   127             self._onFulfill(msg.id, user);
   128           },
   129           reason => {
   130             self._onReject(msg.id, reason);
   131           }
   132         ).then(null, Components.utils.reportError);
   133         break;
   134     }
   135   }
   136 };
   138 FxAccountsMgmtService.init();

mercurial