OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * UI Pages. Note the order must be in sync with the | 6 * UI Pages. Note the order must be in sync with the |
7 * ArcAuthService::UIPage enum. | 7 * ArcAuthService::UIPage enum. |
8 * @type {Array<string>} | 8 * @type {Array<string>} |
9 */ | 9 */ |
10 var UI_PAGES = ['none', | 10 var UI_PAGES = ['none', |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 */ | 45 */ |
46 function closeWindowInternally() { | 46 function closeWindowInternally() { |
47 windowClosedInternally = true; | 47 windowClosedInternally = true; |
48 appWindow.close(); | 48 appWindow.close(); |
49 appWindow = null; | 49 appWindow = null; |
50 } | 50 } |
51 | 51 |
52 /** | 52 /** |
53 * Sends a native message to ArcSupportHost. | 53 * Sends a native message to ArcSupportHost. |
54 * @param {string} code The action code in message. | 54 * @param {string} code The action code in message. |
| 55 * @param {Object=} opt_Props Extra properties for the message. |
55 */ | 56 */ |
56 function sendNativeMessage(code) { | 57 function sendNativeMessage(code, opt_Props) { |
57 message = {'action': code}; | 58 var message = Object.assign({'action': code}, opt_Props); |
58 port.postMessage(message); | 59 port.postMessage(message); |
59 } | 60 } |
60 | 61 |
61 /** | 62 /** |
62 * Applies localization for html content. | 63 * Applies localization for html content. |
63 * @param {!Object} data Localized strings and relevant information. | 64 * @param {!Object} data Localized strings and relevant information. |
64 */ | 65 */ |
65 function setLocalization(data) { | 66 function setLocalization(data) { |
66 var doc = appWindow.contentWindow.document; | 67 var doc = appWindow.contentWindow.document; |
67 var loadTimeData = appWindow.contentWindow.loadTimeData; | 68 var loadTimeData = appWindow.contentWindow.loadTimeData; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 return; | 118 return; |
118 } | 119 } |
119 | 120 |
120 var doc = appWindow.contentWindow.document; | 121 var doc = appWindow.contentWindow.document; |
121 var pages = doc.getElementsByClassName('section'); | 122 var pages = doc.getElementsByClassName('section'); |
122 for (var i = 0; i < pages.length; i++) { | 123 for (var i = 0; i < pages.length; i++) { |
123 pages[i].hidden = pages[i].id != pageDivId; | 124 pages[i].hidden = pages[i].id != pageDivId; |
124 } | 125 } |
125 | 126 |
126 if (pageDivId == 'lso-loading') { | 127 if (pageDivId == 'lso-loading') { |
127 webview.src = 'https://accounts.google.com/o/oauth2/programmatic_auth?' + | 128 webview.src = 'https://accounts.google.com/o/oauth2/v2/auth?client_id=' + |
128 'scope=https://www.google.com/accounts/OAuthLogin&client_id' + | 129 '1070009224336-sdh77n7uot3oc99ais00jmuft6sk2fg9.apps.' + |
129 '=1070009224336-sdh77n7uot3oc99ais00jmuft6sk2fg9.apps.' + | 130 'googleusercontent.com&response_type=code&redirect_uri=oob&' + |
130 'googleusercontent.com'; | 131 'scope=https://www.google.com/accounts/OAuthLogin'; |
131 } | 132 } |
132 appWindow.show(); | 133 appWindow.show(); |
133 } | 134 } |
134 | 135 |
135 /** | 136 /** |
| 137 * Sets error message. |
| 138 * @param {string} error message. |
| 139 */ |
| 140 function setErrorMessage(error) { |
| 141 if (!appWindow) { |
| 142 return; |
| 143 } |
| 144 var doc = appWindow.contentWindow.document; |
| 145 var messageElement = doc.getElementById('error-message'); |
| 146 messageElement.innerText = error; |
| 147 } |
| 148 |
| 149 /** |
136 * Shows requested page. | 150 * Shows requested page. |
137 * @param {int} pageId Index of the page to show. Must be in the array range of | 151 * @param {int} pageId Index of the page to show. Must be in the array range of |
138 * UI_PAGES. | 152 * UI_PAGES. |
139 * @param {string} status associated with page string status, error message for | 153 * @param {string} status associated with page string status, error message for |
140 * example. | 154 * example. |
141 */ | 155 */ |
142 function showPageWithStatus(pageId, status) { | 156 function showPageWithStatus(pageId, status) { |
143 if (!appWindow) { | 157 if (!appWindow) { |
144 return; | 158 return; |
145 } | 159 } |
146 | 160 |
147 if (UI_PAGES[pageId] == 'error') { | 161 if (UI_PAGES[pageId] == 'error') { |
148 var doc = appWindow.contentWindow.document; | 162 setErrorMessage(status); |
149 var messageElement = doc.getElementById('error-message'); | |
150 messageElement.innerText = status; | |
151 } | 163 } |
152 showPage(UI_PAGES[pageId]); | 164 showPage(UI_PAGES[pageId]); |
153 } | 165 } |
154 | 166 |
155 chrome.app.runtime.onLaunched.addListener(function() { | 167 chrome.app.runtime.onLaunched.addListener(function() { |
156 var onAppContentLoad = function() { | 168 var onAppContentLoad = function() { |
157 var doc = appWindow.contentWindow.document; | 169 var doc = appWindow.contentWindow.document; |
158 webview = doc.getElementById('arc-support'); | 170 webview = doc.getElementById('arc-support'); |
159 | 171 |
160 var onWebviewRequestCompleted = function(details) { | 172 var isApprovalResponse = function(url) { |
161 showPage('lso'); | 173 var resultUrlPrefix = 'https://accounts.google.com/o/oauth2/approval?'; |
162 var resultUrlPrefix = | 174 return url.substring(0, resultUrlPrefix.length) == resultUrlPrefix; |
163 'https://accounts.google.com/o/oauth2/programmatic_auth?'; | 175 }; |
164 if (details.statusCode == 200 && | 176 |
165 details.url.substring(0, resultUrlPrefix.length) == resultUrlPrefix) { | 177 var onWebviewRequestResponseStarted = function(details) { |
166 sendNativeMessage('checkAuthCode'); | 178 if (isApprovalResponse(details.url)) { |
| 179 showPage('arc-loading'); |
167 } | 180 } |
168 }; | 181 }; |
169 | 182 |
| 183 var onWebviewContentLoad = function() { |
| 184 if (!isApprovalResponse(webview.src)) { |
| 185 // Show LSO page when its content is ready. |
| 186 showPage('lso'); |
| 187 return; |
| 188 } |
| 189 |
| 190 webview.executeScript({code: 'document.title;'}, function(results) { |
| 191 var authCodePrefix = 'Success code='; |
| 192 if (results[0].substring(0, authCodePrefix.length) == |
| 193 authCodePrefix) { |
| 194 var authCode = results[0].substring(authCodePrefix.length); |
| 195 sendNativeMessage('setAuthCode', {code: authCode}); |
| 196 } else { |
| 197 setErrorMessage(appWindow.contentWindow.loadTimeData.getString( |
| 198 'authorizationFailed')); |
| 199 showPage('error'); |
| 200 } |
| 201 }); |
| 202 }; |
| 203 |
170 var requestFilter = { | 204 var requestFilter = { |
171 urls: ['<all_urls>'], | 205 urls: ['<all_urls>'], |
172 types: ['main_frame'] | 206 types: ['main_frame'] |
173 }; | 207 }; |
174 | 208 |
175 webview.request.onCompleted.addListener(onWebviewRequestCompleted, | 209 webview.request.onResponseStarted.addListener( |
176 requestFilter); | 210 onWebviewRequestResponseStarted, requestFilter); |
| 211 webview.addEventListener('contentload', onWebviewContentLoad); |
177 | 212 |
178 var onGetStarted = function() { | 213 var onGetStarted = function() { |
179 sendNativeMessage('checkAuthCode'); | 214 sendNativeMessage('startLSO'); |
180 }; | 215 }; |
181 | 216 |
182 var onRetry = function() { | 217 var onRetry = function() { |
183 sendNativeMessage('checkAuthCode'); | 218 sendNativeMessage('startLSO'); |
184 }; | 219 }; |
185 | 220 |
186 doc.getElementById('get-started').addEventListener('click', onGetStarted); | 221 doc.getElementById('get-started').addEventListener('click', onGetStarted); |
187 doc.getElementById('retry').addEventListener('click', onRetry); | 222 doc.getElementById('retry').addEventListener('click', onRetry); |
188 | 223 |
189 connectPort(); | 224 connectPort(); |
190 }; | 225 }; |
191 | 226 |
192 var onWindowCreated = function(createdWindow) { | 227 var onWindowCreated = function(createdWindow) { |
193 appWindow = createdWindow; | 228 appWindow = createdWindow; |
(...skipping 17 matching lines...) Expand all Loading... |
211 type: 'chrome', | 246 type: 'chrome', |
212 color: '#67a030' | 247 color: '#67a030' |
213 }, | 248 }, |
214 'innerBounds': { | 249 'innerBounds': { |
215 'width': 800, | 250 'width': 800, |
216 'height': 600 | 251 'height': 600 |
217 } | 252 } |
218 }; | 253 }; |
219 chrome.app.window.create('main.html', options, onWindowCreated); | 254 chrome.app.window.create('main.html', options, onWindowCreated); |
220 }); | 255 }); |
OLD | NEW |