summaryrefslogtreecommitdiffstats
path: root/mobile/android/modules/Accounts.jsm
blob: a611f3c58fe2eca5880002f0570bbf64a07b1939 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

this.EXPORTED_SYMBOLS = ["Accounts"];

const { utils: Cu } = Components;

Cu.import("resource://gre/modules/Deprecated.jsm"); /*global Deprecated */
Cu.import("resource://gre/modules/Messaging.jsm"); /*global Messaging */
Cu.import("resource://gre/modules/Promise.jsm"); /*global Promise */
Cu.import("resource://gre/modules/Services.jsm"); /*global Services */

/**
 * A promise-based API for querying the existence of Sync accounts,
 * and accessing the Sync setup wizard.
 *
 * Usage:
 *
 *   Cu.import("resource://gre/modules/Accounts.jsm");
 *   Accounts.anySyncAccountsExist().then(
 *     (exist) => {
 *       console.log("Accounts exist? " + exist);
 *       if (!exist) {
 *         Accounts.launchSetup();
 *       }
 *     },
 *     (err) => {
 *       console.log("We failed so hard.");
 *     }
 *   );
 */
var Accounts = Object.freeze({
  _accountsExist: function (kind) {
    return Messaging.sendRequestForResult({
      type: "Accounts:Exist",
      kind: kind
    }).then(data => data.exists);
  },

  firefoxAccountsExist: function () {
    return this._accountsExist("fxa");
  },

  syncAccountsExist: function () {
    Deprecated.warning("The legacy Sync account type has been removed from Firefox for Android. " +
                       "Please use `firefoxAccountsExist` instead.",
                       "https://developer.mozilla.org/en-US/Add-ons/Firefox_for_Android/API/Accounts.jsm");
    return Promise.resolve(false);
  },

  anySyncAccountsExist: function () {
    return this._accountsExist("any");
  },

  /**
   * Fire-and-forget: open the Firefox accounts activity, which
   * will be the Getting Started screen if FxA isn't yet set up.
   *
   * Optional extras are passed, as a JSON string, to the Firefox
   * Account Getting Started activity in the extras bundle of the
   * activity launch intent, under the key "extras".
   *
   * There is no return value from this method.
   */
  launchSetup: function (extras) {
    Messaging.sendRequest({
      type: "Accounts:Create",
      extras: extras
    });
  },

  _addDefaultEndpoints: function (json) {
    let newData = Cu.cloneInto(json, {}, { cloneFunctions: false });
    let associations = {
      authServerEndpoint: 'identity.fxaccounts.auth.uri',
      profileServerEndpoint: 'identity.fxaccounts.remote.profile.uri',
      tokenServerEndpoint: 'identity.sync.tokenserver.uri'
    };
    for (let key in associations) {
      newData[key] = newData[key] || Services.urlFormatter.formatURLPref(associations[key]);
    }
    return newData;
  },

  /**
   * Create a new Android Account corresponding to the given
   * fxa-content-server "login" JSON datum.  The new account will be
   * in the "Engaged" state, and will start syncing immediately.
   *
   * It is an error if an Android Account already exists.
   *
   * Returns a Promise that resolves to a boolean indicating success.
   */
  createFirefoxAccountFromJSON: function (json) {
    return Messaging.sendRequestForResult({
      type: "Accounts:CreateFirefoxAccountFromJSON",
      json: this._addDefaultEndpoints(json)
    });
  },

  /**
   * Move an existing Android Account to the "Engaged" state with the given
   * fxa-content-server "login" JSON datum.  The account will (re)start
   * syncing immediately, unless the user has manually configured the account
   * to not Sync.
   *
   * It is an error if no Android Account exists.
   *
   * Returns a Promise that resolves to a boolean indicating success.
   */
  updateFirefoxAccountFromJSON: function (json) {
    return Messaging.sendRequestForResult({
      type: "Accounts:UpdateFirefoxAccountFromJSON",
      json: this._addDefaultEndpoints(json)
    });
  },

  /**
   * Notify that profile for Android Account has updated.
   * The account will re-fetch the profile image.
   *
   * It is an error if no Android Account exists.
   *
   * There is no return value from this method.
   */
  notifyFirefoxAccountProfileChanged: function () {
    Messaging.sendRequest({
      type: "Accounts:ProfileUpdated",
    });
  },

  /**
   * Fetch information about an existing Android Firefox Account.
   *
   * Returns a Promise that resolves to null if no Android Firefox Account
   * exists, or an object including at least a string-valued 'email' key.
   */
  getFirefoxAccount: function () {
    return Messaging.sendRequestForResult({
      type: "Accounts:Exist",
      kind: "fxa",
    }).then(data => {
      if (!data || !data.exists) {
        return null;
      }
      delete data.exists;
      return data;
    });
  },

  /**
   * Delete an existing Android Firefox Account.
   *
   * It is an error if no Android Account exists.
   *
   * Returns a Promise that resolves to a boolean indicating success.
   */
  deleteFirefoxAccount: function () {
    return Messaging.sendRequestForResult({
      type: "Accounts:DeleteFirefoxAccount",
    });
  },

  showSyncPreferences: function () {
    // Only show Sync preferences of an existing Android Account.
    return Accounts.getFirefoxAccount().then(account => {
      if (!account) {
        throw new Error("Can't show Sync preferences of non-existent Firefox Account!");
      }
      return Messaging.sendRequestForResult({
        type: "Accounts:ShowSyncPreferences"
      });
    });
  }
});