| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 /** | 5 /** |
| 6 * WallpaperManager constructor. | 6 * WallpaperManager constructor. |
| 7 * | 7 * |
| 8 * WallpaperManager objects encapsulate the functionality of the wallpaper | 8 * WallpaperManager objects encapsulate the functionality of the wallpaper |
| 9 * manager extension. | 9 * manager extension. |
| 10 * | 10 * |
| 11 * @constructor | 11 * @constructor |
| 12 * @param {HTMLElement} dialogDom The DOM node containing the prototypical | 12 * @param {HTMLElement} dialogDom The DOM node containing the prototypical |
| 13 * extension UI. | 13 * extension UI. |
| 14 */ | 14 */ |
| 15 | 15 |
| 16 function WallpaperManager(dialogDom) { | 16 function WallpaperManager(dialogDom) { |
| 17 this.dialogDom_ = dialogDom; | 17 this.dialogDom_ = dialogDom; |
| 18 this.document_ = dialogDom.ownerDocument; | 18 this.document_ = dialogDom.ownerDocument; |
| 19 this.selectedCategory = null; | 19 this.selectedCategory = null; |
| 20 this.currentButter_ = null; | 20 this.butterBar_ = new ButterBar(this.dialogDom_); |
| 21 this.customWallpaperData_ = null; | 21 this.customWallpaperData_ = null; |
| 22 this.fetchManifest_(); | 22 this.fetchManifest_(); |
| 23 this.initDom_(); | 23 this.initDom_(); |
| 24 } | 24 } |
| 25 | 25 |
| 26 // Anonymous 'namespace'. | 26 // Anonymous 'namespace'. |
| 27 // TODO(bshe): Get rid of anonymous namespace. | 27 // TODO(bshe): Get rid of anonymous namespace. |
| 28 (function() { | 28 (function() { |
| 29 | 29 |
| 30 /** | 30 /** |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 /** | 65 /** |
| 66 * Parses a string as manifest(JSON). Sets manifest to an empty object if a | 66 * Parses a string as manifest(JSON). Sets manifest to an empty object if a |
| 67 * parsing exception is catched. | 67 * parsing exception is catched. |
| 68 * @param {string} response The string to parse as JSON. | 68 * @param {string} response The string to parse as JSON. |
| 69 */ | 69 */ |
| 70 WallpaperManager.prototype.parseManifest_ = function(response) { | 70 WallpaperManager.prototype.parseManifest_ = function(response) { |
| 71 try { | 71 try { |
| 72 this.manifest_ = JSON.parse(response); | 72 this.manifest_ = JSON.parse(response); |
| 73 } catch (e) { | 73 } catch (e) { |
| 74 // TODO(bshe): Shows an error butter bar to notify json parse exception. | 74 this.butterBar_.showError_('Failed to parse manifest.'); |
| 75 this.manifest_ = {}; | 75 this.manifest_ = {}; |
| 76 } | 76 } |
| 77 }; | 77 }; |
| 78 | 78 |
| 79 /** | 79 /** |
| 80 * Requests wallpaper manifest file from server. | 80 * Requests wallpaper manifest file from server. |
| 81 */ | 81 */ |
| 82 WallpaperManager.prototype.fetchManifest_ = function() { | 82 WallpaperManager.prototype.fetchManifest_ = function() { |
| 83 var xhr = new XMLHttpRequest(); | 83 var xhr = new XMLHttpRequest(); |
| 84 var locale = navigator.language; | 84 var locale = navigator.language; |
| 85 xhr.open('GET', ManifestBaseURL + locale + '.json', false); | 85 xhr.open('GET', ManifestBaseURL + locale + '.json', false); |
| 86 xhr.send(null); | 86 xhr.send(null); |
| 87 // TODO(bshe): We should save the downloaded manifest to local disk. Other | 87 // TODO(bshe): We should save the downloaded manifest to local disk. Other |
| 88 // components may want to use it (i.e. screen saver). | 88 // components may want to use it (i.e. screen saver). |
| 89 if (xhr.status === 200) { | 89 if (xhr.status === 200) { |
| 90 this.parseManifest_(xhr.responseText); | 90 this.parseManifest_(xhr.responseText); |
| 91 } else if (xhr.status === 404) { | 91 } else { |
| 92 // Fall back to en locale if current locale is not supported. | 92 // Fall back to en locale if current locale is not supported. |
| 93 xhr.open('GET', ManifestBaseURL + 'en.json', false); | 93 xhr.open('GET', ManifestBaseURL + 'en.json', false); |
| 94 xhr.send(null); | 94 xhr.send(null); |
| 95 if (xhr.status === 200) { | 95 if (xhr.status === 200) { |
| 96 this.parseManifest_(xhr.responseText); | 96 this.parseManifest_(xhr.responseText); |
| 97 } else { |
| 98 this.manifest_ = {}; |
| 99 this.butterBar_.showError_('Failed to download manifest.'); |
| 97 } | 100 } |
| 98 } | 101 } |
| 99 | 102 |
| 100 // TODO(bshe): Fall back to saved manifest if there is a problem fetching | 103 // TODO(bshe): Fall back to saved manifest if there is a problem fetching |
| 101 // manifest from server. | 104 // manifest from server. |
| 102 }; | 105 }; |
| 103 | 106 |
| 104 /** | 107 /** |
| 105 * One-time initialization of various DOM nodes. | 108 * One-time initialization of various DOM nodes. |
| 106 */ | 109 */ |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 173 |
| 171 if (this.wallpaperRequest_) | 174 if (this.wallpaperRequest_) |
| 172 this.wallpaperRequest_.abort(); | 175 this.wallpaperRequest_.abort(); |
| 173 // TODO(bshe): XMLHttpRequest may be cancelled by window.close(). We should | 176 // TODO(bshe): XMLHttpRequest may be cancelled by window.close(). We should |
| 174 // either wait for response before closing window or use an extension | 177 // either wait for response before closing window or use an extension |
| 175 // background page to get response afte window closed. | 178 // background page to get response afte window closed. |
| 176 this.wallpaperRequest_ = new XMLHttpRequest(); | 179 this.wallpaperRequest_ = new XMLHttpRequest(); |
| 177 this.wallpaperRequest_.open('GET', wallpaperURL, true); | 180 this.wallpaperRequest_.open('GET', wallpaperURL, true); |
| 178 this.wallpaperRequest_.responseType = 'arraybuffer'; | 181 this.wallpaperRequest_.responseType = 'arraybuffer'; |
| 179 this.wallpaperRequest_.send(null); | 182 this.wallpaperRequest_.send(null); |
| 183 this.butterBar_.setRequest(this.wallpaperRequest_); |
| 180 var self = this; | 184 var self = this; |
| 181 this.wallpaperRequest_.addEventListener('load', function(e) { | 185 this.wallpaperRequest_.addEventListener('load', function(e) { |
| 182 //TODO(bshe): Add error handling code. | |
| 183 if (self.wallpaperRequest_.status === 200) { | 186 if (self.wallpaperRequest_.status === 200) { |
| 184 var image = self.wallpaperRequest_.response; | 187 var image = self.wallpaperRequest_.response; |
| 185 chrome.wallpaperPrivate.setWallpaper(image, | 188 chrome.wallpaperPrivate.setWallpaper(image, |
| 186 selectedItem.layout, | 189 selectedItem.layout, |
| 187 wallpaperURL); | 190 wallpaperURL); |
| 191 } else { |
| 192 // Displays the error text in butter bar. |
| 193 self.butterBar_.showError_(self.wallpaperRequest_.statusText); |
| 188 } | 194 } |
| 189 self.wallpaperRequest_ = null; | 195 self.wallpaperRequest_ = null; |
| 190 }); | 196 }); |
| 191 this.setWallpaperAttribution_(selectedItem); | 197 this.setWallpaperAttribution_(selectedItem); |
| 192 }; | 198 }; |
| 193 | 199 |
| 194 /** | 200 /** |
| 195 * Set attributions of wallpaper with given URL. If URL is not valid, clear | 201 * Set attributions of wallpaper with given URL. If URL is not valid, clear |
| 196 * the attributions. | 202 * the attributions. |
| 197 * @param {{baseURL: string, dynamicURL: string, layout: string, | 203 * @param {{baseURL: string, dynamicURL: string, layout: string, |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 authorWebsite: this.manifest_.wallpaper_list[key].author_website | 354 authorWebsite: this.manifest_.wallpaper_list[key].author_website |
| 349 }; | 355 }; |
| 350 wallpapersDataModel.push(wallpaperInfo); | 356 wallpapersDataModel.push(wallpaperInfo); |
| 351 } | 357 } |
| 352 } | 358 } |
| 353 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 359 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
| 354 } | 360 } |
| 355 }; | 361 }; |
| 356 | 362 |
| 357 })(); | 363 })(); |
| OLD | NEW |