Index: chrome/browser/resources/chromeos/arc_opt_in/background.js |
diff --git a/chrome/browser/resources/chromeos/arc_opt_in/background.js b/chrome/browser/resources/chromeos/arc_opt_in/background.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..85384b6ccb232c4be9dfeb73173beb1d1f6b2c7f |
--- /dev/null |
+++ b/chrome/browser/resources/chromeos/arc_opt_in/background.js |
@@ -0,0 +1,69 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+var appWindow = null; |
xiyuan
2016/02/11 17:57:21
Please document global variables and functions.
khmel
2016/02/12 02:45:24
Done.
|
+var webview = null; |
+var port = null; |
+ |
+function sendNativeMessage(code) { |
+ message = {'action': code}; |
+ port.postMessage(message); |
+} |
+ |
+function onNativeMessage(message) { |
+ if (!message.action) { |
+ return; |
+ } |
+ |
+ if (message.action == 'closeUI') { |
+ if (appWindow) { |
+ appWindow.close(); |
+ appWindow = null; |
+ } |
+ } |
+} |
+ |
+function connectPort() { |
+ var hostName = 'com.google.arc_opt_in'; |
+ port = chrome.runtime.connectNative(hostName); |
+ port.onMessage.addListener(onNativeMessage); |
+ port.onDisconnect.addListener(onDisconnected); |
xiyuan
2016/02/11 17:57:21
onDisconnected is not defined.
khmel
2016/02/12 02:45:24
Done.
|
+} |
+ |
+chrome.app.runtime.onLaunched.addListener(function() { |
+ var options = { |
xiyuan
2016/02/11 17:57:20
nit: move this down to chrome.app.window.create ca
khmel
2016/02/12 02:45:24
Done.
|
+ 'id': 'arc_opt_in_wnd', |
+ 'singleton': true, |
xiyuan
2016/02/11 17:57:21
'singleton' is deprecated. We should have only one
khmel
2016/02/12 02:45:24
Yes, is see that it is redundant.
|
+ 'innerBounds': { |
+ 'width': 500, |
+ 'height': 600 |
+ } |
+ }; |
+ |
+ var onContentLoad = function() { |
+ var doc = appWindow.document; |
+ webview = doc.getElementById('arc_opt_in'); |
+ |
+ var onLoadStop = function() { |
+ webview.executeScript({ |
xiyuan
2016/02/11 17:57:21
Would "webview.src" give us the URL?
khmel
2016/02/12 02:45:24
:) This simplifies code.
|
+ code: 'window.location.href;' |
+ }, function(results) { |
+ if (results[0].indexOf( |
+ 'https://accounts.google.com/o/oauth2/programmatic_auth') == 0) { |
+ sendNativeMessage('fetchAuthCode'); |
+ } |
+ }); |
+ }; |
+ |
+ webview.addEventListener('loadstop', onLoadStop); |
+ }; |
+ |
+ var onWindowCreated = function(createdWindow) { |
+ appWindow = createdWindow.contentWindow; |
+ appWindow.onload = onContentLoad; |
+ connectPort(); |
+ }; |
+ |
+ chrome.app.window.create('main.html', options, onWindowCreated); |
xiyuan
2016/02/11 17:57:21
What happens if user closes the window without goi
khmel
2016/02/12 02:45:24
Good point, need to opt out arc. Thanks!
|
+}); |