OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * UI Pages. Note the order must be in sync with the ArcAuthService::UIPage | 6 * UI Pages. Note the order must be in sync with the ArcAuthService::UIPage |
7 * enum. | 7 * enum. |
8 * @type {Array<string>} | 8 * @type {Array<string>} |
9 */ | 9 */ |
10 var UI_PAGES = ['none', | 10 var UI_PAGES = ['none', |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 */ | 52 */ |
53 var termsReloadTimeout = null; | 53 var termsReloadTimeout = null; |
54 | 54 |
55 /** | 55 /** |
56 * Stores current device id. | 56 * Stores current device id. |
57 * @type {string} | 57 * @type {string} |
58 */ | 58 */ |
59 var currentDeviceId = null; | 59 var currentDeviceId = null; |
60 | 60 |
61 /** | 61 /** |
62 * Indicates that OptIn flow is started in silent mode and no user interaction | |
63 * is expected. | |
64 * @type {boolean} | |
65 */ | |
66 var silentMode = false; | |
67 | |
68 /** | |
69 * Timeout to retry LSO in silent mode. | |
70 * @type {object} | |
71 */ | |
72 var retryTimeout = null; | |
73 | |
74 /** | |
62 * Closes current window in response to request from native code. This does not | 75 * Closes current window in response to request from native code. This does not |
63 * issue 'cancelAuthCode' message to native code. | 76 * issue 'cancelAuthCode' message to native code. |
64 */ | 77 */ |
65 function closeWindowInternally() { | 78 function closeWindowInternally() { |
66 windowClosedInternally = true; | 79 windowClosedInternally = true; |
67 appWindow.close(); | 80 appWindow.close(); |
68 appWindow = null; | 81 appWindow = null; |
82 cancelRetry(); | |
69 } | 83 } |
70 | 84 |
71 /** | 85 /** |
72 * Sends a native message to ArcSupportHost. | 86 * Sends a native message to ArcSupportHost. |
73 * @param {string} code The action code in message. | 87 * @param {string} code The action code in message. |
74 * @param {Object=} opt_Props Extra properties for the message. | 88 * @param {Object=} opt_Props Extra properties for the message. |
75 */ | 89 */ |
76 function sendNativeMessage(code, opt_Props) { | 90 function sendNativeMessage(code, opt_Props) { |
77 var message = Object.assign({'action': code}, opt_Props); | 91 var message = Object.assign({'action': code}, opt_Props); |
78 port.postMessage(message); | 92 port.postMessage(message); |
79 } | 93 } |
80 | 94 |
81 /** | 95 /** |
82 * Applies localization for html content and sets terms webview. | 96 * Applies localization for html content and sets terms webview. |
83 * @param {!Object} data Localized strings and relevant information. | 97 * @param {!Object} data Localized strings and relevant information. |
84 * @param {string} deviceId Current device id. | 98 * @param {string} deviceId Current device id. |
99 * @param {boolean} _silentMode Indicates if OptIn started in silent mode. | |
85 */ | 100 */ |
86 function initialize(data, deviceId) { | 101 function initialize(data, deviceId, _silentMode) { |
xiyuan
2016/06/27 23:20:42
This looks strange. To avoid the name conflict, we
khmel
2016/06/28 20:05:38
Thanks, that it is what I was looking for.
| |
87 currentDeviceId = deviceId; | 102 currentDeviceId = deviceId; |
103 silentMode = _silentMode; | |
88 var doc = appWindow.contentWindow.document; | 104 var doc = appWindow.contentWindow.document; |
89 var loadTimeData = appWindow.contentWindow.loadTimeData; | 105 var loadTimeData = appWindow.contentWindow.loadTimeData; |
90 loadTimeData.data = data; | 106 loadTimeData.data = data; |
91 appWindow.contentWindow.i18nTemplate.process(doc, loadTimeData); | 107 appWindow.contentWindow.i18nTemplate.process(doc, loadTimeData); |
92 var countryCode = data.countryCode.toLowerCase(); | 108 var countryCode = data.countryCode.toLowerCase(); |
93 | 109 |
94 var scriptSetCountryCode = 'document.countryCode = \'' + countryCode + '\';'; | 110 var scriptSetCountryCode = 'document.countryCode = \'' + countryCode + '\';'; |
95 termsView.addContentScripts([ | 111 termsView.addContentScripts([ |
96 { name: 'preProcess', | 112 { name: 'preProcess', |
97 matches: ['https://play.google.com/*'], | 113 matches: ['https://play.google.com/*'], |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 if (!message.action) { | 180 if (!message.action) { |
165 return; | 181 return; |
166 } | 182 } |
167 | 183 |
168 if (!appWindow) { | 184 if (!appWindow) { |
169 console.warn('Received native message when window is not available.'); | 185 console.warn('Received native message when window is not available.'); |
170 return; | 186 return; |
171 } | 187 } |
172 | 188 |
173 if (message.action == 'initialize') { | 189 if (message.action == 'initialize') { |
174 initialize(message.data, message.deviceId); | 190 initialize(message.data, message.deviceId, message.silentMode); |
175 } else if (message.action == 'setMetricsMode') { | 191 } else if (message.action == 'setMetricsMode') { |
176 setMetricsMode(message.text, message.canEnable, message.on); | 192 setMetricsMode(message.text, message.canEnable, message.on); |
177 } else if (message.action == 'closeUI') { | 193 } else if (message.action == 'closeUI') { |
178 closeWindowInternally(); | 194 closeWindowInternally(); |
179 } else if (message.action == 'showPage') { | 195 } else if (message.action == 'showPage') { |
180 showPageWithStatus(message.page, message.status); | 196 showPageWithStatus(message.page, message.status); |
181 } | 197 } |
182 } | 198 } |
183 | 199 |
184 /** | 200 /** |
185 * Connects to ArcSupportHost. | 201 * Connects to ArcSupportHost. |
186 */ | 202 */ |
187 function connectPort() { | 203 function connectPort() { |
188 var hostName = 'com.google.arc_support'; | 204 var hostName = 'com.google.arc_support'; |
189 port = chrome.runtime.connectNative(hostName); | 205 port = chrome.runtime.connectNative(hostName); |
190 port.onMessage.addListener(onNativeMessage); | 206 port.onMessage.addListener(onNativeMessage); |
191 } | 207 } |
192 | 208 |
193 /** | 209 /** |
210 * Cancels current request to get authority code from LSO if it was previously | |
211 * scheduled. | |
212 */ | |
213 function cancelRetry() { | |
214 if (!retryTimeout) { | |
215 return; | |
216 } | |
217 clearTimeout(retryTimeout); | |
218 retryTimeout = null; | |
219 } | |
220 | |
221 /** | |
222 * Schedules next retry to get authority code from LSO. Previous request is | |
223 * automatically canceled. | |
224 */ | |
225 function scheduleRetry() { | |
226 cancelRetry(); | |
227 var retry = function() { | |
228 showPage('lso-loading'); | |
229 }; | |
230 retryTimeout = setTimeout(retry, 60000); | |
231 } | |
232 | |
233 /** | |
194 * Shows requested page and hide others. Show appWindow if it was hidden before | 234 * Shows requested page and hide others. Show appWindow if it was hidden before |
195 * for non 'none' pages. For 'none' page this closes host window. | 235 * for non 'none' pages. For 'none' page this closes host window. |
196 * @param {string} pageDivId id of divider of the page to show. | 236 * @param {string} pageDivId id of divider of the page to show. |
197 */ | 237 */ |
198 function showPage(pageDivId) { | 238 function showPage(pageDivId) { |
199 if (!appWindow) { | 239 if (!appWindow) { |
200 return; | 240 return; |
201 } | 241 } |
202 | 242 |
203 if (pageDivId == 'none') { | 243 if (pageDivId == 'none') { |
(...skipping 16 matching lines...) Expand all Loading... | |
220 } | 260 } |
221 | 261 |
222 if (pageDivId == 'lso-loading') { | 262 if (pageDivId == 'lso-loading') { |
223 lsoView.src = 'https://accounts.google.com/o/oauth2/v2/auth?client_id=' + | 263 lsoView.src = 'https://accounts.google.com/o/oauth2/v2/auth?client_id=' + |
224 '1070009224336-sdh77n7uot3oc99ais00jmuft6sk2fg9.apps.' + | 264 '1070009224336-sdh77n7uot3oc99ais00jmuft6sk2fg9.apps.' + |
225 'googleusercontent.com&response_type=code&redirect_uri=oob&' + | 265 'googleusercontent.com&response_type=code&redirect_uri=oob&' + |
226 'scope=https://www.google.com/accounts/OAuthLogin&' + | 266 'scope=https://www.google.com/accounts/OAuthLogin&' + |
227 'device_type=arc_plus_plus&device_id=' + currentDeviceId + | 267 'device_type=arc_plus_plus&device_id=' + currentDeviceId + |
228 '&hl=' + navigator.language; | 268 '&hl=' + navigator.language; |
229 } | 269 } |
230 appWindow.show(); | 270 |
271 if (!silentMode) { | |
272 appWindow.show(); | |
273 } else { | |
274 if (pageDivId == 'arc-loading') { | |
275 cancelRetry(); | |
276 } else { | |
277 scheduleRetry(); | |
278 } | |
279 } | |
231 } | 280 } |
232 | 281 |
233 /** | 282 /** |
234 * Sets error message. | 283 * Sets error message. |
235 * @param {string} error message. | 284 * @param {string} error message. |
236 */ | 285 */ |
237 function setErrorMessage(error) { | 286 function setErrorMessage(error) { |
238 if (!appWindow) { | 287 if (!appWindow) { |
239 return; | 288 return; |
240 } | 289 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 var onLsoViewRequestResponseStarted = function(details) { | 339 var onLsoViewRequestResponseStarted = function(details) { |
291 if (isApprovalResponse(details.url)) { | 340 if (isApprovalResponse(details.url)) { |
292 showPage('arc-loading'); | 341 showPage('arc-loading'); |
293 } | 342 } |
294 }; | 343 }; |
295 | 344 |
296 var onLsoViewContentLoad = function() { | 345 var onLsoViewContentLoad = function() { |
297 if (!isApprovalResponse(lsoView.src)) { | 346 if (!isApprovalResponse(lsoView.src)) { |
298 // Show LSO page when its content is ready. | 347 // Show LSO page when its content is ready. |
299 showPage('lso'); | 348 showPage('lso'); |
349 if (silentMode) { | |
350 var submitAproveCode = | |
xiyuan
2016/06/27 23:20:42
submitAproveCode -> submitAp*p*roveCode
khmel
2016/06/28 20:05:38
Done.
| |
351 'document.getElementById("connect_approve").submit();'; | |
352 lsoView.executeScript({code: submitAproveCode}, function(results) {}); | |
353 } | |
300 return; | 354 return; |
301 } | 355 } |
302 | 356 |
303 lsoView.executeScript({code: 'document.title;'}, function(results) { | 357 lsoView.executeScript({code: 'document.title;'}, function(results) { |
304 var authCodePrefix = 'Success code='; | 358 var authCodePrefix = 'Success code='; |
305 if (results[0].substring(0, authCodePrefix.length) == | 359 if (results[0].substring(0, authCodePrefix.length) == |
306 authCodePrefix) { | 360 authCodePrefix) { |
307 var authCode = results[0].substring(authCodePrefix.length); | 361 var authCode = results[0].substring(authCodePrefix.length); |
308 sendNativeMessage('setAuthCode', {code: authCode}); | 362 sendNativeMessage('setAuthCode', {code: authCode}); |
363 cancelRetry(); | |
309 } else { | 364 } else { |
310 setErrorMessage(appWindow.contentWindow.loadTimeData.getString( | 365 setErrorMessage(appWindow.contentWindow.loadTimeData.getString( |
311 'authorizationFailed')); | 366 'authorizationFailed')); |
312 showPage('error'); | 367 showPage('error'); |
313 } | 368 } |
314 }); | 369 }); |
315 }; | 370 }; |
316 | 371 |
317 var requestFilter = { | 372 var requestFilter = { |
318 urls: ['<all_urls>'], | 373 urls: ['<all_urls>'], |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
391 appWindow = createdWindow; | 446 appWindow = createdWindow; |
392 appWindow.contentWindow.onload = onAppContentLoad; | 447 appWindow.contentWindow.onload = onAppContentLoad; |
393 createdWindow.onClosed.addListener(onWindowClosed); | 448 createdWindow.onClosed.addListener(onWindowClosed); |
394 }; | 449 }; |
395 | 450 |
396 var onWindowClosed = function() { | 451 var onWindowClosed = function() { |
397 if (termsReloadTimeout) { | 452 if (termsReloadTimeout) { |
398 clearTimeout(termsReloadTimeout); | 453 clearTimeout(termsReloadTimeout); |
399 termsReloadTimeout = null; | 454 termsReloadTimeout = null; |
400 } | 455 } |
456 cancelRetry(); | |
401 | 457 |
402 if (windowClosedInternally) { | 458 if (windowClosedInternally) { |
403 return; | 459 return; |
404 } | 460 } |
405 sendNativeMessage('cancelAuthCode'); | 461 sendNativeMessage('cancelAuthCode'); |
406 }; | 462 }; |
407 | 463 |
408 windowClosedInternally = false; | 464 windowClosedInternally = false; |
409 var options = { | 465 var options = { |
410 'id': 'play_store_wnd', | 466 'id': 'play_store_wnd', |
411 'resizable': false, | 467 'resizable': false, |
412 'hidden': true, | 468 'hidden': true, |
413 'frame': { | 469 'frame': { |
414 type: 'chrome', | 470 type: 'chrome', |
415 color: '#ffffff' | 471 color: '#ffffff' |
416 }, | 472 }, |
417 'innerBounds': { | 473 'innerBounds': { |
418 'width': 960, | 474 'width': 960, |
419 'height': 688 | 475 'height': 688 |
420 } | 476 } |
421 }; | 477 }; |
422 chrome.app.window.create('main.html', options, onWindowCreated); | 478 chrome.app.window.create('main.html', options, onWindowCreated); |
423 }); | 479 }); |
OLD | NEW |