1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/b2g/components/FxAccountsUIGlue.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,78 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +"use strict" 1.9 + 1.10 +const { interfaces: Ci, utils: Cu } = Components; 1.11 + 1.12 +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 1.13 +Cu.import("resource://gre/modules/Services.jsm"); 1.14 +Cu.import("resource://gre/modules/Promise.jsm"); 1.15 +Cu.import("resource://gre/modules/FxAccountsCommon.js"); 1.16 + 1.17 +XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", 1.18 + "@mozilla.org/uuid-generator;1", 1.19 + "nsIUUIDGenerator"); 1.20 + 1.21 +XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", 1.22 + "resource://gre/modules/SystemAppProxy.jsm"); 1.23 + 1.24 +function FxAccountsUIGlue() { 1.25 +} 1.26 + 1.27 +FxAccountsUIGlue.prototype = { 1.28 + 1.29 + _contentRequest: function(aEventName, aData) { 1.30 + let deferred = Promise.defer(); 1.31 + 1.32 + let id = uuidgen.generateUUID().toString(); 1.33 + 1.34 + SystemAppProxy.addEventListener("mozFxAccountsRPContentEvent", 1.35 + function onContentEvent(result) { 1.36 + let msg = result.detail; 1.37 + if (!msg || !msg.id || msg.id != id) { 1.38 + deferred.reject("InternalErrorWrongContentEvent"); 1.39 + SystemAppProxy.removeEventListener("mozFxAccountsRPContentEvent", 1.40 + onContentEvent); 1.41 + return; 1.42 + } 1.43 + 1.44 + log.debug("Got content event " + JSON.stringify(msg)); 1.45 + 1.46 + if (msg.error) { 1.47 + deferred.reject(msg); 1.48 + } else { 1.49 + deferred.resolve(msg.result); 1.50 + } 1.51 + SystemAppProxy.removeEventListener("mozFxAccountsRPContentEvent", 1.52 + onContentEvent); 1.53 + }); 1.54 + 1.55 + let detail = { 1.56 + eventName: aEventName, 1.57 + id: id, 1.58 + data: aData 1.59 + }; 1.60 + log.debug("Send chrome event " + JSON.stringify(detail)); 1.61 + SystemAppProxy._sendCustomEvent("mozFxAccountsUnsolChromeEvent", detail); 1.62 + 1.63 + return deferred.promise; 1.64 + }, 1.65 + 1.66 + signInFlow: function() { 1.67 + return this._contentRequest("openFlow"); 1.68 + }, 1.69 + 1.70 + refreshAuthentication: function(aAccountId) { 1.71 + return this._contentRequest("refreshAuthentication", { 1.72 + accountId: aAccountId 1.73 + }); 1.74 + }, 1.75 + 1.76 + classID: Components.ID("{51875c14-91d7-4b8c-b65d-3549e101228c}"), 1.77 + 1.78 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIFxAccountsUIGlue]) 1.79 +}; 1.80 + 1.81 +this.NSGetFactory = XPCOMUtils.generateNSGetFactory([FxAccountsUIGlue]);