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 13 matching lines...) Expand all Loading... | |
| 24 }; | 24 }; |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * Tries to change wallpaper to a new one in the background. May fail due to a | 27 * Tries to change wallpaper to a new one in the background. May fail due to a |
| 28 * network issue. | 28 * network issue. |
| 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 }; | |
| 35 var onNotFound = function() { | |
|
bshe
2015/03/09 14:16:09
If not found, I think we should just use a random
xdai1
2015/03/09 21:07:10
If not found (404), we randomly set a wallpaper di
| |
| 34 self.fallbackToLocalRss_(); | 36 self.fallbackToLocalRss_(); |
| 35 }; | 37 }; |
| 36 // Try to fetch newest rss as document from server first. If any error occurs, | 38 // Try to fetch newest rss as document from server first. If any error occurs, |
| 37 // proceed with local copy of rss. | 39 // proceed with local copy of rss. |
| 38 WallpaperUtil.fetchURL(Constants.WallpaperRssURL, 'document', function(xhr) { | 40 WallpaperUtil.fetchURL(Constants.WallpaperRssURL, 'document', function(xhr) { |
| 39 WallpaperUtil.saveToStorage(Constants.AccessRssKey, | 41 WallpaperUtil.saveToStorage(Constants.AccessRssKey, |
| 40 new XMLSerializer().serializeToString(xhr.responseXML), false); | 42 new XMLSerializer().serializeToString(xhr.responseXML), false); |
| 41 self.updateSurpriseWallpaper(xhr.responseXML); | 43 self.updateSurpriseWallpaper(xhr.responseXML); |
| 42 }, onFailure); | 44 }, onFailure, onNotFound); |
| 43 }; | 45 }; |
| 44 | 46 |
| 45 /** | 47 /** |
| 46 * Retries changing the wallpaper 1 hour later. This is called when fetching the | 48 * Retries changing the wallpaper 1 hour later. This is called when fetching the |
| 47 * rss or wallpaper from server fails. | 49 * rss or wallpaper from server fails. |
| 48 * @private | 50 * @private |
| 49 */ | 51 */ |
| 50 SurpriseWallpaper.prototype.retryLater_ = function() { | 52 SurpriseWallpaper.prototype.retryLater_ = function() { |
| 51 chrome.alarms.create('RetryAlarm', {delayInMinutes: 60}); | 53 chrome.alarms.create('RetryAlarm', {delayInMinutes: 60}); |
| 52 }; | 54 }; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 82 item = items[i]; | 84 item = items[i]; |
| 83 var disableDate = new Date(item.getElementsByTagNameNS( | 85 var disableDate = new Date(item.getElementsByTagNameNS( |
| 84 Constants.WallpaperNameSpaceURI, 'disableDate')[0].textContent). | 86 Constants.WallpaperNameSpaceURI, 'disableDate')[0].textContent). |
| 85 getTime(); | 87 getTime(); |
| 86 var enableDate = new Date(item.getElementsByTagNameNS( | 88 var enableDate = new Date(item.getElementsByTagNameNS( |
| 87 Constants.WallpaperNameSpaceURI, 'enableDate')[0].textContent). | 89 Constants.WallpaperNameSpaceURI, 'enableDate')[0].textContent). |
| 88 getTime(); | 90 getTime(); |
| 89 var regionsString = item.getElementsByTagNameNS( | 91 var regionsString = item.getElementsByTagNameNS( |
| 90 Constants.WallpaperNameSpaceURI, 'regions')[0].textContent; | 92 Constants.WallpaperNameSpaceURI, 'regions')[0].textContent; |
| 91 var regions = regionsString.split(', '); | 93 var regions = regionsString.split(', '); |
| 94 | |
| 95 var self = this; | |
| 96 var onSuccess = function(url, layout) { | |
| 97 var dateString = new Date().toDateString(); | |
| 98 WallpaperUtil.saveWallpaperInfo(url, layout, | |
| 99 Constants.WallpaperSourceEnum.Online); | |
| 100 WallpaperUtil.saveToStorage( | |
| 101 Constants.AccessLastSurpriseWallpaperChangedDate, | |
| 102 dateString, true); | |
| 103 }; | |
| 104 var onFailure = function() { | |
| 105 self.retryLater_(); | |
| 106 }; | |
| 107 var onNotFound = function() { | |
| 108 self.updateRandomWallpaper_(); | |
|
bshe
2015/03/09 14:16:09
would be nice to handle the not found case in fail
xdai1
2015/03/09 21:07:10
Done.
| |
| 109 }; | |
| 110 | |
| 92 if (enableDate <= date && disableDate > date && | 111 if (enableDate <= date && disableDate > date && |
| 93 regions.indexOf(navigator.language) != -1) { | 112 regions.indexOf(navigator.language) != -1) { |
| 94 var self = this; | 113 this.setWallpaperFromRssItem_(item, onSuccess, onFailure, onNotFound); |
| 95 this.setWallpaperFromRssItem_(item, | |
| 96 function() {}, | |
| 97 function() { | |
| 98 self.retryLater_(); | |
| 99 self.updateRandomWallpaper_(); | |
| 100 }); | |
| 101 return; | 114 return; |
| 102 } | 115 } |
| 103 } | 116 } |
| 104 } | 117 } |
| 105 // No surprise wallpaper for today at current locale or fetching rss feed | 118 // No surprise wallpaper for today at current locale or fetching rss feed |
| 106 // fails. Fallback to use a random one from wallpaper server. | 119 // fails. Fallback to use a random one from wallpaper server. |
| 107 this.updateRandomWallpaper_(); | 120 this.updateRandomWallpaper_(); |
| 108 }; | 121 }; |
| 109 | 122 |
| 110 /** | 123 /** |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 139 var filtered = manifest.wallpaper_list.filter(function(element) { | 152 var filtered = manifest.wallpaper_list.filter(function(element) { |
| 140 // Older version manifest do not have available_for_surprise_me field. | 153 // Older version manifest do not have available_for_surprise_me field. |
| 141 // In this case, no wallpaper should be filtered out. | 154 // In this case, no wallpaper should be filtered out. |
| 142 return element.available_for_surprise_me || | 155 return element.available_for_surprise_me || |
| 143 element.available_for_surprise_me == undefined; | 156 element.available_for_surprise_me == undefined; |
| 144 }); | 157 }); |
| 145 var index = Math.floor(Math.random() * filtered.length); | 158 var index = Math.floor(Math.random() * filtered.length); |
| 146 var wallpaper = filtered[index]; | 159 var wallpaper = filtered[index]; |
| 147 var wallpaperURL = wallpaper.base_url + Constants.HighResolutionSuffix; | 160 var wallpaperURL = wallpaper.base_url + Constants.HighResolutionSuffix; |
| 148 var onSuccess = function() { | 161 var onSuccess = function() { |
| 162 WallpaperUtil.saveWallpaperInfo(wallpaperURL, wallpaper.default_layout, | |
| 163 Constants.WallpaperSourceEnum.Online); | |
| 149 WallpaperUtil.saveToStorage( | 164 WallpaperUtil.saveToStorage( |
| 150 Constants.AccessLastSurpriseWallpaperChangedDate, | 165 Constants.AccessLastSurpriseWallpaperChangedDate, |
| 151 dateString, | 166 dateString, |
| 152 true); | 167 true); |
| 153 }; | 168 }; |
| 154 WallpaperUtil.setOnlineWallpaper(wallpaperURL, wallpaper.default_layout, | 169 WallpaperUtil.setOnlineWallpaper(wallpaperURL, wallpaper.default_layout, |
| 155 onSuccess, self.retryLater_.bind(self)); | 170 onSuccess, self.retryLater_.bind(self)); |
| 156 } | 171 } |
| 157 }); | 172 }); |
| 158 }; | 173 }; |
| 159 | 174 |
| 160 /** | 175 /** |
| 161 * Sets wallpaper to the wallpaper specified by item from rss. If downloading | 176 * Sets wallpaper to the wallpaper specified by item from rss. If downloading |
| 162 * the wallpaper fails, retry one hour later. | 177 * the wallpaper fails, retry one hour later. |
| 163 * @param {Element} item The wallpaper rss item element. | 178 * @param {Element} item The wallpaper rss item element. |
| 164 * @param {function} onSuccess Success callback. | 179 * @param {function} onSuccess Success callback. |
| 165 * @param {function} onFailure Failure callback. | 180 * @param {function} onFailure Failure callback. |
| 181 * @param {function} onNotFound Not found callback. | |
| 166 * @private | 182 * @private |
| 167 */ | 183 */ |
| 168 SurpriseWallpaper.prototype.setWallpaperFromRssItem_ = function(item, | 184 SurpriseWallpaper.prototype.setWallpaperFromRssItem_ = function(item, |
| 169 onSuccess, | 185 onSuccess, |
| 170 onFailure) { | 186 onFailure, |
| 187 onNotFound) { | |
| 171 var url = item.querySelector('link').textContent; | 188 var url = item.querySelector('link').textContent; |
| 172 var layout = item.getElementsByTagNameNS( | 189 var layout = item.getElementsByTagNameNS( |
| 173 Constants.WallpaperNameSpaceURI, 'layout')[0].textContent; | 190 Constants.WallpaperNameSpaceURI, 'layout')[0].textContent; |
| 174 var self = this; | 191 var self = this; |
| 175 WallpaperUtil.fetchURL(url, 'arraybuffer', function(xhr) { | 192 WallpaperUtil.fetchURL(url, 'arraybuffer', function(xhr) { |
| 176 if (xhr.response != null) { | 193 if (xhr.response != null) { |
| 177 chrome.wallpaperPrivate.setCustomWallpaper(xhr.response, layout, false, | 194 chrome.wallpaperPrivate.setCustomWallpaper(xhr.response, layout, false, |
| 178 'surprise_wallpaper', | 195 'surprise_wallpaper', onSuccess(url, layout)); |
| 179 onSuccess); | |
| 180 } else { | 196 } else { |
| 181 onFailure(); | 197 onFailure(); |
| 182 } | 198 } |
| 183 }, onFailure); | 199 }, onFailure, onNotFound); |
| 184 }; | 200 }; |
| 185 | 201 |
| 186 /** | 202 /** |
| 187 * Disables the wallpaper surprise me feature. Clear all alarms and states. | 203 * Disables the wallpaper surprise me feature. Clear all alarms and states. |
| 188 */ | 204 */ |
| 189 SurpriseWallpaper.prototype.disable = function() { | 205 SurpriseWallpaper.prototype.disable = function() { |
| 190 chrome.alarms.clearAll(); | 206 chrome.alarms.clearAll(); |
| 191 // Makes last changed date invalid. | 207 // Makes last changed date invalid. |
| 192 WallpaperUtil.saveToStorage(Constants.AccessLastSurpriseWallpaperChangedDate, | 208 WallpaperUtil.saveToStorage(Constants.AccessLastSurpriseWallpaperChangedDate, |
| 193 '', true); | 209 '', true); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 270 WallpaperUtil.requestSyncFS(function() {}); | 286 WallpaperUtil.requestSyncFS(function() {}); |
| 271 if (changes[Constants.AccessSurpriseMeEnabledKey]) { | 287 if (changes[Constants.AccessSurpriseMeEnabledKey]) { |
| 272 if (changes[Constants.AccessSurpriseMeEnabledKey].newValue) { | 288 if (changes[Constants.AccessSurpriseMeEnabledKey].newValue) { |
| 273 SurpriseWallpaper.getInstance().next(); | 289 SurpriseWallpaper.getInstance().next(); |
| 274 } else { | 290 } else { |
| 275 SurpriseWallpaper.getInstance().disable(); | 291 SurpriseWallpaper.getInstance().disable(); |
| 276 } | 292 } |
| 277 } | 293 } |
| 278 | 294 |
| 279 if (changes[Constants.AccessSyncWallpaperInfoKey]) { | 295 if (changes[Constants.AccessSyncWallpaperInfoKey]) { |
| 280 var newValue = changes[Constants.AccessSyncWallpaperInfoKey].newValue; | 296 var syncInfo = changes[Constants.AccessSyncWallpaperInfoKey].newValue; |
| 281 Constants.WallpaperLocalStorage.get(Constants.AccessLocalWallpaperInfoKey, | 297 |
| 282 function(items) { | 298 Constants.WallpaperSyncStorage.get(Constants.AccessSurpriseMeEnabledKey, |
| 283 // Normally, the wallpaper info saved in local storage and sync storage | 299 function(enabledItems) { |
| 284 // are the same. If the synced value changed by sync service, they may | 300 var syncSurpriseMeEnabled = |
| 285 // different. In that case, change wallpaper to the one saved in sync | 301 enabledItems[Constants.AccessSurpriseMeEnabledKey]; |
| 286 // storage and update the local value. | 302 |
| 287 var localValue = items[Constants.AccessLocalWallpaperInfoKey]; | 303 Constants.WallpaperSyncStorage.get( |
| 288 if (localValue == undefined || | 304 Constants.AccessLastSurpriseWallpaperChangedDate, function(items) { |
| 289 localValue.url != newValue.url || | 305 var syncLastSurpriseMeChangedDate = |
| 290 localValue.layout != newValue.layout || | 306 items[Constants.AccessLastSurpriseWallpaperChangedDate]; |
| 291 localValue.source != newValue.source) { | 307 |
| 292 if (newValue.source == Constants.WallpaperSourceEnum.Online) { | 308 var today = new Date().toDateString(); |
| 293 // TODO(bshe): Consider schedule an alarm to set online wallpaper | 309 // If SurpriseMe is enabled and surprise wallpaper hasn't been changed |
| 294 // later when failed. Note that we need to cancel the retry if user | 310 // today, we should not sync the change, instead onAlarm() will be |
| 295 // set another wallpaper before retry alarm invoked. | 311 // triggered to update a surprise me wallpaper. |
| 296 WallpaperUtil.setOnlineWallpaper(newValue.url, newValue.layout, | 312 if (!syncSurpriseMeEnabled || |
| 297 function() {}, function() {}); | 313 (syncSurpriseMeEnabled && |
| 298 } else if (newValue.source == Constants.WallpaperSourceEnum.Custom) { | 314 syncLastSurpriseMeChangedDate == today)) { |
| 299 WallpaperUtil.setCustomWallpaperFromSyncFS(newValue.url, | 315 Constants.WallpaperLocalStorage.get( |
| 300 newValue.layout); | 316 Constants.AccessLocalWallpaperInfoKey, function(infoItems) { |
| 301 } else if (newValue.source == Constants.WallpaperSourceEnum.Default) { | 317 var localInfo = infoItems[Constants.AccessLocalWallpaperInfoKey]; |
| 302 chrome.wallpaperPrivate.resetWallpaper(); | 318 // Normally, the wallpaper info saved in local storage and sync |
| 319 // storage are the same. If the synced value changed by sync | |
| 320 // service, they may different. In that case, change wallpaper to | |
| 321 // the one saved in sync storage and update the local value. | |
| 322 if (localInfo == undefined || | |
| 323 localInfo.url != syncInfo.url || | |
| 324 localInfo.layout != syncInfo.layout || | |
| 325 localInfo.source != syncInfo.source) { | |
| 326 if (syncInfo.source == Constants.WallpaperSourceEnum.Online) { | |
| 327 // TODO(bshe): Consider schedule an alarm to set online | |
| 328 // wallpaper later when failed. Note that we need to cancel | |
| 329 // the retry if user set another wallpaper before retry alarm | |
| 330 // invoked. | |
| 331 WallpaperUtil.setOnlineWallpaper(syncInfo.url, | |
| 332 syncInfo.layout, function() {}, function() {}); | |
| 333 } else if (syncInfo.source == | |
| 334 Constants.WallpaperSourceEnum.Custom) { | |
| 335 WallpaperUtil.setCustomWallpaperFromSyncFS(syncInfo.url, | |
| 336 syncInfo.layout); | |
| 337 } else if (syncInfo.source == | |
| 338 Constants.WallpaperSourceEnum.Default) { | |
| 339 chrome.wallpaperPrivate.resetWallpaper(); | |
| 340 } | |
| 341 WallpaperUtil.saveToStorage( | |
| 342 Constants.AccessLocalWallpaperInfoKey, syncInfo, false); | |
| 343 } | |
| 344 }); | |
| 303 } | 345 } |
| 304 WallpaperUtil.saveToStorage(Constants.AccessLocalWallpaperInfoKey, | 346 }); |
| 305 newValue, false); | |
| 306 } | |
| 307 }); | 347 }); |
| 308 } | 348 } |
| 309 }); | 349 }); |
| 310 }); | 350 }); |
| 311 | 351 |
| 312 chrome.alarms.onAlarm.addListener(function() { | 352 chrome.alarms.onAlarm.addListener(function() { |
| 313 SurpriseWallpaper.getInstance().next(); | 353 SurpriseWallpaper.getInstance().next(); |
| 314 }); | 354 }); |
| OLD | NEW |