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