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.butterBar_ = new ButterBar(this.dialogDom_); | 20 this.butterBar_ = new ButterBar(this.dialogDom_); |
21 this.customWallpaperData_ = null; | 21 this.customWallpaperData_ = null; |
22 this.currentWallpaper_ = null; | 22 this.currentWallpaper_ = null; |
23 this.wallpaperRequest_ = null; | |
23 this.fetchManifest_(); | 24 this.fetchManifest_(); |
24 this.initDom_(); | |
25 } | 25 } |
26 | 26 |
27 // Anonymous 'namespace'. | 27 // Anonymous 'namespace'. |
28 // TODO(bshe): Get rid of anonymous namespace. | 28 // TODO(bshe): Get rid of anonymous namespace. |
29 (function() { | 29 (function() { |
30 | 30 |
31 /** | 31 /** |
32 * Base URL of the manifest file. | 32 * Base URL of the manifest file. |
33 */ | 33 */ |
34 /** @const */ var ManifestBaseURL = 'https://commondatastorage.googleapis.' + | 34 /** @const */ var ManifestBaseURL = 'https://commondatastorage.googleapis.' + |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 } catch (e) { | 74 } catch (e) { |
75 this.butterBar_.showError_('Failed to parse manifest.'); | 75 this.butterBar_.showError_('Failed to parse manifest.'); |
76 this.manifest_ = {}; | 76 this.manifest_ = {}; |
77 } | 77 } |
78 }; | 78 }; |
79 | 79 |
80 /** | 80 /** |
81 * Requests wallpaper manifest file from server. | 81 * Requests wallpaper manifest file from server. |
82 */ | 82 */ |
83 WallpaperManager.prototype.fetchManifest_ = function() { | 83 WallpaperManager.prototype.fetchManifest_ = function() { |
84 var xhr = new XMLHttpRequest(); | |
85 var locale = navigator.language; | 84 var locale = navigator.language; |
86 var urls = [ | 85 var urls = [ |
87 ManifestBaseURL + locale + '.json', | 86 ManifestBaseURL + locale + '.json', |
88 // Fallback url. Use 'en' locale by default. | 87 // Fallback url. Use 'en' locale by default. |
89 ManifestBaseURL + 'en.json']; | 88 ManifestBaseURL + 'en.json']; |
89 var self = this; | |
90 | 90 |
91 for (var i = 0; i < urls.length; i++) { | 91 var asyncFetchManifestFromUrls = function(urls, func, callback) { |
flackr
2012/11/13 16:18:30
To make this a bit easier to read, perhaps you cou
bshe
2012/11/13 19:33:22
Done.
| |
92 xhr.open('GET', urls[i], false); | 92 var index = 0; |
93 var loop = { | |
94 next: function() { | |
95 if (index < urls.length) { | |
96 func(loop, urls[index]); | |
97 index++; | |
98 } else { | |
99 callback(); | |
100 } | |
101 }, | |
102 | |
103 done: function() { | |
104 callback(); | |
flackr
2012/11/13 16:18:30
It looks like if there are no manifests we will no
bshe
2012/11/13 19:33:22
No. That was a mistake. Fixed it in new patch.
On
| |
105 } | |
106 }; | |
107 loop.next(); | |
108 } | |
flackr
2012/11/13 16:18:30
Need semicolon at end of line.
bshe
2012/11/13 19:33:22
Done.
| |
109 | |
110 var fetchManifestAsync = function(loop, url) { | |
111 var xhr = new XMLHttpRequest(); | |
112 xhr.open('GET', url, true); | |
93 try { | 113 try { |
94 xhr.send(null); | 114 xhr.send(null); |
95 // TODO(bshe): We should save the downloaded manifest to local disk. | 115 xhr.addEventListener('load', function(e) { |
96 // Other components may want to use it (i.e. screen saver). | 116 // TODO(bshe): We should save the downloaded manifest to local disk. |
97 if (xhr.status === 200) { | 117 // Other components may want to use it (e.g. screen saver). |
98 this.parseManifest_(xhr.responseText); | 118 if (xhr.status === 200) { |
99 return; | 119 self.parseManifest_(xhr.responseText); |
100 } | 120 self.initDom_(); |
121 loop.done(); | |
flackr
2012/11/13 16:18:30
As the code is currently, you'll call initDom twic
bshe
2012/11/13 19:33:22
Should be fixed in the new patch.
On 2012/11/13 1
| |
122 } else { | |
123 loop.next(); | |
124 } | |
125 }); | |
101 } catch (e) { | 126 } catch (e) { |
102 this.manifest_ = {}; | 127 self.manifest_ = {}; |
103 this.butterBar_.showError_(str('connectionFailed')); | 128 self.butterBar_.showError_(str('connectionFailed')); |
104 return; | 129 loop.done(); |
105 } | 130 } |
106 } | 131 } |
flackr
2012/11/13 16:18:30
Semicolon at end of line.
bshe
2012/11/13 19:33:22
Done.
| |
107 this.manifest_ = {}; | 132 |
108 this.butterBar_.showError_(str('connectionFailed')); | 133 asyncFetchManifestFromUrls(urls, fetchManifestAsync, this.initDom_); |
109 | 134 |
110 // TODO(bshe): Fall back to saved manifest if there is a problem fetching | 135 // TODO(bshe): Fall back to saved manifest if there is a problem fetching |
111 // manifest from server. | 136 // manifest from server. |
112 }; | 137 }; |
113 | 138 |
114 /** | 139 /** |
115 * One-time initialization of various DOM nodes. | 140 * One-time initialization of various DOM nodes. |
116 */ | 141 */ |
117 WallpaperManager.prototype.initDom_ = function() { | 142 WallpaperManager.prototype.initDom_ = function() { |
118 i18nTemplate.process(this.document_, loadTimeData); | 143 i18nTemplate.process(this.document_, loadTimeData); |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
402 selectedItem = wallpaperInfo; | 427 selectedItem = wallpaperInfo; |
403 } | 428 } |
404 } | 429 } |
405 } | 430 } |
406 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 431 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
407 this.wallpaperGrid_.selectedItem = selectedItem; | 432 this.wallpaperGrid_.selectedItem = selectedItem; |
408 } | 433 } |
409 }; | 434 }; |
410 | 435 |
411 })(); | 436 })(); |
OLD | NEW |