b2g/components/FxAccountsMgmtService.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/b2g/components/FxAccountsMgmtService.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,138 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/**
     1.9 + * Some specific (certified) apps need to get access to certain Firefox Accounts
    1.10 + * functionality that allows them to manage accounts (this is mostly sign up,
    1.11 + * sign in, logout and delete) and get information about the currently existing
    1.12 + * ones.
    1.13 + *
    1.14 + * This service listens for requests coming from these apps, triggers the
    1.15 + * appropriate Fx Accounts flows and send reponses back to the UI.
    1.16 + *
    1.17 + * The communication mechanism is based in mozFxAccountsContentEvent (for
    1.18 + * messages coming from the UI) and mozFxAccountsChromeEvent (for messages
    1.19 + * sent from the chrome side) custom events.
    1.20 + */
    1.21 +
    1.22 +"use strict";
    1.23 +
    1.24 +this.EXPORTED_SYMBOLS = ["FxAccountsMgmtService"];
    1.25 +
    1.26 +const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
    1.27 +
    1.28 +Cu.import("resource://gre/modules/Services.jsm");
    1.29 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.30 +Cu.import("resource://gre/modules/FxAccountsCommon.js");
    1.31 +
    1.32 +XPCOMUtils.defineLazyModuleGetter(this, "FxAccountsManager",
    1.33 +  "resource://gre/modules/FxAccountsManager.jsm");
    1.34 +
    1.35 +XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy",
    1.36 +                                  "resource://gre/modules/SystemAppProxy.jsm");
    1.37 +
    1.38 +this.FxAccountsMgmtService = {
    1.39 +  _onFulfill: function(aMsgId, aData) {
    1.40 +    SystemAppProxy._sendCustomEvent("mozFxAccountsChromeEvent", {
    1.41 +      id: aMsgId,
    1.42 +      data: aData ? aData : null
    1.43 +    });
    1.44 +  },
    1.45 +
    1.46 +  _onReject: function(aMsgId, aReason) {
    1.47 +    SystemAppProxy._sendCustomEvent("mozFxAccountsChromeEvent", {
    1.48 +      id: aMsgId,
    1.49 +      error: aReason ? aReason : null
    1.50 +    });
    1.51 +  },
    1.52 +
    1.53 +  init: function() {
    1.54 +    Services.obs.addObserver(this, "content-start", false);
    1.55 +    Services.obs.addObserver(this, ONLOGIN_NOTIFICATION, false);
    1.56 +    Services.obs.addObserver(this, ONVERIFIED_NOTIFICATION, false);
    1.57 +    Services.obs.addObserver(this, ONLOGOUT_NOTIFICATION, false);
    1.58 +  },
    1.59 +
    1.60 +  observe: function(aSubject, aTopic, aData) {
    1.61 +    log.debug("Observed " + aTopic);
    1.62 +    switch (aTopic) {
    1.63 +      case "content-start":
    1.64 +        SystemAppProxy.addEventListener("mozFxAccountsContentEvent",
    1.65 +                                        FxAccountsMgmtService);
    1.66 +        Services.obs.removeObserver(this, "content-start");
    1.67 +        break;
    1.68 +      case ONLOGIN_NOTIFICATION:
    1.69 +      case ONVERIFIED_NOTIFICATION:
    1.70 +      case ONLOGOUT_NOTIFICATION:
    1.71 +        // FxAccounts notifications have the form of fxaccounts:*
    1.72 +        SystemAppProxy._sendCustomEvent("mozFxAccountsUnsolChromeEvent", {
    1.73 +          eventName: aTopic.substring(aTopic.indexOf(":") + 1)
    1.74 +        });
    1.75 +        break;
    1.76 +    }
    1.77 +  },
    1.78 +
    1.79 +  handleEvent: function(aEvent) {
    1.80 +    let msg = aEvent.detail;
    1.81 +    log.debug("Got content msg " + JSON.stringify(msg));
    1.82 +    let self = FxAccountsMgmtService;
    1.83 +
    1.84 +    if (!msg.id) {
    1.85 +      return;
    1.86 +    }
    1.87 +
    1.88 +    let data = msg.data;
    1.89 +    if (!data) {
    1.90 +      return;
    1.91 +    }
    1.92 +
    1.93 +    switch(data.method) {
    1.94 +      case "getAccounts":
    1.95 +        FxAccountsManager.getAccount().then(
    1.96 +          account => {
    1.97 +            // We only expose the email and verification status so far.
    1.98 +            self._onFulfill(msg.id, account);
    1.99 +          },
   1.100 +          reason => {
   1.101 +            self._onReject(msg.id, reason);
   1.102 +          }
   1.103 +        ).then(null, Components.utils.reportError);
   1.104 +        break;
   1.105 +      case "logout":
   1.106 +        FxAccountsManager.signOut().then(
   1.107 +          () => {
   1.108 +            self._onFulfill(msg.id);
   1.109 +          },
   1.110 +          reason => {
   1.111 +            self._onReject(msg.id, reason);
   1.112 +          }
   1.113 +        ).then(null, Components.utils.reportError);
   1.114 +        break;
   1.115 +      case "queryAccount":
   1.116 +        FxAccountsManager.queryAccount(data.accountId).then(
   1.117 +          result => {
   1.118 +            self._onFulfill(msg.id, result);
   1.119 +          },
   1.120 +          reason => {
   1.121 +            self._onReject(msg.id, reason);
   1.122 +          }
   1.123 +        ).then(null, Components.utils.reportError);
   1.124 +        break;
   1.125 +      case "signIn":
   1.126 +      case "signUp":
   1.127 +      case "refreshAuthentication":
   1.128 +        FxAccountsManager[data.method](data.accountId, data.password).then(
   1.129 +          user => {
   1.130 +            self._onFulfill(msg.id, user);
   1.131 +          },
   1.132 +          reason => {
   1.133 +            self._onReject(msg.id, reason);
   1.134 +          }
   1.135 +        ).then(null, Components.utils.reportError);
   1.136 +        break;
   1.137 +    }
   1.138 +  }
   1.139 +};
   1.140 +
   1.141 +FxAccountsMgmtService.init();

mercurial