Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / media_galleries / media_galleries_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 // Defines the Chrome Extensions Media Galleries API functions for accessing
6 // user's media files, as specified in the extension API IDL.
7
8 #ifndef CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_
9 #define CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_
10
11 #include <string>
12 #include <vector>
13
14 #include "base/callback_forward.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "chrome/browser/extensions/chrome_extension_function.h"
18 #include "chrome/browser/media_galleries/gallery_watch_manager_observer.h"
19 #include "chrome/browser/media_galleries/media_file_system_registry.h"
20 #include "chrome/browser/media_galleries/media_scan_manager_observer.h"
21 #include "chrome/common/extensions/api/media_galleries.h"
22 #include "chrome/common/media_galleries/metadata_types.h"
23 #include "components/storage_monitor/media_storage_util.h"
24 #include "extensions/browser/browser_context_keyed_api_factory.h"
25 #include "extensions/browser/event_router.h"
26
27 namespace MediaGalleries = extensions::api::media_galleries;
28
29 class MediaGalleriesScanResultController;
30
31 namespace content {
32 class BlobHandle;
33 class WebContents;
34 }
35
36 namespace metadata {
37 class SafeMediaMetadataParser;
38 }
39
40 namespace extensions {
41
42 class Extension;
43
44 // The profile-keyed service that manages the media galleries extension API.
45 // Created at the same time as the Profile. This is also the event router.
46 class MediaGalleriesEventRouter : public BrowserContextKeyedAPI,
47                                   public GalleryWatchManagerObserver,
48                                   public MediaScanManagerObserver,
49                                   public extensions::EventRouter::Observer {
50  public:
51   // KeyedService implementation.
52   void Shutdown() override;
53
54   // BrowserContextKeyedAPI implementation.
55   static BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter>*
56       GetFactoryInstance();
57
58   // Convenience method to get the MediaGalleriesAPI for a profile.
59   static MediaGalleriesEventRouter* Get(content::BrowserContext* context);
60
61   bool ExtensionHasGalleryChangeListener(const std::string& extension_id) const;
62   bool ExtensionHasScanProgressListener(const std::string& extension_id) const;
63
64   // MediaScanManagerObserver implementation.
65   void OnScanStarted(const std::string& extension_id) override;
66   void OnScanCancelled(const std::string& extension_id) override;
67   void OnScanFinished(const std::string& extension_id,
68                       int gallery_count,
69                       const MediaGalleryScanResult& file_counts) override;
70   void OnScanError(const std::string& extension_id) override;
71
72  private:
73   friend class BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter>;
74
75   void DispatchEventToExtension(const std::string& extension_id,
76                                 const std::string& event_name,
77                                 scoped_ptr<base::ListValue> event_args);
78
79   explicit MediaGalleriesEventRouter(content::BrowserContext* context);
80   ~MediaGalleriesEventRouter() override;
81
82   // BrowserContextKeyedAPI implementation.
83   static const char* service_name() {
84     return "MediaGalleriesAPI";
85   }
86   static const bool kServiceIsNULLWhileTesting = true;
87
88   // GalleryWatchManagerObserver
89   void OnGalleryChanged(const std::string& extension_id,
90                         MediaGalleryPrefId gallery_id) override;
91   void OnGalleryWatchDropped(const std::string& extension_id,
92                              MediaGalleryPrefId gallery_id) override;
93
94   // extensions::EventRouter::Observer implementation.
95   void OnListenerRemoved(const EventListenerInfo& details) override;
96
97   // Current profile.
98   Profile* profile_;
99
100   base::WeakPtrFactory<MediaGalleriesEventRouter> weak_ptr_factory_;
101
102   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesEventRouter);
103 };
104
105 class MediaGalleriesGetMediaFileSystemsFunction
106     : public ChromeAsyncExtensionFunction {
107  public:
108   DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMediaFileSystems",
109                              MEDIAGALLERIES_GETMEDIAFILESYSTEMS)
110
111  protected:
112   ~MediaGalleriesGetMediaFileSystemsFunction() override;
113   bool RunAsync() override;
114
115  private:
116   // Bottom half for RunAsync, invoked after the preferences is initialized.
117   void OnPreferencesInit(
118       MediaGalleries::GetMediaFileSystemsInteractivity interactive);
119
120   // Always show the dialog.
121   void AlwaysShowDialog(const std::vector<MediaFileSystemInfo>& filesystems);
122
123   // If no galleries are found, show the dialog, otherwise return them.
124   void ShowDialogIfNoGalleries(
125       const std::vector<MediaFileSystemInfo>& filesystems);
126
127   // Grabs galleries from the media file system registry and passes them to
128   // |ReturnGalleries|.
129   void GetAndReturnGalleries();
130
131   // Returns galleries to the caller.
132   void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
133
134   // Shows the configuration dialog to edit gallery preferences.
135   void ShowDialog();
136
137   // A helper method that calls
138   // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
139   void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
140 };
141
142 class MediaGalleriesGetAllMediaFileSystemMetadataFunction
143     : public ChromeAsyncExtensionFunction {
144  public:
145   DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllMediaFileSystemMetadata",
146                              MEDIAGALLERIES_GETALLMEDIAFILESYSTEMMETADATA)
147
148  protected:
149   ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() override;
150   bool RunAsync() override;
151
152  private:
153   // Bottom half for RunAsync, invoked after the preferences is initialized.
154   // Gets the list of permitted galleries and checks if they are available.
155   void OnPreferencesInit();
156
157   // Callback to run upon getting the list of available devices.
158   // Sends the list of media filesystem metadata back to the extension.
159   void OnGetGalleries(
160       const MediaGalleryPrefIdSet& permitted_gallery_ids,
161       const storage_monitor::MediaStorageUtil::DeviceIdSet* available_devices);
162 };
163
164 class MediaGalleriesAddUserSelectedFolderFunction
165     : public ChromeAsyncExtensionFunction {
166  public:
167   DECLARE_EXTENSION_FUNCTION("mediaGalleries.addUserSelectedFolder",
168                              MEDIAGALLERIES_ADDUSERSELECTEDFOLDER)
169
170  protected:
171   ~MediaGalleriesAddUserSelectedFolderFunction() override;
172   bool RunAsync() override;
173
174  private:
175   // Bottom half for RunAsync, invoked after the preferences is initialized.
176   void OnPreferencesInit();
177
178   // Callback for the directory prompt request, with the input from the user.
179   // If |selected_directory| is empty, then the user canceled.
180   // Either handle the user canceled case or add the selected gallery.
181   void OnDirectorySelected(const base::FilePath& selected_directory);
182
183   // Callback for the directory prompt request. |pref_id| is for the gallery
184   // the user just added. |filesystems| is the entire list of file systems.
185   // The fsid for the file system that corresponds to |pref_id| will be
186   // appended to the list of file systems returned to the caller. The
187   // Javascript binding for this API will interpret the list appropriately.
188   void ReturnGalleriesAndId(
189       MediaGalleryPrefId pref_id,
190       const std::vector<MediaFileSystemInfo>& filesystems);
191
192   // A helper method that calls
193   // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
194   void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
195 };
196
197 class MediaGalleriesDropPermissionForMediaFileSystemFunction
198     : public ChromeAsyncExtensionFunction {
199  public:
200   DECLARE_EXTENSION_FUNCTION("mediaGalleries.dropPermissionForMediaFileSystem",
201                              MEDIAGALLERIES_DROPPERMISSIONFORMEDIAFILESYSTEM)
202
203  protected:
204   ~MediaGalleriesDropPermissionForMediaFileSystemFunction() override;
205   bool RunAsync() override;
206
207  private:
208   // Bottom half for RunAsync, invoked after the preferences is initialized.
209   void OnPreferencesInit(MediaGalleryPrefId pref_id);
210 };
211
212 class MediaGalleriesStartMediaScanFunction
213     : public ChromeAsyncExtensionFunction {
214  public:
215   DECLARE_EXTENSION_FUNCTION("mediaGalleries.startMediaScan",
216                              MEDIAGALLERIES_STARTMEDIASCAN)
217
218  protected:
219   ~MediaGalleriesStartMediaScanFunction() override;
220   bool RunAsync() override;
221
222  private:
223   // Bottom half for RunAsync, invoked after the preferences is initialized.
224   void OnPreferencesInit();
225 };
226
227 class MediaGalleriesCancelMediaScanFunction
228     : public ChromeAsyncExtensionFunction {
229  public:
230   DECLARE_EXTENSION_FUNCTION("mediaGalleries.cancelMediaScan",
231                              MEDIAGALLERIES_CANCELMEDIASCAN)
232
233  protected:
234   ~MediaGalleriesCancelMediaScanFunction() override;
235   bool RunAsync() override;
236
237  private:
238   // Bottom half for RunAsync, invoked after the preferences is initialized.
239   void OnPreferencesInit();
240 };
241
242 class MediaGalleriesAddScanResultsFunction
243     : public ChromeAsyncExtensionFunction {
244  public:
245   DECLARE_EXTENSION_FUNCTION("mediaGalleries.addScanResults",
246                              MEDIAGALLERIES_ADDSCANRESULTS)
247
248  protected:
249   ~MediaGalleriesAddScanResultsFunction() override;
250   bool RunAsync() override;
251
252   // Pulled out for testing.
253   virtual MediaGalleriesScanResultController* MakeDialog(
254       content::WebContents* web_contents,
255       const extensions::Extension& extension,
256       const base::Closure& on_finish);
257
258  private:
259   // Bottom half for RunAsync, invoked after the preferences is initialized.
260   void OnPreferencesInit();
261
262   // Grabs galleries from the media file system registry and passes them to
263   // ReturnGalleries().
264   void GetAndReturnGalleries();
265
266   // Returns galleries to the caller.
267   void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
268 };
269
270 class MediaGalleriesGetMetadataFunction : public ChromeAsyncExtensionFunction {
271  public:
272   DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMetadata",
273                              MEDIAGALLERIES_GETMETADATA)
274
275  protected:
276   ~MediaGalleriesGetMetadataFunction() override;
277   bool RunAsync() override;
278
279  private:
280   // Bottom half for RunAsync, invoked after the preferences is initialized.
281   void OnPreferencesInit(MediaGalleries::GetMetadataType metadata_type,
282                          const std::string& blob_uuid);
283
284   void GetMetadata(MediaGalleries::GetMetadataType metadata_type,
285                    const std::string& blob_uuid,
286                    scoped_ptr<std::string> blob_header,
287                    int64 total_blob_length);
288
289   void OnSafeMediaMetadataParserDone(
290       bool parse_success, scoped_ptr<base::DictionaryValue> result_dictionary,
291       scoped_ptr<std::vector<metadata::AttachedImage> > attached_images);
292
293   void ConstructNextBlob(
294       scoped_ptr<base::DictionaryValue> result_dictionary,
295       scoped_ptr<std::vector<metadata::AttachedImage> > attached_images,
296       scoped_ptr<std::vector<std::string> > blob_uuids,
297       scoped_ptr<content::BlobHandle> current_blob);
298 };
299
300 class MediaGalleriesAddGalleryWatchFunction
301     : public ChromeAsyncExtensionFunction {
302  public:
303   DECLARE_EXTENSION_FUNCTION("mediaGalleries.addGalleryWatch",
304                              MEDIAGALLERIES_ADDGALLERYWATCH);
305
306  protected:
307   ~MediaGalleriesAddGalleryWatchFunction() override;
308   bool RunAsync() override;
309
310  private:
311   void OnPreferencesInit(const std::string& pref_id);
312
313   // Gallery watch request handler.
314   void HandleResponse(MediaGalleryPrefId gallery_id, const std::string& error);
315 };
316
317 class MediaGalleriesRemoveGalleryWatchFunction
318     : public ChromeAsyncExtensionFunction {
319  public:
320   DECLARE_EXTENSION_FUNCTION("mediaGalleries.removeGalleryWatch",
321                              MEDIAGALLERIES_REMOVEGALLERYWATCH);
322
323  protected:
324   ~MediaGalleriesRemoveGalleryWatchFunction() override;
325   bool RunAsync() override;
326
327  private:
328   void OnPreferencesInit(const std::string& pref_id);
329 };
330
331 class MediaGalleriesGetAllGalleryWatchFunction
332     : public ChromeAsyncExtensionFunction {
333  public:
334   DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllGalleryWatch",
335                              MEDIAGALLERIES_GETALLGALLERYWATCH);
336
337  protected:
338   ~MediaGalleriesGetAllGalleryWatchFunction() override;
339   bool RunAsync() override;
340
341  private:
342   void OnPreferencesInit();
343 };
344
345 class MediaGalleriesRemoveAllGalleryWatchFunction
346     : public ChromeAsyncExtensionFunction {
347  public:
348   DECLARE_EXTENSION_FUNCTION("mediaGalleries.removeAllGalleryWatch",
349                              MEDIAGALLERIES_REMOVEALLGALLERYWATCH);
350
351  protected:
352   ~MediaGalleriesRemoveAllGalleryWatchFunction() override;
353   bool RunAsync() override;
354
355  private:
356   void OnPreferencesInit();
357 };
358
359 }  // namespace extensions
360
361 #endif  // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_