Upstream version 7.36.149.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/media_file_system_registry.h"
19 #include "chrome/browser/media_galleries/media_scan_manager_observer.h"
20 #include "chrome/common/extensions/api/media_galleries.h"
21 #include "components/storage_monitor/media_storage_util.h"
22 #include "extensions/browser/browser_context_keyed_api_factory.h"
23
24 namespace MediaGalleries = extensions::api::media_galleries;
25
26 class MediaGalleriesScanResultDialogController;
27
28 namespace content {
29 class WebContents;
30 }
31
32 namespace metadata {
33 class SafeMediaMetadataParser;
34 }
35
36 namespace extensions {
37
38 class Extension;
39
40 // The profile-keyed service that manages the media galleries extension API.
41 // Created at the same time as the Profile. This is also the event router.
42 class MediaGalleriesEventRouter : public BrowserContextKeyedAPI,
43                                   public MediaScanManagerObserver {
44  public:
45   // KeyedService implementation.
46   virtual void Shutdown() OVERRIDE;
47
48   // BrowserContextKeyedAPI implementation.
49   static BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter>*
50       GetFactoryInstance();
51
52   // Convenience method to get the MediaGalleriesAPI for a profile.
53   static MediaGalleriesEventRouter* Get(content::BrowserContext* context);
54
55   bool ExtensionHasScanProgressListener(const std::string& extension_id) const;
56
57   // MediaScanManagerObserver implementation.
58   virtual void OnScanStarted(const std::string& extension_id) OVERRIDE;
59   virtual void OnScanCancelled(const std::string& extension_id) OVERRIDE;
60   virtual void OnScanFinished(
61       const std::string& extension_id,
62       int gallery_count,
63       const MediaGalleryScanResult& file_counts) OVERRIDE;
64   virtual void OnScanError(const std::string& extension_id) OVERRIDE;
65
66  private:
67   friend class BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter>;
68
69   void DispatchEventToExtension(const std::string& extension_id,
70                                 const std::string& event_name,
71                                 scoped_ptr<base::ListValue> event_args);
72
73   explicit MediaGalleriesEventRouter(content::BrowserContext* context);
74   virtual ~MediaGalleriesEventRouter();
75
76   // BrowserContextKeyedAPI implementation.
77   static const char* service_name() {
78     return "MediaGalleriesAPI";
79   }
80   static const bool kServiceIsNULLWhileTesting = true;
81
82   // Current profile.
83   Profile* profile_;
84
85   base::WeakPtrFactory<MediaGalleriesEventRouter> weak_ptr_factory_;
86
87   DISALLOW_COPY_AND_ASSIGN(MediaGalleriesEventRouter);
88 };
89
90 class MediaGalleriesGetMediaFileSystemsFunction
91     : public ChromeAsyncExtensionFunction {
92  public:
93   DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMediaFileSystems",
94                              MEDIAGALLERIES_GETMEDIAFILESYSTEMS)
95
96  protected:
97   virtual ~MediaGalleriesGetMediaFileSystemsFunction();
98   virtual bool RunAsync() OVERRIDE;
99
100  private:
101   // Bottom half for RunAsync, invoked after the preferences is initialized.
102   void OnPreferencesInit(
103       MediaGalleries::GetMediaFileSystemsInteractivity interactive);
104
105   // Always show the dialog.
106   void AlwaysShowDialog(const std::vector<MediaFileSystemInfo>& filesystems);
107
108   // If no galleries are found, show the dialog, otherwise return them.
109   void ShowDialogIfNoGalleries(
110       const std::vector<MediaFileSystemInfo>& filesystems);
111
112   // Grabs galleries from the media file system registry and passes them to
113   // |ReturnGalleries|.
114   void GetAndReturnGalleries();
115
116   // Returns galleries to the caller.
117   void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
118
119   // Shows the configuration dialog to edit gallery preferences.
120   void ShowDialog();
121
122   // A helper method that calls
123   // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
124   void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
125 };
126
127 class MediaGalleriesGetAllMediaFileSystemMetadataFunction
128     : public ChromeAsyncExtensionFunction {
129  public:
130   DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllMediaFileSystemMetadata",
131                              MEDIAGALLERIES_GETALLMEDIAFILESYSTEMMETADATA)
132
133  protected:
134   virtual ~MediaGalleriesGetAllMediaFileSystemMetadataFunction();
135   virtual bool RunAsync() OVERRIDE;
136
137  private:
138   // Bottom half for RunAsync, invoked after the preferences is initialized.
139   // Gets the list of permitted galleries and checks if they are available.
140   void OnPreferencesInit();
141
142   // Callback to run upon getting the list of available devices.
143   // Sends the list of media filesystem metadata back to the extension.
144   void OnGetGalleries(
145       const MediaGalleryPrefIdSet& permitted_gallery_ids,
146       const storage_monitor::MediaStorageUtil::DeviceIdSet* available_devices);
147 };
148
149 class MediaGalleriesAddUserSelectedFolderFunction
150     : public ChromeAsyncExtensionFunction {
151  public:
152   DECLARE_EXTENSION_FUNCTION("mediaGalleries.addUserSelectedFolder",
153                              MEDIAGALLERIES_ADDUSERSELECTEDFOLDER)
154
155  protected:
156   virtual ~MediaGalleriesAddUserSelectedFolderFunction();
157   virtual bool RunAsync() OVERRIDE;
158
159  private:
160   // Bottom half for RunAsync, invoked after the preferences is initialized.
161   void OnPreferencesInit();
162
163   // Callback for the directory prompt request, with the input from the user.
164   // If |selected_directory| is empty, then the user canceled.
165   // Either handle the user canceled case or add the selected gallery.
166   void OnDirectorySelected(const base::FilePath& selected_directory);
167
168   // Callback for the directory prompt request. |pref_id| is for the gallery
169   // the user just added. |filesystems| is the entire list of file systems.
170   // The fsid for the file system that corresponds to |pref_id| will be
171   // appended to the list of file systems returned to the caller. The
172   // Javascript binding for this API will interpret the list appropriately.
173   void ReturnGalleriesAndId(
174       MediaGalleryPrefId pref_id,
175       const std::vector<MediaFileSystemInfo>& filesystems);
176
177   // A helper method that calls
178   // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
179   void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
180 };
181
182 class MediaGalleriesDropPermissionForMediaFileSystemFunction
183     : public ChromeAsyncExtensionFunction {
184  public:
185   DECLARE_EXTENSION_FUNCTION("mediaGalleries.dropPermissionForMediaFileSystem",
186                              MEDIAGALLERIES_DROPPERMISSIONFORMEDIAFILESYSTEM)
187
188  protected:
189   virtual ~MediaGalleriesDropPermissionForMediaFileSystemFunction();
190   virtual bool RunAsync() OVERRIDE;
191
192  private:
193   // Bottom half for RunAsync, invoked after the preferences is initialized.
194   void OnPreferencesInit(MediaGalleryPrefId pref_id);
195 };
196
197 class MediaGalleriesStartMediaScanFunction
198     : public ChromeAsyncExtensionFunction {
199  public:
200   DECLARE_EXTENSION_FUNCTION("mediaGalleries.startMediaScan",
201                              MEDIAGALLERIES_STARTMEDIASCAN)
202
203  protected:
204   virtual ~MediaGalleriesStartMediaScanFunction();
205   virtual bool RunAsync() OVERRIDE;
206
207  private:
208   // Bottom half for RunAsync, invoked after the preferences is initialized.
209   void OnPreferencesInit();
210 };
211
212 class MediaGalleriesCancelMediaScanFunction
213     : public ChromeAsyncExtensionFunction {
214  public:
215   DECLARE_EXTENSION_FUNCTION("mediaGalleries.cancelMediaScan",
216                              MEDIAGALLERIES_CANCELMEDIASCAN)
217
218  protected:
219   virtual ~MediaGalleriesCancelMediaScanFunction();
220   virtual bool RunAsync() OVERRIDE;
221
222  private:
223   // Bottom half for RunAsync, invoked after the preferences is initialized.
224   void OnPreferencesInit();
225 };
226
227 class MediaGalleriesAddScanResultsFunction
228     : public ChromeAsyncExtensionFunction {
229  public:
230   DECLARE_EXTENSION_FUNCTION("mediaGalleries.addScanResults",
231                              MEDIAGALLERIES_ADDSCANRESULTS)
232
233  protected:
234   virtual ~MediaGalleriesAddScanResultsFunction();
235   virtual bool RunAsync() OVERRIDE;
236
237   // Pulled out for testing.
238   virtual MediaGalleriesScanResultDialogController* MakeDialog(
239       content::WebContents* web_contents,
240       const extensions::Extension& extension,
241       const base::Closure& on_finish);
242
243  private:
244   // Bottom half for RunAsync, invoked after the preferences is initialized.
245   void OnPreferencesInit();
246
247   // Grabs galleries from the media file system registry and passes them to
248   // ReturnGalleries().
249   void GetAndReturnGalleries();
250
251   // Returns galleries to the caller.
252   void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
253 };
254
255 class MediaGalleriesGetMetadataFunction : public ChromeAsyncExtensionFunction {
256  public:
257   DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMetadata",
258                              MEDIAGALLERIES_GETMETADATA)
259
260  protected:
261   virtual ~MediaGalleriesGetMetadataFunction();
262   virtual bool RunAsync() OVERRIDE;
263
264  private:
265   // Bottom half for RunAsync, invoked after the preferences is initialized.
266   void OnPreferencesInit(bool mime_type_only, const std::string& blob_uuid);
267
268   void SniffMimeType(bool mime_type_only,
269                      const std::string& blob_uuid,
270                      scoped_ptr<std::string> blob_header,
271                      int64 total_blob_length);
272
273   void OnSafeMediaMetadataParserDone(
274       bool parse_success, base::DictionaryValue* metadata_dictionary);
275 };
276
277 }  // namespace extensions
278
279 #endif  // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_