Home | History | Annotate | Download | only in extensions
      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 "ash/desktop_background/desktop_background_controller.h"
      9 #include "base/threading/sequenced_worker_pool.h"
     10 #include "chrome/browser/chromeos/login/user.h"
     11 #include "chrome/browser/extensions/extension_function.h"
     12 #include "net/url_request/url_fetcher_delegate.h"
     13 #include "ui/gfx/image/image_skia.h"
     14 
     15 namespace chromeos {
     16 class UserImage;
     17 }
     18 
     19 // Wallpaper manager strings.
     20 class WallpaperPrivateGetStringsFunction : public SyncExtensionFunction {
     21  public:
     22   DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.getStrings",
     23                              WALLPAPERPRIVATE_GETSTRINGS)
     24 
     25  protected:
     26   virtual ~WallpaperPrivateGetStringsFunction() {}
     27 
     28   // SyncExtensionFunction overrides.
     29   virtual bool RunImpl() OVERRIDE;
     30 };
     31 
     32 // Wallpaper manager function base. It contains a JPEG decoder to decode
     33 // wallpaper data.
     34 class WallpaperFunctionBase : public AsyncExtensionFunction {
     35  public:
     36   WallpaperFunctionBase();
     37 
     38  protected:
     39   virtual ~WallpaperFunctionBase();
     40 
     41   // A class to decode JPEG file.
     42   class WallpaperDecoder;
     43 
     44   // Holds an instance of WallpaperDecoder.
     45   static WallpaperDecoder* wallpaper_decoder_;
     46 
     47   // Starts to decode |data|. Must run on UI thread.
     48   void StartDecode(const std::string& data);
     49 
     50   // Handles failure or cancel cases. Passes error message to Javascript side.
     51   void OnFailureOrCancel(const std::string& error);
     52 
     53  private:
     54   virtual void OnWallpaperDecoded(const gfx::ImageSkia& wallpaper) = 0;
     55 };
     56 
     57 class WallpaperPrivateSetWallpaperIfExistsFunction
     58     : public WallpaperFunctionBase {
     59  public:
     60   DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setWallpaperIfExists",
     61                              WALLPAPERPRIVATE_SETWALLPAPERIFEXISTS)
     62 
     63   WallpaperPrivateSetWallpaperIfExistsFunction();
     64 
     65  protected:
     66   virtual ~WallpaperPrivateSetWallpaperIfExistsFunction();
     67 
     68   // AsyncExtensionFunction overrides.
     69   virtual bool RunImpl() OVERRIDE;
     70 
     71  private:
     72   virtual void OnWallpaperDecoded(const gfx::ImageSkia& wallpaper) OVERRIDE;
     73 
     74   // File doesn't exist. Sets javascript callback parameter to false.
     75   void OnFileNotExists(const std::string& error);
     76 
     77   // Reads file specified by |file_path|. If success, post a task to start
     78   // decoding the file.
     79   void ReadFileAndInitiateStartDecode(const base::FilePath& file_path,
     80                                       const base::FilePath& fallback_path);
     81 
     82   // Online wallpaper URL or file name of custom wallpaper.
     83   std::string urlOrFile_;
     84 
     85   // Layout of the loaded wallpaper.
     86   ash::WallpaperLayout layout_;
     87 
     88   // Type of the loaded wallpaper.
     89   chromeos::User::WallpaperType type_;
     90 
     91   // Sequence token associated with wallpaper operations. Shared with
     92   // WallpaperManager.
     93   base::SequencedWorkerPool::SequenceToken sequence_token_;
     94 
     95 };
     96 
     97 class WallpaperPrivateSetWallpaperFunction : public WallpaperFunctionBase {
     98  public:
     99   DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setWallpaper",
    100                              WALLPAPERPRIVATE_SETWALLPAPER)
    101 
    102   WallpaperPrivateSetWallpaperFunction();
    103 
    104  protected:
    105   virtual ~WallpaperPrivateSetWallpaperFunction();
    106 
    107   // AsyncExtensionFunction overrides.
    108   virtual bool RunImpl() OVERRIDE;
    109 
    110  private:
    111   virtual void OnWallpaperDecoded(const gfx::ImageSkia& wallpaper) OVERRIDE;
    112 
    113   // Saves the image data to a file.
    114   void SaveToFile();
    115 
    116   // Sets wallpaper to the decoded image.
    117   void SetDecodedWallpaper(scoped_ptr<gfx::ImageSkia> wallpaper);
    118 
    119   // Layout of the downloaded wallpaper.
    120   ash::WallpaperLayout layout_;
    121 
    122   // The decoded wallpaper. It may accessed from UI thread to set wallpaper or
    123   // FILE thread to resize and save wallpaper to disk.
    124   gfx::ImageSkia wallpaper_;
    125 
    126   // Email address of logged in user.
    127   std::string email_;
    128 
    129   // High resolution wallpaper URL.
    130   std::string url_;
    131 
    132   // String representation of downloaded wallpaper.
    133   std::string image_data_;
    134 
    135   // Sequence token associated with wallpaper operations. Shared with
    136   // WallpaperManager.
    137   base::SequencedWorkerPool::SequenceToken sequence_token_;
    138 };
    139 
    140 class WallpaperPrivateResetWallpaperFunction
    141     : public AsyncExtensionFunction {
    142  public:
    143   DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.resetWallpaper",
    144                              WALLPAPERPRIVATE_RESETWALLPAPER)
    145 
    146   WallpaperPrivateResetWallpaperFunction();
    147 
    148  protected:
    149   virtual ~WallpaperPrivateResetWallpaperFunction();
    150 
    151   // AsyncExtensionFunction overrides.
    152   virtual bool RunImpl() OVERRIDE;
    153 };
    154 
    155 class WallpaperPrivateSetCustomWallpaperFunction
    156     : public WallpaperFunctionBase {
    157  public:
    158   DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setCustomWallpaper",
    159                              WALLPAPERPRIVATE_SETCUSTOMWALLPAPER)
    160 
    161   WallpaperPrivateSetCustomWallpaperFunction();
    162 
    163  protected:
    164   virtual ~WallpaperPrivateSetCustomWallpaperFunction();
    165 
    166   // AsyncExtensionFunction overrides.
    167   virtual bool RunImpl() OVERRIDE;
    168 
    169  private:
    170   virtual void OnWallpaperDecoded(const gfx::ImageSkia& wallpaper) OVERRIDE;
    171 
    172   // Generates thumbnail of custom wallpaper. A simple STRETCH is used for
    173   // generating thunbail.
    174   void GenerateThumbnail(const base::FilePath& thumbnail_path,
    175                          scoped_ptr<gfx::ImageSkia> image);
    176 
    177   // Thumbnail is ready. Calls api function javascript callback.
    178   void ThumbnailGenerated(base::RefCountedBytes* data);
    179 
    180   // Layout of the downloaded wallpaper.
    181   ash::WallpaperLayout layout_;
    182 
    183   // True if need to generate thumbnail and pass to callback.
    184   bool generate_thumbnail_;
    185 
    186   // Unique file name of the custom wallpaper.
    187   std::string file_name_;
    188 
    189   // Email address of logged in user.
    190   std::string email_;
    191 
    192   // String representation of downloaded wallpaper.
    193   std::string image_data_;
    194 
    195   // Sequence token associated with wallpaper operations. Shared with
    196   // WallpaperManager.
    197   base::SequencedWorkerPool::SequenceToken sequence_token_;
    198 };
    199 
    200 class WallpaperPrivateSetCustomWallpaperLayoutFunction
    201     : public AsyncExtensionFunction {
    202  public:
    203   DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.setCustomWallpaperLayout",
    204                              WALLPAPERPRIVATE_SETCUSTOMWALLPAPERLAYOUT)
    205 
    206   WallpaperPrivateSetCustomWallpaperLayoutFunction();
    207 
    208  protected:
    209   virtual ~WallpaperPrivateSetCustomWallpaperLayoutFunction();
    210 
    211   // AsyncExtensionFunction overrides.
    212   virtual bool RunImpl() OVERRIDE;
    213 };
    214 
    215 class WallpaperPrivateMinimizeInactiveWindowsFunction
    216     : public AsyncExtensionFunction {
    217  public:
    218   DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.minimizeInactiveWindows",
    219                              WALLPAPERPRIVATE_MINIMIZEINACTIVEWINDOWS)
    220 
    221   WallpaperPrivateMinimizeInactiveWindowsFunction();
    222 
    223  protected:
    224   virtual ~WallpaperPrivateMinimizeInactiveWindowsFunction();
    225 
    226   // AsyncExtensionFunction overrides.
    227   virtual bool RunImpl() OVERRIDE;
    228 };
    229 
    230 class WallpaperPrivateRestoreMinimizedWindowsFunction
    231     : public AsyncExtensionFunction {
    232  public:
    233   DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.restoreMinimizedWindows",
    234                              WALLPAPERPRIVATE_RESTOREMINIMIZEDWINDOWS)
    235 
    236   WallpaperPrivateRestoreMinimizedWindowsFunction();
    237 
    238  protected:
    239   virtual ~WallpaperPrivateRestoreMinimizedWindowsFunction();
    240 
    241   // AsyncExtensionFunction overrides.
    242   virtual bool RunImpl() OVERRIDE;
    243 };
    244 
    245 class WallpaperPrivateGetThumbnailFunction : public AsyncExtensionFunction {
    246  public:
    247   DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.getThumbnail",
    248                              WALLPAPERPRIVATE_GETTHUMBNAIL)
    249 
    250   WallpaperPrivateGetThumbnailFunction();
    251 
    252  protected:
    253   virtual ~WallpaperPrivateGetThumbnailFunction();
    254 
    255   // AsyncExtensionFunction overrides.
    256   virtual bool RunImpl() OVERRIDE;
    257 
    258  private:
    259   // Failed to get thumbnail for |file_name|.
    260   void Failure(const std::string& file_name);
    261 
    262   // Returns true to suppress javascript console error. Called when the
    263   // requested thumbnail is not found or corrupted in thumbnail directory.
    264   void FileNotLoaded();
    265 
    266   // Sets data field to the loaded thumbnail binary data in the results. Called
    267   // when requested wallpaper thumbnail loaded successfully.
    268   void FileLoaded(const std::string& data);
    269 
    270   // Gets thumbnail from |path|. If |path| does not exist, call FileNotLoaded().
    271   void Get(const base::FilePath& path);
    272 
    273   // Sequence token associated with wallpaper operations. Shared with
    274   // WallpaperManager.
    275   base::SequencedWorkerPool::SequenceToken sequence_token_;
    276 };
    277 
    278 class WallpaperPrivateSaveThumbnailFunction : public AsyncExtensionFunction {
    279  public:
    280   DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.saveThumbnail",
    281                              WALLPAPERPRIVATE_SAVETHUMBNAIL)
    282 
    283   WallpaperPrivateSaveThumbnailFunction();
    284 
    285  protected:
    286   virtual ~WallpaperPrivateSaveThumbnailFunction();
    287 
    288   // AsyncExtensionFunction overrides.
    289   virtual bool RunImpl() OVERRIDE;
    290 
    291  private:
    292   // Failed to save thumbnail for |file_name|.
    293   void Failure(const std::string& file_name);
    294 
    295   // Saved thumbnail to thumbnail directory.
    296   void Success();
    297 
    298   // Saves thumbnail to thumbnail directory as |file_name|.
    299   void Save(const std::string& data, const std::string& file_name);
    300 
    301   // Sequence token associated with wallpaper operations. Shared with
    302   // WallpaperManager.
    303   base::SequencedWorkerPool::SequenceToken sequence_token_;
    304 };
    305 
    306 class WallpaperPrivateGetOfflineWallpaperListFunction
    307     : public AsyncExtensionFunction {
    308  public:
    309   DECLARE_EXTENSION_FUNCTION("wallpaperPrivate.getOfflineWallpaperList",
    310                              WALLPAPERPRIVATE_GETOFFLINEWALLPAPERLIST)
    311   WallpaperPrivateGetOfflineWallpaperListFunction();
    312 
    313  protected:
    314   virtual ~WallpaperPrivateGetOfflineWallpaperListFunction();
    315 
    316   // AsyncExtensionFunction overrides.
    317   virtual bool RunImpl() OVERRIDE;
    318 
    319  private:
    320   // Enumerates the list of files in wallpaper directory of given |source|.
    321   void GetList(const std::string& email, const std::string& source);
    322 
    323   // Sends the list of files to extension api caller. If no files or no
    324   // directory, sends empty list.
    325   void OnComplete(const std::vector<std::string>& file_list);
    326 
    327   // Sequence token associated with wallpaper operations. Shared with
    328   // WallpaperManager.
    329   base::SequencedWorkerPool::SequenceToken sequence_token_;
    330 };
    331 
    332 #endif  // CHROME_BROWSER_CHROMEOS_EXTENSIONS_WALLPAPER_PRIVATE_API_H_
    333