Chromium Code Reviews| 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 |
| index 56c757621c4fa5b49c18e9a4c8c27449ca0f24a1..da2577e706eb625f13eff928f3b09243e6d93c77 100644 |
| --- a/chrome/browser/resources/chromeos/arc_support/background.js |
| +++ b/chrome/browser/resources/chromeos/arc_support/background.js |
| @@ -124,15 +124,28 @@ function showPage(pageDivId) { |
| } |
| if (pageDivId == 'lso-loading') { |
| - webview.src = 'https://accounts.google.com/o/oauth2/programmatic_auth?' + |
| - 'scope=https://www.google.com/accounts/OAuthLogin&client_id' + |
| - '=1070009224336-sdh77n7uot3oc99ais00jmuft6sk2fg9.apps.' + |
| - 'googleusercontent.com'; |
| + webview.src = 'https://accounts.google.com/o/oauth2/v2/auth?client_id=' + |
| + '1070009224336-sdh77n7uot3oc99ais00jmuft6sk2fg9.apps.' + |
| + 'googleusercontent.com&response_type=code&redirect_uri=oob&' + |
| + 'scope=https://www.google.com/accounts/OAuthLogin'; |
| } |
| appWindow.show(); |
| } |
| /** |
| + * Sets error message. |
| + * @param {string} error message. |
| + */ |
| +function setErrorMessage(error) { |
| + if (!appWindow) { |
| + return; |
| + } |
| + var doc = appWindow.contentWindow.document; |
| + var messageElement = doc.getElementById('error-message'); |
| + messageElement.innerText = error; |
| +} |
| + |
| +/** |
| * Shows requested page. |
| * @param {int} pageId Index of the page to show. Must be in the array range of |
| * UI_PAGES. |
| @@ -145,9 +158,7 @@ function showPageWithStatus(pageId, status) { |
| } |
| if (UI_PAGES[pageId] == 'error') { |
| - var doc = appWindow.contentWindow.document; |
| - var messageElement = doc.getElementById('error-message'); |
| - messageElement.innerText = status; |
| + setErrorMessage(status); |
| } |
| showPage(UI_PAGES[pageId]); |
| } |
| @@ -157,16 +168,62 @@ chrome.app.runtime.onLaunched.addListener(function() { |
| var doc = appWindow.contentWindow.document; |
| webview = doc.getElementById('arc-support'); |
| - var onWebviewRequestCompleted = function(details) { |
| - showPage('lso'); |
| + var isApprovalResponse = function(details) { |
| var resultUrlPrefix = |
| - 'https://accounts.google.com/o/oauth2/programmatic_auth?'; |
| - if (details.statusCode == 200 && |
| - details.url.substring(0, resultUrlPrefix.length) == resultUrlPrefix) { |
| - sendNativeMessage('checkAuthCode'); |
| + 'https://accounts.google.com/o/oauth2/approval?'; |
| + return details.statusCode == 200 && |
| + details.url.substring(0, resultUrlPrefix.length) == |
| + resultUrlPrefix; |
| + }; |
| + |
| + var onWebviewRequestResponseStarted = function(details) { |
| + if (isApprovalResponse(details)) { |
| + // Hide LSO page that displays auth code. |
| + showPage('arc-loading'); |
| + } else { |
| + showPage('lso'); |
| } |
| }; |
| + var onWebviewRequestCompleted = function(details) { |
| + if (!isApprovalResponse(details)) { |
| + return; |
| + } |
| + |
| + var attemptCount = 10; |
|
khmel
2016/03/30 15:24:55
This LSO page returns auth code in title.
I know,
|
| + var extractAuthCode = function() { |
| + if (webview.src == details.url) { |
| + webview.executeScript({code: 'document.title;' |
| + }, function(results) { |
| + var authCodePrefix = 'Success code='; |
| + if (results[0].substring(0, authCodePrefix.length) == |
| + authCodePrefix) { |
| + var authCode = results[0].substring(authCodePrefix.length); |
| + console.log('Auth code:' + authCode); |
| + var message = { |
| + 'action': 'setAuthCode', |
| + 'code': authCode |
| + }; |
| + port.postMessage(message); |
| + } else { |
| + setErrorMessage('Authorization failed.'); |
| + showPage('error'); |
| + } |
| + }); |
| + } else { |
| + if (attemptCount > 0) { |
| + attemptCount -= 1; |
| + setTimeout(extractAuthCode, 100); |
| + } else { |
| + setErrorMessage('Authorization time out.'); |
| + showPage('error'); |
| + } |
| + } |
| + }; |
| + |
| + extractAuthCode(); |
| + }; |
| + |
| var requestFilter = { |
| urls: ['<all_urls>'], |
| types: ['main_frame'] |
| @@ -174,13 +231,15 @@ chrome.app.runtime.onLaunched.addListener(function() { |
| webview.request.onCompleted.addListener(onWebviewRequestCompleted, |
| requestFilter); |
| + webview.request.onResponseStarted.addListener( |
| + onWebviewRequestResponseStarted, requestFilter); |
| var onGetStarted = function() { |
| - sendNativeMessage('checkAuthCode'); |
| + sendNativeMessage('startLSO'); |
| }; |
| var onRetry = function() { |
| - sendNativeMessage('checkAuthCode'); |
| + sendNativeMessage('startLSO'); |
| }; |
| doc.getElementById('get-started').addEventListener('click', onGetStarted); |