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: const { interfaces: Ci, utils: Cu } = Components; michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/Promise.jsm"); michael@0: Cu.import("resource://gre/modules/FxAccountsCommon.js"); michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", michael@0: "@mozilla.org/uuid-generator;1", michael@0: "nsIUUIDGenerator"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", michael@0: "resource://gre/modules/SystemAppProxy.jsm"); michael@0: michael@0: function FxAccountsUIGlue() { michael@0: } michael@0: michael@0: FxAccountsUIGlue.prototype = { michael@0: michael@0: _contentRequest: function(aEventName, aData) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: let id = uuidgen.generateUUID().toString(); michael@0: michael@0: SystemAppProxy.addEventListener("mozFxAccountsRPContentEvent", michael@0: function onContentEvent(result) { michael@0: let msg = result.detail; michael@0: if (!msg || !msg.id || msg.id != id) { michael@0: deferred.reject("InternalErrorWrongContentEvent"); michael@0: SystemAppProxy.removeEventListener("mozFxAccountsRPContentEvent", michael@0: onContentEvent); michael@0: return; michael@0: } michael@0: michael@0: log.debug("Got content event " + JSON.stringify(msg)); michael@0: michael@0: if (msg.error) { michael@0: deferred.reject(msg); michael@0: } else { michael@0: deferred.resolve(msg.result); michael@0: } michael@0: SystemAppProxy.removeEventListener("mozFxAccountsRPContentEvent", michael@0: onContentEvent); michael@0: }); michael@0: michael@0: let detail = { michael@0: eventName: aEventName, michael@0: id: id, michael@0: data: aData michael@0: }; michael@0: log.debug("Send chrome event " + JSON.stringify(detail)); michael@0: SystemAppProxy._sendCustomEvent("mozFxAccountsUnsolChromeEvent", detail); michael@0: michael@0: return deferred.promise; michael@0: }, michael@0: michael@0: signInFlow: function() { michael@0: return this._contentRequest("openFlow"); michael@0: }, michael@0: michael@0: refreshAuthentication: function(aAccountId) { michael@0: return this._contentRequest("refreshAuthentication", { michael@0: accountId: aAccountId michael@0: }); michael@0: }, michael@0: michael@0: classID: Components.ID("{51875c14-91d7-4b8c-b65d-3549e101228c}"), michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIFxAccountsUIGlue]) michael@0: }; michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([FxAccountsUIGlue]);