Chromium Code Reviews| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 }); | 231 }); |
| 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 } |
| 241 } else if (changes[Constants.AccessWallpaperInfoKey]) { | |
|
flackr
2013/04/26 03:51:16
This shouldn't be else if, just if. Both keys may
bshe
2013/04/26 16:56:51
Done.
| |
| 242 var newValue = changes[Constants.AccessWallpaperInfoKey].newValue; | |
| 243 var oldValue = changes[Constants.AccessWallpaperInfoKey].oldValue; | |
| 244 // oldValue equals undefined indicate that sync just happened. | |
| 245 if (oldValue == undefined && | |
|
flackr
2013/04/26 03:51:16
Why only if oldValue is undefined? I think it woul
bshe
2013/04/26 16:56:51
You are right. It doesn't handle the case you ment
| |
| 246 newValue.source == Constants.WallpaperSourceEnum.Online) { | |
| 247 // TODO(bshe): Consider schedule an alarm to set online wallpaper later | |
| 248 // when failed. Note that we need to cancel the retry if user set another | |
| 249 // wallpaper before retry alarm invoked. | |
| 250 WallpaperUtil.setOnlineWallpaper(newValue.url, newValue.layout, | |
| 251 function() {}, function() {}); | |
| 252 } | |
| 239 } | 253 } |
| 240 }); | 254 }); |
| 241 | 255 |
| 242 chrome.alarms.onAlarm.addListener(function() { | 256 chrome.alarms.onAlarm.addListener(function() { |
| 243 SurpriseWallpaper.getInstance().next(); | 257 SurpriseWallpaper.getInstance().next(); |
| 244 }); | 258 }); |
| OLD | NEW |