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
3 // found in the LICENSE file.
6 strings
: null, // Object that contains all the flags
7 syncFs
: null, // syncFileSystem handler
8 webkitFs
: null // webkitFileSystem handler
12 * Deletes |wallpaperFileName| and its associated thumbnail from local FS.
13 * @param {string} wallpaperFilename Name of the file that will be deleted
15 WallpaperUtil
.deleteWallpaperFromLocalFS = function(wallpaperFilename
) {
16 WallpaperUtil
.requestLocalFS(function(fs
) {
17 var originalPath
= Constants
.WallpaperDirNameEnum
.ORIGINAL
+ '/' +
19 var thumbnailPath
= Constants
.WallpaperDirNameEnum
.THUMBNAIL
+ '/' +
21 fs
.root
.getFile(originalPath
,
24 fe
.remove(function() {}, null);
26 // NotFoundError is expected. After we receive a delete
27 // event from either original wallpaper or wallpaper
28 // thumbnail, we delete both of them in local FS to achieve
29 // a faster synchronization. So each file is expected to be
30 // deleted twice and the second attempt is a noop.
32 if (e
.name
!= 'NotFoundError')
33 WallpaperUtil
.onFileSystemError(e
);
35 fs
.root
.getFile(thumbnailPath
,
38 fe
.remove(function() {}, null);
41 if (e
.name
!= 'NotFoundError')
42 WallpaperUtil
.onFileSystemError(e
);
48 * Loads a wallpaper from sync file system and saves it and its thumbnail to
50 * @param {string} wallpaperFileEntry File name of wallpaper image.
52 WallpaperUtil
.storeWallpaperFromSyncFSToLocalFS = function(wallpaperFileEntry
) {
53 var filenName
= wallpaperFileEntry
.name
;
54 var storeDir
= Constants
.WallpaperDirNameEnum
.ORIGINAL
;
55 if (filenName
.indexOf(Constants
.CustomWallpaperThumbnailSuffix
) != -1)
56 storeDir
= Constants
.WallpaperDirNameEnum
.THUMBNAIL
;
57 filenName
= filenName
.replace(Constants
.CustomWallpaperThumbnailSuffix
, '');
58 wallpaperFileEntry
.file(function(file
) {
59 var reader
= new FileReader();
60 reader
.onloadend = function() {
61 WallpaperUtil
.storeWallpaperToLocalFS(filenName
, reader
.result
, storeDir
);
63 reader
.readAsArrayBuffer(file
);
64 }, WallpaperUtil
.onFileSystemError
);
68 * Deletes |wallpaperFileName| and its associated thumbnail from syncFileSystem.
69 * @param {string} wallpaperFilename Name of the file that will be deleted.
71 WallpaperUtil
.deleteWallpaperFromSyncFS = function(wallpaperFilename
) {
72 var thumbnailFilename
= wallpaperFilename
+
73 Constants
.CustomWallpaperThumbnailSuffix
;
74 var success = function(fs
) {
75 fs
.root
.getFile(wallpaperFilename
,
78 fe
.remove(function() {}, null);
80 WallpaperUtil
.onFileSystemError
);
81 fs
.root
.getFile(thumbnailFilename
,
84 fe
.remove(function() {}, null);
86 WallpaperUtil
.onFileSystemError
);
88 WallpaperUtil
.requestSyncFS(success
);
92 * Executes callback after requesting the sync settings.
93 * @param {function} callback The callback will be executed.
95 WallpaperUtil
.enabledSyncThemesCallback = function(callback
) {
96 chrome
.wallpaperPrivate
.getSyncSetting(function(setting
) {
97 callback(setting
.syncThemes
);
102 * Request a syncFileSystem handle and run callback on it.
103 * @param {function} callback The callback to execute after syncFileSystem
104 * handler is available.
106 WallpaperUtil
.requestSyncFS = function(callback
) {
107 WallpaperUtil
.enabledSyncThemesCallback(function(syncEnabled
) {
110 if (WallpaperUtil
.syncFs
) {
111 callback(WallpaperUtil
.syncFs
);
113 chrome
.syncFileSystem
.requestFileSystem(function(fs
) {
114 WallpaperUtil
.syncFs
= fs
;
115 callback(WallpaperUtil
.syncFs
);
122 * Request a Local Fs handle and run callback on it.
123 * @param {function} callback The callback to execute after Local handler is
126 WallpaperUtil
.requestLocalFS = function(callback
) {
127 if (WallpaperUtil
.webkitFs
) {
128 callback(WallpaperUtil
.webkitFs
);
130 window
.webkitRequestFileSystem(window
.PERSISTENT
, 1024 * 1024 * 100,
132 WallpaperUtil
.webkitFs
= fs
;
139 * Print error to console.error.
140 * @param {Event} e The error will be printed to console.error.
142 // TODO(ranj): Handle different errors differently.
143 WallpaperUtil
.onFileSystemError = function(e
) {
148 * Write jpeg/png file data into file entry.
149 * @param {FileEntry} fileEntry The file entry that going to be writen.
150 * @param {ArrayBuffer} wallpaperData Data for image file.
151 * @param {function=} writeCallback The callback that will be executed after
154 WallpaperUtil
.writeFile = function(fileEntry
, wallpaperData
, writeCallback
) {
155 fileEntry
.createWriter(function(fileWriter
) {
156 var blob
= new Blob([new Int8Array(wallpaperData
)]);
157 fileWriter
.write(blob
);
160 }, WallpaperUtil
.onFileSystemError
);
164 * Write jpeg/png file data into syncFileSystem.
165 * @param {string} wallpaperFilename The filename that going to be writen.
166 * @param {ArrayBuffer} wallpaperData Data for image file.
168 WallpaperUtil
.storeWallpaperToSyncFS = function(wallpaperFilename
,
170 var callback = function(fs
) {
171 fs
.root
.getFile(wallpaperFilename
,
173 function() {}, // already exists
174 function(e
) { // not exists, create
175 fs
.root
.getFile(wallpaperFilename
, {create
: true},
177 WallpaperUtil
.writeFile(
180 WallpaperUtil
.onFileSystemError
);
183 WallpaperUtil
.requestSyncFS(callback
);
187 * Stores jpeg/png wallpaper into |localDir| in local file system.
188 * @param {string} wallpaperFilename File name of wallpaper image.
189 * @param {ArrayBuffer} wallpaperData The wallpaper data.
190 * @param {string} saveDir The path to store wallpaper in local file system.
192 WallpaperUtil
.storeWallpaperToLocalFS = function(wallpaperFilename
,
193 wallpaperData
, saveDir
) {
194 if (!wallpaperData
) {
195 console
.error('wallpaperData is null');
198 var getDirSuccess = function(dirEntry
) {
199 dirEntry
.getFile(wallpaperFilename
,
201 function() {}, // already exists
202 function(e
) { // not exists, create
203 dirEntry
.getFile(wallpaperFilename
, {create
: true},
205 WallpaperUtil
.writeFile(fe
,
208 WallpaperUtil
.onFileSystemError
);
211 WallpaperUtil
.requestLocalFS(function(fs
) {
212 fs
.root
.getDirectory(saveDir
, {create
: true}, getDirSuccess
,
213 WallpaperUtil
.onFileSystemError
);
218 * Sets wallpaper from synced file system.
219 * @param {string} wallpaperFilename File name used to set wallpaper.
220 * @param {string} wallpaperLayout Layout used to set wallpaper.
221 * @param {function=} onSuccess Callback if set successfully.
223 WallpaperUtil
.setCustomWallpaperFromSyncFS = function(
224 wallpaperFilename
, wallpaperLayout
, onSuccess
) {
225 var setWallpaperFromSyncCallback = function(fs
) {
226 if (!wallpaperFilename
) {
227 console
.error('wallpaperFilename is not provided.');
230 if (!wallpaperLayout
)
231 wallpaperLayout
= 'CENTER_CROPPED';
232 fs
.root
.getFile(wallpaperFilename
, {create
: false}, function(fileEntry
) {
233 fileEntry
.file(function(file
) {
234 var reader
= new FileReader();
235 reader
.onloadend = function() {
236 chrome
.wallpaperPrivate
.setCustomWallpaper(
241 function(thumbnailData
) {
242 // TODO(ranj): Ignore 'canceledWallpaper' error.
243 if (chrome
.runtime
.lastError
) {
244 console
.error(chrome
.runtime
.lastError
.message
);
247 WallpaperUtil
.storeWallpaperToLocalFS(wallpaperFilename
,
248 reader
.result
, Constants
.WallpaperDirNameEnum
.ORIGINAL
);
249 WallpaperUtil
.storeWallpaperToLocalFS(wallpaperFilename
,
250 reader
.result
, Constants
.WallpaperDirNameEnum
.THUMBNAIL
);
255 reader
.readAsArrayBuffer(file
);
256 }, WallpaperUtil
.onFileSystemError
);
257 }, function(e
) {} // fail to read file, expected due to download delay
260 WallpaperUtil
.requestSyncFS(setWallpaperFromSyncCallback
);
264 * Saves value to local storage that associates with key.
265 * @param {string} key The key that associates with value.
266 * @param {string} value The value to save to local storage.
267 * @param {function=} opt_callback The callback on success.
269 WallpaperUtil
.saveToLocalStorage = function(key
, value
, opt_callback
) {
272 Constants
.WallpaperLocalStorage
.set(items
, opt_callback
);
276 * Saves value to sync storage that associates with key if sync theme is
278 * @param {string} key The key that associates with value.
279 * @param {string} value The value to save to sync storage.
280 * @param {function=} opt_callback The callback on success.
282 WallpaperUtil
.saveToSyncStorage = function(key
, value
, opt_callback
) {
285 WallpaperUtil
.enabledSyncThemesCallback(function(syncEnabled
) {
287 Constants
.WallpaperSyncStorage
.set(items
, opt_callback
);
292 * Saves user's wallpaper infomation to local and sync storage. Note that local
293 * value should be saved first.
294 * @param {string} url The url address of wallpaper. For custom wallpaper, it is
296 * @param {string} layout The wallpaper layout.
297 * @param {string} source The wallpaper source.
299 WallpaperUtil
.saveWallpaperInfo = function(url
, layout
, source
) {
300 var wallpaperInfo
= {
305 WallpaperUtil
.saveToLocalStorage(Constants
.AccessLocalWallpaperInfoKey
,
306 wallpaperInfo
, function() {
307 WallpaperUtil
.saveToSyncStorage(Constants
.AccessSyncWallpaperInfoKey
,
313 * Downloads resources from url. Calls onSuccess and opt_onFailure accordingly.
314 * @param {string} url The url address where we should fetch resources.
315 * @param {string} type The response type of XMLHttprequest.
316 * @param {function} onSuccess The success callback. It must be called with
317 * current XMLHttprequest object.
318 * @param {function} onFailure The failure callback.
319 * @param {XMLHttpRequest=} opt_xhr The XMLHttpRequest object.
321 WallpaperUtil
.fetchURL = function(url
, type
, onSuccess
, onFailure
, opt_xhr
) {
326 xhr
= new XMLHttpRequest();
329 // Do not use loadend here to handle both success and failure case. It gets
330 // complicated with abortion. Unexpected error message may show up. See
331 // http://crbug.com/242581.
332 xhr
.addEventListener('load', function(e
) {
333 if (this.status
== 200) {
336 onFailure(this.status
);
339 xhr
.addEventListener('error', onFailure
);
340 xhr
.open('GET', url
, true);
341 xhr
.responseType
= type
;
349 * Sets wallpaper to online wallpaper specified by url and layout
350 * @param {string} url The url address where we should fetch resources.
351 * @param {string} layout The layout of online wallpaper.
352 * @param {function} onSuccess The success callback.
353 * @param {function} onFailure The failure callback.
355 WallpaperUtil
.setOnlineWallpaper = function(url
, layout
, onSuccess
, onFailure
) {
357 chrome
.wallpaperPrivate
.setWallpaperIfExists(url
, layout
, function(exists
) {
363 self
.fetchURL(url
, 'arraybuffer', function(xhr
) {
364 if (xhr
.response
!= null) {
365 chrome
.wallpaperPrivate
.setWallpaper(xhr
.response
, layout
, url
,
367 self
.saveWallpaperInfo(url
, layout
,
368 Constants
.WallpaperSourceEnum
.Online
);
377 * Runs chrome.test.sendMessage in test environment. Does nothing if running
378 * in production environment.
380 * @param {string} message Test message to send.
382 WallpaperUtil
.testSendMessage = function(message
) {
383 var test
= chrome
.test
|| window
.top
.chrome
.test
;
385 test
.sendMessage(message
);