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 /** | |
| 6 * Chrome window that hosts UI. Only one window is allowed. | |
| 7 * @type {chrome.app.window.AppWindow} | |
| 8 */ | |
| 9 var appWindow = null; | |
| 10 | |
| 11 /** | |
| 12 * Contains Web content provided by Google authorization server. | |
| 13 * @type {WebView} | |
| 14 */ | |
| 15 var webview = null; | |
| 16 | |
| 17 /** | |
| 18 * Used for bidirectional communication with native code. | |
| 19 * @type {chrome.runtime.Port} | |
| 20 */ | |
| 21 var port = null; | |
| 22 | |
| 23 /** | |
| 24 * Indicates that window was closed internally and it is not required to send | |
| 25 * closure notification. | |
| 26 * @type {boolean} | |
| 27 */ | |
| 28 var windowClosedInternally = false; | |
| 29 | |
| 30 /** | |
| 31 * Sends a native message to ArcSupportHost. | |
| 32 * @param {string} code The action code in message. | |
| 33 */ | |
| 34 function sendNativeMessage(code) { | |
| 35 message = {'action': code}; | |
| 36 port.postMessage(message); | |
| 37 } | |
| 38 | |
| 39 /** | |
| 40 * Handles native messages received from ArcSupportHost. | |
| 41 * @param {!Object} message The message received. | |
| 42 */ | |
| 43 function onNativeMessage(message) { | |
| 44 if (!message.action) { | |
| 45 return; | |
| 46 } | |
| 47 | |
| 48 if (message.action == 'closeUI') { | |
| 49 if (appWindow) { | |
| 50 windowClosedInternally = true; | |
| 51 appWindow.close(); | |
| 52 appWindow = null; | |
| 53 } | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 /** | |
| 58 * Connects to ArcSupportHost. | |
| 59 */ | |
| 60 function connectPort() { | |
| 61 var hostName = 'com.google.arc_support'; | |
| 62 port = chrome.runtime.connectNative(hostName); | |
| 63 port.onMessage.addListener(onNativeMessage); | |
| 64 } | |
| 65 | |
| 66 chrome.app.runtime.onLaunched.addListener(function() { | |
| 67 var onAppContentLoad = function() { | |
| 68 var doc = appWindow.contentWindow.document; | |
| 69 webview = doc.getElementById('arc_support'); | |
| 70 | |
| 71 var onWebviewRequestCompleted = function(details) { | |
| 72 if (details.statusCode == 200) { | |
| 73 sendNativeMessage('fetchAuthCode'); | |
| 74 } | |
| 75 }; | |
| 76 | |
| 77 var requestFilter = { | |
| 78 urls: ['https://accounts.google.com/o/oauth2/programmatic_auth?*'], | |
| 79 types: ['main_frame'] | |
| 80 }; | |
| 81 | |
| 82 var requestFilterOptions = ['responseHeaders']; | |
| 83 | |
| 84 webview.request.onCompleted.addListener(onWebviewRequestCompleted, | |
| 85 requestFilter, | |
| 86 requestFilterOptions); | |
|
xiyuan
2016/02/12 18:25:43
nit: since we don't parsing http header, we can ge
| |
| 87 }; | |
| 88 | |
| 89 var onWindowCreated = function(createdWindow) { | |
| 90 appWindow = createdWindow; | |
| 91 appWindow.contentWindow.onload = onAppContentLoad; | |
| 92 createdWindow.onClosed.addListener(onWindowClosed); | |
| 93 connectPort(); | |
| 94 }; | |
| 95 | |
| 96 var onWindowClosed = function() { | |
| 97 if (windowClosedInternally) { | |
| 98 return; | |
| 99 } | |
| 100 sendNativeMessage('cancelAuthCode'); | |
| 101 }; | |
| 102 | |
| 103 var options = { | |
| 104 'id': 'arc_support_wnd', | |
| 105 'innerBounds': { | |
| 106 'width': 500, | |
| 107 'height': 600 | |
| 108 } | |
| 109 }; | |
| 110 chrome.app.window.create('main.html', options, onWindowCreated); | |
| 111 }); | |
| OLD | NEW |