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.
5 // Defines the Chrome Extensions Media Galleries API functions for accessing
6 // user's media files, as specified in the extension API IDL.
8 #ifndef CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_
9 #define CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_
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"
27 namespace MediaGalleries = extensions::api::media_galleries;
29 class MediaGalleriesScanResultController;
37 class SafeMediaMetadataParser;
40 namespace extensions {
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 {
51 // KeyedService implementation.
52 void Shutdown() override;
54 // BrowserContextKeyedAPI implementation.
55 static BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter>*
58 // Convenience method to get the MediaGalleriesAPI for a profile.
59 static MediaGalleriesEventRouter* Get(content::BrowserContext* context);
61 bool ExtensionHasGalleryChangeListener(const std::string& extension_id) const;
62 bool ExtensionHasScanProgressListener(const std::string& extension_id) const;
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,
69 const MediaGalleryScanResult& file_counts) override;
70 void OnScanError(const std::string& extension_id) override;
73 friend class BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter>;
75 void DispatchEventToExtension(const std::string& extension_id,
76 const std::string& event_name,
77 scoped_ptr<base::ListValue> event_args);
79 explicit MediaGalleriesEventRouter(content::BrowserContext* context);
80 ~MediaGalleriesEventRouter() override;
82 // BrowserContextKeyedAPI implementation.
83 static const char* service_name() {
84 return "MediaGalleriesAPI";
86 static const bool kServiceIsNULLWhileTesting = true;
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;
94 // extensions::EventRouter::Observer implementation.
95 void OnListenerRemoved(const EventListenerInfo& details) override;
100 base::WeakPtrFactory<MediaGalleriesEventRouter> weak_ptr_factory_;
102 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesEventRouter);
105 class MediaGalleriesGetMediaFileSystemsFunction
106 : public ChromeAsyncExtensionFunction {
108 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMediaFileSystems",
109 MEDIAGALLERIES_GETMEDIAFILESYSTEMS)
112 ~MediaGalleriesGetMediaFileSystemsFunction() override;
113 bool RunAsync() override;
116 // Bottom half for RunAsync, invoked after the preferences is initialized.
117 void OnPreferencesInit(
118 MediaGalleries::GetMediaFileSystemsInteractivity interactive);
120 // Always show the dialog.
121 void AlwaysShowDialog(const std::vector<MediaFileSystemInfo>& filesystems);
123 // If no galleries are found, show the dialog, otherwise return them.
124 void ShowDialogIfNoGalleries(
125 const std::vector<MediaFileSystemInfo>& filesystems);
127 // Grabs galleries from the media file system registry and passes them to
128 // |ReturnGalleries|.
129 void GetAndReturnGalleries();
131 // Returns galleries to the caller.
132 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
134 // Shows the configuration dialog to edit gallery preferences.
137 // A helper method that calls
138 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
139 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
142 class MediaGalleriesGetAllMediaFileSystemMetadataFunction
143 : public ChromeAsyncExtensionFunction {
145 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllMediaFileSystemMetadata",
146 MEDIAGALLERIES_GETALLMEDIAFILESYSTEMMETADATA)
149 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() override;
150 bool RunAsync() override;
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();
157 // Callback to run upon getting the list of available devices.
158 // Sends the list of media filesystem metadata back to the extension.
160 const MediaGalleryPrefIdSet& permitted_gallery_ids,
161 const storage_monitor::MediaStorageUtil::DeviceIdSet* available_devices);
164 class MediaGalleriesAddUserSelectedFolderFunction
165 : public ChromeAsyncExtensionFunction {
167 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addUserSelectedFolder",
168 MEDIAGALLERIES_ADDUSERSELECTEDFOLDER)
171 ~MediaGalleriesAddUserSelectedFolderFunction() override;
172 bool RunAsync() override;
175 // Bottom half for RunAsync, invoked after the preferences is initialized.
176 void OnPreferencesInit();
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);
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);
192 // A helper method that calls
193 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
194 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
197 class MediaGalleriesDropPermissionForMediaFileSystemFunction
198 : public ChromeAsyncExtensionFunction {
200 DECLARE_EXTENSION_FUNCTION("mediaGalleries.dropPermissionForMediaFileSystem",
201 MEDIAGALLERIES_DROPPERMISSIONFORMEDIAFILESYSTEM)
204 ~MediaGalleriesDropPermissionForMediaFileSystemFunction() override;
205 bool RunAsync() override;
208 // Bottom half for RunAsync, invoked after the preferences is initialized.
209 void OnPreferencesInit(MediaGalleryPrefId pref_id);
212 class MediaGalleriesStartMediaScanFunction
213 : public ChromeAsyncExtensionFunction {
215 DECLARE_EXTENSION_FUNCTION("mediaGalleries.startMediaScan",
216 MEDIAGALLERIES_STARTMEDIASCAN)
219 ~MediaGalleriesStartMediaScanFunction() override;
220 bool RunAsync() override;
223 // Bottom half for RunAsync, invoked after the preferences is initialized.
224 void OnPreferencesInit();
227 class MediaGalleriesCancelMediaScanFunction
228 : public ChromeAsyncExtensionFunction {
230 DECLARE_EXTENSION_FUNCTION("mediaGalleries.cancelMediaScan",
231 MEDIAGALLERIES_CANCELMEDIASCAN)
234 ~MediaGalleriesCancelMediaScanFunction() override;
235 bool RunAsync() override;
238 // Bottom half for RunAsync, invoked after the preferences is initialized.
239 void OnPreferencesInit();
242 class MediaGalleriesAddScanResultsFunction
243 : public ChromeAsyncExtensionFunction {
245 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addScanResults",
246 MEDIAGALLERIES_ADDSCANRESULTS)
249 ~MediaGalleriesAddScanResultsFunction() override;
250 bool RunAsync() override;
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);
259 // Bottom half for RunAsync, invoked after the preferences is initialized.
260 void OnPreferencesInit();
262 // Grabs galleries from the media file system registry and passes them to
263 // ReturnGalleries().
264 void GetAndReturnGalleries();
266 // Returns galleries to the caller.
267 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
270 class MediaGalleriesGetMetadataFunction : public ChromeAsyncExtensionFunction {
272 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMetadata",
273 MEDIAGALLERIES_GETMETADATA)
276 ~MediaGalleriesGetMetadataFunction() override;
277 bool RunAsync() override;
280 // Bottom half for RunAsync, invoked after the preferences is initialized.
281 void OnPreferencesInit(MediaGalleries::GetMetadataType metadata_type,
282 const std::string& blob_uuid);
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);
289 void OnSafeMediaMetadataParserDone(
290 bool parse_success, scoped_ptr<base::DictionaryValue> result_dictionary,
291 scoped_ptr<std::vector<metadata::AttachedImage> > attached_images);
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);
300 class MediaGalleriesAddGalleryWatchFunction
301 : public ChromeAsyncExtensionFunction {
303 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addGalleryWatch",
304 MEDIAGALLERIES_ADDGALLERYWATCH);
307 ~MediaGalleriesAddGalleryWatchFunction() override;
308 bool RunAsync() override;
311 void OnPreferencesInit(const std::string& pref_id);
313 // Gallery watch request handler.
314 void HandleResponse(MediaGalleryPrefId gallery_id, const std::string& error);
317 class MediaGalleriesRemoveGalleryWatchFunction
318 : public ChromeAsyncExtensionFunction {
320 DECLARE_EXTENSION_FUNCTION("mediaGalleries.removeGalleryWatch",
321 MEDIAGALLERIES_REMOVEGALLERYWATCH);
324 ~MediaGalleriesRemoveGalleryWatchFunction() override;
325 bool RunAsync() override;
328 void OnPreferencesInit(const std::string& pref_id);
331 class MediaGalleriesGetAllGalleryWatchFunction
332 : public ChromeAsyncExtensionFunction {
334 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllGalleryWatch",
335 MEDIAGALLERIES_GETALLGALLERYWATCH);
338 ~MediaGalleriesGetAllGalleryWatchFunction() override;
339 bool RunAsync() override;
342 void OnPreferencesInit();
345 class MediaGalleriesRemoveAllGalleryWatchFunction
346 : public ChromeAsyncExtensionFunction {
348 DECLARE_EXTENSION_FUNCTION("mediaGalleries.removeAllGalleryWatch",
349 MEDIAGALLERIES_REMOVEALLGALLERYWATCH);
352 ~MediaGalleriesRemoveAllGalleryWatchFunction() override;
353 bool RunAsync() override;
356 void OnPreferencesInit();
359 } // namespace extensions
361 #endif // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_