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 = null; | 8 var wallpaperPickerWindow = null; |
9 | 9 |
10 var surpriseWallpaper = null; | 10 var surpriseWallpaper = null; |
11 | 11 |
12 function SurpriseWallpaper() { | 12 function SurpriseWallpaper() {} |
13 } | |
14 | 13 |
15 /** | 14 /** |
16 * Gets SurpriseWallpaper instance. In case it hasn't been initialized, a new | 15 * Gets SurpriseWallpaper instance. In case it hasn't been initialized, a new |
17 * instance is created. | 16 * instance is created. |
18 * @return {SurpriseWallpaper} A SurpriseWallpaper instance. | 17 * @return {SurpriseWallpaper} A SurpriseWallpaper instance. |
19 */ | 18 */ |
20 SurpriseWallpaper.getInstance = function() { | 19 SurpriseWallpaper.getInstance = function() { |
21 if (!surpriseWallpaper) | 20 if (!surpriseWallpaper) |
22 surpriseWallpaper = new SurpriseWallpaper(); | 21 surpriseWallpaper = new SurpriseWallpaper(); |
23 return surpriseWallpaper; | 22 return surpriseWallpaper; |
24 }; | 23 }; |
25 | 24 |
26 /** | 25 /** |
27 * Tries to change wallpaper to a new one in the background. May fail due to a | 26 * Tries to change wallpaper to a new one in the background. May fail due to a |
28 * network issue. | 27 * network issue. |
29 */ | 28 */ |
30 SurpriseWallpaper.prototype.tryChangeWallpaper = function() { | 29 SurpriseWallpaper.prototype.tryChangeWallpaper = function() { |
31 var self = this; | 30 var self = this; |
32 var onFailure = function(status) { | 31 var onFailure = function(status) { |
33 if (status != 404) | 32 if (status != 404) |
34 self.fallbackToLocalRss_(); | 33 self.fallbackToLocalRss_(); |
35 else | 34 else |
36 self.updateRandomWallpaper_(); | 35 self.updateRandomWallpaper_(); |
37 }; | 36 }; |
38 // Try to fetch newest rss as document from server first. If the requested | 37 // Try to fetch newest rss as document from server first. If the requested |
39 // URL is not found (404 error), set a random wallpaper displayed in the | 38 // 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 | 39 // wallpaper picker. If any other error occurs, proceed with local copy of |
41 // rss. | 40 // rss. |
42 WallpaperUtil.fetchURL(Constants.WallpaperRssURL, 'document', function(xhr) { | 41 WallpaperUtil.fetchURL(Constants.WallpaperRssURL, 'document', function(xhr) { |
43 WallpaperUtil.saveToLocalStorage(Constants.AccessLocalRssKey, | 42 WallpaperUtil.saveToLocalStorage( |
| 43 Constants.AccessLocalRssKey, |
44 new XMLSerializer().serializeToString(xhr.responseXML)); | 44 new XMLSerializer().serializeToString(xhr.responseXML)); |
45 self.updateSurpriseWallpaper(xhr.responseXML); | 45 self.updateSurpriseWallpaper(xhr.responseXML); |
46 }, onFailure); | 46 }, onFailure); |
47 }; | 47 }; |
48 | 48 |
49 /** | 49 /** |
50 * 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 |
51 * rss or wallpaper from server fails. | 51 * rss or wallpaper from server fails. |
52 * @private | 52 * @private |
53 */ | 53 */ |
54 SurpriseWallpaper.prototype.retryLater_ = function() { | 54 SurpriseWallpaper.prototype.retryLater_ = function() { |
55 chrome.alarms.create('RetryAlarm', {delayInMinutes: 60}); | 55 chrome.alarms.create('RetryAlarm', {delayInMinutes: 60}); |
56 }; | 56 }; |
57 | 57 |
58 /** | 58 /** |
59 * Fetches the cached rss feed from local storage in the event of being unable | 59 * Fetches the cached rss feed from local storage in the event of being unable |
60 * to download the online feed. | 60 * to download the online feed. |
61 * @private | 61 * @private |
62 */ | 62 */ |
63 SurpriseWallpaper.prototype.fallbackToLocalRss_ = function() { | 63 SurpriseWallpaper.prototype.fallbackToLocalRss_ = function() { |
64 var self = this; | 64 var self = this; |
65 Constants.WallpaperLocalStorage.get(Constants.AccessLocalRssKey, | 65 Constants.WallpaperLocalStorage.get( |
66 function(items) { | 66 Constants.AccessLocalRssKey, function(items) { |
67 var rssString = items[Constants.AccessLocalRssKey]; | 67 var rssString = items[Constants.AccessLocalRssKey]; |
68 if (rssString) { | 68 if (rssString) { |
69 self.updateSurpriseWallpaper(new DOMParser().parseFromString(rssString, | 69 self.updateSurpriseWallpaper( |
70 'text/xml')); | 70 new DOMParser().parseFromString(rssString, 'text/xml')); |
71 } else { | 71 } else { |
72 self.updateSurpriseWallpaper(); | 72 self.updateSurpriseWallpaper(); |
73 } | 73 } |
74 }); | 74 }); |
75 }; | 75 }; |
76 | 76 |
77 /** | 77 /** |
78 * Starts to change wallpaper. Called after rss is fetched. | 78 * Starts to change wallpaper. Called after rss is fetched. |
79 * @param {Document=} opt_rss The fetched rss document. If opt_rss is null, uses | 79 * @param {Document=} opt_rss The fetched rss document. If opt_rss is null, uses |
80 * a random wallpaper. | 80 * a random wallpaper. |
81 */ | 81 */ |
82 SurpriseWallpaper.prototype.updateSurpriseWallpaper = function(opt_rss) { | 82 SurpriseWallpaper.prototype.updateSurpriseWallpaper = function(opt_rss) { |
83 if (opt_rss) { | 83 if (opt_rss) { |
84 var items = opt_rss.querySelectorAll('item'); | 84 var items = opt_rss.querySelectorAll('item'); |
85 var date = new Date(new Date().toDateString()).getTime(); | 85 var date = new Date(new Date().toDateString()).getTime(); |
86 for (var i = 0; i < items.length; i++) { | 86 for (var i = 0; i < items.length; i++) { |
87 item = items[i]; | 87 item = items[i]; |
88 var disableDate = new Date(item.getElementsByTagNameNS( | 88 var disableDate = |
89 Constants.WallpaperNameSpaceURI, 'disableDate')[0].textContent). | 89 new Date(item.getElementsByTagNameNS( |
90 getTime(); | 90 Constants.WallpaperNameSpaceURI, 'disableDate')[0] |
91 var enableDate = new Date(item.getElementsByTagNameNS( | 91 .textContent) |
92 Constants.WallpaperNameSpaceURI, 'enableDate')[0].textContent). | 92 .getTime(); |
93 getTime(); | 93 var enableDate = |
| 94 new Date(item.getElementsByTagNameNS( |
| 95 Constants.WallpaperNameSpaceURI, 'enableDate')[0] |
| 96 .textContent) |
| 97 .getTime(); |
94 var regionsString = item.getElementsByTagNameNS( | 98 var regionsString = item.getElementsByTagNameNS( |
95 Constants.WallpaperNameSpaceURI, 'regions')[0].textContent; | 99 Constants.WallpaperNameSpaceURI, 'regions')[0] |
| 100 .textContent; |
96 var regions = regionsString.split(', '); | 101 var regions = regionsString.split(', '); |
97 if (enableDate <= date && disableDate > date && | 102 if (enableDate <= date && disableDate > date && |
98 regions.indexOf(navigator.language) != -1) { | 103 regions.indexOf(navigator.language) != -1) { |
99 var self = this; | 104 var self = this; |
100 this.setWallpaperFromRssItem_(item, | 105 this.setWallpaperFromRssItem_( |
101 function() {}, | 106 item, function() {}, |
102 function(status) { | 107 function(status) { |
103 if (status != 404) | 108 if (status != 404) |
104 self.retryLater_(); | 109 self.retryLater_(); |
105 else | 110 else |
106 self.updateRandomWallpaper_(); | 111 self.updateRandomWallpaper_(); |
107 }); | 112 }); |
108 return; | 113 return; |
109 } | 114 } |
110 } | 115 } |
111 } | 116 } |
112 // No surprise wallpaper for today at current locale or fetching rss feed | 117 // No surprise wallpaper for today at current locale or fetching rss feed |
113 // fails. Fallback to use a random one from wallpaper server. | 118 // fails. Fallback to use a random one from wallpaper server. |
114 this.updateRandomWallpaper_(); | 119 this.updateRandomWallpaper_(); |
115 }; | 120 }; |
116 | 121 |
117 /** | 122 /** |
(...skipping 22 matching lines...) Expand all Loading... |
140 | 145 |
141 /** | 146 /** |
142 * Sets wallpaper to one of the wallpapers displayed in wallpaper picker. If | 147 * Sets wallpaper to one of the wallpapers displayed in wallpaper picker. If |
143 * the wallpaper download fails, retry one hour later. Wallpapers that are | 148 * the wallpaper download fails, retry one hour later. Wallpapers that are |
144 * disabled for surprise me are excluded. | 149 * disabled for surprise me are excluded. |
145 * @param {string} dateString String representation of current local date. | 150 * @param {string} dateString String representation of current local date. |
146 * @private | 151 * @private |
147 */ | 152 */ |
148 SurpriseWallpaper.prototype.setRandomWallpaper_ = function(dateString) { | 153 SurpriseWallpaper.prototype.setRandomWallpaper_ = function(dateString) { |
149 var self = this; | 154 var self = this; |
150 Constants.WallpaperLocalStorage.get(Constants.AccessLocalManifestKey, | 155 Constants.WallpaperLocalStorage.get( |
151 function(items) { | 156 Constants.AccessLocalManifestKey, function(items) { |
152 var manifest = items[Constants.AccessLocalManifestKey]; | 157 var manifest = items[Constants.AccessLocalManifestKey]; |
153 if (manifest && manifest.wallpaper_list) { | 158 if (manifest && manifest.wallpaper_list) { |
154 var filtered = manifest.wallpaper_list.filter(function(element) { | 159 var filtered = manifest.wallpaper_list.filter(function(element) { |
155 // Older version manifest do not have available_for_surprise_me field. | 160 // Older version manifest do not have available_for_surprise_me |
156 // In this case, no wallpaper should be filtered out. | 161 // field. In this case, no wallpaper should be filtered out. |
157 return element.available_for_surprise_me || | 162 return element.available_for_surprise_me || |
158 element.available_for_surprise_me == undefined; | 163 element.available_for_surprise_me == undefined; |
| 164 }); |
| 165 var index = Math.floor(Math.random() * filtered.length); |
| 166 var wallpaper = filtered[index]; |
| 167 var wallpaperURL = |
| 168 wallpaper.base_url + Constants.HighResolutionSuffix; |
| 169 var onSuccess = function() { |
| 170 WallpaperUtil.saveWallpaperInfo( |
| 171 wallpaperURL, wallpaper.default_layout, |
| 172 Constants.WallpaperSourceEnum.Daily, ''); |
| 173 WallpaperUtil.saveToLocalStorage( |
| 174 Constants.AccessLastSurpriseWallpaperChangedDate, dateString, |
| 175 function() { |
| 176 WallpaperUtil.saveToSyncStorage( |
| 177 Constants.AccessLastSurpriseWallpaperChangedDate, |
| 178 dateString); |
| 179 }); |
| 180 }; |
| 181 WallpaperUtil.setOnlineWallpaper( |
| 182 wallpaperURL, wallpaper.default_layout, onSuccess, |
| 183 self.retryLater_.bind(self)); |
| 184 } |
159 }); | 185 }); |
160 var index = Math.floor(Math.random() * filtered.length); | |
161 var wallpaper = filtered[index]; | |
162 var wallpaperURL = wallpaper.base_url + Constants.HighResolutionSuffix; | |
163 var onSuccess = function() { | |
164 WallpaperUtil.saveWallpaperInfo(wallpaperURL, wallpaper.default_layout, | |
165 Constants.WallpaperSourceEnum.Daily, ''); | |
166 WallpaperUtil.saveToLocalStorage( | |
167 Constants.AccessLastSurpriseWallpaperChangedDate, | |
168 dateString, function() { | |
169 WallpaperUtil.saveToSyncStorage( | |
170 Constants.AccessLastSurpriseWallpaperChangedDate, | |
171 dateString); | |
172 }); | |
173 }; | |
174 WallpaperUtil.setOnlineWallpaper(wallpaperURL, wallpaper.default_layout, | |
175 onSuccess, self.retryLater_.bind(self)); | |
176 } | |
177 }); | |
178 }; | 186 }; |
179 | 187 |
180 /** | 188 /** |
181 * Sets wallpaper to the wallpaper specified by item from rss. If downloading | 189 * Sets wallpaper to the wallpaper specified by item from rss. If downloading |
182 * the wallpaper fails, retry one hour later. | 190 * the wallpaper fails, retry one hour later. |
183 * @param {Element} item The wallpaper rss item element. | 191 * @param {Element} item The wallpaper rss item element. |
184 * @param {function} onSuccess Success callback. | 192 * @param {function} onSuccess Success callback. |
185 * @param {function} onFailure Failure callback. | 193 * @param {function} onFailure Failure callback. |
186 * @private | 194 * @private |
187 */ | 195 */ |
188 SurpriseWallpaper.prototype.setWallpaperFromRssItem_ = function(item, | 196 SurpriseWallpaper.prototype.setWallpaperFromRssItem_ = function( |
189 onSuccess, | 197 item, onSuccess, onFailure) { |
190 onFailure) { | |
191 var url = item.querySelector('link').textContent; | 198 var url = item.querySelector('link').textContent; |
192 var layout = item.getElementsByTagNameNS( | 199 var layout = |
193 Constants.WallpaperNameSpaceURI, 'layout')[0].textContent; | 200 item.getElementsByTagNameNS(Constants.WallpaperNameSpaceURI, 'layout')[0] |
| 201 .textContent; |
194 var self = this; | 202 var self = this; |
195 WallpaperUtil.fetchURL(url, 'arraybuffer', function(xhr) { | 203 WallpaperUtil.fetchURL(url, 'arraybuffer', function(xhr) { |
196 if (xhr.response != null) { | 204 if (xhr.response != null) { |
197 chrome.wallpaperPrivate.setCustomWallpaper(xhr.response, layout, false, | 205 chrome.wallpaperPrivate.setCustomWallpaper( |
198 'surprise_wallpaper', | 206 xhr.response, layout, false, 'surprise_wallpaper', onSuccess); |
199 onSuccess); | 207 WallpaperUtil.saveWallpaperInfo( |
200 WallpaperUtil.saveWallpaperInfo(url, layout, | 208 url, layout, Constants.WallpaperSourceEnum.Daily, ''); |
201 Constants.WallpaperSourceEnum.Daily, ''); | |
202 var dateString = new Date().toDateString(); | 209 var dateString = new Date().toDateString(); |
203 WallpaperUtil.saveToLocalStorage( | 210 WallpaperUtil.saveToLocalStorage( |
204 Constants.AccessLastSurpriseWallpaperChangedDate, | 211 Constants.AccessLastSurpriseWallpaperChangedDate, dateString, |
205 dateString, function() { | 212 function() { |
206 WallpaperUtil.saveToSyncStorage( | 213 WallpaperUtil.saveToSyncStorage( |
207 Constants.AccessLastSurpriseWallpaperChangedDate, dataString); | 214 Constants.AccessLastSurpriseWallpaperChangedDate, dataString); |
208 }); | 215 }); |
209 } else { | 216 } else { |
210 self.updateRandomWallpaper_(); | 217 self.updateRandomWallpaper_(); |
211 } | 218 } |
212 }, onFailure); | 219 }, onFailure); |
213 }; | 220 }; |
214 | 221 |
215 /** | 222 /** |
216 * Disables the wallpaper surprise me feature. Clear all alarms and states. | 223 * Disables the wallpaper surprise me feature. Clear all alarms and states. |
217 */ | 224 */ |
218 SurpriseWallpaper.prototype.disable = function() { | 225 SurpriseWallpaper.prototype.disable = function() { |
219 chrome.alarms.clearAll(); | 226 chrome.alarms.clearAll(); |
220 // Makes last changed date invalid. | 227 // Makes last changed date invalid. |
221 WallpaperUtil.saveToLocalStorage( | 228 WallpaperUtil.saveToLocalStorage( |
222 Constants.AccessLastSurpriseWallpaperChangedDate, '', function() { | 229 Constants.AccessLastSurpriseWallpaperChangedDate, '', function() { |
223 WallpaperUtil.saveToSyncStorage( | 230 WallpaperUtil.saveToSyncStorage( |
224 Constants.AccessLastSurpriseWallpaperChangedDate, ''); | 231 Constants.AccessLastSurpriseWallpaperChangedDate, ''); |
225 }); | 232 }); |
226 }; | 233 }; |
227 | 234 |
228 /** | 235 /** |
229 * Changes current wallpaper and sets up an alarm to schedule next change around | 236 * Changes current wallpaper and sets up an alarm to schedule next change around |
230 * midnight. | 237 * midnight. |
231 */ | 238 */ |
232 SurpriseWallpaper.prototype.next = function() { | 239 SurpriseWallpaper.prototype.next = function() { |
233 var nextUpdate = this.nextUpdateTime(new Date()); | 240 var nextUpdate = this.nextUpdateTime(new Date()); |
234 chrome.alarms.create({when: nextUpdate}); | 241 chrome.alarms.create({when: nextUpdate}); |
235 this.tryChangeWallpaper(); | 242 this.tryChangeWallpaper(); |
236 }; | 243 }; |
237 | 244 |
238 /** | 245 /** |
239 * Calculates when the next wallpaper change should be triggered. | 246 * Calculates when the next wallpaper change should be triggered. |
240 * @param {Date} now Current time. | 247 * @param {Date} now Current time. |
241 * @return {number} The time when next wallpaper change should happen. | 248 * @return {number} The time when next wallpaper change should happen. |
242 */ | 249 */ |
243 SurpriseWallpaper.prototype.nextUpdateTime = function(now) { | 250 SurpriseWallpaper.prototype.nextUpdateTime = function(now) { |
244 var nextUpdate = new Date(now.setDate(now.getDate() + 1)).toDateString(); | 251 var nextUpdate = new Date(now.setDate(now.getDate() + 1)).toDateString(); |
245 return new Date(nextUpdate).getTime(); | 252 return new Date(nextUpdate).getTime(); |
246 }; | 253 }; |
247 | 254 |
248 chrome.app.runtime.onLaunched.addListener(function() { | 255 chrome.app.runtime.onLaunched.addListener(function() { |
249 if (wallpaperPickerWindow && !wallpaperPickerWindow.contentWindow.closed) { | 256 if (wallpaperPickerWindow && !wallpaperPickerWindow.contentWindow.closed) { |
250 wallpaperPickerWindow.focus(); | 257 wallpaperPickerWindow.focus(); |
251 chrome.wallpaperPrivate.minimizeInactiveWindows(); | 258 chrome.wallpaperPrivate.minimizeInactiveWindows(); |
252 return; | 259 return; |
253 } | 260 } |
254 | 261 |
255 chrome.app.window.create('main.html', { | 262 chrome.app.window.create( |
256 frame: 'none', | 263 'main.html', { |
257 width: WALLPAPER_PICKER_WIDTH, | 264 frame: 'none', |
258 height: WALLPAPER_PICKER_HEIGHT, | 265 width: WALLPAPER_PICKER_WIDTH, |
259 resizable: false, | 266 height: WALLPAPER_PICKER_HEIGHT, |
260 alphaEnabled: true | 267 resizable: false, |
261 }, function(w) { | 268 alphaEnabled: true |
262 wallpaperPickerWindow = w; | 269 }, |
263 chrome.wallpaperPrivate.minimizeInactiveWindows(); | 270 function(w) { |
264 w.onClosed.addListener(function() { | 271 wallpaperPickerWindow = w; |
265 wallpaperPickerWindow = null; | 272 chrome.wallpaperPrivate.minimizeInactiveWindows(); |
266 chrome.wallpaperPrivate.restoreMinimizedWindows(); | 273 w.onClosed.addListener(function() { |
267 }); | 274 wallpaperPickerWindow = null; |
268 WallpaperUtil.testSendMessage('wallpaper-window-created'); | 275 chrome.wallpaperPrivate.restoreMinimizedWindows(); |
269 }); | 276 }); |
| 277 WallpaperUtil.testSendMessage('wallpaper-window-created'); |
| 278 }); |
270 }); | 279 }); |
271 | 280 |
272 chrome.syncFileSystem.onFileStatusChanged.addListener(function(detail) { | 281 chrome.syncFileSystem.onFileStatusChanged.addListener(function(detail) { |
273 WallpaperUtil.enabledSyncThemesCallback(function(syncEnabled) { | 282 WallpaperUtil.enabledSyncThemesCallback(function(syncEnabled) { |
274 if (!syncEnabled) | 283 if (!syncEnabled) |
275 return; | 284 return; |
276 if (detail.status != 'synced' || detail.direction != 'remote_to_local') | 285 if (detail.status != 'synced' || detail.direction != 'remote_to_local') |
277 return; | 286 return; |
278 if (detail.action == 'added') { | 287 if (detail.action == 'added') { |
279 // TODO(xdai): Get rid of this setCustomWallpaperFromSyncFS logic. | 288 // TODO(xdai): Get rid of this setCustomWallpaperFromSyncFS logic. |
280 // WallpaperInfo might have been saved in the sync filesystem before the | 289 // WallpaperInfo might have been saved in the sync filesystem before the |
281 // corresonding custom wallpaper and thumbnail are saved, thus the | 290 // corresonding custom wallpaper and thumbnail are saved, thus the |
282 // onChanged() might not set the custom wallpaper correctly. So we need | 291 // onChanged() might not set the custom wallpaper correctly. So we need |
283 // setCustomWallpaperFromSyncFS() to be called here again to make sure | 292 // setCustomWallpaperFromSyncFS() to be called here again to make sure |
284 // custom wallpaper is set. | 293 // custom wallpaper is set. |
285 Constants.WallpaperLocalStorage.get( | 294 Constants.WallpaperLocalStorage.get( |
286 Constants.AccessLocalWallpaperInfoKey, function(items) { | 295 Constants.AccessLocalWallpaperInfoKey, function(items) { |
287 var localData = items[Constants.AccessLocalWallpaperInfoKey]; | 296 var localData = items[Constants.AccessLocalWallpaperInfoKey]; |
288 if (localData && | 297 if (localData && localData.url == detail.fileEntry.name && |
289 localData.url == detail.fileEntry.name && | 298 localData.source == Constants.WallpaperSourceEnum.Custom) { |
290 localData.source == Constants.WallpaperSourceEnum.Custom) { | 299 WallpaperUtil.setCustomWallpaperFromSyncFS( |
291 WallpaperUtil.setCustomWallpaperFromSyncFS(localData.url, | 300 localData.url, localData.layout); |
292 localData.layout); | 301 } |
293 } | 302 }); |
294 }); | |
295 // We only need to store the custom wallpaper if it was set by the | 303 // We only need to store the custom wallpaper if it was set by the |
296 // built-in wallpaper picker. | 304 // built-in wallpaper picker. |
297 if (!detail.fileEntry.name.startsWith( | 305 if (!detail.fileEntry.name.startsWith( |
298 Constants.ThirdPartyWallpaperPrefix)) { | 306 Constants.ThirdPartyWallpaperPrefix)) { |
299 WallpaperUtil.storeWallpaperFromSyncFSToLocalFS(detail.fileEntry); | 307 WallpaperUtil.storeWallpaperFromSyncFSToLocalFS(detail.fileEntry); |
300 } | 308 } |
301 } else if (detail.action == 'deleted') { | 309 } else if (detail.action == 'deleted') { |
302 var fileName = detail.fileEntry.name.replace( | 310 var fileName = detail.fileEntry.name.replace( |
303 Constants.CustomWallpaperThumbnailSuffix, ''); | 311 Constants.CustomWallpaperThumbnailSuffix, ''); |
304 WallpaperUtil.deleteWallpaperFromLocalFS(fileName); | 312 WallpaperUtil.deleteWallpaperFromLocalFS(fileName); |
305 } | 313 } |
306 }); | 314 }); |
307 }); | 315 }); |
308 | 316 |
(...skipping 28 matching lines...) Expand all Loading... |
337 wpDocument.querySelector('#checkbox').classList.remove('checked'); | 345 wpDocument.querySelector('#checkbox').classList.remove('checked'); |
338 wpDocument.querySelector('#categories-list').disabled = false; | 346 wpDocument.querySelector('#categories-list').disabled = false; |
339 wpDocument.querySelector('#wallpaper-grid').disabled = false; | 347 wpDocument.querySelector('#wallpaper-grid').disabled = false; |
340 }); | 348 }); |
341 } else { | 349 } else { |
342 wpDocument.querySelector('#wallpaper-set-by-message').textContent = | 350 wpDocument.querySelector('#wallpaper-set-by-message').textContent = |
343 ''; | 351 ''; |
344 wpDocument.querySelector('#wallpaper-grid').classList.remove('small'); | 352 wpDocument.querySelector('#wallpaper-grid').classList.remove('small'); |
345 Constants.WallpaperSyncStorage.get( | 353 Constants.WallpaperSyncStorage.get( |
346 Constants.AccessSyncSurpriseMeEnabledKey, function(item) { | 354 Constants.AccessSyncSurpriseMeEnabledKey, function(item) { |
347 var enable = item[Constants.AccessSyncSurpriseMeEnabledKey]; | 355 var enable = item[Constants.AccessSyncSurpriseMeEnabledKey]; |
348 if (enable) { | 356 if (enable) { |
349 wpDocument.querySelector('#checkbox').classList.add('checked'); | 357 wpDocument.querySelector('#checkbox') |
350 if (wpDocument.querySelector('.check')) | 358 .classList.add('checked'); |
351 wpDocument.querySelector('.check').style.visibility = 'hidden'; | 359 if (wpDocument.querySelector('.check')) |
352 } else { | 360 wpDocument.querySelector('.check').style.visibility = |
353 wpDocument.querySelector('#checkbox').classList.remove('checked'); | 361 'hidden'; |
354 if (wpDocument.querySelector('.check')) | 362 } else { |
355 wpDocument.querySelector('.check').style.visibility = 'visible'; | 363 wpDocument.querySelector('#checkbox') |
356 } | 364 .classList.remove('checked'); |
357 wpDocument.querySelector('#categories-list').disabled = enable; | 365 if (wpDocument.querySelector('.check')) |
358 wpDocument.querySelector('#wallpaper-grid').disabled = enable; | 366 wpDocument.querySelector('.check').style.visibility = |
359 }); | 367 'visible'; |
| 368 } |
| 369 wpDocument.querySelector('#categories-list').disabled = enable; |
| 370 wpDocument.querySelector('#wallpaper-grid').disabled = enable; |
| 371 }); |
360 } | 372 } |
361 }; | 373 }; |
362 | 374 |
363 if (changes[Constants.AccessLocalWallpaperInfoKey]) { | 375 if (changes[Constants.AccessLocalWallpaperInfoKey]) { |
364 // If the old wallpaper is a third party wallpaper we should remove it | 376 // If the old wallpaper is a third party wallpaper we should remove it |
365 // from the local & sync file system to free space. | 377 // from the local & sync file system to free space. |
366 var oldInfo = changes[Constants.AccessLocalWallpaperInfoKey].oldValue; | 378 var oldInfo = changes[Constants.AccessLocalWallpaperInfoKey].oldValue; |
367 if (oldInfo && | 379 if (oldInfo && |
368 oldInfo.url.indexOf(Constants.ThirdPartyWallpaperPrefix) != -1) { | 380 oldInfo.url.indexOf(Constants.ThirdPartyWallpaperPrefix) != -1) { |
369 WallpaperUtil.deleteWallpaperFromLocalFS(oldInfo.url); | 381 WallpaperUtil.deleteWallpaperFromLocalFS(oldInfo.url); |
370 WallpaperUtil.deleteWallpaperFromSyncFS(oldInfo.url); | 382 WallpaperUtil.deleteWallpaperFromSyncFS(oldInfo.url); |
371 } | 383 } |
372 | 384 |
373 var newInfo = changes[Constants.AccessLocalWallpaperInfoKey].newValue; | 385 var newInfo = changes[Constants.AccessLocalWallpaperInfoKey].newValue; |
374 if (newInfo && newInfo.hasOwnProperty('appName')) | 386 if (newInfo && newInfo.hasOwnProperty('appName')) |
375 updateCheckMarkAndAppNameIfAppliable(newInfo.appName); | 387 updateCheckMarkAndAppNameIfAppliable(newInfo.appName); |
376 } | 388 } |
377 | 389 |
378 if (changes[Constants.AccessSyncWallpaperInfoKey]) { | 390 if (changes[Constants.AccessSyncWallpaperInfoKey]) { |
379 var syncInfo = changes[Constants.AccessSyncWallpaperInfoKey].newValue; | 391 var syncInfo = changes[Constants.AccessSyncWallpaperInfoKey].newValue; |
380 | 392 |
381 Constants.WallpaperSyncStorage.get( | 393 Constants.WallpaperSyncStorage.get( |
382 Constants.AccessSyncSurpriseMeEnabledKey, function(enabledItems) { | 394 Constants.AccessSyncSurpriseMeEnabledKey, function(enabledItems) { |
383 var syncSurpriseMeEnabled = | 395 var syncSurpriseMeEnabled = |
384 enabledItems[Constants.AccessSyncSurpriseMeEnabledKey]; | 396 enabledItems[Constants.AccessSyncSurpriseMeEnabledKey]; |
385 | 397 |
386 Constants.WallpaperSyncStorage.get( | 398 Constants.WallpaperSyncStorage.get( |
387 Constants.AccessLastSurpriseWallpaperChangedDate, | 399 Constants.AccessLastSurpriseWallpaperChangedDate, |
388 function(items) { | 400 function(items) { |
389 var syncLastSurpriseMeChangedDate = | 401 var syncLastSurpriseMeChangedDate = |
390 items[Constants.AccessLastSurpriseWallpaperChangedDate]; | 402 items[Constants.AccessLastSurpriseWallpaperChangedDate]; |
391 | 403 |
392 var today = new Date().toDateString(); | 404 var today = new Date().toDateString(); |
393 // If SurpriseMe is enabled and surprise wallpaper hasn't been | 405 // If SurpriseMe is enabled and surprise wallpaper hasn't |
394 // changed today, we should not sync the change, instead onAlarm() | 406 // been changed today, we should not sync the change, |
395 // will be triggered to update a surprise me wallpaper. | 407 // instead onAlarm() will be triggered to update a surprise |
396 if (!syncSurpriseMeEnabled || | 408 // me wallpaper. |
397 (syncSurpriseMeEnabled && | 409 if (!syncSurpriseMeEnabled || |
398 syncLastSurpriseMeChangedDate == today)) { | 410 (syncSurpriseMeEnabled && |
399 Constants.WallpaperLocalStorage.get( | 411 syncLastSurpriseMeChangedDate == today)) { |
400 Constants.AccessLocalWallpaperInfoKey, function(infoItems) { | 412 Constants.WallpaperLocalStorage.get( |
401 var localInfo = | 413 Constants.AccessLocalWallpaperInfoKey, |
402 infoItems[Constants.AccessLocalWallpaperInfoKey]; | 414 function(infoItems) { |
403 // Normally, the wallpaper info saved in local storage and sync | 415 var localInfo = |
404 // storage are the same. If the synced value changed by sync | 416 infoItems[Constants |
405 // service, they may different. In that case, change wallpaper | 417 .AccessLocalWallpaperInfoKey]; |
406 // to the one saved in sync storage and update the local value. | 418 // Normally, the wallpaper info saved in local |
407 if (!localInfo || | 419 // storage and sync storage are the same. If the |
408 localInfo.url != syncInfo.url || | 420 // synced value changed by sync service, they may |
409 localInfo.layout != syncInfo.layout || | 421 // different. In that case, change wallpaper to the |
410 localInfo.source != syncInfo.source) { | 422 // one saved in sync storage and update the local |
411 if (syncInfo.source == Constants.WallpaperSourceEnum.Online) { | 423 // value. |
412 // TODO(bshe): Consider schedule an alarm to set online | 424 if (!localInfo || localInfo.url != syncInfo.url || |
413 // wallpaper later when failed. Note that we need to cancel | 425 localInfo.layout != syncInfo.layout || |
414 // the retry if user set another wallpaper before retry | 426 localInfo.source != syncInfo.source) { |
415 // alarm invoked. | 427 if (syncInfo.source == |
416 WallpaperUtil.setOnlineWallpaper(syncInfo.url, | 428 Constants.WallpaperSourceEnum.Online) { |
417 syncInfo.layout, function() {}, function() {}); | 429 // TODO(bshe): Consider schedule an alarm to set |
418 } else if (syncInfo.source == | 430 // online wallpaper later when failed. Note that |
419 Constants.WallpaperSourceEnum.Custom) { | 431 // we need to cancel the retry if user set |
420 WallpaperUtil.setCustomWallpaperFromSyncFS(syncInfo.url, | 432 // another wallpaper before retry alarm invoked. |
421 syncInfo.layout); | 433 WallpaperUtil.setOnlineWallpaper( |
422 } else if (syncInfo.source == | 434 syncInfo.url, syncInfo.layout, |
423 Constants.WallpaperSourceEnum.Default) { | 435 function() {}, function() {}); |
424 chrome.wallpaperPrivate.resetWallpaper(); | 436 } else if ( |
425 } | 437 syncInfo.source == |
| 438 Constants.WallpaperSourceEnum.Custom) { |
| 439 WallpaperUtil.setCustomWallpaperFromSyncFS( |
| 440 syncInfo.url, syncInfo.layout); |
| 441 } else if ( |
| 442 syncInfo.source == |
| 443 Constants.WallpaperSourceEnum.Default) { |
| 444 chrome.wallpaperPrivate.resetWallpaper(); |
| 445 } |
426 | 446 |
427 // If the old wallpaper is a third party wallpaper we should | 447 // If the old wallpaper is a third party wallpaper |
428 // remove it from the local & sync file system to free space. | 448 // we should remove it from the local & sync file |
429 if (localInfo && localInfo.url.indexOf( | 449 // system to free space. |
430 Constants.ThirdPartyWallpaperPrefix) != -1) { | 450 if (localInfo && |
431 WallpaperUtil.deleteWallpaperFromLocalFS(localInfo.url); | 451 localInfo.url.indexOf( |
432 WallpaperUtil.deleteWallpaperFromSyncFS(localInfo.url); | 452 Constants.ThirdPartyWallpaperPrefix) != |
433 } | 453 -1) { |
| 454 WallpaperUtil.deleteWallpaperFromLocalFS( |
| 455 localInfo.url); |
| 456 WallpaperUtil.deleteWallpaperFromSyncFS( |
| 457 localInfo.url); |
| 458 } |
434 | 459 |
435 if (syncInfo && syncInfo.hasOwnProperty('appName')) | 460 if (syncInfo && |
436 updateCheckMarkAndAppNameIfAppliable(syncInfo.appName); | 461 syncInfo.hasOwnProperty('appName')) |
| 462 updateCheckMarkAndAppNameIfAppliable( |
| 463 syncInfo.appName); |
437 | 464 |
438 WallpaperUtil.saveToLocalStorage( | 465 WallpaperUtil.saveToLocalStorage( |
439 Constants.AccessLocalWallpaperInfoKey, syncInfo); | 466 Constants.AccessLocalWallpaperInfoKey, |
440 } | 467 syncInfo); |
441 }); | 468 } |
442 } | 469 }); |
443 }); | 470 } |
444 }); | 471 }); |
| 472 }); |
445 } | 473 } |
446 } else { | 474 } else { |
447 // If sync theme is disabled, use values from chrome.storage.local to | 475 // If sync theme is disabled, use values from chrome.storage.local to |
448 // track wallpaper changes. | 476 // track wallpaper changes. |
449 if (changes[Constants.AccessLocalSurpriseMeEnabledKey]) { | 477 if (changes[Constants.AccessLocalSurpriseMeEnabledKey]) { |
450 if (changes[Constants.AccessLocalSurpriseMeEnabledKey].newValue) { | 478 if (changes[Constants.AccessLocalSurpriseMeEnabledKey].newValue) { |
451 SurpriseWallpaper.getInstance().next(); | 479 SurpriseWallpaper.getInstance().next(); |
452 } else { | 480 } else { |
453 SurpriseWallpaper.getInstance().disable(); | 481 SurpriseWallpaper.getInstance().disable(); |
454 } | 482 } |
455 } | 483 } |
456 } | 484 } |
457 }); | 485 }); |
458 }); | 486 }); |
459 | 487 |
460 chrome.alarms.onAlarm.addListener(function() { | 488 chrome.alarms.onAlarm.addListener(function() { |
461 SurpriseWallpaper.getInstance().next(); | 489 SurpriseWallpaper.getInstance().next(); |
462 }); | 490 }); |
463 | 491 |
464 chrome.wallpaperPrivate.onWallpaperChangedBy3rdParty.addListener(function( | 492 chrome.wallpaperPrivate.onWallpaperChangedBy3rdParty.addListener(function( |
465 wallpaper, thumbnail, layout, appName) { | 493 wallpaper, thumbnail, layout, appName) { |
466 WallpaperUtil.saveToLocalStorage( | 494 WallpaperUtil.saveToLocalStorage( |
467 Constants.AccessLocalSurpriseMeEnabledKey, false, function() { | 495 Constants.AccessLocalSurpriseMeEnabledKey, false, function() { |
468 WallpaperUtil.saveToSyncStorage(Constants.AccessSyncSurpriseMeEnabledKey, | 496 WallpaperUtil.saveToSyncStorage( |
469 false); | 497 Constants.AccessSyncSurpriseMeEnabledKey, false); |
470 }); | 498 }); |
471 SurpriseWallpaper.getInstance().disable(); | 499 SurpriseWallpaper.getInstance().disable(); |
472 | 500 |
473 // Make third party wallpaper syncable through different devices. | 501 // Make third party wallpaper syncable through different devices. |
474 var filename = Constants.ThirdPartyWallpaperPrefix + new Date().getTime(); | 502 var filename = Constants.ThirdPartyWallpaperPrefix + new Date().getTime(); |
475 var thumbnailFilename = filename + Constants.CustomWallpaperThumbnailSuffix; | 503 var thumbnailFilename = filename + Constants.CustomWallpaperThumbnailSuffix; |
476 WallpaperUtil.storeWallpaperToSyncFS(filename, wallpaper); | 504 WallpaperUtil.storeWallpaperToSyncFS(filename, wallpaper); |
477 WallpaperUtil.storeWallpaperToSyncFS(thumbnailFilename, thumbnail); | 505 WallpaperUtil.storeWallpaperToSyncFS(thumbnailFilename, thumbnail); |
478 WallpaperUtil.saveWallpaperInfo( | 506 WallpaperUtil.saveWallpaperInfo( |
479 filename, layout, Constants.WallpaperSourceEnum.ThirdParty, appName); | 507 filename, layout, Constants.WallpaperSourceEnum.ThirdParty, appName); |
480 }); | 508 }); |
OLD | NEW |