Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(571)

Side by Side Diff: chrome/browser/resources/chromeos/wallpaper_manager/js/wallpaper_manager.js

Issue 11028121: Convert wallpaper picker to v2 app (Closed) Base URL: http://git.chromium.org/chromium/src.git@AppsV2
Patch Set: Write async loop to make it more readable Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 * Requests wallpaper manifest file from server.
68 */
69 WallpaperManager.prototype.fetchManifest_ = function() {
70 var locale = navigator.language;
71 var urls = [
72 ManifestBaseURL + locale + '.json',
73 // Fallback url. Use 'en' locale by default.
74 ManifestBaseURL + 'en.json'];
75
76 var asyncFetchManifestFromUrls = function(urls, func, successCallback,
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 failure();
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();
102 xhr.open('GET', url, true);
103 try {
104 xhr.send(null);
105 xhr.addEventListener('load', function(e) {
flackr 2012/11/13 22:22:31 Reading the XHR documentation it seems like 'load'
bshe 2012/11/15 00:17:57 Done. I used loadend event. On 2012/11/13 22:22:3
106 // TODO(bshe): We should save the downloaded manifest to local disk.
107 // Other components may want to use it (e.g. screen saver).
108 if (xhr.status === 200) {
flackr 2012/11/13 22:22:31 nit: braces not needed anymore on this if since th
bshe 2012/11/15 00:17:57 Done.
109 loop.success(xhr.responseText);
110 } else {
111 loop.next();
112 }
113 });
114 } catch (e) {
115 loop.failure();
116 }
117 };
118
119 asyncFetchManifestFromUrls(urls, fetchManifestAsync,
120 this.onLoadManifestSuccess_.bind(this),
121 this.onLoadManifestFailed_.bind(this));
122 };
123
124 /**
67 * Parses a string as manifest(JSON). Sets manifest to an empty object if a 125 * Parses a string as manifest(JSON). Sets manifest to an empty object if a
68 * parsing exception is catched. 126 * parsing exception is catched. Called after manifest is successfully loaded.
69 * @param {string} response The string to parse as JSON. 127 * @param {string} response The string to parse as JSON.
70 */ 128 */
71 WallpaperManager.prototype.parseManifest_ = function(response) { 129 WallpaperManager.prototype.onLoadManifestSuccess_ = function(response) {
72 try { 130 try {
73 this.manifest_ = JSON.parse(response); 131 this.manifest_ = JSON.parse(response);
74 } catch (e) { 132 } catch (e) {
75 this.butterBar_.showError_('Failed to parse manifest.'); 133 this.butterBar_.showError_('Failed to parse manifest.');
flackr 2012/11/13 22:22:31 This isn't a localized string, and the message is
bshe 2012/11/15 00:17:57 Done. On 2012/11/13 22:22:31, flackr wrote:
76 this.manifest_ = {}; 134 this.manifest_ = {};
77 } 135 }
136 this.initDom_();
137 };
138
139 // Sets manifest to an empty object and shows connection error. Called after
140 // manifest failed to load.
141 WallpaperManager.prototype.onLoadManifestFailed_ = function() {
142 // TODO(bshe): Fall back to saved manifest if there is a problem fetching
143 // manifest from server.
144 this.manifest_ = {};
145 this.butterBar_.showError_(str('connectionFailed'));
146 this.initDom_();
78 }; 147 };
79 148
80 /** 149 /**
81 * Requests wallpaper manifest file from server.
82 */
83 WallpaperManager.prototype.fetchManifest_ = function() {
84 var xhr = new XMLHttpRequest();
85 var locale = navigator.language;
86 var urls = [
87 ManifestBaseURL + locale + '.json',
88 // Fallback url. Use 'en' locale by default.
89 ManifestBaseURL + 'en.json'];
90
91 for (var i = 0; i < urls.length; i++) {
92 xhr.open('GET', urls[i], false);
93 try {
94 xhr.send(null);
95 // TODO(bshe): We should save the downloaded manifest to local disk.
96 // Other components may want to use it (i.e. screen saver).
97 if (xhr.status === 200) {
98 this.parseManifest_(xhr.responseText);
99 return;
100 }
101 } catch (e) {
102 this.manifest_ = {};
103 this.butterBar_.showError_(str('connectionFailed'));
104 return;
105 }
106 }
107 this.manifest_ = {};
108 this.butterBar_.showError_(str('connectionFailed'));
109
110 // TODO(bshe): Fall back to saved manifest if there is a problem fetching
111 // manifest from server.
112 };
113
114 /**
115 * One-time initialization of various DOM nodes. 150 * One-time initialization of various DOM nodes.
116 */ 151 */
117 WallpaperManager.prototype.initDom_ = function() { 152 WallpaperManager.prototype.initDom_ = function() {
118 i18nTemplate.process(this.document_, loadTimeData); 153 i18nTemplate.process(this.document_, loadTimeData);
119 this.initCategoriesList_(); 154 this.initCategoriesList_();
120 this.initThumbnailsGrid_(); 155 this.initThumbnailsGrid_();
121 156
122 this.currentWallpaper_ = str('currentWallpaper'); 157 this.currentWallpaper_ = str('currentWallpaper');
123 if (this.currentWallpaper_ == 'CUSTOM') { 158 if (this.currentWallpaper_ == 'CUSTOM') {
124 // Custom is the last one in the categories list. 159 // Custom is the last one in the categories list.
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after
402 selectedItem = wallpaperInfo; 437 selectedItem = wallpaperInfo;
403 } 438 }
404 } 439 }
405 } 440 }
406 this.wallpaperGrid_.dataModel = wallpapersDataModel; 441 this.wallpaperGrid_.dataModel = wallpapersDataModel;
407 this.wallpaperGrid_.selectedItem = selectedItem; 442 this.wallpaperGrid_.selectedItem = selectedItem;
408 } 443 }
409 }; 444 };
410 445
411 })(); 446 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698