|
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/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 this.EXPORTED_SYMBOLS = ["Accounts"]; |
|
8 |
|
9 const { utils: Cu } = Components; |
|
10 |
|
11 Cu.import("resource://gre/modules/Messaging.jsm"); |
|
12 Cu.import("resource://gre/modules/Services.jsm"); |
|
13 Cu.import("resource://gre/modules/Promise.jsm"); |
|
14 |
|
15 /** |
|
16 * A promise-based API for querying the existence of Sync accounts, |
|
17 * and accessing the Sync setup wizard. |
|
18 * |
|
19 * Usage: |
|
20 * |
|
21 * Cu.import("resource://gre/modules/Accounts.jsm"); |
|
22 * Accounts.anySyncAccountsExist().then( |
|
23 * (exist) => { |
|
24 * console.log("Accounts exist? " + exist); |
|
25 * if (!exist) { |
|
26 * Accounts.launchSetup(); |
|
27 * } |
|
28 * }, |
|
29 * (err) => { |
|
30 * console.log("We failed so hard."); |
|
31 * } |
|
32 * ); |
|
33 */ |
|
34 let Accounts = Object.freeze({ |
|
35 _accountsExist: function (kind) { |
|
36 let deferred = Promise.defer(); |
|
37 |
|
38 sendMessageToJava({ |
|
39 type: "Accounts:Exist", |
|
40 kind: kind, |
|
41 }, (data, error) => { |
|
42 if (error) { |
|
43 deferred.reject(error); |
|
44 } else { |
|
45 deferred.resolve(data.exists); |
|
46 } |
|
47 }); |
|
48 |
|
49 return deferred.promise; |
|
50 }, |
|
51 |
|
52 firefoxAccountsExist: function () { |
|
53 return this._accountsExist("fxa"); |
|
54 }, |
|
55 |
|
56 syncAccountsExist: function () { |
|
57 return this._accountsExist("sync11"); |
|
58 }, |
|
59 |
|
60 anySyncAccountsExist: function () { |
|
61 return this._accountsExist("any"); |
|
62 }, |
|
63 |
|
64 /** |
|
65 * Fire-and-forget: open the Firefox accounts activity, which |
|
66 * will be the Getting Started screen if FxA isn't yet set up. |
|
67 * |
|
68 * There is no return value from this method. |
|
69 */ |
|
70 launchSetup: function () { |
|
71 sendMessageToJava({ |
|
72 type: "Accounts:Create", |
|
73 }); |
|
74 }, |
|
75 }); |
|
76 |