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 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_WALLPAPER_PRIVATE_API_H_ 6 #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_WALLPAPER_PRIVATE_API_H_ 7 8 #include "base/threading/sequenced_worker_pool.h" 9 #include "chrome/browser/chromeos/extensions/wallpaper_function_base.h" 10 #include "chrome/common/extensions/api/wallpaper_private.h" 11 #include "net/url_request/url_fetcher_delegate.h" 12 13 namespace chromeos { 14 class UserImage; 15 } // namespace chromeos 16 17 // Wallpaper manager strings. 18 class WallpaperPrivateGetStringsFunction : public SyncExtensionFunction { 19 public: 20 DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.getStrings", 21 WALLPAPERPRIVATE_GETSTRINGS) 22 23 protected: ~WallpaperPrivateGetStringsFunction()24 virtual ~WallpaperPrivateGetStringsFunction() {} 25 26 // SyncExtensionFunction overrides. 27 virtual bool RunSync() OVERRIDE; 28 }; 29 30 class WallpaperPrivateSetWallpaperIfExistsFunction 31 : public WallpaperFunctionBase { 32 public: 33 DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setWallpaperIfExists", 34 WALLPAPERPRIVATE_SETWALLPAPERIFEXISTS) 35 36 WallpaperPrivateSetWallpaperIfExistsFunction(); 37 38 protected: 39 virtual ~WallpaperPrivateSetWallpaperIfExistsFunction(); 40 41 // AsyncExtensionFunction overrides. 42 virtual bool RunAsync() OVERRIDE; 43 44 private: 45 virtual void OnWallpaperDecoded(const gfx::ImageSkia& image) OVERRIDE; 46 47 // File doesn't exist. Sets javascript callback parameter to false. 48 void OnFileNotExists(const std::string& error); 49 50 // Reads file specified by |file_path|. If success, post a task to start 51 // decoding the file. 52 void ReadFileAndInitiateStartDecode(const base::FilePath& file_path, 53 const base::FilePath& fallback_path); 54 55 scoped_ptr<extensions::api::wallpaper_private::SetWallpaperIfExists::Params> 56 params; 57 58 // User id of the active user when this api is been called. 59 std::string user_id_; 60 61 // Sequence token associated with wallpaper operations. Shared with 62 // WallpaperManager. 63 base::SequencedWorkerPool::SequenceToken sequence_token_; 64 }; 65 66 class WallpaperPrivateSetWallpaperFunction : public WallpaperFunctionBase { 67 public: 68 DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setWallpaper", 69 WALLPAPERPRIVATE_SETWALLPAPER) 70 71 WallpaperPrivateSetWallpaperFunction(); 72 73 protected: 74 virtual ~WallpaperPrivateSetWallpaperFunction(); 75 76 // AsyncExtensionFunction overrides. 77 virtual bool RunAsync() OVERRIDE; 78 79 private: 80 virtual void OnWallpaperDecoded(const gfx::ImageSkia& image) OVERRIDE; 81 82 // Saves the image data to a file. 83 void SaveToFile(); 84 85 // Sets wallpaper to the decoded image. 86 void SetDecodedWallpaper(scoped_ptr<gfx::ImageSkia> image); 87 88 scoped_ptr<extensions::api::wallpaper_private::SetWallpaper::Params> params; 89 90 // The decoded wallpaper. It may accessed from UI thread to set wallpaper or 91 // FILE thread to resize and save wallpaper to disk. 92 gfx::ImageSkia wallpaper_; 93 94 // User id of the active user when this api is been called. 95 std::string user_id_; 96 97 // Sequence token associated with wallpaper operations. Shared with 98 // WallpaperManager. 99 base::SequencedWorkerPool::SequenceToken sequence_token_; 100 }; 101 102 class WallpaperPrivateResetWallpaperFunction 103 : public AsyncExtensionFunction { 104 public: 105 DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.resetWallpaper", 106 WALLPAPERPRIVATE_RESETWALLPAPER) 107 108 WallpaperPrivateResetWallpaperFunction(); 109 110 protected: 111 virtual ~WallpaperPrivateResetWallpaperFunction(); 112 113 // AsyncExtensionFunction overrides. 114 virtual bool RunAsync() OVERRIDE; 115 }; 116 117 class WallpaperPrivateSetCustomWallpaperFunction 118 : public WallpaperFunctionBase { 119 public: 120 DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setCustomWallpaper", 121 WALLPAPERPRIVATE_SETCUSTOMWALLPAPER) 122 123 WallpaperPrivateSetCustomWallpaperFunction(); 124 125 protected: 126 virtual ~WallpaperPrivateSetCustomWallpaperFunction(); 127 128 // AsyncExtensionFunction overrides. 129 virtual bool RunAsync() OVERRIDE; 130 131 private: 132 virtual void OnWallpaperDecoded(const gfx::ImageSkia& wallpaper) OVERRIDE; 133 134 // Generates thumbnail of custom wallpaper. A simple STRETCH is used for 135 // generating thunbail. 136 void GenerateThumbnail(const base::FilePath& thumbnail_path, 137 scoped_ptr<gfx::ImageSkia> image); 138 139 // Thumbnail is ready. Calls api function javascript callback. 140 void ThumbnailGenerated(base::RefCountedBytes* data); 141 142 scoped_ptr<extensions::api::wallpaper_private::SetCustomWallpaper::Params> 143 params; 144 145 // User id of the active user when this api is been called. 146 std::string user_id_; 147 148 // User id hash of the logged in user. 149 std::string user_id_hash_; 150 151 // Sequence token associated with wallpaper operations. Shared with 152 // WallpaperManager. 153 base::SequencedWorkerPool::SequenceToken sequence_token_; 154 }; 155 156 class WallpaperPrivateSetCustomWallpaperLayoutFunction 157 : public AsyncExtensionFunction { 158 public: 159 DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setCustomWallpaperLayout", 160 WALLPAPERPRIVATE_SETCUSTOMWALLPAPERLAYOUT) 161 162 WallpaperPrivateSetCustomWallpaperLayoutFunction(); 163 164 protected: 165 virtual ~WallpaperPrivateSetCustomWallpaperLayoutFunction(); 166 167 // AsyncExtensionFunction overrides. 168 virtual bool RunAsync() OVERRIDE; 169 }; 170 171 class WallpaperPrivateMinimizeInactiveWindowsFunction 172 : public AsyncExtensionFunction { 173 public: 174 DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.minimizeInactiveWindows", 175 WALLPAPERPRIVATE_MINIMIZEINACTIVEWINDOWS) 176 177 WallpaperPrivateMinimizeInactiveWindowsFunction(); 178 179 protected: 180 virtual ~WallpaperPrivateMinimizeInactiveWindowsFunction(); 181 182 // AsyncExtensionFunction overrides. 183 virtual bool RunAsync() OVERRIDE; 184 }; 185 186 class WallpaperPrivateRestoreMinimizedWindowsFunction 187 : public AsyncExtensionFunction { 188 public: 189 DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.restoreMinimizedWindows", 190 WALLPAPERPRIVATE_RESTOREMINIMIZEDWINDOWS) 191 192 WallpaperPrivateRestoreMinimizedWindowsFunction(); 193 194 protected: 195 virtual ~WallpaperPrivateRestoreMinimizedWindowsFunction(); 196 197 // AsyncExtensionFunction overrides. 198 virtual bool RunAsync() OVERRIDE; 199 }; 200 201 class WallpaperPrivateGetThumbnailFunction : public AsyncExtensionFunction { 202 public: 203 DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.getThumbnail", 204 WALLPAPERPRIVATE_GETTHUMBNAIL) 205 206 WallpaperPrivateGetThumbnailFunction(); 207 208 protected: 209 virtual ~WallpaperPrivateGetThumbnailFunction(); 210 211 // AsyncExtensionFunction overrides. 212 virtual bool RunAsync() OVERRIDE; 213 214 private: 215 // Failed to get thumbnail for |file_name|. 216 void Failure(const std::string& file_name); 217 218 // Returns true to suppress javascript console error. Called when the 219 // requested thumbnail is not found or corrupted in thumbnail directory. 220 void FileNotLoaded(); 221 222 // Sets data field to the loaded thumbnail binary data in the results. Called 223 // when requested wallpaper thumbnail loaded successfully. 224 void FileLoaded(const std::string& data); 225 226 // Gets thumbnail from |path|. If |path| does not exist, call FileNotLoaded(). 227 void Get(const base::FilePath& path); 228 229 // Sequence token associated with wallpaper operations. Shared with 230 // WallpaperManager. 231 base::SequencedWorkerPool::SequenceToken sequence_token_; 232 }; 233 234 class WallpaperPrivateSaveThumbnailFunction : public AsyncExtensionFunction { 235 public: 236 DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.saveThumbnail", 237 WALLPAPERPRIVATE_SAVETHUMBNAIL) 238 239 WallpaperPrivateSaveThumbnailFunction(); 240 241 protected: 242 virtual ~WallpaperPrivateSaveThumbnailFunction(); 243 244 // AsyncExtensionFunction overrides. 245 virtual bool RunAsync() OVERRIDE; 246 247 private: 248 // Failed to save thumbnail for |file_name|. 249 void Failure(const std::string& file_name); 250 251 // Saved thumbnail to thumbnail directory. 252 void Success(); 253 254 // Saves thumbnail to thumbnail directory as |file_name|. 255 void Save(const std::string& data, const std::string& file_name); 256 257 // Sequence token associated with wallpaper operations. Shared with 258 // WallpaperManager. 259 base::SequencedWorkerPool::SequenceToken sequence_token_; 260 }; 261 262 class WallpaperPrivateGetOfflineWallpaperListFunction 263 : public AsyncExtensionFunction { 264 public: 265 DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.getOfflineWallpaperList", 266 WALLPAPERPRIVATE_GETOFFLINEWALLPAPERLIST) 267 WallpaperPrivateGetOfflineWallpaperListFunction(); 268 269 protected: 270 virtual ~WallpaperPrivateGetOfflineWallpaperListFunction(); 271 272 // AsyncExtensionFunction overrides. 273 virtual bool RunAsync() OVERRIDE; 274 275 private: 276 // Enumerates the list of files in online wallpaper directory. 277 void GetList(); 278 279 // Sends the list of files to extension api caller. If no files or no 280 // directory, sends empty list. 281 void OnComplete(const std::vector<std::string>& file_list); 282 283 // Sequence token associated with wallpaper operations. Shared with 284 // WallpaperManager. 285 base::SequencedWorkerPool::SequenceToken sequence_token_; 286 }; 287 288 #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_WALLPAPER_PRIVATE_API_H_ 289