|
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 const { interfaces: Ci, utils: Cu } = Components; |
|
8 |
|
9 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
10 Cu.import("resource://gre/modules/Services.jsm"); |
|
11 Cu.import("resource://gre/modules/Promise.jsm"); |
|
12 Cu.import("resource://gre/modules/FxAccountsCommon.js"); |
|
13 |
|
14 XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", |
|
15 "@mozilla.org/uuid-generator;1", |
|
16 "nsIUUIDGenerator"); |
|
17 |
|
18 XPCOMUtils.defineLazyModuleGetter(this, "SystemAppProxy", |
|
19 "resource://gre/modules/SystemAppProxy.jsm"); |
|
20 |
|
21 function FxAccountsUIGlue() { |
|
22 } |
|
23 |
|
24 FxAccountsUIGlue.prototype = { |
|
25 |
|
26 _contentRequest: function(aEventName, aData) { |
|
27 let deferred = Promise.defer(); |
|
28 |
|
29 let id = uuidgen.generateUUID().toString(); |
|
30 |
|
31 SystemAppProxy.addEventListener("mozFxAccountsRPContentEvent", |
|
32 function onContentEvent(result) { |
|
33 let msg = result.detail; |
|
34 if (!msg || !msg.id || msg.id != id) { |
|
35 deferred.reject("InternalErrorWrongContentEvent"); |
|
36 SystemAppProxy.removeEventListener("mozFxAccountsRPContentEvent", |
|
37 onContentEvent); |
|
38 return; |
|
39 } |
|
40 |
|
41 log.debug("Got content event " + JSON.stringify(msg)); |
|
42 |
|
43 if (msg.error) { |
|
44 deferred.reject(msg); |
|
45 } else { |
|
46 deferred.resolve(msg.result); |
|
47 } |
|
48 SystemAppProxy.removeEventListener("mozFxAccountsRPContentEvent", |
|
49 onContentEvent); |
|
50 }); |
|
51 |
|
52 let detail = { |
|
53 eventName: aEventName, |
|
54 id: id, |
|
55 data: aData |
|
56 }; |
|
57 log.debug("Send chrome event " + JSON.stringify(detail)); |
|
58 SystemAppProxy._sendCustomEvent("mozFxAccountsUnsolChromeEvent", detail); |
|
59 |
|
60 return deferred.promise; |
|
61 }, |
|
62 |
|
63 signInFlow: function() { |
|
64 return this._contentRequest("openFlow"); |
|
65 }, |
|
66 |
|
67 refreshAuthentication: function(aAccountId) { |
|
68 return this._contentRequest("refreshAuthentication", { |
|
69 accountId: aAccountId |
|
70 }); |
|
71 }, |
|
72 |
|
73 classID: Components.ID("{51875c14-91d7-4b8c-b65d-3549e101228c}"), |
|
74 |
|
75 QueryInterface: XPCOMUtils.generateQI([Ci.nsIFxAccountsUIGlue]) |
|
76 }; |
|
77 |
|
78 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([FxAccountsUIGlue]); |