Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 var appWindow = null; | |
|
xiyuan
2016/02/11 17:57:21
Please document global variables and functions.
khmel
2016/02/12 02:45:24
Done.
| |
| 6 var webview = null; | |
| 7 var port = null; | |
| 8 | |
| 9 function sendNativeMessage(code) { | |
| 10 message = {'action': code}; | |
| 11 port.postMessage(message); | |
| 12 } | |
| 13 | |
| 14 function onNativeMessage(message) { | |
| 15 if (!message.action) { | |
| 16 return; | |
| 17 } | |
| 18 | |
| 19 if (message.action == 'closeUI') { | |
| 20 if (appWindow) { | |
| 21 appWindow.close(); | |
| 22 appWindow = null; | |
| 23 } | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 function connectPort() { | |
| 28 var hostName = 'com.google.arc_opt_in'; | |
| 29 port = chrome.runtime.connectNative(hostName); | |
| 30 port.onMessage.addListener(onNativeMessage); | |
| 31 port.onDisconnect.addListener(onDisconnected); | |
|
xiyuan
2016/02/11 17:57:21
onDisconnected is not defined.
khmel
2016/02/12 02:45:24
Done.
| |
| 32 } | |
| 33 | |
| 34 chrome.app.runtime.onLaunched.addListener(function() { | |
| 35 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.
| |
| 36 'id': 'arc_opt_in_wnd', | |
| 37 '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.
| |
| 38 'innerBounds': { | |
| 39 'width': 500, | |
| 40 'height': 600 | |
| 41 } | |
| 42 }; | |
| 43 | |
| 44 var onContentLoad = function() { | |
| 45 var doc = appWindow.document; | |
| 46 webview = doc.getElementById('arc_opt_in'); | |
| 47 | |
| 48 var onLoadStop = function() { | |
| 49 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.
| |
| 50 code: 'window.location.href;' | |
| 51 }, function(results) { | |
| 52 if (results[0].indexOf( | |
| 53 'https://accounts.google.com/o/oauth2/programmatic_auth') == 0) { | |
| 54 sendNativeMessage('fetchAuthCode'); | |
| 55 } | |
| 56 }); | |
| 57 }; | |
| 58 | |
| 59 webview.addEventListener('loadstop', onLoadStop); | |
| 60 }; | |
| 61 | |
| 62 var onWindowCreated = function(createdWindow) { | |
| 63 appWindow = createdWindow.contentWindow; | |
| 64 appWindow.onload = onContentLoad; | |
| 65 connectPort(); | |
| 66 }; | |
| 67 | |
| 68 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!
| |
| 69 }); | |
| OLD | NEW |