Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: chrome/browser/resources/chromeos/arc_support/background.js

Issue 2102793002: arc: Implement silent OptIn mode for managed Arc. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nits Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/chromeos/arc/arc_support_host.cc ('k') | chrome/chrome_browser_chromeos.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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) {
87 currentDeviceId = deviceId; 102 currentDeviceId = deviceId;
103 window.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
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
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
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 submitApproveCode =
351 'document.getElementById("connect_approve").submit();';
352 lsoView.executeScript({code: submitApproveCode}, function(results) {
353 });
354 }
300 return; 355 return;
301 } 356 }
302 357
303 lsoView.executeScript({code: 'document.title;'}, function(results) { 358 lsoView.executeScript({code: 'document.title;'}, function(results) {
304 var authCodePrefix = 'Success code='; 359 var authCodePrefix = 'Success code=';
305 if (results[0].substring(0, authCodePrefix.length) == 360 if (results[0].substring(0, authCodePrefix.length) ==
306 authCodePrefix) { 361 authCodePrefix) {
307 var authCode = results[0].substring(authCodePrefix.length); 362 var authCode = results[0].substring(authCodePrefix.length);
308 sendNativeMessage('setAuthCode', {code: authCode}); 363 sendNativeMessage('setAuthCode', {code: authCode});
364 cancelRetry();
309 } else { 365 } else {
310 setErrorMessage(appWindow.contentWindow.loadTimeData.getString( 366 setErrorMessage(appWindow.contentWindow.loadTimeData.getString(
311 'authorizationFailed')); 367 'authorizationFailed'));
312 showPage('error'); 368 showPage('error');
313 } 369 }
314 }); 370 });
315 }; 371 };
316 372
317 var requestFilter = { 373 var requestFilter = {
318 urls: ['<all_urls>'], 374 urls: ['<all_urls>'],
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 appWindow = createdWindow; 447 appWindow = createdWindow;
392 appWindow.contentWindow.onload = onAppContentLoad; 448 appWindow.contentWindow.onload = onAppContentLoad;
393 createdWindow.onClosed.addListener(onWindowClosed); 449 createdWindow.onClosed.addListener(onWindowClosed);
394 }; 450 };
395 451
396 var onWindowClosed = function() { 452 var onWindowClosed = function() {
397 if (termsReloadTimeout) { 453 if (termsReloadTimeout) {
398 clearTimeout(termsReloadTimeout); 454 clearTimeout(termsReloadTimeout);
399 termsReloadTimeout = null; 455 termsReloadTimeout = null;
400 } 456 }
457 cancelRetry();
401 458
402 if (windowClosedInternally) { 459 if (windowClosedInternally) {
403 return; 460 return;
404 } 461 }
405 sendNativeMessage('cancelAuthCode'); 462 sendNativeMessage('cancelAuthCode');
406 }; 463 };
407 464
408 windowClosedInternally = false; 465 windowClosedInternally = false;
409 var options = { 466 var options = {
410 'id': 'play_store_wnd', 467 'id': 'play_store_wnd',
411 'resizable': false, 468 'resizable': false,
412 'hidden': true, 469 'hidden': true,
413 'frame': { 470 'frame': {
414 type: 'chrome', 471 type: 'chrome',
415 color: '#ffffff' 472 color: '#ffffff'
416 }, 473 },
417 'innerBounds': { 474 'innerBounds': {
418 'width': 960, 475 'width': 960,
419 'height': 688 476 'height': 688
420 } 477 }
421 }; 478 };
422 chrome.app.window.create('main.html', options, onWindowCreated); 479 chrome.app.window.create('main.html', options, onWindowCreated);
423 }); 480 });
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/arc_support_host.cc ('k') | chrome/chrome_browser_chromeos.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698