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