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

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

Issue 2944703004: Run clang-format on .js files in c/b/r/chromeos (Closed)
Patch Set: Created 3 years, 6 months 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 var WallpaperUtil = { 5 var WallpaperUtil = {
6 strings: null, // Object that contains all the flags 6 strings: null, // Object that contains all the flags
7 syncFs: null, // syncFileSystem handler 7 syncFs: null, // syncFileSystem handler
8 webkitFs: null // webkitFileSystem handler 8 webkitFs: null // webkitFileSystem handler
9 }; 9 };
10 10
11 /** 11 /**
12 * Deletes |wallpaperFileName| and its associated thumbnail from local FS. 12 * Deletes |wallpaperFileName| and its associated thumbnail from local FS.
13 * @param {string} wallpaperFilename Name of the file that will be deleted 13 * @param {string} wallpaperFilename Name of the file that will be deleted
14 */ 14 */
15 WallpaperUtil.deleteWallpaperFromLocalFS = function(wallpaperFilename) { 15 WallpaperUtil.deleteWallpaperFromLocalFS = function(wallpaperFilename) {
16 WallpaperUtil.requestLocalFS(function(fs) { 16 WallpaperUtil.requestLocalFS(function(fs) {
17 var originalPath = Constants.WallpaperDirNameEnum.ORIGINAL + '/' + 17 var originalPath =
18 wallpaperFilename; 18 Constants.WallpaperDirNameEnum.ORIGINAL + '/' + wallpaperFilename;
19 var thumbnailPath = Constants.WallpaperDirNameEnum.THUMBNAIL + '/' + 19 var thumbnailPath =
20 wallpaperFilename; 20 Constants.WallpaperDirNameEnum.THUMBNAIL + '/' + wallpaperFilename;
21 fs.root.getFile(originalPath, 21 fs.root.getFile(
22 {create: false}, 22 originalPath, {create: false},
23 function(fe) { 23 function(fe) {
24 fe.remove(function() {}, null); 24 fe.remove(function() {}, null);
25 }, 25 },
26 // NotFoundError is expected. After we receive a delete 26 // NotFoundError is expected. After we receive a delete
27 // event from either original wallpaper or wallpaper 27 // event from either original wallpaper or wallpaper
28 // thumbnail, we delete both of them in local FS to achieve 28 // thumbnail, we delete both of them in local FS to achieve
29 // a faster synchronization. So each file is expected to be 29 // a faster synchronization. So each file is expected to be
30 // deleted twice and the second attempt is a noop. 30 // deleted twice and the second attempt is a noop.
31 function(e) { 31 function(e) {
32 if (e.name != 'NotFoundError') 32 if (e.name != 'NotFoundError')
33 WallpaperUtil.onFileSystemError(e); 33 WallpaperUtil.onFileSystemError(e);
34 }); 34 });
35 fs.root.getFile(thumbnailPath, 35 fs.root.getFile(
36 {create: false}, 36 thumbnailPath, {create: false},
37 function(fe) { 37 function(fe) {
38 fe.remove(function() {}, null); 38 fe.remove(function() {}, null);
39 }, 39 },
40 function(e) { 40 function(e) {
41 if (e.name != 'NotFoundError') 41 if (e.name != 'NotFoundError')
42 WallpaperUtil.onFileSystemError(e); 42 WallpaperUtil.onFileSystemError(e);
43 }); 43 });
44 }); 44 });
45 }; 45 };
46 46
47 /** 47 /**
48 * Loads a wallpaper from sync file system and saves it and its thumbnail to 48 * Loads a wallpaper from sync file system and saves it and its thumbnail to
49 * local file system. 49 * local file system.
50 * @param {string} wallpaperFileEntry File name of wallpaper image. 50 * @param {string} wallpaperFileEntry File name of wallpaper image.
51 */ 51 */
52 WallpaperUtil.storeWallpaperFromSyncFSToLocalFS = function(wallpaperFileEntry) { 52 WallpaperUtil.storeWallpaperFromSyncFSToLocalFS = function(wallpaperFileEntry) {
53 var filenName = wallpaperFileEntry.name; 53 var filenName = wallpaperFileEntry.name;
54 var storeDir = Constants.WallpaperDirNameEnum.ORIGINAL; 54 var storeDir = Constants.WallpaperDirNameEnum.ORIGINAL;
55 if (filenName.indexOf(Constants.CustomWallpaperThumbnailSuffix) != -1) 55 if (filenName.indexOf(Constants.CustomWallpaperThumbnailSuffix) != -1)
56 storeDir = Constants.WallpaperDirNameEnum.THUMBNAIL; 56 storeDir = Constants.WallpaperDirNameEnum.THUMBNAIL;
57 filenName = filenName.replace(Constants.CustomWallpaperThumbnailSuffix, ''); 57 filenName = filenName.replace(Constants.CustomWallpaperThumbnailSuffix, '');
58 wallpaperFileEntry.file(function(file) { 58 wallpaperFileEntry.file(function(file) {
59 var reader = new FileReader(); 59 var reader = new FileReader();
60 reader.onloadend = function() { 60 reader.onloadend = function() {
61 WallpaperUtil.storeWallpaperToLocalFS(filenName, reader.result, storeDir); 61 WallpaperUtil.storeWallpaperToLocalFS(filenName, reader.result, storeDir);
62 }; 62 };
63 reader.readAsArrayBuffer(file); 63 reader.readAsArrayBuffer(file);
64 }, WallpaperUtil.onFileSystemError); 64 }, WallpaperUtil.onFileSystemError);
65 }; 65 };
66 66
67 /** 67 /**
68 * Deletes |wallpaperFileName| and its associated thumbnail from syncFileSystem. 68 * Deletes |wallpaperFileName| and its associated thumbnail from syncFileSystem.
69 * @param {string} wallpaperFilename Name of the file that will be deleted. 69 * @param {string} wallpaperFilename Name of the file that will be deleted.
70 */ 70 */
71 WallpaperUtil.deleteWallpaperFromSyncFS = function(wallpaperFilename) { 71 WallpaperUtil.deleteWallpaperFromSyncFS = function(wallpaperFilename) {
72 var thumbnailFilename = wallpaperFilename + 72 var thumbnailFilename =
73 Constants.CustomWallpaperThumbnailSuffix; 73 wallpaperFilename + Constants.CustomWallpaperThumbnailSuffix;
74 var success = function(fs) { 74 var success = function(fs) {
75 fs.root.getFile(wallpaperFilename, 75 fs.root.getFile(
76 {create: false}, 76 wallpaperFilename, {create: false},
77 function(fe) { 77 function(fe) {
78 fe.remove(function() {}, null); 78 fe.remove(function() {}, null);
79 }, 79 },
80 function(e) { 80 function(e) {
81 // NotFoundError is expected under the following scenario: 81 // NotFoundError is expected under the following scenario:
82 // The user uses a same account on device A and device B. 82 // The user uses a same account on device A and device B.
83 // The current wallpaper is a third party wallpaper. Then 83 // The current wallpaper is a third party wallpaper. Then
84 // the user changes it to a ONLINE wallpaper on device A. 84 // the user changes it to a ONLINE wallpaper on device A.
85 // Sync file system change and local file system change 85 // Sync file system change and local file system change
86 // will then both be fired on device B, which makes the 86 // will then both be fired on device B, which makes the
87 // third party wallpaper be deleted twice from the sync 87 // third party wallpaper be deleted twice from the sync
88 // file system. We should ignore this error. 88 // file system. We should ignore this error.
89 if (e.name != 'NotFoundError') 89 if (e.name != 'NotFoundError')
90 WallpaperUtil.onFileSystemError(e); 90 WallpaperUtil.onFileSystemError(e);
91 }); 91 });
92 fs.root.getFile(thumbnailFilename, 92 fs.root.getFile(
93 {create: false}, 93 thumbnailFilename, {create: false},
94 function(fe) { 94 function(fe) {
95 fe.remove(function() {}, null); 95 fe.remove(function() {}, null);
96 }, 96 },
97 function(e) { 97 function(e) {
98 // Same as above. 98 // Same as above.
99 if (e.name != 'NotFoundError') 99 if (e.name != 'NotFoundError')
100 WallpaperUtil.onFileSystemError(e); 100 WallpaperUtil.onFileSystemError(e);
101 }); 101 });
102 }; 102 };
103 WallpaperUtil.requestSyncFS(success); 103 WallpaperUtil.requestSyncFS(success);
104 }; 104 };
105 105
106 /** 106 /**
107 * Executes callback after requesting the sync settings. 107 * Executes callback after requesting the sync settings.
108 * @param {function} callback The callback will be executed. 108 * @param {function} callback The callback will be executed.
109 */ 109 */
110 WallpaperUtil.enabledSyncThemesCallback = function(callback) { 110 WallpaperUtil.enabledSyncThemesCallback = function(callback) {
111 chrome.wallpaperPrivate.getSyncSetting(function(setting) { 111 chrome.wallpaperPrivate.getSyncSetting(function(setting) {
(...skipping 23 matching lines...) Expand all
135 135
136 /** 136 /**
137 * Request a Local Fs handle and run callback on it. 137 * Request a Local Fs handle and run callback on it.
138 * @param {function} callback The callback to execute after Local handler is 138 * @param {function} callback The callback to execute after Local handler is
139 * available. 139 * available.
140 */ 140 */
141 WallpaperUtil.requestLocalFS = function(callback) { 141 WallpaperUtil.requestLocalFS = function(callback) {
142 if (WallpaperUtil.webkitFs) { 142 if (WallpaperUtil.webkitFs) {
143 callback(WallpaperUtil.webkitFs); 143 callback(WallpaperUtil.webkitFs);
144 } else { 144 } else {
145 window.webkitRequestFileSystem(window.PERSISTENT, 1024 * 1024 * 100, 145 window.webkitRequestFileSystem(
146 function(fs) { 146 window.PERSISTENT, 1024 * 1024 * 100, function(fs) {
147 WallpaperUtil.webkitFs = fs; 147 WallpaperUtil.webkitFs = fs;
148 callback(fs); 148 callback(fs);
149 }); 149 });
150 } 150 }
151 }; 151 };
152 152
153 /** 153 /**
154 * Print error to console.error. 154 * Print error to console.error.
155 * @param {Event} e The error will be printed to console.error. 155 * @param {Event} e The error will be printed to console.error.
156 */ 156 */
157 // TODO(ranj): Handle different errors differently. 157 // TODO(ranj): Handle different errors differently.
158 WallpaperUtil.onFileSystemError = function(e) { 158 WallpaperUtil.onFileSystemError = function(e) {
159 console.error(e); 159 console.error(e);
(...skipping 13 matching lines...) Expand all
173 if (writeCallback) 173 if (writeCallback)
174 writeCallback(); 174 writeCallback();
175 }, WallpaperUtil.onFileSystemError); 175 }, WallpaperUtil.onFileSystemError);
176 }; 176 };
177 177
178 /** 178 /**
179 * Write jpeg/png file data into syncFileSystem. 179 * Write jpeg/png file data into syncFileSystem.
180 * @param {string} wallpaperFilename The filename that going to be writen. 180 * @param {string} wallpaperFilename The filename that going to be writen.
181 * @param {ArrayBuffer} wallpaperData Data for image file. 181 * @param {ArrayBuffer} wallpaperData Data for image file.
182 */ 182 */
183 WallpaperUtil.storeWallpaperToSyncFS = function(wallpaperFilename, 183 WallpaperUtil.storeWallpaperToSyncFS = function(
184 wallpaperData) { 184 wallpaperFilename, wallpaperData) {
185 var callback = function(fs) { 185 var callback = function(fs) {
186 fs.root.getFile(wallpaperFilename, 186 fs.root.getFile(
187 {create: false}, 187 wallpaperFilename, {create: false}, function() {}, // already exists
188 function() {}, // already exists 188 function(e) { // not exists, create
189 function(e) { // not exists, create 189 fs.root.getFile(wallpaperFilename, {create: true}, function(fe) {
190 fs.root.getFile(wallpaperFilename, {create: true}, 190 WallpaperUtil.writeFile(fe, wallpaperData);
191 function(fe) { 191 }, WallpaperUtil.onFileSystemError);
192 WallpaperUtil.writeFile( 192 });
193 fe, wallpaperData);
194 },
195 WallpaperUtil.onFileSystemError);
196 });
197 }; 193 };
198 WallpaperUtil.requestSyncFS(callback); 194 WallpaperUtil.requestSyncFS(callback);
199 }; 195 };
200 196
201 /** 197 /**
202 * Stores jpeg/png wallpaper into |localDir| in local file system. 198 * Stores jpeg/png wallpaper into |localDir| in local file system.
203 * @param {string} wallpaperFilename File name of wallpaper image. 199 * @param {string} wallpaperFilename File name of wallpaper image.
204 * @param {ArrayBuffer} wallpaperData The wallpaper data. 200 * @param {ArrayBuffer} wallpaperData The wallpaper data.
205 * @param {string} saveDir The path to store wallpaper in local file system. 201 * @param {string} saveDir The path to store wallpaper in local file system.
206 */ 202 */
207 WallpaperUtil.storeWallpaperToLocalFS = function(wallpaperFilename, 203 WallpaperUtil.storeWallpaperToLocalFS = function(
208 wallpaperData, saveDir) { 204 wallpaperFilename, wallpaperData, saveDir) {
209 if (!wallpaperData) { 205 if (!wallpaperData) {
210 console.error('wallpaperData is null'); 206 console.error('wallpaperData is null');
211 return; 207 return;
212 } 208 }
213 var getDirSuccess = function(dirEntry) { 209 var getDirSuccess = function(dirEntry) {
214 dirEntry.getFile(wallpaperFilename, 210 dirEntry.getFile(
215 {create: false}, 211 wallpaperFilename, {create: false}, function() {}, // already exists
216 function() {}, // already exists 212 function(e) { // not exists, create
217 function(e) { // not exists, create 213 dirEntry.getFile(wallpaperFilename, {create: true}, function(fe) {
218 dirEntry.getFile(wallpaperFilename, {create: true}, 214 WallpaperUtil.writeFile(fe, wallpaperData);
219 function(fe) { 215 }, WallpaperUtil.onFileSystemError);
220 WallpaperUtil.writeFile(fe, 216 });
221 wallpaperData);
222 },
223 WallpaperUtil.onFileSystemError);
224 });
225 }; 217 };
226 WallpaperUtil.requestLocalFS(function(fs) { 218 WallpaperUtil.requestLocalFS(function(fs) {
227 fs.root.getDirectory(saveDir, {create: true}, getDirSuccess, 219 fs.root.getDirectory(
228 WallpaperUtil.onFileSystemError); 220 saveDir, {create: true}, getDirSuccess,
221 WallpaperUtil.onFileSystemError);
229 }); 222 });
230 }; 223 };
231 224
232 /** 225 /**
233 * Sets wallpaper from synced file system. 226 * Sets wallpaper from synced file system.
234 * @param {string} wallpaperFilename File name used to set wallpaper. 227 * @param {string} wallpaperFilename File name used to set wallpaper.
235 * @param {string} wallpaperLayout Layout used to set wallpaper. 228 * @param {string} wallpaperLayout Layout used to set wallpaper.
236 * @param {function=} onSuccess Callback if set successfully. 229 * @param {function=} onSuccess Callback if set successfully.
237 */ 230 */
238 WallpaperUtil.setCustomWallpaperFromSyncFS = function( 231 WallpaperUtil.setCustomWallpaperFromSyncFS = function(
239 wallpaperFilename, wallpaperLayout, onSuccess) { 232 wallpaperFilename, wallpaperLayout, onSuccess) {
240 var setWallpaperFromSyncCallback = function(fs) { 233 var setWallpaperFromSyncCallback = function(fs) {
241 if (!wallpaperFilename) { 234 if (!wallpaperFilename) {
242 console.error('wallpaperFilename is not provided.'); 235 console.error('wallpaperFilename is not provided.');
243 return; 236 return;
244 } 237 }
245 if (!wallpaperLayout) 238 if (!wallpaperLayout)
246 wallpaperLayout = 'CENTER_CROPPED'; 239 wallpaperLayout = 'CENTER_CROPPED';
247 fs.root.getFile(wallpaperFilename, {create: false}, function(fileEntry) { 240 fs.root.getFile(
248 fileEntry.file(function(file) { 241 wallpaperFilename, {create: false},
249 var reader = new FileReader(); 242 function(fileEntry) {
250 reader.onloadend = function() { 243 fileEntry.file(function(file) {
251 chrome.wallpaperPrivate.setCustomWallpaper( 244 var reader = new FileReader();
252 reader.result, 245 reader.onloadend = function() {
253 wallpaperLayout, 246 chrome.wallpaperPrivate.setCustomWallpaper(
254 true, 247 reader.result, wallpaperLayout, true, wallpaperFilename,
255 wallpaperFilename, 248 function(thumbnailData) {
256 function(thumbnailData) { 249 // TODO(ranj): Ignore 'canceledWallpaper' error.
257 // TODO(ranj): Ignore 'canceledWallpaper' error. 250 if (chrome.runtime.lastError) {
258 if (chrome.runtime.lastError) { 251 console.error(chrome.runtime.lastError.message);
259 console.error(chrome.runtime.lastError.message); 252 return;
260 return; 253 }
261 } 254 if (onSuccess)
262 if (onSuccess) 255 onSuccess();
263 onSuccess(); 256 });
264 }); 257 };
265 }; 258 reader.readAsArrayBuffer(file);
266 reader.readAsArrayBuffer(file); 259 }, WallpaperUtil.onFileSystemError);
267 }, WallpaperUtil.onFileSystemError); 260 },
268 }, function(e) {} // fail to read file, expected due to download delay 261 function(e) {} // fail to read file, expected due to download delay
269 ); 262 );
270 }; 263 };
271 WallpaperUtil.requestSyncFS(setWallpaperFromSyncCallback); 264 WallpaperUtil.requestSyncFS(setWallpaperFromSyncCallback);
272 }; 265 };
273 266
274 /** 267 /**
275 * Saves value to local storage that associates with key. 268 * Saves value to local storage that associates with key.
276 * @param {string} key The key that associates with value. 269 * @param {string} key The key that associates with value.
277 * @param {string} value The value to save to local storage. 270 * @param {string} value The value to save to local storage.
278 * @param {function=} opt_callback The callback on success. 271 * @param {function=} opt_callback The callback on success.
279 */ 272 */
(...skipping 29 matching lines...) Expand all
309 * @param {string} appName The third party app name. If the current wallpaper is 302 * @param {string} appName The third party app name. If the current wallpaper is
310 * set by the built-in wallpaper picker, it is set to an empty string. 303 * set by the built-in wallpaper picker, it is set to an empty string.
311 */ 304 */
312 WallpaperUtil.saveWallpaperInfo = function(url, layout, source, appName) { 305 WallpaperUtil.saveWallpaperInfo = function(url, layout, source, appName) {
313 chrome.wallpaperPrivate.recordWallpaperUMA(source); 306 chrome.wallpaperPrivate.recordWallpaperUMA(source);
314 307
315 // In order to keep the wallpaper sync working across different versions, we 308 // In order to keep the wallpaper sync working across different versions, we
316 // have to revert DAILY/THIRDPARTY type wallpaper info to ONLINE/CUSTOM type 309 // have to revert DAILY/THIRDPARTY type wallpaper info to ONLINE/CUSTOM type
317 // after record the correct UMA stats. 310 // after record the correct UMA stats.
318 source = (source == Constants.WallpaperSourceEnum.Daily) ? 311 source = (source == Constants.WallpaperSourceEnum.Daily) ?
319 Constants.WallpaperSourceEnum.Online : source; 312 Constants.WallpaperSourceEnum.Online :
313 source;
320 source = (source == Constants.WallpaperSourceEnum.ThirdParty) ? 314 source = (source == Constants.WallpaperSourceEnum.ThirdParty) ?
321 Constants.WallpaperSourceEnum.Custom : source; 315 Constants.WallpaperSourceEnum.Custom :
316 source;
322 var wallpaperInfo = { 317 var wallpaperInfo = {
323 url: url, 318 url: url,
324 layout: layout, 319 layout: layout,
325 source: source, 320 source: source,
326 appName: appName, 321 appName: appName,
327 }; 322 };
328 WallpaperUtil.saveToLocalStorage(Constants.AccessLocalWallpaperInfoKey, 323 WallpaperUtil.saveToLocalStorage(
329 wallpaperInfo, function() { 324 Constants.AccessLocalWallpaperInfoKey, wallpaperInfo, function() {
330 WallpaperUtil.saveToSyncStorage(Constants.AccessSyncWallpaperInfoKey, 325 WallpaperUtil.saveToSyncStorage(
331 wallpaperInfo); 326 Constants.AccessSyncWallpaperInfoKey, wallpaperInfo);
332 }); 327 });
333 }; 328 };
334 329
335 /** 330 /**
336 * Downloads resources from url. Calls onSuccess and opt_onFailure accordingly. 331 * Downloads resources from url. Calls onSuccess and opt_onFailure accordingly.
337 * @param {string} url The url address where we should fetch resources. 332 * @param {string} url The url address where we should fetch resources.
338 * @param {string} type The response type of XMLHttprequest. 333 * @param {string} type The response type of XMLHttprequest.
339 * @param {function} onSuccess The success callback. It must be called with 334 * @param {function} onSuccess The success callback. It must be called with
340 * current XMLHttprequest object. 335 * current XMLHttprequest object.
341 * @param {function} onFailure The failure callback. 336 * @param {function} onFailure The failure callback.
342 * @param {XMLHttpRequest=} opt_xhr The XMLHttpRequest object. 337 * @param {XMLHttpRequest=} opt_xhr The XMLHttpRequest object.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 WallpaperUtil.setOnlineWallpaper = function(url, layout, onSuccess, onFailure) { 373 WallpaperUtil.setOnlineWallpaper = function(url, layout, onSuccess, onFailure) {
379 var self = this; 374 var self = this;
380 chrome.wallpaperPrivate.setWallpaperIfExists(url, layout, function(exists) { 375 chrome.wallpaperPrivate.setWallpaperIfExists(url, layout, function(exists) {
381 if (exists) { 376 if (exists) {
382 onSuccess(); 377 onSuccess();
383 return; 378 return;
384 } 379 }
385 380
386 self.fetchURL(url, 'arraybuffer', function(xhr) { 381 self.fetchURL(url, 'arraybuffer', function(xhr) {
387 if (xhr.response != null) { 382 if (xhr.response != null) {
388 chrome.wallpaperPrivate.setWallpaper(xhr.response, layout, url, 383 chrome.wallpaperPrivate.setWallpaper(
389 onSuccess); 384 xhr.response, layout, url, onSuccess);
390 } else { 385 } else {
391 onFailure(); 386 onFailure();
392 } 387 }
393 }, onFailure); 388 }, onFailure);
394 }); 389 });
395 }; 390 };
396 391
397 /** 392 /**
398 * Runs chrome.test.sendMessage in test environment. Does nothing if running 393 * Runs chrome.test.sendMessage in test environment. Does nothing if running
399 * in production environment. 394 * in production environment.
400 * 395 *
401 * @param {string} message Test message to send. 396 * @param {string} message Test message to send.
402 */ 397 */
403 WallpaperUtil.testSendMessage = function(message) { 398 WallpaperUtil.testSendMessage = function(message) {
404 var test = chrome.test || window.top.chrome.test; 399 var test = chrome.test || window.top.chrome.test;
405 if (test) 400 if (test)
406 test.sendMessage(message); 401 test.sendMessage(message);
407 }; 402 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698