Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 // TODO(vadimt): Remove alerts. | 7 // TODO(vadimt): Remove alerts. |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * @fileoverview Utility objects and functions for Google Now extension. | 10 * @fileoverview Utility objects and functions for Google Now extension. |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 alert('Argument ' + callbackParameter + ' of ' + functionName + | 276 alert('Argument ' + callbackParameter + ' of ' + functionName + |
| 277 ' is not a function'); | 277 ' is not a function'); |
| 278 } | 278 } |
| 279 arguments[callbackParameter] = wrapCallback( | 279 arguments[callbackParameter] = wrapCallback( |
| 280 callback, functionName == 'addListener'); | 280 callback, functionName == 'addListener'); |
| 281 return originalFunction.apply(namespace, arguments); | 281 return originalFunction.apply(namespace, arguments); |
| 282 }; | 282 }; |
| 283 } | 283 } |
| 284 | 284 |
| 285 instrumentApiFunction(chrome.alarms.onAlarm, 'addListener', 0); | 285 instrumentApiFunction(chrome.alarms.onAlarm, 'addListener', 0); |
| 286 instrumentApiFunction(chrome.identity, 'getAuthToken', 1); | |
|
vadimt
2013/07/30 23:27:23
We instrument only functions with callbacks that a
robliao
2013/07/30 23:58:03
Done.
| |
| 287 instrumentApiFunction(chrome.identity, 'removeCachedAuthToken', 1); | |
| 286 instrumentApiFunction(chrome.runtime.onSuspend, 'addListener', 0); | 288 instrumentApiFunction(chrome.runtime.onSuspend, 'addListener', 0); |
| 287 | 289 |
| 288 chrome.runtime.onSuspend.addListener(function() { | 290 chrome.runtime.onSuspend.addListener(function() { |
| 289 var stringifiedPendingCallbacks = JSON.stringify(pendingCallbacks); | 291 var stringifiedPendingCallbacks = JSON.stringify(pendingCallbacks); |
| 290 verify( | 292 verify( |
| 291 queue.length == 0 && stringifiedPendingCallbacks == '{}', | 293 queue.length == 0 && stringifiedPendingCallbacks == '{}', |
| 292 'Incomplete task or pending callbacks when unloading event page,' + | 294 'Incomplete task or pending callbacks when unloading event page,' + |
| 293 ' queue = ' + JSON.stringify(queue) + | 295 ' queue = ' + JSON.stringify(queue) + |
| 294 ', pendingCallbacks = ' + stringifiedPendingCallbacks); | 296 ', pendingCallbacks = ' + stringifiedPendingCallbacks); |
| 295 }); | 297 }); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 }); | 408 }); |
| 407 | 409 |
| 408 return { | 410 return { |
| 409 start: start, | 411 start: start, |
| 410 planForNext: planForNext, | 412 planForNext: planForNext, |
| 411 stop: stop, | 413 stop: stop, |
| 412 isRunning: isRunning | 414 isRunning: isRunning |
| 413 }; | 415 }; |
| 414 } | 416 } |
| 415 | 417 |
| 418 /** | |
| 419 * Wraps chrome.identity to provide limited listening support for | |
| 420 * the sign in state by polling periodically for the auth token. | |
| 421 * @return {object} The Authentication Manager interface. | |
|
vadimt
2013/07/30 23:27:23
{Object}
robliao
2013/07/30 23:58:03
Done.
| |
| 422 */ | |
| 423 function buildAuthenticationManager() { | |
| 424 var alarmName = 'sign-in-alarm'; | |
| 425 | |
| 426 // Holds callbacks for calls to isSignedIn. | |
| 427 // Multiple calls to isSignedIn are possible while getAuthToken is | |
| 428 // returning back to us. Queue up the callbacks so that only | |
| 429 // one call to getAuthToken is required. | |
| 430 var isSignedInRequests = []; | |
|
vadimt
2013/07/30 23:27:23
Do we queue them for perf reasons? If so, no need
robliao
2013/07/30 23:58:03
Sounds good then.
On 2013/07/30 23:27:23, vadimt
| |
| 431 | |
| 432 /** | |
| 433 * Determines if the user is signed in and provides a token if signed in. | |
| 434 * @param {function(boolean, string)} onSuccess Called on completion. | |
|
vadimt
2013/07/30 23:27:23
It's called even when getAuthToken fails, therefor
robliao
2013/07/30 23:58:03
In our case, getAuthToken can fail and we treat th
vadimt
2013/07/31 02:29:49
But onSuccess implies there is onFailure. If it's
robliao
2013/07/31 14:46:24
sgtm
On 2013/07/31 02:29:49, vadimt wrote:
| |
| 435 * The boolean is true if the user is signed in, false otherwise. | |
| 436 * If the user is signed in, the string contains the token. | |
| 437 */ | |
| 438 function isSignedIn(onSuccess) { | |
| 439 isSignedInRequests.push(onSuccess); | |
| 440 if (isSignedInRequests.length == 1) { | |
| 441 chrome.identity.getAuthToken({interactive: false}, function(token) { | |
| 442 var signedIn = !chrome.runtime.lastError && !!token; | |
| 443 isSignedInRequests.forEach(function(queuedCallback) { | |
| 444 queuedCallback(signedIn, token); | |
|
vadimt
2013/07/30 23:27:23
It would be enough to simply pass token. If it's u
robliao
2013/07/30 23:58:03
This was done to remove all of the !!'s that were
vadimt
2013/07/31 02:29:49
But what you essentially do is passing a value and
robliao
2013/07/31 14:46:24
Done.
| |
| 445 }); | |
| 446 isSignedInRequests = []; | |
| 447 checkAndNotifyListeners(signedIn); | |
| 448 }); | |
| 449 } | |
| 450 } | |
| 451 | |
| 452 /** | |
| 453 * Removes the specified cached token. | |
| 454 * @param {string} token Authentication Token to remove from the cache. | |
| 455 * @param {function} onSuccess Called on completion. | |
| 456 */ | |
| 457 function removeToken(token, onSuccess) { | |
| 458 chrome.identity.removeCachedAuthToken({token: token}, function() { | |
| 459 // Removing the token from the cache will change the sign in state. | |
| 460 // Repoll now to check the state and notify listeners. | |
| 461 // This also lets Chrome now about a possible problem with the token. | |
| 462 isSignedIn(function() {}); | |
| 463 onSuccess(); | |
| 464 }); | |
| 465 } | |
| 466 | |
| 467 var listeners = []; | |
|
vadimt
2013/07/30 23:27:23
Please add a TODO item to get rid of this and impl
robliao
2013/07/30 23:58:03
Done.
| |
| 468 | |
| 469 /** | |
| 470 * Registers a listener that gets called back when the signed in state | |
| 471 * is found to be changed. | |
| 472 * @param {function} callback Called when the answer to isSignedIn changes. | |
| 473 */ | |
| 474 function addListener(callback) { | |
| 475 listeners.push(callback); | |
| 476 } | |
| 477 | |
| 478 // Tracks the last answer of isSignedIn and is intentionally initialized | |
| 479 // to undefined. checkAndNotifyListeners will not notify the listeners if | |
| 480 // this is undefined because technically, no sign in state change occurred. | |
| 481 var lastReturnedSignedInState; | |
|
vadimt
2013/07/30 23:27:23
Please explicitly assign null. No reasons to use u
robliao
2013/07/30 23:58:03
Done.
| |
| 482 | |
| 483 function checkAndNotifyListeners(currentSignedInState) { | |
| 484 if ((lastReturnedSignedInState != currentSignedInState) && | |
| 485 (lastReturnedSignedInState != undefined)) { | |
|
vadimt
2013/07/30 23:27:23
Please use !== to not deal with JS strangeness.
robliao
2013/07/30 23:58:03
Done.
| |
| 486 for (var listenerIndex in listeners) { | |
| 487 listeners[listenerIndex](); | |
| 488 } | |
| 489 } | |
| 490 lastReturnedSignedInState = currentSignedInState; | |
| 491 } | |
| 492 | |
| 493 chrome.alarms.onAlarm.addListener(function(alarm) { | |
| 494 if (alarm.name == alarmName) { | |
| 495 isSignedIn(function() {}); | |
| 496 } | |
| 497 }); | |
| 498 | |
| 499 // Poll for the sign in state every hour. | |
| 500 // One hour is just an arbitrary amount of time chosen. | |
| 501 chrome.alarms.create(alarmName, {periodInMinutes: 60}); | |
| 502 | |
| 503 return { | |
| 504 addListener: addListener, | |
| 505 isSignedIn: isSignedIn, | |
| 506 removeToken: removeToken | |
| 507 }; | |
| 508 } | |
| OLD | NEW |