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 webview.request.onCompleted.addListener(onWebviewRequestCompleted, |
| 83 requestFilter); |
| 84 }; |
| 85 |
| 86 var onWindowCreated = function(createdWindow) { |
| 87 appWindow = createdWindow; |
| 88 appWindow.contentWindow.onload = onAppContentLoad; |
| 89 createdWindow.onClosed.addListener(onWindowClosed); |
| 90 connectPort(); |
| 91 }; |
| 92 |
| 93 var onWindowClosed = function() { |
| 94 if (windowClosedInternally) { |
| 95 return; |
| 96 } |
| 97 sendNativeMessage('cancelAuthCode'); |
| 98 }; |
| 99 |
| 100 windowClosedInternally = false; |
| 101 var options = { |
| 102 'id': 'arc_support_wnd', |
| 103 'innerBounds': { |
| 104 'width': 500, |
| 105 'height': 600 |
| 106 } |
| 107 }; |
| 108 chrome.app.window.create('main.html', options, onWindowCreated); |
| 109 }); |
OLD | NEW |