Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict" |
michael@0 | 6 | |
michael@0 | 7 | const { interfaces: Ci, utils: Cu } = Components; |
michael@0 | 8 | |
michael@0 | 9 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 10 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | Cu.import("resource://gre/modules/Promise.jsm"); |
michael@0 | 12 | Cu.import("resource://gre/modules/FxAccountsCommon.js"); |
michael@0 | 13 | |
michael@0 | 14 | XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", |
michael@0 | 15 | "@mozilla.org/uuid-generator;1", |
michael@0 | 16 | "nsIUUIDGenerator"); |
michael@0 | 17 | |
michael@0 | 18 | XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", |
michael@0 | 19 | "resource://gre/modules/SystemAppProxy.jsm"); |
michael@0 | 20 | |
michael@0 | 21 | function FxAccountsUIGlue() { |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | FxAccountsUIGlue.prototype = { |
michael@0 | 25 | |
michael@0 | 26 | _contentRequest: function(aEventName, aData) { |
michael@0 | 27 | let deferred = Promise.defer(); |
michael@0 | 28 | |
michael@0 | 29 | let id = uuidgen.generateUUID().toString(); |
michael@0 | 30 | |
michael@0 | 31 | SystemAppProxy.addEventListener("mozFxAccountsRPContentEvent", |
michael@0 | 32 | function onContentEvent(result) { |
michael@0 | 33 | let msg = result.detail; |
michael@0 | 34 | if (!msg || !msg.id || msg.id != id) { |
michael@0 | 35 | deferred.reject("InternalErrorWrongContentEvent"); |
michael@0 | 36 | SystemAppProxy.removeEventListener("mozFxAccountsRPContentEvent", |
michael@0 | 37 | onContentEvent); |
michael@0 | 38 | return; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | log.debug("Got content event " + JSON.stringify(msg)); |
michael@0 | 42 | |
michael@0 | 43 | if (msg.error) { |
michael@0 | 44 | deferred.reject(msg); |
michael@0 | 45 | } else { |
michael@0 | 46 | deferred.resolve(msg.result); |
michael@0 | 47 | } |
michael@0 | 48 | SystemAppProxy.removeEventListener("mozFxAccountsRPContentEvent", |
michael@0 | 49 | onContentEvent); |
michael@0 | 50 | }); |
michael@0 | 51 | |
michael@0 | 52 | let detail = { |
michael@0 | 53 | eventName: aEventName, |
michael@0 | 54 | id: id, |
michael@0 | 55 | data: aData |
michael@0 | 56 | }; |
michael@0 | 57 | log.debug("Send chrome event " + JSON.stringify(detail)); |
michael@0 | 58 | SystemAppProxy._sendCustomEvent("mozFxAccountsUnsolChromeEvent", detail); |
michael@0 | 59 | |
michael@0 | 60 | return deferred.promise; |
michael@0 | 61 | }, |
michael@0 | 62 | |
michael@0 | 63 | signInFlow: function() { |
michael@0 | 64 | return this._contentRequest("openFlow"); |
michael@0 | 65 | }, |
michael@0 | 66 | |
michael@0 | 67 | refreshAuthentication: function(aAccountId) { |
michael@0 | 68 | return this._contentRequest("refreshAuthentication", { |
michael@0 | 69 | accountId: aAccountId |
michael@0 | 70 | }); |
michael@0 | 71 | }, |
michael@0 | 72 | |
michael@0 | 73 | classID: Components.ID("{51875c14-91d7-4b8c-b65d-3549e101228c}"), |
michael@0 | 74 | |
michael@0 | 75 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIFxAccountsUIGlue]) |
michael@0 | 76 | }; |
michael@0 | 77 | |
michael@0 | 78 | this.NSGetFactory = XPCOMUtils.generateNSGetFactory([FxAccountsUIGlue]); |