OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
skare_
2013/01/18 21:05:24
2013
vadimt
2013/01/18 22:16:08
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @fileoverview The event page for Google Now for Chrome implementation. | |
skare_
2013/01/18 21:05:24
check indentation on block/jsdoc comments (here an
vadimt
2013/01/18 22:16:08
Done.
| |
7 * The Google Now event page gets Google Now cards from the server and shows | |
8 * them as Chrome notifications. | |
9 * The service performs periodic updating of Google Now cards. | |
10 * Each updating of the cards includes 3 steps: | |
11 * 1. Obtaining the location of the machine; | |
12 * 2. Making a server request based on that location; | |
13 * 3. Showing the received cards as notifications. | |
14 */ | |
15 | |
16 // TODO(vadimt): Use background permission to show notifications even when all | |
17 // browser windows are closed. | |
18 // TODO(vadimt): Remove the C++ implementation. | |
19 // TODO(vadimt): Decide what to do in incognito mode. | |
20 // TODO(vadimt): Gather UMAs. | |
21 // TODO(vadimt): Honor the flag the enables Google Now integration. | |
22 // TODO(vadimt): Figure out the final values of the constants. | |
23 | |
24 // TODO(vadimt): Figure out the server name. Use it in the manifest and for | |
25 // NOTIFICATION_CARDS_URL. Meanwhile, to use the feature, you need to manually | |
26 // edit NOTIFICATION_CARDS_URL before building Chrome. | |
27 /** | |
28 * URL to retrieve notification cards. | |
29 */ | |
30 var NOTIFICATION_CARDS_URL = ''; | |
31 | |
32 /** | |
33 * Standard response code for successful HTTP requests. | |
34 */ | |
35 var HTTP_OK = 200; | |
skare_
2013/01/18 21:05:24
just 200 or 2xx (probably ok, for GETs that will c
vadimt
2013/01/18 22:16:08
Done.
| |
36 | |
37 /** | |
38 * Period for polling for Google Now Notifications cards to use when the period | |
39 * from the server is not available. | |
40 */ | |
41 var DEFAULT_POLLING_PERIOD_SECONDS = 300; // 5 minutes | |
skare_
2013/01/18 21:05:24
tiny nit: 2 spaces between code and comments
vadimt
2013/01/18 22:16:08
Done.
| |
42 | |
43 /** | |
44 * Parse JSON response of the notifications server, show notifications and | |
45 * schedule next update. | |
46 * @param {string} response Server response. | |
47 */ | |
48 function ParseAndShowNotificationCards(response) { | |
49 var parsedResponse = JSON.parse(response); | |
50 var cards = parsedResponse.cards; | |
skare_
2013/01/18 21:05:24
guard against parsedResponse being empty or having
vadimt
2013/01/18 22:16:08
Then this will throw an exception, the execution w
| |
51 | |
52 for (var i = 0; i != cards.length; ++i) | |
53 chrome.experimental.notification.show(cards[i], function(showInfo) {}); | |
54 | |
55 ScheduleNextUpdate(parsedResponse.expiration_timestamp_seconds); | |
rgustafson
2013/01/18 22:21:00
Also handle if expiration_timestamp_seconds isn't
vadimt
2013/01/18 23:05:12
The mentioned cases will cause an exception. So, I
skare_
2013/01/18 23:21:24
yeah, that will work but I think explicit error ha
vadimt
2013/01/19 00:51:21
Done.
| |
56 } | |
57 | |
58 /** | |
59 * Request notification cards from the server. | |
60 * @param {string} requestParameters Query string for the request. | |
61 */ | |
62 function RequestNotificationCards(requestParameters) { | |
63 // TODO(vadimt): Figure out how to send user's identity to the server. Make | |
64 // sure we never get responses like 'Select an account' page. | |
rgustafson
2013/01/18 22:21:00
Select account page is already gone. Change to "ha
vadimt
2013/01/18 23:05:12
Done. And I already have an umbrella TODO for hand
| |
65 var request = new XMLHttpRequest(); | |
66 request.onreadystatechange = function(event) { | |
67 if (request.readyState == request.DONE && request.status == HTTP_OK) | |
68 ParseAndShowNotificationCards(request.response); | |
69 } | |
70 request.open('GET', NOTIFICATION_CARDS_URL + requestParameters, true); | |
71 request.send(); | |
72 } | |
73 | |
74 /** | |
75 * Request notification cards from the server when we have geolocation. | |
76 * @param {object} position Location of this computer. | |
skare_
2013/01/18 21:05:24
nit: capitalize {Object} if you're using it, but c
vadimt
2013/01/18 22:16:08
Done.
| |
77 */ | |
78 function RequestNotificationCardsWithLocation(position) { | |
79 var requestParameters = | |
80 '?q=' + position.coords.latitude + | |
skare_
2013/01/18 21:05:24
just checking -- have cgiargs names been establish
vadimt
2013/01/18 22:16:08
Added a TODO item.
| |
81 ',' + position.coords.longitude + | |
82 ',' + position.coords.accuracy; | |
83 | |
84 RequestNotificationCards(requestParameters); | |
85 } | |
86 | |
87 /** | |
88 * Request notification cards from the server when we don't have geolocation. | |
89 * @param {object} positionError Position error. | |
skare_
2013/01/18 21:05:24
you can omit this param if you don't need it.
You
vadimt
2013/01/18 22:16:08
I'd prefer to keep the argument for readability.
S
| |
90 */ | |
91 function RequestNotificationCardsWithoutLocation(positionError) { | |
92 RequestNotificationCards(''); | |
93 } | |
94 | |
95 /** | |
96 * Obtain new location; request and show notification cards based on this | |
97 * location. | |
98 */ | |
99 function UpdateNotificationsCards() { | |
100 // Immediately schedule the update after the default period. If we | |
101 // successfully retrieve, parse and show the notifications cards, we'll | |
102 // schedule next update based on the expiration timestamp received from the | |
103 // server. At that point scheduled time will be overwritten by the new one | |
104 // based on the expiration timestamp. | |
105 // TODO(vadimt): Implement exponential backoff with randomized jitter. | |
106 ScheduleNextUpdate(DEFAULT_POLLING_PERIOD_SECONDS); | |
107 | |
108 // TODO(vadimt): Use chrome.* geolocation API once it's ready. | |
109 navigator.geolocation.getCurrentPosition( | |
110 RequestNotificationCardsWithLocation, | |
111 RequestNotificationCardsWithoutLocation); | |
112 } | |
113 | |
114 /** | |
115 * Schedule next update for notification cards. | |
116 * @param {int} delaySeconds Length of time in seconds after which the alarm | |
117 * event should fire. | |
118 */ | |
119 function ScheduleNextUpdate(delaySeconds) { | |
120 chrome.alarms.create({ when: Date.now() + delaySeconds * 1000 }); | |
121 } | |
122 | |
123 /** | |
124 * Initialize the event page on install or on browser startup. | |
125 */ | |
126 function Initialize() { | |
127 UpdateNotificationsCards(); | |
128 } | |
129 | |
130 chrome.runtime.onInstalled.addListener(function(details) { | |
131 if (details.reason != 'chrome_update') | |
132 Initialize(); | |
133 }); | |
134 | |
135 chrome.runtime.onStartup.addListener(function() { | |
136 Initialize(); | |
137 }); | |
138 | |
139 chrome.alarms.onAlarm.addListener(function(alarm) { | |
140 UpdateNotificationsCards(); | |
141 }); | |
OLD | NEW |