OLD | NEW |
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 |
| 7 syncFs: null // syncFileSystem handler |
| 8 }; |
| 9 |
| 10 /** |
| 11 * Run experimental features. |
| 12 * @param {function} callback The callback will be executed if 'isExperimental' |
| 13 * flag is set to true. |
| 14 */ |
| 15 WallpaperUtil.enabledExperimentalFeatureCallback = function(callback) { |
| 16 if (WallpaperUtil.strings) { |
| 17 if (WallpaperUtil.strings.isExperimental) |
| 18 callback(); |
| 19 } else { |
| 20 chrome.wallpaperPrivate.getStrings(function(strings) { |
| 21 WallpaperUtil.strings = strings; |
| 22 if (WallpaperUtil.strings.isExperimental) |
| 23 callback(); |
| 24 }); |
| 25 } |
| 26 }; |
| 27 |
| 28 /** |
| 29 * Request a syncFileSystem handle and run callback on it. |
| 30 * @param {function} callback The callback to execute after syncFileSystem |
| 31 * handler is available. |
| 32 */ |
| 33 WallpaperUtil.requestSyncFs = function(callback) { |
| 34 if (WallpaperUtil.syncFs) { |
| 35 callback(WallpaperUtil.syncFs); |
| 36 } else { |
| 37 chrome.syncFileSystem.requestFileSystem(function(fs) { |
| 38 WallpaperUtil.syncFs = fs; |
| 39 callback(WallpaperUtil.syncFs); |
| 40 }); |
| 41 } |
| 42 }; |
| 43 |
| 44 /** |
| 45 * Print error to console.error. |
| 46 * @param {Event} e The error will be printed to console.error. |
| 47 */ |
| 48 // TODO(ranj): Handle different errors differently. |
| 49 WallpaperUtil.onFileSystemError = function(e) { |
| 50 console.error(e); |
| 51 }; |
| 52 |
| 53 /** |
| 54 * Write jpeg/png file data into file entry. |
| 55 * @param {FileEntry} fileEntry The file entry that going to be writen. |
| 56 * @param {ArrayBuffer} wallpaperData Data for image file. |
| 57 * @param {function=} writeCallback The callback that will be executed after |
| 58 * writing data. |
| 59 */ |
| 60 WallpaperUtil.writeFile = function(fileEntry, wallpaperData, writeCallback) { |
| 61 var uint8arr = new Uint8Array(wallpaperData); |
| 62 fileEntry.createWriter(function(fileWriter) { |
| 63 var blob = new Blob([new Int8Array(wallpaperData)]); |
| 64 fileWriter.write(blob); |
| 65 if (writeCallback) |
| 66 writeCallback(); |
| 67 }, WallpaperUtil.onFileSystemError); |
| 68 }; |
| 69 |
| 70 /** |
| 71 * Write jpeg/png file data into syncFileSystem. |
| 72 * @param {string} wallpaperFilename The filename that going to be writen. |
| 73 * @param {ArrayBuffer} wallpaperData Data for image file. |
| 74 * @param {function} onSuccess The callback that will be executed after. |
| 75 * writing data |
| 76 */ |
| 77 WallpaperUtil.storePictureToSyncFileSystem = function( |
| 78 wallpaperFilename, wallpaperData, onSuccess) { |
| 79 var callback = function(fs) { |
| 80 fs.root.getFile(wallpaperFilename, |
| 81 {create: false}, |
| 82 function() { onSuccess();}, // already exists |
| 83 function(e) { // not exists, create |
| 84 fs.root.getFile(wallpaperFilename, {create: true}, |
| 85 function(fe) { |
| 86 WallpaperUtil.writeFile( |
| 87 fe, wallpaperData, onSuccess); |
| 88 }, |
| 89 WallpaperUtil.onFileSystemError); |
| 90 }); |
| 91 }; |
| 92 WallpaperUtil.requestSyncFs(callback); |
| 93 }; |
| 94 |
| 95 /** |
| 96 * Stores jpeg/png wallpaper into |localDir| in local file system. |
| 97 * @param {string} wallpaperFilename File name of wallpaper image. |
| 98 * @param {ArrayBuffer} wallpaperData The wallpaper data. |
| 99 * @param {string} saveDir The path to store wallpaper in local file system. |
| 100 */ |
| 101 WallpaperUtil.storePictureToLocal = function(wallpaperFilename, wallpaperData, |
| 102 saveDir) { |
| 103 if (!wallpaperData) { |
| 104 console.error('wallpaperData is null'); |
| 105 return; |
| 106 } |
| 107 var getDirSuccess = function(dirEntry) { |
| 108 dirEntry.getFile(wallpaperFilename, |
| 109 {create: false}, |
| 110 function() {}, // already exists |
| 111 function(e) { // not exists, create |
| 112 dirEntry.getFile(wallpaperFilename, {create: true}, |
| 113 function(fe) { |
| 114 WallpaperUtil.writeFile(fe, |
| 115 wallpaperData); |
| 116 }, |
| 117 WallpaperUtil.onFileSystemError); |
| 118 }); |
| 119 }; |
| 120 window.webkitRequestFileSystem(window.PERSISTENT, 1024 * 1024 * 100, |
| 121 function(fs) { |
| 122 fs.root.getDirectory( |
| 123 saveDir, {create: true}, getDirSuccess, |
| 124 WallpaperUtil.onFileSystemError); |
| 125 }, |
| 126 WallpaperUtil.onFileSystemError); |
| 127 }; |
| 128 |
| 129 /** |
| 130 * Sets wallpaper from synced file system. |
| 131 * @param {string} wallpaperFilename File name used to set wallpaper. |
| 132 * @param {string} wallpaperLayout Layout used to set wallpaper. |
| 133 * @param {function=} onSuccess Callback if set successfully. |
| 134 */ |
| 135 WallpaperUtil.setSyncCustomWallpaper = function( |
| 136 wallpaperFilename, wallpaperLayout, onSuccess) { |
| 137 var setWallpaperFromSyncCallback = function(fs) { |
| 138 if (!wallpaperFilename) { |
| 139 console.error('wallpaperFilename is not provided.'); |
| 140 return; |
| 141 } |
| 142 if (!wallpaperLayout) |
| 143 wallpaperLayout = 'CENTER_CROPPED'; |
| 144 fs.root.getFile(wallpaperFilename, {create: false}, function(fileEntry) { |
| 145 fileEntry.file(function(file) { |
| 146 var reader = new FileReader(); |
| 147 reader.onloadend = function() { |
| 148 chrome.wallpaperPrivate.setCustomWallpaper( |
| 149 reader.result, |
| 150 wallpaperLayout, |
| 151 true, |
| 152 wallpaperFilename, |
| 153 function(thumbnailData) { |
| 154 // TODO(ranj): Ignore 'canceledWallpaper' error. |
| 155 if (chrome.runtime.lastError) { |
| 156 console.error(chrome.runtime.lastError.message); |
| 157 return; |
| 158 } |
| 159 WallpaperUtil.storePictureToLocal(wallpaperFilename, |
| 160 reader.result, 'original'); |
| 161 WallpaperUtil.storePictureToLocal(wallpaperFilename, |
| 162 reader.result, 'thumbnail'); |
| 163 if (onSuccess) |
| 164 onSuccess(); |
| 165 }); |
| 166 }; |
| 167 reader.readAsArrayBuffer(file); |
| 168 }, WallpaperUtil.onFileSystemError); |
| 169 }, function(e) {} // fail to read file, expected due to download delay |
| 170 ); |
| 171 }; |
| 172 WallpaperUtil.requestSyncFs(setWallpaperFromSyncCallback); |
| 173 }; |
6 | 174 |
7 /** | 175 /** |
8 * Saves value to local storage that associates with key. | 176 * Saves value to local storage that associates with key. |
9 * @param {string} key The key that associates with value. | 177 * @param {string} key The key that associates with value. |
10 * @param {string} value The value to save to local storage. | 178 * @param {string} value The value to save to local storage. |
11 * @param {boolen} sync True if the value is saved to sync storage. | 179 * @param {boolen} sync True if the value is saved to sync storage. |
12 * @param {function=} opt_callback The callback on success, or on failure. | 180 * @param {function=} opt_callback The callback on success, or on failure. |
13 */ | 181 */ |
14 WallpaperUtil.saveToStorage = function(key, value, sync, opt_callback) { | 182 WallpaperUtil.saveToStorage = function(key, value, sync, opt_callback) { |
15 var items = {}; | 183 var items = {}; |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 chrome.wallpaperPrivate.setWallpaper(xhr.response, layout, url, | 265 chrome.wallpaperPrivate.setWallpaper(xhr.response, layout, url, |
98 onSuccess); | 266 onSuccess); |
99 self.saveWallpaperInfo(url, layout, | 267 self.saveWallpaperInfo(url, layout, |
100 Constants.WallpaperSourceEnum.Online); | 268 Constants.WallpaperSourceEnum.Online); |
101 } else { | 269 } else { |
102 onFailure(); | 270 onFailure(); |
103 } | 271 } |
104 }, onFailure); | 272 }, onFailure); |
105 }); | 273 }); |
106 }; | 274 }; |
OLD | NEW |