|
1 /* -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- / |
|
2 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 // This JS shim contains the callbacks to fire DOMRequest events for |
|
8 // navigator.pay API within the payment processor's scope. |
|
9 |
|
10 "use strict"; |
|
11 |
|
12 let { classes: Cc, interfaces: Ci, utils: Cu } = Components; |
|
13 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
14 Cu.import("resource://gre/modules/Services.jsm"); |
|
15 |
|
16 XPCOMUtils.defineLazyServiceGetter(this, "cpmm", |
|
17 "@mozilla.org/childprocessmessagemanager;1", |
|
18 "nsIMessageSender"); |
|
19 |
|
20 XPCOMUtils.defineLazyServiceGetter(this, "uuidgen", |
|
21 "@mozilla.org/uuid-generator;1", |
|
22 "nsIUUIDGenerator"); |
|
23 |
|
24 XPCOMUtils.defineLazyModuleGetter(this, "Logger", |
|
25 "resource://gre/modules/identity/LogUtils.jsm"); |
|
26 |
|
27 function log(...aMessageArgs) { |
|
28 Logger.log.apply(Logger, ["injected identity.js"].concat(aMessageArgs)); |
|
29 } |
|
30 |
|
31 log("\n\n======================= identity.js =======================\n\n"); |
|
32 |
|
33 // This script may be injected more than once into an iframe. |
|
34 // Ensure we don't redefine contstants |
|
35 if (typeof kIdentityJSLoaded === 'undefined') { |
|
36 const kIdentityDelegateWatch = "identity-delegate-watch"; |
|
37 const kIdentityDelegateRequest = "identity-delegate-request"; |
|
38 const kIdentityDelegateLogout = "identity-delegate-logout"; |
|
39 const kIdentityDelegateReady = "identity-delegate-ready"; |
|
40 const kIdentityDelegateFinished = "identity-delegate-finished"; |
|
41 const kIdentityControllerDoMethod = "identity-controller-doMethod"; |
|
42 const kIdentktyJSLoaded = true; |
|
43 } |
|
44 |
|
45 var showUI = false; |
|
46 var options = {}; |
|
47 var isLoaded = false; |
|
48 var func = null; |
|
49 |
|
50 /* |
|
51 * Message back to the SignInToWebsite pipe. Message should be an |
|
52 * object with the following keys: |
|
53 * |
|
54 * method: one of 'login', 'logout', 'ready' |
|
55 * assertion: optional assertion |
|
56 */ |
|
57 function identityCall(message) { |
|
58 if (options._internal) { |
|
59 message._internal = options._internal; |
|
60 } |
|
61 sendAsyncMessage(kIdentityControllerDoMethod, message); |
|
62 } |
|
63 |
|
64 /* |
|
65 * To close the dialog, we first tell the gecko SignInToWebsite manager that it |
|
66 * can clean up. Then we tell the gaia component that we are finished. It is |
|
67 * necessary to notify gecko first, so that the message can be sent before gaia |
|
68 * destroys our context. |
|
69 */ |
|
70 function closeIdentityDialog() { |
|
71 // tell gecko we're done. |
|
72 func = null; options = null; |
|
73 sendAsyncMessage(kIdentityDelegateFinished); |
|
74 } |
|
75 |
|
76 /* |
|
77 * doInternalWatch - call the internal.watch api and relay the results |
|
78 * up to the controller. |
|
79 */ |
|
80 function doInternalWatch() { |
|
81 log("doInternalWatch:", options, isLoaded); |
|
82 if (options && isLoaded) { |
|
83 let BrowserID = content.wrappedJSObject.BrowserID; |
|
84 BrowserID.internal.watch(function(aParams, aInternalParams) { |
|
85 identityCall(aParams); |
|
86 if (aParams.method === "ready") { |
|
87 closeIdentityDialog(); |
|
88 } |
|
89 }, |
|
90 JSON.stringify(options), |
|
91 function(...things) { |
|
92 // internal watch log callback |
|
93 log("(watch) internal: ", things); |
|
94 } |
|
95 ); |
|
96 } |
|
97 } |
|
98 |
|
99 function doInternalRequest() { |
|
100 log("doInternalRequest:", options && isLoaded); |
|
101 if (options && isLoaded) { |
|
102 var stringifiedOptions = JSON.stringify(options); |
|
103 content.wrappedJSObject.BrowserID.internal.get( |
|
104 options.origin, |
|
105 function(assertion, internalParams) { |
|
106 internalParams = internalParams || {}; |
|
107 if (assertion) { |
|
108 identityCall({ |
|
109 method: 'login', |
|
110 assertion: assertion, |
|
111 _internalParams: internalParams}); |
|
112 } else { |
|
113 identityCall({ |
|
114 method: 'cancel' |
|
115 }); |
|
116 } |
|
117 closeIdentityDialog(); |
|
118 }, |
|
119 stringifiedOptions); |
|
120 } |
|
121 } |
|
122 function doInternalLogout(aOptions) { |
|
123 log("doInternalLogout:", (options && isLoaded)); |
|
124 if (options && isLoaded) { |
|
125 let BrowserID = content.wrappedJSObject.BrowserID; |
|
126 BrowserID.internal.logout(options.origin, function() { |
|
127 identityCall({method:'logout'}); |
|
128 closeIdentityDialog(); |
|
129 }); |
|
130 } |
|
131 } |
|
132 |
|
133 addEventListener("DOMContentLoaded", function(e) { |
|
134 content.addEventListener("load", function(e) { |
|
135 isLoaded = true; |
|
136 // bring da func |
|
137 if (func) func(); |
|
138 }); |
|
139 }); |
|
140 |
|
141 // listen for request |
|
142 addMessageListener(kIdentityDelegateRequest, function(aMessage) { |
|
143 log("injected identity.js received", kIdentityDelegateRequest); |
|
144 options = aMessage.json; |
|
145 showUI = true; |
|
146 func = doInternalRequest; |
|
147 func(); |
|
148 }); |
|
149 |
|
150 // listen for watch |
|
151 addMessageListener(kIdentityDelegateWatch, function(aMessage) { |
|
152 log("injected identity.js received", kIdentityDelegateWatch); |
|
153 options = aMessage.json; |
|
154 showUI = false; |
|
155 func = doInternalWatch; |
|
156 func(); |
|
157 }); |
|
158 |
|
159 // listen for logout |
|
160 addMessageListener(kIdentityDelegateLogout, function(aMessage) { |
|
161 log("injected identity.js received", kIdentityDelegateLogout); |
|
162 options = aMessage.json; |
|
163 showUI = false; |
|
164 func = doInternalLogout; |
|
165 func(); |
|
166 }); |