Index: chrome/browser/resources/chromeos/arc_support/background.js |
diff --git a/chrome/browser/resources/chromeos/arc_support/background.js b/chrome/browser/resources/chromeos/arc_support/background.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a0ce0a5ad7a92534e4ff1869976ab8f8cf73f113 |
--- /dev/null |
+++ b/chrome/browser/resources/chromeos/arc_support/background.js |
@@ -0,0 +1,80 @@ |
+// 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. |
+ |
+// Chrome window that hosts UI. Only one window is allowed. |
xiyuan
2016/02/12 17:06:59
nit: Use JsDoc style and also doc type
/**
* Chr
khmel
2016/02/12 18:15:14
Done.
|
+var appWindow = null; |
+ |
+// Contains Web content provided by Google authorization server. |
xiyuan
2016/02/12 17:06:59
/**
* Contains Web content provided by Google aut
khmel
2016/02/12 18:15:15
Done.
|
+var webview = null; |
+ |
+// Used for bidirectional communication with native code. |
xiyuan
2016/02/12 17:06:59
/**
* Used for bidirectional communication with n
khmel
2016/02/12 18:15:14
Done.
|
+var port = null; |
+ |
+// Indicates that window was closed internally and it is not required to send |
+// closure notification. |
xiyuan
2016/02/12 17:06:59
/**
* Indicates that window was closed internally
khmel
2016/02/12 18:15:14
Done.
|
+var windowClosedInternally = false; |
+ |
+function sendNativeMessage(code) { |
xiyuan
2016/02/12 17:06:59
/**
* Sends a native message to ArcSupportHost.
khmel
2016/02/12 18:15:14
Done.
|
+ message = {'action': code}; |
+ port.postMessage(message); |
+} |
+ |
+function onNativeMessage(message) { |
xiyuan
2016/02/12 17:06:59
/**
* Handles native messages received from ArcSu
khmel
2016/02/12 18:15:15
Done.
|
+ if (!message.action) { |
+ return; |
+ } |
+ |
+ if (message.action == 'closeUI') { |
+ if (appWindow) { |
+ windowClosedInternally = true; |
+ appWindow.contentWindow.close(); |
xiyuan
2016/02/12 17:06:59
nit: appWindow.close();
khmel
2016/02/12 18:15:15
Done.
|
+ appWindow = null; |
+ } |
+ } |
+} |
+ |
+function connectPort() { |
xiyuan
2016/02/12 17:06:59
/**
* Connects to ArcSupportHost.
*/
khmel
2016/02/12 18:15:15
Done.
|
+ var hostName = 'com.google.arc_support'; |
+ port = chrome.runtime.connectNative(hostName); |
+ port.onMessage.addListener(onNativeMessage); |
+} |
+ |
+chrome.app.runtime.onLaunched.addListener(function() { |
+ var onContentLoad = function() { |
xiyuan
2016/02/12 17:06:59
nit: onContentLoad -> onAppContentLoad to make it
khmel
2016/02/12 18:15:14
Done.
|
+ var doc = appWindow.contentWindow.document; |
+ webview = doc.getElementById('arc_support'); |
+ |
+ var onLoadStop = function() { |
+ if (webview.src.indexOf( |
+ 'https://accounts.google.com/o/oauth2/programmatic_auth') == 0) { |
+ sendNativeMessage('fetchAuthCode'); |
+ } |
+ }; |
+ |
+ webview.addEventListener('loadstop', onLoadStop); |
xiyuan
2016/02/12 17:06:59
loadstop is indeed tricky and triggering unnecessa
khmel
2016/02/12 18:15:14
Thanks for hint! It works much better and predicta
|
+ }; |
+ |
+ var onWindowCreated = function(createdWindow) { |
+ appWindow = createdWindow; |
+ appWindow.contentWindow.onload = onContentLoad; |
+ createdWindow.onClosed.addListener(onWindowClosed); |
+ connectPort(); |
+ }; |
+ |
+ var onWindowClosed = function() { |
+ if (windowClosedInternally) { |
+ return; |
+ } |
+ sendNativeMessage('cancelAuthCode'); |
+ }; |
+ |
+ var options = { |
+ 'id': 'arc_support_wnd', |
+ 'innerBounds': { |
+ 'width': 500, |
+ 'height': 600 |
+ } |
+ }; |
+ chrome.app.window.create('main.html', options, onWindowCreated); |
+}); |