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

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

Issue 844503005: Convert now component to use GCM rather than pushMessaging. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: removing old comment Created 5 years, 10 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 Utility objects and functions for Google Now extension. 8 * @fileoverview Utility objects and functions for Google Now extension.
9 * Most important entities here: 9 * Most important entities here:
10 * (1) 'wrapper' is a module used to add error handling and other services to 10 * (1) 'wrapper' is a module used to add error handling and other services to
(...skipping 12 matching lines...) Expand all
23 */ 23 */
24 24
25 // TODO(vadimt): Use server name in the manifest. 25 // TODO(vadimt): Use server name in the manifest.
26 26
27 /** 27 /**
28 * Notification server URL. 28 * Notification server URL.
29 */ 29 */
30 var NOTIFICATION_CARDS_URL = 'https://www.googleapis.com/chromenow/v1'; 30 var NOTIFICATION_CARDS_URL = 'https://www.googleapis.com/chromenow/v1';
31 31
32 /** 32 /**
33 * GCM registration URL.
34 */
35 var GCM_REGISTRATION_URL =
36 'https://android.googleapis.com/gcm/googlenotification';
37
38 /**
39 * DevConsole project ID for GCM API use.
40 */
41 var GCM_PROJECT_ID = '437902709571';
42
43 /**
33 * Returns true if debug mode is enabled. 44 * Returns true if debug mode is enabled.
34 * localStorage returns items as strings, which means if we store a boolean, 45 * localStorage returns items as strings, which means if we store a boolean,
35 * it returns a string. Use this function to compare against true. 46 * it returns a string. Use this function to compare against true.
36 * @return {boolean} Whether debug mode is enabled. 47 * @return {boolean} Whether debug mode is enabled.
37 */ 48 */
38 function isInDebugMode() { 49 function isInDebugMode() {
39 return localStorage.debug_mode === 'true'; 50 return localStorage.debug_mode === 'true';
40 } 51 }
41 52
42 /** 53 /**
(...skipping 1009 matching lines...) Expand 10 before | Expand all | Expand 10 after
1052 // One hour is just an arbitrary amount of time chosen. 1063 // One hour is just an arbitrary amount of time chosen.
1053 chrome.alarms.create(alarmName, {periodInMinutes: 60}); 1064 chrome.alarms.create(alarmName, {periodInMinutes: 60});
1054 1065
1055 return { 1066 return {
1056 addListener: addListener, 1067 addListener: addListener,
1057 getAuthToken: getAuthToken, 1068 getAuthToken: getAuthToken,
1058 isSignedIn: isSignedIn, 1069 isSignedIn: isSignedIn,
1059 removeToken: removeToken 1070 removeToken: removeToken
1060 }; 1071 };
1061 } 1072 }
1073
1074 /**
1075 * Ensures the extension is ready to listen for GCM messages.
1076 */
1077 function registerForGcm() {
1078 // We don't need to use the key at this point, just ensure it's set up,
1079 getGcmNotificationKey().then(function(unusedKey) {});
1080 }
1081
1082 /**
1083 * Returns a Promise resolving to either a cached or new GCM notification key.
1084 * Rejects if registration fails.
1085 * @return {Promise} A Promise that resolves to a potentially-cached GCM key.
1086 */
1087 function getGcmNotificationKey() {
1088 return fillFromChromeLocalStorage({gcmNotificationKey: undefined})
1089 .then(function(items) {
1090 return new Promise(function(resolve, reject) {
1091 if (items.gcmNotificationKey) {
1092 console.log('reused gcm key from storage.');
1093 resolve(items.gcmNotificationKey);
1094 } else {
1095 requestNewGcmNotificationKey().then(function(key) {
1096 resolve(key);
1097 }).catch(function() {
1098 reject();
robliao 2015/02/18 00:04:25 I think this can be written as... fillFromChromeL
skare_ 2015/02/19 16:48:10 yes it can. thanks, done.
1099 });
1100 }
1101 });
1102 });
1103 }
1104
1105 /**
1106 * Determines the active account's username.
1107 * @return {Promise} A promise to determine the current account's username.
1108 */
1109 function getUsername() {
robliao 2015/02/18 00:04:25 Would it make sense to have the authentication man
skare_ 2015/02/19 16:48:10 Done.
1110 return new Promise(function(resolve) {
1111 instrumented.webstorePrivate.getBrowserLogin(function(accountInfo) {
1112 resolve(accountInfo.login);
1113 });
1114 });
1115 }
1116
1117 /**
1118 * Returns a promise resolving to a GCM Notificaiton Key. May call
1119 * chrome.gcm.register() first if required. Rejects on registration failure.
1120 * @return {Promise} A Promise that resolves to a fresh GCM Notification key.
1121 */
1122 function requestNewGcmNotificationKey() {
1123 return getGcmRegistrationId().then(function(gcmId) {
1124 authenticationManager.getAuthToken().then(function(token) {
1125 getUsername().then(function(username) {
1126 return new Promise(function(resolve, reject) {
1127 var xhr = new XMLHttpRequest();
1128 xhr.responseType = 'text';
1129 xhr.open('POST', GCM_REGISTRATION_URL, true);
1130 xhr.setRequestHeader('Content-Type', 'application/json');
1131 xhr.setRequestHeader('Authorization', 'Bearer ' + token);
1132 xhr.setRequestHeader('project_id', GCM_PROJECT_ID);
1133 var payload = {
1134 'operation': 'add',
1135 'notification_key_name': username,
1136 'registration_ids': [gcmId]
1137 };
1138 xhr.onloadend = function() {
1139 if (xhr.status != 200) {
1140 reject();
1141 }
1142 var obj = JSON.parse(xhr.responseText);
1143 var key = obj && obj.notification_key;
1144 if (!key) {
1145 reject();
1146 }
1147 console.log('gcm notification key POST: ' + key);
1148 chrome.storage.local.set({gcmNotificationKey: key});
1149 resolve(key);
1150 };
1151 xhr.send(JSON.stringify(payload));
1152 });
1153 });
1154 }).catch(function() {
1155 // Couldn't obtain a GCM ID. Ignore and fallback to polling.
1156 });
1157 });
1158 }
1159
1160 /**
1161 * Returns a promise resolving to either a cached or new GCM registration ID.
1162 * Rejects if registration fails.
1163 * @return {Promise} A Promise that resolves to a GCM registration ID.
1164 */
1165 function getGcmRegistrationId() {
1166 return fillFromChromeLocalStorage({gcmRegistrationId: undefined})
1167 .then(function(items) {
robliao 2015/02/18 00:04:25 OPTIONAL, since it doesn't really buy that much: S
skare_ 2015/02/19 16:48:10 Done. Gets rid of an else and some spaces which is
1168 return new Promise(function(resolve, reject) {
1169 if (items.gcmRegistrationId) {
1170 console.log('reused gcm registration id from storage.');
1171 resolve(items.gcmRegistrationId);
1172 } else {
1173 chrome.gcm.register([GCM_PROJECT_ID], function(registrationId) {
1174 console.log('gcm.register(): ' + registrationId);
1175 if (registrationId) {
1176 chrome.storage.local.set({gcmRegistrationId: registrationId});
1177 resolve(registrationId);
1178 } else {
1179 reject();
1180 }
1181 });
1182 }
1183 });
1184 });
1185 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698