michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["Accounts"]; michael@0: michael@0: const { utils: Cu } = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Messaging.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/Promise.jsm"); michael@0: michael@0: /** michael@0: * A promise-based API for querying the existence of Sync accounts, michael@0: * and accessing the Sync setup wizard. michael@0: * michael@0: * Usage: michael@0: * michael@0: * Cu.import("resource://gre/modules/Accounts.jsm"); michael@0: * Accounts.anySyncAccountsExist().then( michael@0: * (exist) => { michael@0: * console.log("Accounts exist? " + exist); michael@0: * if (!exist) { michael@0: * Accounts.launchSetup(); michael@0: * } michael@0: * }, michael@0: * (err) => { michael@0: * console.log("We failed so hard."); michael@0: * } michael@0: * ); michael@0: */ michael@0: let Accounts = Object.freeze({ michael@0: _accountsExist: function (kind) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: sendMessageToJava({ michael@0: type: "Accounts:Exist", michael@0: kind: kind, michael@0: }, (data, error) => { michael@0: if (error) { michael@0: deferred.reject(error); michael@0: } else { michael@0: deferred.resolve(data.exists); michael@0: } michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: }, michael@0: michael@0: firefoxAccountsExist: function () { michael@0: return this._accountsExist("fxa"); michael@0: }, michael@0: michael@0: syncAccountsExist: function () { michael@0: return this._accountsExist("sync11"); michael@0: }, michael@0: michael@0: anySyncAccountsExist: function () { michael@0: return this._accountsExist("any"); michael@0: }, michael@0: michael@0: /** michael@0: * Fire-and-forget: open the Firefox accounts activity, which michael@0: * will be the Getting Started screen if FxA isn't yet set up. michael@0: * michael@0: * There is no return value from this method. michael@0: */ michael@0: launchSetup: function () { michael@0: sendMessageToJava({ michael@0: type: "Accounts:Create", michael@0: }); michael@0: }, michael@0: }); michael@0: