// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef CHROME_BROWSER_CHROMEOS_EXTENSIONS_WALLPAPER_PRIVATE_API_H_ #define CHROME_BROWSER_CHROMEOS_EXTENSIONS_WALLPAPER_PRIVATE_API_H_ #include "ash/desktop_background/desktop_background_controller.h" #include "base/threading/sequenced_worker_pool.h" #include "chrome/browser/chromeos/login/user.h" #include "chrome/browser/extensions/extension_function.h" #include "net/url_request/url_fetcher_delegate.h" #include "ui/gfx/image/image_skia.h" namespace chromeos { class UserImage; } // Wallpaper manager strings. class WallpaperPrivateGetStringsFunction : public SyncExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.getStrings", WALLPAPERPRIVATE_GETSTRINGS) protected: virtual ~WallpaperPrivateGetStringsFunction() {} // SyncExtensionFunction overrides. virtual bool RunImpl() OVERRIDE; }; // Wallpaper manager function base. It contains a JPEG decoder to decode // wallpaper data. class WallpaperFunctionBase : public AsyncExtensionFunction { public: WallpaperFunctionBase(); protected: virtual ~WallpaperFunctionBase(); // A class to decode JPEG file. class WallpaperDecoder; // Holds an instance of WallpaperDecoder. static WallpaperDecoder* wallpaper_decoder_; // Starts to decode |data|. Must run on UI thread. void StartDecode(const std::string& data); // Handles failure or cancel cases. Passes error message to Javascript side. void OnFailureOrCancel(const std::string& error); private: virtual void OnWallpaperDecoded(const gfx::ImageSkia& wallpaper) = 0; }; class WallpaperPrivateSetWallpaperIfExistsFunction : public WallpaperFunctionBase { public: DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setWallpaperIfExists", WALLPAPERPRIVATE_SETWALLPAPERIFEXISTS) WallpaperPrivateSetWallpaperIfExistsFunction(); protected: virtual ~WallpaperPrivateSetWallpaperIfExistsFunction(); // AsyncExtensionFunction overrides. virtual bool RunImpl() OVERRIDE; private: virtual void OnWallpaperDecoded(const gfx::ImageSkia& wallpaper) OVERRIDE; // File doesn't exist. Sets javascript callback parameter to false. void OnFileNotExists(const std::string& error); // Reads file specified by |file_path|. If success, post a task to start // decoding the file. void ReadFileAndInitiateStartDecode(const base::FilePath& file_path, const base::FilePath& fallback_path); // Online wallpaper URL or file name of custom wallpaper. std::string urlOrFile_; // Layout of the loaded wallpaper. ash::WallpaperLayout layout_; // Type of the loaded wallpaper. chromeos::User::WallpaperType type_; // Sequence token associated with wallpaper operations. Shared with // WallpaperManager. base::SequencedWorkerPool::SequenceToken sequence_token_; }; class WallpaperPrivateSetWallpaperFunction : public WallpaperFunctionBase { public: DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setWallpaper", WALLPAPERPRIVATE_SETWALLPAPER) WallpaperPrivateSetWallpaperFunction(); protected: virtual ~WallpaperPrivateSetWallpaperFunction(); // AsyncExtensionFunction overrides. virtual bool RunImpl() OVERRIDE; private: virtual void OnWallpaperDecoded(const gfx::ImageSkia& wallpaper) OVERRIDE; // Saves the image data to a file. void SaveToFile(); // Sets wallpaper to the decoded image. void SetDecodedWallpaper(scoped_ptr wallpaper); // Layout of the downloaded wallpaper. ash::WallpaperLayout layout_; // The decoded wallpaper. It may accessed from UI thread to set wallpaper or // FILE thread to resize and save wallpaper to disk. gfx::ImageSkia wallpaper_; // Email address of logged in user. std::string email_; // High resolution wallpaper URL. std::string url_; // String representation of downloaded wallpaper. std::string image_data_; // Sequence token associated with wallpaper operations. Shared with // WallpaperManager. base::SequencedWorkerPool::SequenceToken sequence_token_; }; class WallpaperPrivateResetWallpaperFunction : public AsyncExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.resetWallpaper", WALLPAPERPRIVATE_RESETWALLPAPER) WallpaperPrivateResetWallpaperFunction(); protected: virtual ~WallpaperPrivateResetWallpaperFunction(); // AsyncExtensionFunction overrides. virtual bool RunImpl() OVERRIDE; }; class WallpaperPrivateSetCustomWallpaperFunction : public WallpaperFunctionBase { public: DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setCustomWallpaper", WALLPAPERPRIVATE_SETCUSTOMWALLPAPER) WallpaperPrivateSetCustomWallpaperFunction(); protected: virtual ~WallpaperPrivateSetCustomWallpaperFunction(); // AsyncExtensionFunction overrides. virtual bool RunImpl() OVERRIDE; private: virtual void OnWallpaperDecoded(const gfx::ImageSkia& wallpaper) OVERRIDE; // Generates thumbnail of custom wallpaper. A simple STRETCH is used for // generating thunbail. void GenerateThumbnail(const base::FilePath& thumbnail_path, scoped_ptr image); // Thumbnail is ready. Calls api function javascript callback. void ThumbnailGenerated(base::RefCountedBytes* data); // Layout of the downloaded wallpaper. ash::WallpaperLayout layout_; // True if need to generate thumbnail and pass to callback. bool generate_thumbnail_; // Unique file name of the custom wallpaper. std::string file_name_; // Email address of logged in user. std::string email_; // String representation of downloaded wallpaper. std::string image_data_; // Sequence token associated with wallpaper operations. Shared with // WallpaperManager. base::SequencedWorkerPool::SequenceToken sequence_token_; }; class WallpaperPrivateSetCustomWallpaperLayoutFunction : public AsyncExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setCustomWallpaperLayout", WALLPAPERPRIVATE_SETCUSTOMWALLPAPERLAYOUT) WallpaperPrivateSetCustomWallpaperLayoutFunction(); protected: virtual ~WallpaperPrivateSetCustomWallpaperLayoutFunction(); // AsyncExtensionFunction overrides. virtual bool RunImpl() OVERRIDE; }; class WallpaperPrivateMinimizeInactiveWindowsFunction : public AsyncExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.minimizeInactiveWindows", WALLPAPERPRIVATE_MINIMIZEINACTIVEWINDOWS) WallpaperPrivateMinimizeInactiveWindowsFunction(); protected: virtual ~WallpaperPrivateMinimizeInactiveWindowsFunction(); // AsyncExtensionFunction overrides. virtual bool RunImpl() OVERRIDE; }; class WallpaperPrivateRestoreMinimizedWindowsFunction : public AsyncExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.restoreMinimizedWindows", WALLPAPERPRIVATE_RESTOREMINIMIZEDWINDOWS) WallpaperPrivateRestoreMinimizedWindowsFunction(); protected: virtual ~WallpaperPrivateRestoreMinimizedWindowsFunction(); // AsyncExtensionFunction overrides. virtual bool RunImpl() OVERRIDE; }; class WallpaperPrivateGetThumbnailFunction : public AsyncExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.getThumbnail", WALLPAPERPRIVATE_GETTHUMBNAIL) WallpaperPrivateGetThumbnailFunction(); protected: virtual ~WallpaperPrivateGetThumbnailFunction(); // AsyncExtensionFunction overrides. virtual bool RunImpl() OVERRIDE; private: // Failed to get thumbnail for |file_name|. void Failure(const std::string& file_name); // Returns true to suppress javascript console error. Called when the // requested thumbnail is not found or corrupted in thumbnail directory. void FileNotLoaded(); // Sets data field to the loaded thumbnail binary data in the results. Called // when requested wallpaper thumbnail loaded successfully. void FileLoaded(const std::string& data); // Gets thumbnail from |path|. If |path| does not exist, call FileNotLoaded(). void Get(const base::FilePath& path); // Sequence token associated with wallpaper operations. Shared with // WallpaperManager. base::SequencedWorkerPool::SequenceToken sequence_token_; }; class WallpaperPrivateSaveThumbnailFunction : public AsyncExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.saveThumbnail", WALLPAPERPRIVATE_SAVETHUMBNAIL) WallpaperPrivateSaveThumbnailFunction(); protected: virtual ~WallpaperPrivateSaveThumbnailFunction(); // AsyncExtensionFunction overrides. virtual bool RunImpl() OVERRIDE; private: // Failed to save thumbnail for |file_name|. void Failure(const std::string& file_name); // Saved thumbnail to thumbnail directory. void Success(); // Saves thumbnail to thumbnail directory as |file_name|. void Save(const std::string& data, const std::string& file_name); // Sequence token associated with wallpaper operations. Shared with // WallpaperManager. base::SequencedWorkerPool::SequenceToken sequence_token_; }; class WallpaperPrivateGetOfflineWallpaperListFunction : public AsyncExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.getOfflineWallpaperList", WALLPAPERPRIVATE_GETOFFLINEWALLPAPERLIST) WallpaperPrivateGetOfflineWallpaperListFunction(); protected: virtual ~WallpaperPrivateGetOfflineWallpaperListFunction(); // AsyncExtensionFunction overrides. virtual bool RunImpl() OVERRIDE; private: // Enumerates the list of files in wallpaper directory of given |source|. void GetList(const std::string& email, const std::string& source); // Sends the list of files to extension api caller. If no files or no // directory, sends empty list. void OnComplete(const std::vector& file_list); // Sequence token associated with wallpaper operations. Shared with // WallpaperManager. base::SequencedWorkerPool::SequenceToken sequence_token_; }; #endif // CHROME_BROWSER_CHROMEOS_EXTENSIONS_WALLPAPER_PRIVATE_API_H_