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