Index: chrome/browser/resources/google_now/background.js |
diff --git a/chrome/browser/resources/google_now/background.js b/chrome/browser/resources/google_now/background.js |
index bd76627d1029d5fd2c25aded3fa196ca0794b301..c8299f247b0f6aa1d717b6afe9fe92a71df852d9 100644 |
--- a/chrome/browser/resources/google_now/background.js |
+++ b/chrome/browser/resources/google_now/background.js |
@@ -32,6 +32,8 @@ |
*/ |
var HTTP_OK = 200; |
+var HTTP_FORBIDDEN = 403; |
+ |
/** |
* Initial period for polling for Google Now Notifications cards to use when the |
* period from the server is not available. |
@@ -98,6 +100,8 @@ function areTasksConflicting(newTaskName, scheduledTaskName) { |
var tasks = buildTaskManager(areTasksConflicting); |
// Add error processing to API calls. |
+tasks.instrumentApiFunction(chrome.identity, 'getAuthToken', 1); |
+tasks.instrumentApiFunction(chrome.identity, 'removeCachedAuthToken', 1); |
tasks.instrumentApiFunction(chrome.location.onLocationUpdate, 'addListener', 0); |
tasks.instrumentApiFunction(chrome.notifications, 'create', 2); |
tasks.instrumentApiFunction(chrome.notifications, 'update', 2); |
@@ -155,6 +159,45 @@ function recordEvent(event) { |
} |
/** |
+ * Adds authorization behavior to the request. |
+ * @param {XMLHttpRequest} request Server request. |
+ * @param {function(boolean)} callbackBoolean Completion callback with 'success' |
+ * parameter. |
+ */ |
+function setAuthorization(request, callbackBoolean) { |
+ tasks.debugSetStepName('setAuthorization-getAuthToken'); |
+ chrome.identity.getAuthToken({interactive: false}, function(token) { |
+ var errorMessage = |
+ chrome.runtime.lastError && chrome.runtime.lastError.message; |
+ console.log('setAuthorization: error=' + errorMessage + ', token=' + token); |
rgustafson
2013/06/19 00:56:51
Does adding the token onto this log really help? I
vadimt
2013/06/19 01:38:07
Done.
|
+ if (chrome.runtime.lastError || !token) { |
+ callbackBoolean(false); |
+ return; |
+ } |
+ |
+ request.setRequestHeader('Authorization', 'Bearer ' + token); |
+ |
+ // Instrument onloadend to remove stale auth tokens. |
+ var originalOnLoadEnd = request.onloadend; |
+ request.onloadend = tasks.wrapCallback(function(event) { |
+ if (request.status == HTTP_FORBIDDEN) { |
rgustafson
2013/06/19 00:56:51
Could we also handle 401 unauthorized here? It wou
vadimt
2013/06/19 01:38:07
Done.
skare_
2013/06/19 05:21:23
half-curiosity -- have you seen "forbidden" in pra
rgustafson
2013/06/19 18:39:10
The server should only be involved in the revoked
|
+ tasks.debugSetStepName('setAuthorization-removeCachedAuthToken'); |
+ chrome.identity.removeCachedAuthToken({token: token}, function() { |
+ // After purging the token cache, call getAuthToken() again to let |
+ // Chrome know about the problem with the token. |
+ chrome.identity.getAuthToken({interactive: false}, function() {}); |
+ originalOnLoadEnd(event); |
+ }); |
+ } else { |
+ originalOnLoadEnd(event); |
+ } |
+ }); |
+ |
+ callbackBoolean(true); |
+ }); |
+} |
+ |
+/** |
* Shows a notification and remembers information associated with it. |
* @param {Object} card Google Now card represented as a set of parameters for |
* showing a Chrome notification. |
@@ -343,10 +386,9 @@ function requestNotificationCards(position, callback) { |
',' + position.coords.longitude + |
',' + position.coords.accuracy; |
- // TODO(vadimt): Figure out how to send user's identity to the server. |
var request = buildServerRequest('notifications'); |
- request.onloadend = tasks.wrapCallback(function(event) { |
+ request.onloadend = function(event) { |
console.log('requestNotificationCards-onloadend ' + request.status); |
if (request.status == HTTP_OK) { |
recordEvent(DiagnosticEvent.REQUEST_FOR_CARDS_SUCCESS); |
@@ -354,10 +396,16 @@ function requestNotificationCards(position, callback) { |
} else { |
callback(); |
} |
- }); |
+ }; |
- tasks.debugSetStepName('requestNotificationCards-send-request'); |
- request.send(requestParameters); |
+ setAuthorization(request, function(success) { |
+ if (success) { |
+ tasks.debugSetStepName('requestNotificationCards-send-request'); |
+ request.send(requestParameters); |
+ } else { |
+ callback(); |
+ } |
+ }); |
} |
/** |
@@ -411,16 +459,22 @@ function requestCardDismissal( |
var requestParameters = 'id=' + notificationId + |
'&dismissalAge=' + (Date.now() - dismissalTimeMs); |
var request = buildServerRequest('dismiss'); |
- request.onloadend = tasks.wrapCallback(function(event) { |
+ request.onloadend = function(event) { |
console.log('requestDismissingCard-onloadend ' + request.status); |
if (request.status == HTTP_OK) |
recordEvent(DiagnosticEvent.DISMISS_REQUEST_SUCCESS); |
callbackBoolean(request.status == HTTP_OK); |
- }); |
+ }; |
- tasks.debugSetStepName('requestCardDismissal-send-request'); |
- request.send(requestParameters); |
+ setAuthorization(request, function(success) { |
+ if (success) { |
+ tasks.debugSetStepName('requestCardDismissal-send-request'); |
+ request.send(requestParameters); |
+ } else { |
+ callbackBoolean(false); |
+ } |
+ }); |
} |
/** |