Index: chrome/browser/resources/google_now/utility.js |
diff --git a/chrome/browser/resources/google_now/utility.js b/chrome/browser/resources/google_now/utility.js |
index dfcb8d8f3ad66b7c2d4534f4866932266826c1bc..b28cf5385f7012ecc2a2075b1f02385cfbaf8d77 100644 |
--- a/chrome/browser/resources/google_now/utility.js |
+++ b/chrome/browser/resources/google_now/utility.js |
@@ -30,6 +30,17 @@ |
var NOTIFICATION_CARDS_URL = 'https://www.googleapis.com/chromenow/v1'; |
/** |
+ * GCM registration URL. |
+ */ |
+var GCM_REGISTRATION_URL = |
+ 'https://android.googleapis.com/gcm/googlenotification'; |
+ |
+/** |
+ * DevConsole project ID for GCM API use. |
+ */ |
+var GCM_PROJECT_ID = '437902709571'; |
+ |
+/** |
* Returns true if debug mode is enabled. |
* localStorage returns items as strings, which means if we store a boolean, |
* it returns a string. Use this function to compare against true. |
@@ -1059,3 +1070,116 @@ function buildAuthenticationManager() { |
removeToken: removeToken |
}; |
} |
+ |
+/** |
+ * Ensures the extension is ready to listen for GCM messages. |
+ */ |
+function registerForGcm() { |
+ // We don't need to use the key at this point, just ensure it's set up, |
+ getGcmNotificationKey().then(function(unusedKey) {}); |
+} |
+ |
+/** |
+ * Returns a Promise resolving to either a cached or new GCM notification key. |
+ * Rejects if registration fails. |
+ * @return {Promise} A Promise that resolves to a potentially-cached GCM key. |
+ */ |
+function getGcmNotificationKey() { |
+ return fillFromChromeLocalStorage({gcmNotificationKey: undefined}) |
+ .then(function(items) { |
+ return new Promise(function(resolve, reject) { |
+ if (items.gcmNotificationKey) { |
+ console.log('reused gcm key from storage.'); |
+ resolve(items.gcmNotificationKey); |
+ } else { |
+ requestNewGcmNotificationKey().then(function(key) { |
+ resolve(key); |
+ }).catch(function() { |
+ 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.
|
+ }); |
+ } |
+ }); |
+ }); |
+} |
+ |
+/** |
+ * Determines the active account's username. |
+ * @return {Promise} A promise to determine the current account's username. |
+ */ |
+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.
|
+ return new Promise(function(resolve) { |
+ instrumented.webstorePrivate.getBrowserLogin(function(accountInfo) { |
+ resolve(accountInfo.login); |
+ }); |
+ }); |
+} |
+ |
+/** |
+ * Returns a promise resolving to a GCM Notificaiton Key. May call |
+ * chrome.gcm.register() first if required. Rejects on registration failure. |
+ * @return {Promise} A Promise that resolves to a fresh GCM Notification key. |
+ */ |
+function requestNewGcmNotificationKey() { |
+ return getGcmRegistrationId().then(function(gcmId) { |
+ authenticationManager.getAuthToken().then(function(token) { |
+ getUsername().then(function(username) { |
+ return new Promise(function(resolve, reject) { |
+ var xhr = new XMLHttpRequest(); |
+ xhr.responseType = 'text'; |
+ xhr.open('POST', GCM_REGISTRATION_URL, true); |
+ xhr.setRequestHeader('Content-Type', 'application/json'); |
+ xhr.setRequestHeader('Authorization', 'Bearer ' + token); |
+ xhr.setRequestHeader('project_id', GCM_PROJECT_ID); |
+ var payload = { |
+ 'operation': 'add', |
+ 'notification_key_name': username, |
+ 'registration_ids': [gcmId] |
+ }; |
+ xhr.onloadend = function() { |
+ if (xhr.status != 200) { |
+ reject(); |
+ } |
+ var obj = JSON.parse(xhr.responseText); |
+ var key = obj && obj.notification_key; |
+ if (!key) { |
+ reject(); |
+ } |
+ console.log('gcm notification key POST: ' + key); |
+ chrome.storage.local.set({gcmNotificationKey: key}); |
+ resolve(key); |
+ }; |
+ xhr.send(JSON.stringify(payload)); |
+ }); |
+ }); |
+ }).catch(function() { |
+ // Couldn't obtain a GCM ID. Ignore and fallback to polling. |
+ }); |
+ }); |
+} |
+ |
+/** |
+ * Returns a promise resolving to either a cached or new GCM registration ID. |
+ * Rejects if registration fails. |
+ * @return {Promise} A Promise that resolves to a GCM registration ID. |
+ */ |
+function getGcmRegistrationId() { |
+ return fillFromChromeLocalStorage({gcmRegistrationId: undefined}) |
+ .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
|
+ return new Promise(function(resolve, reject) { |
+ if (items.gcmRegistrationId) { |
+ console.log('reused gcm registration id from storage.'); |
+ resolve(items.gcmRegistrationId); |
+ } else { |
+ chrome.gcm.register([GCM_PROJECT_ID], function(registrationId) { |
+ console.log('gcm.register(): ' + registrationId); |
+ if (registrationId) { |
+ chrome.storage.local.set({gcmRegistrationId: registrationId}); |
+ resolve(registrationId); |
+ } else { |
+ reject(); |
+ } |
+ }); |
+ } |
+ }); |
+ }); |
+} |