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"
26 #include "extensions/browser/extension_event_histogram_value.h"
28 namespace MediaGalleries
= extensions::api::media_galleries
;
30 class MediaGalleriesScanResultController
;
38 class SafeMediaMetadataParser
;
41 namespace extensions
{
45 // The profile-keyed service that manages the media galleries extension API.
46 // Created at the same time as the Profile. This is also the event router.
47 class MediaGalleriesEventRouter
: public BrowserContextKeyedAPI
,
48 public GalleryWatchManagerObserver
,
49 public MediaScanManagerObserver
,
50 public extensions::EventRouter::Observer
{
52 // KeyedService implementation.
53 void Shutdown() override
;
55 // BrowserContextKeyedAPI implementation.
56 static BrowserContextKeyedAPIFactory
<MediaGalleriesEventRouter
>*
59 // Convenience method to get the MediaGalleriesAPI for a profile.
60 static MediaGalleriesEventRouter
* Get(content::BrowserContext
* context
);
62 bool ExtensionHasGalleryChangeListener(const std::string
& extension_id
) const;
63 bool ExtensionHasScanProgressListener(const std::string
& extension_id
) const;
65 // MediaScanManagerObserver implementation.
66 void OnScanStarted(const std::string
& extension_id
) override
;
67 void OnScanCancelled(const std::string
& extension_id
) override
;
68 void OnScanFinished(const std::string
& extension_id
,
70 const MediaGalleryScanResult
& file_counts
) override
;
71 void OnScanError(const std::string
& extension_id
) override
;
74 friend class BrowserContextKeyedAPIFactory
<MediaGalleriesEventRouter
>;
76 void DispatchEventToExtension(const std::string
& extension_id
,
77 events::HistogramValue histogram_value
,
78 const std::string
& event_name
,
79 scoped_ptr
<base::ListValue
> event_args
);
81 explicit MediaGalleriesEventRouter(content::BrowserContext
* context
);
82 ~MediaGalleriesEventRouter() override
;
84 // BrowserContextKeyedAPI implementation.
85 static const char* service_name() {
86 return "MediaGalleriesAPI";
88 static const bool kServiceIsNULLWhileTesting
= true;
90 // GalleryWatchManagerObserver
91 void OnGalleryChanged(const std::string
& extension_id
,
92 MediaGalleryPrefId gallery_id
) override
;
93 void OnGalleryWatchDropped(const std::string
& extension_id
,
94 MediaGalleryPrefId gallery_id
) override
;
96 // extensions::EventRouter::Observer implementation.
97 void OnListenerRemoved(const EventListenerInfo
& details
) override
;
102 base::WeakPtrFactory
<MediaGalleriesEventRouter
> weak_ptr_factory_
;
104 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesEventRouter
);
107 class MediaGalleriesGetMediaFileSystemsFunction
108 : public ChromeAsyncExtensionFunction
{
110 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMediaFileSystems",
111 MEDIAGALLERIES_GETMEDIAFILESYSTEMS
)
114 ~MediaGalleriesGetMediaFileSystemsFunction() override
;
115 bool RunAsync() override
;
118 // Bottom half for RunAsync, invoked after the preferences is initialized.
119 void OnPreferencesInit(
120 MediaGalleries::GetMediaFileSystemsInteractivity interactive
);
122 // Always show the dialog.
123 void AlwaysShowDialog(const std::vector
<MediaFileSystemInfo
>& filesystems
);
125 // If no galleries are found, show the dialog, otherwise return them.
126 void ShowDialogIfNoGalleries(
127 const std::vector
<MediaFileSystemInfo
>& filesystems
);
129 // Grabs galleries from the media file system registry and passes them to
130 // |ReturnGalleries|.
131 void GetAndReturnGalleries();
133 // Returns galleries to the caller.
134 void ReturnGalleries(const std::vector
<MediaFileSystemInfo
>& filesystems
);
136 // Shows the configuration dialog to edit gallery preferences.
139 // A helper method that calls
140 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
141 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback
& cb
);
144 class MediaGalleriesGetAllMediaFileSystemMetadataFunction
145 : public ChromeAsyncExtensionFunction
{
147 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllMediaFileSystemMetadata",
148 MEDIAGALLERIES_GETALLMEDIAFILESYSTEMMETADATA
)
151 ~MediaGalleriesGetAllMediaFileSystemMetadataFunction() override
;
152 bool RunAsync() override
;
155 // Bottom half for RunAsync, invoked after the preferences is initialized.
156 // Gets the list of permitted galleries and checks if they are available.
157 void OnPreferencesInit();
159 // Callback to run upon getting the list of available devices.
160 // Sends the list of media filesystem metadata back to the extension.
162 const MediaGalleryPrefIdSet
& permitted_gallery_ids
,
163 const storage_monitor::MediaStorageUtil::DeviceIdSet
* available_devices
);
166 class MediaGalleriesAddUserSelectedFolderFunction
167 : public ChromeAsyncExtensionFunction
{
169 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addUserSelectedFolder",
170 MEDIAGALLERIES_ADDUSERSELECTEDFOLDER
)
173 ~MediaGalleriesAddUserSelectedFolderFunction() override
;
174 bool RunAsync() override
;
177 // Bottom half for RunAsync, invoked after the preferences is initialized.
178 void OnPreferencesInit();
180 // Callback for the directory prompt request, with the input from the user.
181 // If |selected_directory| is empty, then the user canceled.
182 // Either handle the user canceled case or add the selected gallery.
183 void OnDirectorySelected(const base::FilePath
& selected_directory
);
185 // Callback for the directory prompt request. |pref_id| is for the gallery
186 // the user just added. |filesystems| is the entire list of file systems.
187 // The fsid for the file system that corresponds to |pref_id| will be
188 // appended to the list of file systems returned to the caller. The
189 // Javascript binding for this API will interpret the list appropriately.
190 void ReturnGalleriesAndId(
191 MediaGalleryPrefId pref_id
,
192 const std::vector
<MediaFileSystemInfo
>& filesystems
);
194 // A helper method that calls
195 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
196 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback
& cb
);
199 class MediaGalleriesDropPermissionForMediaFileSystemFunction
200 : public ChromeAsyncExtensionFunction
{
202 DECLARE_EXTENSION_FUNCTION("mediaGalleries.dropPermissionForMediaFileSystem",
203 MEDIAGALLERIES_DROPPERMISSIONFORMEDIAFILESYSTEM
)
206 ~MediaGalleriesDropPermissionForMediaFileSystemFunction() override
;
207 bool RunAsync() override
;
210 // Bottom half for RunAsync, invoked after the preferences is initialized.
211 void OnPreferencesInit(MediaGalleryPrefId pref_id
);
214 class MediaGalleriesStartMediaScanFunction
215 : public ChromeAsyncExtensionFunction
{
217 DECLARE_EXTENSION_FUNCTION("mediaGalleries.startMediaScan",
218 MEDIAGALLERIES_STARTMEDIASCAN
)
221 ~MediaGalleriesStartMediaScanFunction() override
;
222 bool RunAsync() override
;
225 // Bottom half for RunAsync, invoked after the preferences is initialized.
226 void OnPreferencesInit();
229 class MediaGalleriesCancelMediaScanFunction
230 : public ChromeAsyncExtensionFunction
{
232 DECLARE_EXTENSION_FUNCTION("mediaGalleries.cancelMediaScan",
233 MEDIAGALLERIES_CANCELMEDIASCAN
)
236 ~MediaGalleriesCancelMediaScanFunction() override
;
237 bool RunAsync() override
;
240 // Bottom half for RunAsync, invoked after the preferences is initialized.
241 void OnPreferencesInit();
244 class MediaGalleriesAddScanResultsFunction
245 : public ChromeAsyncExtensionFunction
{
247 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addScanResults",
248 MEDIAGALLERIES_ADDSCANRESULTS
)
251 ~MediaGalleriesAddScanResultsFunction() override
;
252 bool RunAsync() override
;
254 // Pulled out for testing.
255 virtual MediaGalleriesScanResultController
* MakeDialog(
256 content::WebContents
* web_contents
,
257 const extensions::Extension
& extension
,
258 const base::Closure
& on_finish
);
261 // Bottom half for RunAsync, invoked after the preferences is initialized.
262 void OnPreferencesInit();
264 // Grabs galleries from the media file system registry and passes them to
265 // ReturnGalleries().
266 void GetAndReturnGalleries();
268 // Returns galleries to the caller.
269 void ReturnGalleries(const std::vector
<MediaFileSystemInfo
>& filesystems
);
272 class MediaGalleriesGetMetadataFunction
: public ChromeAsyncExtensionFunction
{
274 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMetadata",
275 MEDIAGALLERIES_GETMETADATA
)
278 ~MediaGalleriesGetMetadataFunction() override
;
279 bool RunAsync() override
;
282 // Bottom half for RunAsync, invoked after the preferences is initialized.
283 void OnPreferencesInit(MediaGalleries::GetMetadataType metadata_type
,
284 const std::string
& blob_uuid
);
286 void GetMetadata(MediaGalleries::GetMetadataType metadata_type
,
287 const std::string
& blob_uuid
,
288 scoped_ptr
<std::string
> blob_header
,
289 int64 total_blob_length
);
291 void OnSafeMediaMetadataParserDone(
292 bool parse_success
, scoped_ptr
<base::DictionaryValue
> result_dictionary
,
293 scoped_ptr
<std::vector
<metadata::AttachedImage
> > attached_images
);
295 void ConstructNextBlob(
296 scoped_ptr
<base::DictionaryValue
> result_dictionary
,
297 scoped_ptr
<std::vector
<metadata::AttachedImage
> > attached_images
,
298 scoped_ptr
<std::vector
<std::string
> > blob_uuids
,
299 scoped_ptr
<content::BlobHandle
> current_blob
);
302 class MediaGalleriesAddGalleryWatchFunction
303 : public ChromeAsyncExtensionFunction
{
305 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addGalleryWatch",
306 MEDIAGALLERIES_ADDGALLERYWATCH
);
309 ~MediaGalleriesAddGalleryWatchFunction() override
;
310 bool RunAsync() override
;
313 void OnPreferencesInit(const std::string
& pref_id
);
315 // Gallery watch request handler.
316 void HandleResponse(MediaGalleryPrefId gallery_id
, const std::string
& error
);
319 class MediaGalleriesRemoveGalleryWatchFunction
320 : public ChromeAsyncExtensionFunction
{
322 DECLARE_EXTENSION_FUNCTION("mediaGalleries.removeGalleryWatch",
323 MEDIAGALLERIES_REMOVEGALLERYWATCH
);
326 ~MediaGalleriesRemoveGalleryWatchFunction() override
;
327 bool RunAsync() override
;
330 void OnPreferencesInit(const std::string
& pref_id
);
333 class MediaGalleriesGetAllGalleryWatchFunction
334 : public ChromeAsyncExtensionFunction
{
336 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllGalleryWatch",
337 MEDIAGALLERIES_GETALLGALLERYWATCH
);
340 ~MediaGalleriesGetAllGalleryWatchFunction() override
;
341 bool RunAsync() override
;
344 void OnPreferencesInit();
347 class MediaGalleriesRemoveAllGalleryWatchFunction
348 : public ChromeAsyncExtensionFunction
{
350 DECLARE_EXTENSION_FUNCTION("mediaGalleries.removeAllGalleryWatch",
351 MEDIAGALLERIES_REMOVEALLGALLERYWATCH
);
354 ~MediaGalleriesRemoveAllGalleryWatchFunction() override
;
355 bool RunAsync() override
;
358 void OnPreferencesInit();
361 } // namespace extensions
363 #endif // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_