Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: chrome/browser/resources/chromeos/wallpaper_manager/js/event_page.js

Issue 956403003: Fix the issue that wallpaper changes twice when both "onChanged" and "onAlarm" are fired. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix the failed wallpaper sync test. Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/resources/chromeos/wallpaper_manager/js/util.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 11 matching lines...) Expand all
22 surpriseWallpaper = new SurpriseWallpaper(); 22 surpriseWallpaper = new SurpriseWallpaper();
23 return surpriseWallpaper; 23 return surpriseWallpaper;
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(status) {
33 self.retryLater_(); 33 if (status != 404)
34 self.fallbackToLocalRss_(); 34 self.fallbackToLocalRss_();
35 else
36 self.updateRandomWallpaper_();
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 the requested
37 // proceed with local copy of rss. 39 // URL is not found (404 error), set a random wallpaper displayed in the
40 // wallpaper picker. If any other error occurs, proceed with local copy of
41 // rss.
38 WallpaperUtil.fetchURL(Constants.WallpaperRssURL, 'document', function(xhr) { 42 WallpaperUtil.fetchURL(Constants.WallpaperRssURL, 'document', function(xhr) {
39 WallpaperUtil.saveToStorage(Constants.AccessRssKey, 43 WallpaperUtil.saveToStorage(Constants.AccessRssKey,
40 new XMLSerializer().serializeToString(xhr.responseXML), false); 44 new XMLSerializer().serializeToString(xhr.responseXML), false);
41 self.updateSurpriseWallpaper(xhr.responseXML); 45 self.updateSurpriseWallpaper(xhr.responseXML);
42 }, onFailure); 46 }, onFailure);
43 }; 47 };
44 48
45 /** 49 /**
46 * Retries changing the wallpaper 1 hour later. This is called when fetching the 50 * Retries changing the wallpaper 1 hour later. This is called when fetching the
47 * rss or wallpaper from server fails. 51 * rss or wallpaper from server fails.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 Constants.WallpaperNameSpaceURI, 'enableDate')[0].textContent). 91 Constants.WallpaperNameSpaceURI, 'enableDate')[0].textContent).
88 getTime(); 92 getTime();
89 var regionsString = item.getElementsByTagNameNS( 93 var regionsString = item.getElementsByTagNameNS(
90 Constants.WallpaperNameSpaceURI, 'regions')[0].textContent; 94 Constants.WallpaperNameSpaceURI, 'regions')[0].textContent;
91 var regions = regionsString.split(', '); 95 var regions = regionsString.split(', ');
92 if (enableDate <= date && disableDate > date && 96 if (enableDate <= date && disableDate > date &&
93 regions.indexOf(navigator.language) != -1) { 97 regions.indexOf(navigator.language) != -1) {
94 var self = this; 98 var self = this;
95 this.setWallpaperFromRssItem_(item, 99 this.setWallpaperFromRssItem_(item,
96 function() {}, 100 function() {},
97 function() { 101 function(status) {
98 self.retryLater_(); 102 if (status != 404)
99 self.updateRandomWallpaper_(); 103 self.retryLater_();
104 else
105 self.updateRandomWallpaper_();
100 }); 106 });
101 return; 107 return;
102 } 108 }
103 } 109 }
104 } 110 }
105 // No surprise wallpaper for today at current locale or fetching rss feed 111 // No surprise wallpaper for today at current locale or fetching rss feed
106 // fails. Fallback to use a random one from wallpaper server. 112 // fails. Fallback to use a random one from wallpaper server.
107 this.updateRandomWallpaper_(); 113 this.updateRandomWallpaper_();
108 }; 114 };
109 115
(...skipping 29 matching lines...) Expand all
139 var filtered = manifest.wallpaper_list.filter(function(element) { 145 var filtered = manifest.wallpaper_list.filter(function(element) {
140 // Older version manifest do not have available_for_surprise_me field. 146 // Older version manifest do not have available_for_surprise_me field.
141 // In this case, no wallpaper should be filtered out. 147 // In this case, no wallpaper should be filtered out.
142 return element.available_for_surprise_me || 148 return element.available_for_surprise_me ||
143 element.available_for_surprise_me == undefined; 149 element.available_for_surprise_me == undefined;
144 }); 150 });
145 var index = Math.floor(Math.random() * filtered.length); 151 var index = Math.floor(Math.random() * filtered.length);
146 var wallpaper = filtered[index]; 152 var wallpaper = filtered[index];
147 var wallpaperURL = wallpaper.base_url + Constants.HighResolutionSuffix; 153 var wallpaperURL = wallpaper.base_url + Constants.HighResolutionSuffix;
148 var onSuccess = function() { 154 var onSuccess = function() {
155 WallpaperUtil.saveWallpaperInfo(wallpaperURL, wallpaper.default_layout,
156 Constants.WallpaperSourceEnum.Online);
149 WallpaperUtil.saveToStorage( 157 WallpaperUtil.saveToStorage(
150 Constants.AccessLastSurpriseWallpaperChangedDate, 158 Constants.AccessLastSurpriseWallpaperChangedDate,
151 dateString, 159 dateString,
152 true); 160 true);
153 }; 161 };
154 WallpaperUtil.setOnlineWallpaper(wallpaperURL, wallpaper.default_layout, 162 WallpaperUtil.setOnlineWallpaper(wallpaperURL, wallpaper.default_layout,
155 onSuccess, self.retryLater_.bind(self)); 163 onSuccess, self.retryLater_.bind(self));
156 } 164 }
157 }); 165 });
158 }; 166 };
(...skipping 11 matching lines...) Expand all
170 onFailure) { 178 onFailure) {
171 var url = item.querySelector('link').textContent; 179 var url = item.querySelector('link').textContent;
172 var layout = item.getElementsByTagNameNS( 180 var layout = item.getElementsByTagNameNS(
173 Constants.WallpaperNameSpaceURI, 'layout')[0].textContent; 181 Constants.WallpaperNameSpaceURI, 'layout')[0].textContent;
174 var self = this; 182 var self = this;
175 WallpaperUtil.fetchURL(url, 'arraybuffer', function(xhr) { 183 WallpaperUtil.fetchURL(url, 'arraybuffer', function(xhr) {
176 if (xhr.response != null) { 184 if (xhr.response != null) {
177 chrome.wallpaperPrivate.setCustomWallpaper(xhr.response, layout, false, 185 chrome.wallpaperPrivate.setCustomWallpaper(xhr.response, layout, false,
178 'surprise_wallpaper', 186 'surprise_wallpaper',
179 onSuccess); 187 onSuccess);
188 WallpaperUtil.saveWallpaperInfo(url, layout,
189 Constants.WallpaperSourceEnum.Online);
190 var dateString = new Date().toDateString();
191 WallpaperUtil.saveToStorage(
192 Constants.AccessLastSurpriseWallpaperChangedDate, dateString, true);
180 } else { 193 } else {
181 onFailure(); 194 self.updateRandomWallpaper_();
182 } 195 }
183 }, onFailure); 196 }, onFailure);
184 }; 197 };
185 198
186 /** 199 /**
187 * Disables the wallpaper surprise me feature. Clear all alarms and states. 200 * Disables the wallpaper surprise me feature. Clear all alarms and states.
188 */ 201 */
189 SurpriseWallpaper.prototype.disable = function() { 202 SurpriseWallpaper.prototype.disable = function() {
190 chrome.alarms.clearAll(); 203 chrome.alarms.clearAll();
191 // Makes last changed date invalid. 204 // Makes last changed date invalid.
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 WallpaperUtil.requestSyncFS(function() {}); 283 WallpaperUtil.requestSyncFS(function() {});
271 if (changes[Constants.AccessSurpriseMeEnabledKey]) { 284 if (changes[Constants.AccessSurpriseMeEnabledKey]) {
272 if (changes[Constants.AccessSurpriseMeEnabledKey].newValue) { 285 if (changes[Constants.AccessSurpriseMeEnabledKey].newValue) {
273 SurpriseWallpaper.getInstance().next(); 286 SurpriseWallpaper.getInstance().next();
274 } else { 287 } else {
275 SurpriseWallpaper.getInstance().disable(); 288 SurpriseWallpaper.getInstance().disable();
276 } 289 }
277 } 290 }
278 291
279 if (changes[Constants.AccessSyncWallpaperInfoKey]) { 292 if (changes[Constants.AccessSyncWallpaperInfoKey]) {
280 var newValue = changes[Constants.AccessSyncWallpaperInfoKey].newValue; 293 var syncInfo = changes[Constants.AccessSyncWallpaperInfoKey].newValue;
281 Constants.WallpaperLocalStorage.get(Constants.AccessLocalWallpaperInfoKey, 294
282 function(items) { 295 Constants.WallpaperSyncStorage.get(Constants.AccessSurpriseMeEnabledKey,
283 // Normally, the wallpaper info saved in local storage and sync storage 296 function(enabledItems) {
284 // are the same. If the synced value changed by sync service, they may 297 var syncSurpriseMeEnabled =
285 // different. In that case, change wallpaper to the one saved in sync 298 enabledItems[Constants.AccessSurpriseMeEnabledKey];
286 // storage and update the local value. 299
287 var localValue = items[Constants.AccessLocalWallpaperInfoKey]; 300 Constants.WallpaperSyncStorage.get(
288 if (localValue == undefined || 301 Constants.AccessLastSurpriseWallpaperChangedDate, function(items) {
289 localValue.url != newValue.url || 302 var syncLastSurpriseMeChangedDate =
290 localValue.layout != newValue.layout || 303 items[Constants.AccessLastSurpriseWallpaperChangedDate];
291 localValue.source != newValue.source) { 304
292 if (newValue.source == Constants.WallpaperSourceEnum.Online) { 305 var today = new Date().toDateString();
293 // TODO(bshe): Consider schedule an alarm to set online wallpaper 306 // 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 307 // today, we should not sync the change, instead onAlarm() will be
295 // set another wallpaper before retry alarm invoked. 308 // triggered to update a surprise me wallpaper.
296 WallpaperUtil.setOnlineWallpaper(newValue.url, newValue.layout, 309 if (!syncSurpriseMeEnabled ||
297 function() {}, function() {}); 310 (syncSurpriseMeEnabled &&
298 } else if (newValue.source == Constants.WallpaperSourceEnum.Custom) { 311 syncLastSurpriseMeChangedDate == today)) {
299 WallpaperUtil.setCustomWallpaperFromSyncFS(newValue.url, 312 Constants.WallpaperLocalStorage.get(
300 newValue.layout); 313 Constants.AccessLocalWallpaperInfoKey, function(infoItems) {
301 } else if (newValue.source == Constants.WallpaperSourceEnum.Default) { 314 var localInfo = infoItems[Constants.AccessLocalWallpaperInfoKey];
302 chrome.wallpaperPrivate.resetWallpaper(); 315 // Normally, the wallpaper info saved in local storage and sync
316 // storage are the same. If the synced value changed by sync
317 // service, they may different. In that case, change wallpaper to
318 // the one saved in sync storage and update the local value.
319 if (localInfo == undefined ||
320 localInfo.url != syncInfo.url ||
321 localInfo.layout != syncInfo.layout ||
322 localInfo.source != syncInfo.source) {
323 if (syncInfo.source == Constants.WallpaperSourceEnum.Online) {
324 // TODO(bshe): Consider schedule an alarm to set online
325 // wallpaper later when failed. Note that we need to cancel
326 // the retry if user set another wallpaper before retry alarm
327 // invoked.
328 WallpaperUtil.setOnlineWallpaper(syncInfo.url,
329 syncInfo.layout, function() {}, function() {});
330 } else if (syncInfo.source ==
331 Constants.WallpaperSourceEnum.Custom) {
332 WallpaperUtil.setCustomWallpaperFromSyncFS(syncInfo.url,
333 syncInfo.layout);
334 } else if (syncInfo.source ==
335 Constants.WallpaperSourceEnum.Default) {
336 chrome.wallpaperPrivate.resetWallpaper();
337 }
338 WallpaperUtil.saveToStorage(
339 Constants.AccessLocalWallpaperInfoKey, syncInfo, false);
340 }
341 });
303 } 342 }
304 WallpaperUtil.saveToStorage(Constants.AccessLocalWallpaperInfoKey, 343 });
305 newValue, false);
306 }
307 }); 344 });
308 } 345 }
309 }); 346 });
310 }); 347 });
311 348
312 chrome.alarms.onAlarm.addListener(function() { 349 chrome.alarms.onAlarm.addListener(function() {
313 SurpriseWallpaper.getInstance().next(); 350 SurpriseWallpaper.getInstance().next();
314 }); 351 });
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/resources/chromeos/wallpaper_manager/js/util.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698