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 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 }); | 406 }); |
407 | 407 |
408 return { | 408 return { |
409 start: start, | 409 start: start, |
410 planForNext: planForNext, | 410 planForNext: planForNext, |
411 stop: stop, | 411 stop: stop, |
412 isRunning: isRunning | 412 isRunning: isRunning |
413 }; | 413 }; |
414 } | 414 } |
415 | 415 |
| 416 // TODO(robliao): Ideally, the authentication watcher infrastructure |
| 417 // below would be an API change to chrome.identity. |
| 418 // When this happens, remove the code below. |
| 419 |
| 420 /** |
| 421 * Wraps chrome.identity to provide limited listening support for |
| 422 * the sign in state by polling periodically for the auth token. |
| 423 * @return {Object} The Authentication Manager interface. |
| 424 */ |
| 425 function buildAuthenticationManager() { |
| 426 var alarmName = 'sign-in-alarm'; |
| 427 |
| 428 /** |
| 429 * Determines if the user is signed in and provides a token if signed in. |
| 430 * @param {function(string=)} callback Called on completion. |
| 431 * If the user is signed in, the string contains the token. |
| 432 */ |
| 433 function isSignedIn(callback) { |
| 434 chrome.identity.getAuthToken({interactive: false}, function(token) { |
| 435 token = chrome.runtime.lastError ? undefined : token; |
| 436 callback(token); |
| 437 checkAndNotifyListeners(!!token); |
| 438 }); |
| 439 } |
| 440 |
| 441 /** |
| 442 * Removes the specified cached token. |
| 443 * @param {string} token Authentication Token to remove from the cache. |
| 444 * @param {function} onSuccess Called on completion. |
| 445 */ |
| 446 function removeToken(token, onSuccess) { |
| 447 chrome.identity.removeCachedAuthToken({token: token}, function() { |
| 448 // Removing the token from the cache will change the sign in state. |
| 449 // Repoll now to check the state and notify listeners. |
| 450 // This also lets Chrome now about a possible problem with the token. |
| 451 isSignedIn(function() {}); |
| 452 onSuccess(); |
| 453 }); |
| 454 } |
| 455 |
| 456 var listeners = []; |
| 457 |
| 458 /** |
| 459 * Registers a listener that gets called back when the signed in state |
| 460 * is found to be changed. |
| 461 * @param {function} callback Called when the answer to isSignedIn changes. |
| 462 */ |
| 463 function addListener(callback) { |
| 464 listeners.push(callback); |
| 465 } |
| 466 |
| 467 // Tracks the last answer of isSignedIn. checkAndNotifyListeners will not |
| 468 // notify the listeners if this is null because technically, no sign in |
| 469 // state change occurred. |
| 470 var lastReturnedSignedInState = null; |
| 471 |
| 472 function checkAndNotifyListeners(currentSignedInState) { |
| 473 if ((lastReturnedSignedInState !== currentSignedInState) && |
| 474 (lastReturnedSignedInState !== null)) { |
| 475 for (var listenerIndex in listeners) { |
| 476 listeners[listenerIndex](); |
| 477 } |
| 478 } |
| 479 lastReturnedSignedInState = currentSignedInState; |
| 480 } |
| 481 |
| 482 chrome.alarms.onAlarm.addListener(function(alarm) { |
| 483 if (alarm.name == alarmName) |
| 484 isSignedIn(function() {}); |
| 485 }); |
| 486 |
| 487 // Poll for the sign in state every hour. |
| 488 // One hour is just an arbitrary amount of time chosen. |
| 489 chrome.alarms.create(alarmName, {periodInMinutes: 60}); |
| 490 |
| 491 return { |
| 492 addListener: addListener, |
| 493 isSignedIn: isSignedIn, |
| 494 removeToken: removeToken |
| 495 }; |
| 496 } |
OLD | NEW |