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

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

Issue 21235008: Authentication Manager for Google Now (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@GeoSM-Diag
Patch Set: 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
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 /** 7 /**
8 * @fileoverview The event page for Google Now for Chrome implementation. 8 * @fileoverview The event page for Google Now for Chrome implementation.
9 * The Google Now event page gets Google Now cards from the server and shows 9 * The Google Now event page gets Google Now cards from the server and shows
10 * them as Chrome notifications. 10 * them as Chrome notifications.
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 113
114 return false; 114 return false;
115 } 115 }
116 116
117 var googleGeolocationAccessEnabledPref = 117 var googleGeolocationAccessEnabledPref =
118 chrome.preferencesPrivate.googleGeolocationAccessEnabled; 118 chrome.preferencesPrivate.googleGeolocationAccessEnabled;
119 119
120 var tasks = buildTaskManager(areTasksConflicting); 120 var tasks = buildTaskManager(areTasksConflicting);
121 121
122 // Add error processing to API calls. 122 // Add error processing to API calls.
123 tasks.instrumentApiFunction(chrome.identity, 'getAuthToken', 1);
124 tasks.instrumentApiFunction(chrome.identity, 'removeCachedAuthToken', 1);
125 tasks.instrumentApiFunction(chrome.location.onLocationUpdate, 'addListener', 0); 123 tasks.instrumentApiFunction(chrome.location.onLocationUpdate, 'addListener', 0);
126 tasks.instrumentApiFunction(chrome.notifications, 'create', 2); 124 tasks.instrumentApiFunction(chrome.notifications, 'create', 2);
127 tasks.instrumentApiFunction(chrome.notifications, 'update', 2); 125 tasks.instrumentApiFunction(chrome.notifications, 'update', 2);
128 tasks.instrumentApiFunction(chrome.notifications, 'getAll', 0); 126 tasks.instrumentApiFunction(chrome.notifications, 'getAll', 0);
129 tasks.instrumentApiFunction( 127 tasks.instrumentApiFunction(
130 chrome.notifications.onButtonClicked, 'addListener', 0); 128 chrome.notifications.onButtonClicked, 'addListener', 0);
131 tasks.instrumentApiFunction(chrome.notifications.onClicked, 'addListener', 0); 129 tasks.instrumentApiFunction(chrome.notifications.onClicked, 'addListener', 0);
132 tasks.instrumentApiFunction(chrome.notifications.onClosed, 'addListener', 0); 130 tasks.instrumentApiFunction(chrome.notifications.onClosed, 'addListener', 0);
133 tasks.instrumentApiFunction( 131 tasks.instrumentApiFunction(
134 googleGeolocationAccessEnabledPref.onChange, 132 googleGeolocationAccessEnabledPref.onChange,
135 'addListener', 133 'addListener',
136 0); 134 0);
137 tasks.instrumentApiFunction(chrome.runtime.onInstalled, 'addListener', 0); 135 tasks.instrumentApiFunction(chrome.runtime.onInstalled, 'addListener', 0);
138 tasks.instrumentApiFunction(chrome.runtime.onStartup, 'addListener', 0); 136 tasks.instrumentApiFunction(chrome.runtime.onStartup, 'addListener', 0);
139 tasks.instrumentApiFunction(chrome.tabs, 'create', 1); 137 tasks.instrumentApiFunction(chrome.tabs, 'create', 1);
140 tasks.instrumentApiFunction(storage, 'get', 1); 138 tasks.instrumentApiFunction(storage, 'get', 1);
141 139
142 var updateCardsAttempts = buildAttemptManager( 140 var updateCardsAttempts = buildAttemptManager(
143 'cards-update', 141 'cards-update',
144 requestLocation, 142 requestLocation,
145 INITIAL_POLLING_PERIOD_SECONDS, 143 INITIAL_POLLING_PERIOD_SECONDS,
146 MAXIMUM_POLLING_PERIOD_SECONDS); 144 MAXIMUM_POLLING_PERIOD_SECONDS);
147 var dismissalAttempts = buildAttemptManager( 145 var dismissalAttempts = buildAttemptManager(
148 'dismiss', 146 'dismiss',
149 retryPendingDismissals, 147 retryPendingDismissals,
150 INITIAL_RETRY_DISMISS_PERIOD_SECONDS, 148 INITIAL_RETRY_DISMISS_PERIOD_SECONDS,
151 MAXIMUM_RETRY_DISMISS_PERIOD_SECONDS); 149 MAXIMUM_RETRY_DISMISS_PERIOD_SECONDS);
152 var cardSet = buildCardSet(); 150 var cardSet = buildCardSet();
153 151
152 var authenticationManager = buildAuthenticationManager();
153
154 /** 154 /**
155 * Google Now UMA event identifier. 155 * Google Now UMA event identifier.
156 * @enum {number} 156 * @enum {number}
157 */ 157 */
158 var GoogleNowEvent = { 158 var GoogleNowEvent = {
159 REQUEST_FOR_CARDS_TOTAL: 0, 159 REQUEST_FOR_CARDS_TOTAL: 0,
160 REQUEST_FOR_CARDS_SUCCESS: 1, 160 REQUEST_FOR_CARDS_SUCCESS: 1,
161 CARDS_PARSE_SUCCESS: 2, 161 CARDS_PARSE_SUCCESS: 2,
162 DISMISS_REQUEST_TOTAL: 3, 162 DISMISS_REQUEST_TOTAL: 3,
163 DISMISS_REQUEST_SUCCESS: 4, 163 DISMISS_REQUEST_SUCCESS: 4,
(...skipping 22 matching lines...) Expand all
186 186
187 chrome.metricsPrivate.recordValue(metricDescription, event); 187 chrome.metricsPrivate.recordValue(metricDescription, event);
188 } 188 }
189 189
190 /** 190 /**
191 * Adds authorization behavior to the request. 191 * Adds authorization behavior to the request.
192 * @param {XMLHttpRequest} request Server request. 192 * @param {XMLHttpRequest} request Server request.
193 * @param {function(boolean)} callbackBoolean Completion callback with 'success' 193 * @param {function(boolean)} callbackBoolean Completion callback with 'success'
194 * parameter. 194 * parameter.
195 */ 195 */
196 function setAuthorization(request, callbackBoolean) { 196 function setAuthorization(request, callbackBoolean) {
vadimt 2013/07/30 23:27:23 Why not move the whole setAuthorization into authe
robliao 2013/07/30 23:58:03 I wanted to keep the HTTP portion out of the ident
197 tasks.debugSetStepName('setAuthorization-getAuthToken'); 197 tasks.debugSetStepName('setAuthorization-isSignedIn');
198 chrome.identity.getAuthToken({interactive: false}, function(token) { 198 authenticationManager.isSignedIn(function(signedIn, token) {
199 var errorMessage = 199 if (!signedIn) {
200 chrome.runtime.lastError && chrome.runtime.lastError.message;
201 console.log('setAuthorization: error=' + errorMessage +
202 ', token=' + (token && 'non-empty'));
203 if (chrome.runtime.lastError || !token) {
204 callbackBoolean(false); 200 callbackBoolean(false);
205 return; 201 return;
206 } 202 }
207 203
208 request.setRequestHeader('Authorization', 'Bearer ' + token); 204 request.setRequestHeader('Authorization', 'Bearer ' + token);
209 205
210 // Instrument onloadend to remove stale auth tokens. 206 // Instrument onloadend to remove stale auth tokens.
211 var originalOnLoadEnd = request.onloadend; 207 var originalOnLoadEnd = request.onloadend;
212 request.onloadend = tasks.wrapCallback(function(event) { 208 request.onloadend = tasks.wrapCallback(function(event) {
213 if (request.status == HTTP_FORBIDDEN || 209 if (request.status == HTTP_FORBIDDEN ||
214 request.status == HTTP_UNAUTHORIZED) { 210 request.status == HTTP_UNAUTHORIZED) {
215 tasks.debugSetStepName('setAuthorization-removeCachedAuthToken'); 211 tasks.debugSetStepName('setAuthorization-removeToken');
216 chrome.identity.removeCachedAuthToken({token: token}, function() { 212 authenticationManager.removeToken(token, function() {
217 // After purging the token cache, call getAuthToken() again to let
218 // Chrome know about the problem with the token.
219 chrome.identity.getAuthToken({interactive: false}, function() {});
220 originalOnLoadEnd(event); 213 originalOnLoadEnd(event);
221 }); 214 });
222 } else { 215 } else {
223 originalOnLoadEnd(event); 216 originalOnLoadEnd(event);
224 } 217 }
225 }); 218 });
226 219
227 callbackBoolean(true); 220 callbackBoolean(true);
228 }); 221 });
229 } 222 }
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after
785 setShouldPollCards(shouldPollCards, callback); 778 setShouldPollCards(shouldPollCards, callback);
786 }); 779 });
787 } 780 }
788 781
789 /** 782 /**
790 * Coordinates the behavior of Google Now for Chrome depending on 783 * Coordinates the behavior of Google Now for Chrome depending on
791 * Chrome and extension state. 784 * Chrome and extension state.
792 */ 785 */
793 function onStateChange() { 786 function onStateChange() {
794 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) { 787 tasks.add(STATE_CHANGED_TASK_NAME, function(callback) {
795 tasks.debugSetStepName('onStateChange-getAuthToken'); 788 tasks.debugSetStepName('onStateChange-isSignedIn');
796 chrome.identity.getAuthToken({interactive: false}, function(token) { 789 authenticationManager.isSignedIn(function(signedIn) {
797 var signedIn = 790 signedIn = signedIn && !!NOTIFICATION_CARDS_URL;
798 !chrome.runtime.lastError &&
799 token &&
800 !!NOTIFICATION_CARDS_URL;
801 tasks.debugSetStepName( 791 tasks.debugSetStepName(
802 'onStateChange-get-googleGeolocationAccessEnabledPref'); 792 'onStateChange-get-googleGeolocationAccessEnabledPref');
803 googleGeolocationAccessEnabledPref.get({}, function(prefValue) { 793 googleGeolocationAccessEnabledPref.get({}, function(prefValue) {
804 var geolocationEnabled = !!prefValue.value; 794 var geolocationEnabled = !!prefValue.value;
805 tasks.debugSetStepName( 795 tasks.debugSetStepName(
806 'onStateChange-get-userRespondedToToast'); 796 'onStateChange-get-userRespondedToToast');
807 storage.get('userRespondedToToast', function(items) { 797 storage.get('userRespondedToToast', function(items) {
808 var userRespondedToToast = !!items.userRespondedToToast; 798 var userRespondedToToast = !!items.userRespondedToToast;
809 updateRunningState( 799 updateRunningState(
810 signedIn, 800 signedIn,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 chrome.runtime.onStartup.addListener(function() { 848 chrome.runtime.onStartup.addListener(function() {
859 console.log('onStartup'); 849 console.log('onStartup');
860 initialize(); 850 initialize();
861 }); 851 });
862 852
863 googleGeolocationAccessEnabledPref.onChange.addListener(function(prefValue) { 853 googleGeolocationAccessEnabledPref.onChange.addListener(function(prefValue) {
864 console.log('googleGeolocationAccessEnabledPref onChange ' + prefValue.value); 854 console.log('googleGeolocationAccessEnabledPref onChange ' + prefValue.value);
865 onStateChange(); 855 onStateChange();
866 }); 856 });
867 857
858 authenticationManager.addListener(function() {
859 console.log('signIn State Change');
860 onStateChange();
861 });
862
868 chrome.notifications.onClicked.addListener( 863 chrome.notifications.onClicked.addListener(
869 function(notificationId) { 864 function(notificationId) {
870 chrome.metricsPrivate.recordUserAction('GoogleNow.MessageClicked'); 865 chrome.metricsPrivate.recordUserAction('GoogleNow.MessageClicked');
871 onNotificationClicked(notificationId, function(actionUrls) { 866 onNotificationClicked(notificationId, function(actionUrls) {
872 return actionUrls.messageUrl; 867 return actionUrls.messageUrl;
873 }); 868 });
874 }); 869 });
875 870
876 chrome.notifications.onButtonClicked.addListener( 871 chrome.notifications.onButtonClicked.addListener(
877 function(notificationId, buttonIndex) { 872 function(notificationId, buttonIndex) {
(...skipping 15 matching lines...) Expand all
893 888
894 chrome.location.onLocationUpdate.addListener(function(position) { 889 chrome.location.onLocationUpdate.addListener(function(position) {
895 recordEvent(GoogleNowEvent.LOCATION_UPDATE); 890 recordEvent(GoogleNowEvent.LOCATION_UPDATE);
896 updateNotificationsCards(position); 891 updateNotificationsCards(position);
897 }); 892 });
898 893
899 chrome.omnibox.onInputEntered.addListener(function(text) { 894 chrome.omnibox.onInputEntered.addListener(function(text) {
900 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text; 895 localStorage['server_url'] = NOTIFICATION_CARDS_URL = text;
901 initialize(); 896 initialize();
902 }); 897 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698