Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: chrome/browser/resources/google_now/utility.js

Issue 21235008: Authentication Manager for Google Now (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@GeoSM-Diag
Patch Set: CR Feedback Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/resources/google_now/background_unittest.gtestjs ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 ? null : token;
vadimt 2013/07/31 20:46:17 I suggest using 'undefined' for 2 reasons: 1. getA
robliao 2013/07/31 20:49:15 I'm convinced. On 2013/07/31 20:46:17, vadimt wrot
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;
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
488 // Poll for the sign in state every hour.
489 // One hour is just an arbitrary amount of time chosen.
490 chrome.alarms.create(alarmName, {periodInMinutes: 60});
491
492 return {
493 addListener: addListener,
494 isSignedIn: isSignedIn,
495 removeToken: removeToken
496 };
497 }
OLDNEW
« no previous file with comments | « chrome/browser/resources/google_now/background_unittest.gtestjs ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698