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 22 matching lines...) Expand all Loading... |
57 */ | 57 */ |
58 WallpaperManager.initStrings = function(callback) { | 58 WallpaperManager.initStrings = function(callback) { |
59 chrome.wallpaperPrivate.getStrings(function(strings) { | 59 chrome.wallpaperPrivate.getStrings(function(strings) { |
60 loadTimeData.data = strings; | 60 loadTimeData.data = strings; |
61 if (callback) | 61 if (callback) |
62 callback(); | 62 callback(); |
63 }); | 63 }); |
64 }; | 64 }; |
65 | 65 |
66 /** | 66 /** |
67 * Parses a string as manifest(JSON). Sets manifest to an empty object if a | |
68 * parsing exception is catched. | |
69 * @param {string} response The string to parse as JSON. | |
70 */ | |
71 WallpaperManager.prototype.parseManifest_ = function(response) { | |
72 try { | |
73 this.manifest_ = JSON.parse(response); | |
74 } catch (e) { | |
75 this.butterBar_.showError_('Failed to parse manifest.', | |
76 {help_url: LEARN_MORE_URL}); | |
77 this.manifest_ = {}; | |
78 } | |
79 }; | |
80 | |
81 /** | |
82 * Requests wallpaper manifest file from server. | 67 * Requests wallpaper manifest file from server. |
83 */ | 68 */ |
84 WallpaperManager.prototype.fetchManifest_ = function() { | 69 WallpaperManager.prototype.fetchManifest_ = function() { |
85 var xhr = new XMLHttpRequest(); | |
86 var locale = navigator.language; | 70 var locale = navigator.language; |
87 var urls = [ | 71 var urls = [ |
88 ManifestBaseURL + locale + '.json', | 72 ManifestBaseURL + locale + '.json', |
89 // Fallback url. Use 'en' locale by default. | 73 // Fallback url. Use 'en' locale by default. |
90 ManifestBaseURL + 'en.json']; | 74 ManifestBaseURL + 'en.json']; |
91 | 75 |
92 for (var i = 0; i < urls.length; i++) { | 76 var asyncFetchManifestFromUrls = function(urls, func, successCallback, |
93 xhr.open('GET', urls[i], false); | 77 failureCallback) { |
| 78 var index = 0; |
| 79 var loop = { |
| 80 next: function() { |
| 81 if (index < urls.length) { |
| 82 func(loop, urls[index]); |
| 83 index++; |
| 84 } else { |
| 85 failureCallback(); |
| 86 } |
| 87 }, |
| 88 |
| 89 success: function(response) { |
| 90 successCallback(response); |
| 91 }, |
| 92 |
| 93 failure: function() { |
| 94 failureCallback(); |
| 95 } |
| 96 }; |
| 97 loop.next(); |
| 98 }; |
| 99 |
| 100 var fetchManifestAsync = function(loop, url) { |
| 101 var xhr = new XMLHttpRequest(); |
94 try { | 102 try { |
| 103 xhr.addEventListener('loadend', function(e) { |
| 104 if (this.status == 200 && this.responseText != null) { |
| 105 try { |
| 106 var manifest = JSON.parse(this.responseText); |
| 107 loop.success(manifest); |
| 108 } catch (e) { |
| 109 loop.failure(); |
| 110 } |
| 111 } else { |
| 112 loop.next(); |
| 113 } |
| 114 }); |
| 115 xhr.open('GET', url, true); |
95 xhr.send(null); | 116 xhr.send(null); |
96 // TODO(bshe): We should save the downloaded manifest to local disk. | |
97 // Other components may want to use it (i.e. screen saver). | |
98 if (xhr.status === 200) { | |
99 this.parseManifest_(xhr.responseText); | |
100 return; | |
101 } | |
102 } catch (e) { | 117 } catch (e) { |
103 this.manifest_ = {}; | 118 loop.failure(); |
104 this.butterBar_.showError_(str('connectionFailed'), | |
105 {help_url: LEARN_MORE_URL}); | |
106 return; | |
107 } | 119 } |
108 } | 120 }; |
| 121 |
| 122 asyncFetchManifestFromUrls(urls, fetchManifestAsync, |
| 123 this.onLoadManifestSuccess_.bind(this), |
| 124 this.onLoadManifestFailed_.bind(this)); |
| 125 }; |
| 126 |
| 127 /** |
| 128 * Sets manifest loaded from server. Called after manifest is successfully |
| 129 * loaded. |
| 130 * @param {object} manifest The parsed manifest file. |
| 131 */ |
| 132 WallpaperManager.prototype.onLoadManifestSuccess_ = function(manifest) { |
| 133 this.manifest_ = manifest; |
| 134 this.initDom_(); |
| 135 }; |
| 136 |
| 137 // Sets manifest to an empty object and shows connection error. Called after |
| 138 // manifest failed to load. |
| 139 WallpaperManager.prototype.onLoadManifestFailed_ = function() { |
| 140 // TODO(bshe): Fall back to saved manifest if there is a problem fetching |
| 141 // manifest from server. |
109 this.manifest_ = {}; | 142 this.manifest_ = {}; |
110 this.butterBar_.showError_(str('connectionFailed'), | 143 this.butterBar_.showError_(str('connectionFailed'), |
111 {help_url: LEARN_MORE_URL}); | 144 {help_url: LEARN_MORE_URL}); |
112 | 145 this.initDom_(); |
113 // TODO(bshe): Fall back to saved manifest if there is a problem fetching | |
114 // manifest from server. | |
115 }; | 146 }; |
116 | 147 |
117 /** | 148 /** |
118 * One-time initialization of various DOM nodes. | 149 * One-time initialization of various DOM nodes. |
119 */ | 150 */ |
120 WallpaperManager.prototype.initDom_ = function() { | 151 WallpaperManager.prototype.initDom_ = function() { |
121 i18nTemplate.process(this.document_, loadTimeData); | 152 i18nTemplate.process(this.document_, loadTimeData); |
122 this.initCategoriesList_(); | 153 this.initCategoriesList_(); |
123 this.initThumbnailsGrid_(); | 154 this.initThumbnailsGrid_(); |
124 | 155 |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 selectedItem = wallpaperInfo; | 441 selectedItem = wallpaperInfo; |
411 } | 442 } |
412 } | 443 } |
413 } | 444 } |
414 this.wallpaperGrid_.dataModel = wallpapersDataModel; | 445 this.wallpaperGrid_.dataModel = wallpapersDataModel; |
415 this.wallpaperGrid_.selectedItem = selectedItem; | 446 this.wallpaperGrid_.selectedItem = selectedItem; |
416 } | 447 } |
417 }; | 448 }; |
418 | 449 |
419 })(); | 450 })(); |
OLD | NEW |