OLD | NEW |
---|---|
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 var WALLPAPER_PICKER_WIDTH = 574; | 5 var WALLPAPER_PICKER_WIDTH = 574; |
6 var WALLPAPER_PICKER_HEIGHT = 420; | 6 var WALLPAPER_PICKER_HEIGHT = 420; |
7 | 7 |
8 var wallpaperPickerWindow; | 8 var wallpaperPickerWindow; |
9 | 9 |
10 var surpriseWallpaper = null; | 10 var surpriseWallpaper = null; |
(...skipping 18 matching lines...) Expand all Loading... | |
29 */ | 29 */ |
30 SurpriseWallpaper.prototype.tryChangeWallpaper = function() { | 30 SurpriseWallpaper.prototype.tryChangeWallpaper = function() { |
31 var self = this; | 31 var self = this; |
32 var onFailure = function() { | 32 var onFailure = function() { |
33 self.retryLater_(); | 33 self.retryLater_(); |
34 self.fallbackToLocalRss_(); | 34 self.fallbackToLocalRss_(); |
35 }; | 35 }; |
36 // Try to fetch newest rss as document from server first. If any error occurs, | 36 // Try to fetch newest rss as document from server first. If any error occurs, |
37 // proceed with local copy of rss. | 37 // proceed with local copy of rss. |
38 WallpaperUtil.fetchURL(Constants.WallpaperRssURL, 'document', function(xhr) { | 38 WallpaperUtil.fetchURL(Constants.WallpaperRssURL, 'document', function(xhr) { |
39 WallpaperUtil.saveToLocalStorage(Constants.AccessRssKey, | 39 WallpaperUtil.saveToStorage(Constants.AccessRssKey, |
40 new XMLSerializer().serializeToString(xhr.responseXML)); | 40 new XMLSerializer().serializeToString(xhr.responseXML), false); |
41 self.updateSurpriseWallpaper(xhr.responseXML); | 41 self.updateSurpriseWallpaper(xhr.responseXML); |
42 }, onFailure); | 42 }, onFailure); |
43 }; | 43 }; |
44 | 44 |
45 /** | 45 /** |
46 * Retries changing the wallpaper 1 hour later. This is called when fetching the | 46 * Retries changing the wallpaper 1 hour later. This is called when fetching the |
47 * rss or wallpaper from server fails. | 47 * rss or wallpaper from server fails. |
48 * @private | 48 * @private |
49 */ | 49 */ |
50 SurpriseWallpaper.prototype.retryLater_ = function() { | 50 SurpriseWallpaper.prototype.retryLater_ = function() { |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
132 SurpriseWallpaper.prototype.setRandomWallpaper_ = function(dateString) { | 132 SurpriseWallpaper.prototype.setRandomWallpaper_ = function(dateString) { |
133 var self = this; | 133 var self = this; |
134 Constants.WallpaperLocalStorage.get(Constants.AccessManifestKey, | 134 Constants.WallpaperLocalStorage.get(Constants.AccessManifestKey, |
135 function(items) { | 135 function(items) { |
136 var manifest = items[Constants.AccessManifestKey]; | 136 var manifest = items[Constants.AccessManifestKey]; |
137 if (manifest) { | 137 if (manifest) { |
138 var index = Math.floor(Math.random() * manifest.wallpaper_list.length); | 138 var index = Math.floor(Math.random() * manifest.wallpaper_list.length); |
139 var wallpaper = manifest.wallpaper_list[index]; | 139 var wallpaper = manifest.wallpaper_list[index]; |
140 var wallpaperURL = wallpaper.base_url + Constants.HighResolutionSuffix; | 140 var wallpaperURL = wallpaper.base_url + Constants.HighResolutionSuffix; |
141 var onSuccess = function() { | 141 var onSuccess = function() { |
142 WallpaperUtil.saveToLocalStorage( | 142 WallpaperUtil.saveToStorage( |
143 Constants.AccessLastSurpriseWallpaperChangedDate, dateString); | 143 Constants.AccessLastSurpriseWallpaperChangedDate, |
144 dateString, | |
145 false); | |
144 } | 146 } |
145 WallpaperUtil.setOnlineWallpaper(wallpaperURL, wallpaper.default_layout, | 147 WallpaperUtil.setOnlineWallpaper(wallpaperURL, wallpaper.default_layout, |
146 onSuccess, self.retryLater_.bind(self)); | 148 onSuccess, self.retryLater_.bind(self)); |
147 } | 149 } |
148 }); | 150 }); |
149 }; | 151 }; |
150 | 152 |
151 /** | 153 /** |
152 * Sets wallpaper to the wallpaper specified by item from rss. If downloading | 154 * Sets wallpaper to the wallpaper specified by item from rss. If downloading |
153 * the wallpaper fails, retry one hour later. | 155 * the wallpaper fails, retry one hour later. |
(...skipping 19 matching lines...) Expand all Loading... | |
173 } | 175 } |
174 }, onFailure); | 176 }, onFailure); |
175 }; | 177 }; |
176 | 178 |
177 /** | 179 /** |
178 * Disables the wallpaper surprise me feature. Clear all alarms and states. | 180 * Disables the wallpaper surprise me feature. Clear all alarms and states. |
179 */ | 181 */ |
180 SurpriseWallpaper.prototype.disable = function() { | 182 SurpriseWallpaper.prototype.disable = function() { |
181 chrome.alarms.clearAll(); | 183 chrome.alarms.clearAll(); |
182 // Makes last changed date invalid. | 184 // Makes last changed date invalid. |
183 WallpaperUtil.saveToLocalStorage( | 185 WallpaperUtil.saveToStorage(Constants.AccessLastSurpriseWallpaperChangedDate, |
184 Constants.AccessLastSurpriseWallpaperChangedDate, ''); | 186 '', false); |
185 }; | 187 }; |
186 | 188 |
187 /** | 189 /** |
188 * Changes current wallpaper and sets up an alarm to schedule next change around | 190 * Changes current wallpaper and sets up an alarm to schedule next change around |
189 * midnight. | 191 * midnight. |
190 */ | 192 */ |
191 SurpriseWallpaper.prototype.next = function() { | 193 SurpriseWallpaper.prototype.next = function() { |
192 var nextUpdate = this.nextUpdateTime(new Date()); | 194 var nextUpdate = this.nextUpdateTime(new Date()); |
193 chrome.alarms.create({when: nextUpdate}); | 195 chrome.alarms.create({when: nextUpdate}); |
194 this.tryChangeWallpaper(); | 196 this.tryChangeWallpaper(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 }); | 232 }); |
231 | 233 |
232 chrome.storage.onChanged.addListener(function(changes, namespace) { | 234 chrome.storage.onChanged.addListener(function(changes, namespace) { |
233 if (changes[Constants.AccessSurpriseMeEnabledKey]) { | 235 if (changes[Constants.AccessSurpriseMeEnabledKey]) { |
234 if (changes[Constants.AccessSurpriseMeEnabledKey].newValue) { | 236 if (changes[Constants.AccessSurpriseMeEnabledKey].newValue) { |
235 SurpriseWallpaper.getInstance().next(); | 237 SurpriseWallpaper.getInstance().next(); |
236 } else { | 238 } else { |
237 SurpriseWallpaper.getInstance().disable(); | 239 SurpriseWallpaper.getInstance().disable(); |
238 } | 240 } |
239 } | 241 } |
242 | |
243 if (changes[Constants.AccessSyncWallpaperInfoKey]) { | |
244 var newValue = changes[Constants.AccessSyncWallpaperInfoKey].newValue; | |
245 Constants.WallpaperLocalStorage.get(Constants.AccessLocalWallpaperInfoKey, | |
246 function(items) { | |
247 // Normally, the wallpaper info saved in local storage and sync storage | |
248 // are the same. If the synced value changed by sync service, they may | |
249 // different. In that case, change wallpaper to the one saved in sync | |
250 // storage and update the local value. | |
251 var localValue = items[Constants.AccessLocalWallpaperInfoKey]; | |
252 if (localValue == undefined || | |
253 localValue.url != newValue.url || | |
254 localValue.layout != newValue.layout || | |
255 localValue.source != localValue.source) { | |
flackr
2013/04/26 17:03:57
localValue.source will always be equal to localVal
bshe
2013/04/26 17:34:48
Dooh. Forget to commit and upload my local patch..
| |
256 if (newValue.source == Constants.WallpaperSourceEnum.Online) { | |
257 // TODO(bshe): Consider schedule an alarm to set online wallpaper | |
258 // later when failed. Note that we need to cancel the retry if user | |
259 // set another wallpaper before retry alarm invoked. | |
260 WallpaperUtil.setOnlineWallpaper(newValue.url, newValue.layout, | |
261 function() {}, function() {}); | |
262 } | |
263 WallpaperUtil.saveToStorage(Constants.AccessLocalWallpaperInfoKey, | |
264 newValue, false); | |
265 } | |
266 }); | |
267 } | |
240 }); | 268 }); |
241 | 269 |
242 chrome.alarms.onAlarm.addListener(function() { | 270 chrome.alarms.onAlarm.addListener(function() { |
243 SurpriseWallpaper.getInstance().next(); | 271 SurpriseWallpaper.getInstance().next(); |
244 }); | 272 }); |
OLD | NEW |