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.storage_ = chrome.storage.local; |
18 this.document_ = dialogDom.ownerDocument; | 19 this.document_ = dialogDom.ownerDocument; |
19 this.selectedCategory = null; | 20 this.selectedCategory = null; |
20 this.butterBar_ = new ButterBar(this.dialogDom_); | 21 this.butterBar_ = new ButterBar(this.dialogDom_); |
21 this.customWallpaperData_ = null; | 22 this.customWallpaperData_ = null; |
22 this.currentWallpaper_ = null; | 23 this.currentWallpaper_ = null; |
23 this.wallpaperRequest_ = null; | 24 this.wallpaperRequest_ = null; |
24 this.fetchManifest_(); | 25 this.fetchManifest_(); |
25 } | 26 } |
26 | 27 |
27 // Anonymous 'namespace'. | 28 // Anonymous 'namespace'. |
28 // TODO(bshe): Get rid of anonymous namespace. | 29 // TODO(bshe): Get rid of anonymous namespace. |
29 (function() { | 30 (function() { |
30 | 31 |
31 /** | 32 /** |
32 * Base URL of the manifest file. | 33 * Base URL of the manifest file. |
33 */ | 34 */ |
34 /** @const */ var ManifestBaseURL = 'https://commondatastorage.googleapis.' + | 35 /** @const */ var ManifestBaseURL = 'https://commondatastorage.googleapis.' + |
35 'com/chromeos-wallpaper-public/manifest_'; | 36 'com/chromeos-wallpaper-public/manifest_'; |
36 | 37 |
37 /** | 38 /** |
38 * Suffix to append to baseURL if requesting high resoultion wallpaper. | 39 * Suffix to append to baseURL if requesting high resoultion wallpaper. |
39 */ | 40 */ |
40 /** @const */ var HighResolutionSuffix = '_high_resolution.jpg'; | 41 /** @const */ var HighResolutionSuffix = '_high_resolution.jpg'; |
41 | 42 |
42 /** | 43 /** |
| 44 * Key to access wallpaper manifest in chrome.local.storage. |
| 45 */ |
| 46 /** @const */ var AccessManifestKey = 'wallpaper-picker-manifest-key'; |
| 47 |
| 48 /** |
43 * Returns a translated string. | 49 * Returns a translated string. |
44 * | 50 * |
45 * Wrapper function to make dealing with translated strings more concise. | 51 * Wrapper function to make dealing with translated strings more concise. |
46 * Equivilant to localStrings.getString(id). | 52 * Equivilant to localStrings.getString(id). |
47 * | 53 * |
48 * @param {string} id The id of the string to return. | 54 * @param {string} id The id of the string to return. |
49 * @return {string} The translated string. | 55 * @return {string} The translated string. |
50 */ | 56 */ |
51 function str(id) { | 57 function str(id) { |
52 return loadTimeData.getString(id); | 58 return loadTimeData.getString(id); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 loop.next(); | 118 loop.next(); |
113 } | 119 } |
114 }); | 120 }); |
115 xhr.open('GET', url, true); | 121 xhr.open('GET', url, true); |
116 xhr.send(null); | 122 xhr.send(null); |
117 } catch (e) { | 123 } catch (e) { |
118 loop.failure(); | 124 loop.failure(); |
119 } | 125 } |
120 }; | 126 }; |
121 | 127 |
122 asyncFetchManifestFromUrls(urls, fetchManifestAsync, | 128 if (navigator.onLine) { |
123 this.onLoadManifestSuccess_.bind(this), | 129 asyncFetchManifestFromUrls(urls, fetchManifestAsync, |
124 this.onLoadManifestFailed_.bind(this)); | 130 this.onLoadManifestSuccess_.bind(this), |
| 131 this.onLoadManifestFailed_.bind(this)); |
| 132 } else { |
| 133 // If device is offline, fetches manifest from local storage. |
| 134 // TODO(bshe): Always loading the offline manifest first and replacing |
| 135 // with the online one when available. |
| 136 this.onLoadManifestFailed_(); |
| 137 } |
125 }; | 138 }; |
126 | 139 |
127 /** | 140 /** |
128 * Sets manifest loaded from server. Called after manifest is successfully | 141 * Sets manifest loaded from server. Called after manifest is successfully |
129 * loaded. | 142 * loaded. |
130 * @param {object} manifest The parsed manifest file. | 143 * @param {object} manifest The parsed manifest file. |
131 */ | 144 */ |
132 WallpaperManager.prototype.onLoadManifestSuccess_ = function(manifest) { | 145 WallpaperManager.prototype.onLoadManifestSuccess_ = function(manifest) { |
133 this.manifest_ = manifest; | 146 this.manifest_ = manifest; |
| 147 var items = {}; |
| 148 items[AccessManifestKey] = manifest; |
| 149 this.storage_.set(items, function() {}); |
134 this.initDom_(); | 150 this.initDom_(); |
135 }; | 151 }; |
136 | 152 |
137 // Sets manifest to an empty object and shows connection error. Called after | 153 // Sets manifest to previously saved object if any and shows connection error. |
138 // manifest failed to load. | 154 // Called after manifest failed to load. |
139 WallpaperManager.prototype.onLoadManifestFailed_ = function() { | 155 WallpaperManager.prototype.onLoadManifestFailed_ = function() { |
140 // TODO(bshe): Fall back to saved manifest if there is a problem fetching | 156 var self = this; |
141 // manifest from server. | 157 this.storage_.get(AccessManifestKey, function(items) { |
142 this.manifest_ = {}; | 158 self.manifest_ = items[AccessManifestKey] ? items[AccessManifestKey] : {}; |
143 this.butterBar_.showError_(str('connectionFailed'), | 159 self.butterBar_.showError_(str('connectionFailed'), |
144 {help_url: LEARN_MORE_URL}); | 160 {help_url: LEARN_MORE_URL}); |
145 this.initDom_(); | 161 self.initDom_(); |
| 162 }); |
146 }; | 163 }; |
147 | 164 |
148 /** | 165 /** |
149 * One-time initialization of various DOM nodes. | 166 * One-time initialization of various DOM nodes. |
150 */ | 167 */ |
151 WallpaperManager.prototype.initDom_ = function() { | 168 WallpaperManager.prototype.initDom_ = function() { |
152 i18nTemplate.process(this.document_, loadTimeData); | 169 i18nTemplate.process(this.document_, loadTimeData); |
153 this.initCategoriesList_(); | 170 this.initCategoriesList_(); |
154 this.initThumbnailsGrid_(); | 171 this.initThumbnailsGrid_(); |
155 | 172 |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 selectedItem = wallpaperInfo; | 458 selectedItem = wallpaperInfo; |
442 } | 459 } |
443 } | 460 } |
444 } | 461 } |
445 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 462 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
446 this.wallpaperGrid_.selectedItem = selectedItem; | 463 this.wallpaperGrid_.selectedItem = selectedItem; |
447 } | 464 } |
448 }; | 465 }; |
449 | 466 |
450 })(); | 467 })(); |
OLD | NEW |