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/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"
24 namespace MediaGalleries = extensions::api::media_galleries;
26 class MediaGalleriesScanResultDialogController;
33 class SafeMediaMetadataParser;
36 namespace extensions {
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 {
45 // KeyedService implementation.
46 virtual void Shutdown() OVERRIDE;
48 // BrowserContextKeyedAPI implementation.
49 static BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter>*
52 // Convenience method to get the MediaGalleriesAPI for a profile.
53 static MediaGalleriesEventRouter* Get(content::BrowserContext* context);
55 bool ExtensionHasScanProgressListener(const std::string& extension_id) const;
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,
63 const MediaGalleryScanResult& file_counts) OVERRIDE;
64 virtual void OnScanError(const std::string& extension_id) OVERRIDE;
67 friend class BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter>;
69 void DispatchEventToExtension(const std::string& extension_id,
70 const std::string& event_name,
71 scoped_ptr<base::ListValue> event_args);
73 explicit MediaGalleriesEventRouter(content::BrowserContext* context);
74 virtual ~MediaGalleriesEventRouter();
76 // BrowserContextKeyedAPI implementation.
77 static const char* service_name() {
78 return "MediaGalleriesAPI";
80 static const bool kServiceIsNULLWhileTesting = true;
85 base::WeakPtrFactory<MediaGalleriesEventRouter> weak_ptr_factory_;
87 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesEventRouter);
90 class MediaGalleriesGetMediaFileSystemsFunction
91 : public ChromeAsyncExtensionFunction {
93 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMediaFileSystems",
94 MEDIAGALLERIES_GETMEDIAFILESYSTEMS)
97 virtual ~MediaGalleriesGetMediaFileSystemsFunction();
98 virtual bool RunAsync() OVERRIDE;
101 // Bottom half for RunAsync, invoked after the preferences is initialized.
102 void OnPreferencesInit(
103 MediaGalleries::GetMediaFileSystemsInteractivity interactive);
105 // Always show the dialog.
106 void AlwaysShowDialog(const std::vector<MediaFileSystemInfo>& filesystems);
108 // If no galleries are found, show the dialog, otherwise return them.
109 void ShowDialogIfNoGalleries(
110 const std::vector<MediaFileSystemInfo>& filesystems);
112 // Grabs galleries from the media file system registry and passes them to
113 // |ReturnGalleries|.
114 void GetAndReturnGalleries();
116 // Returns galleries to the caller.
117 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
119 // Shows the configuration dialog to edit gallery preferences.
122 // A helper method that calls
123 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
124 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
127 class MediaGalleriesGetAllMediaFileSystemMetadataFunction
128 : public ChromeAsyncExtensionFunction {
130 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllMediaFileSystemMetadata",
131 MEDIAGALLERIES_GETALLMEDIAFILESYSTEMMETADATA)
134 virtual ~MediaGalleriesGetAllMediaFileSystemMetadataFunction();
135 virtual bool RunAsync() OVERRIDE;
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();
142 // Callback to run upon getting the list of available devices.
143 // Sends the list of media filesystem metadata back to the extension.
145 const MediaGalleryPrefIdSet& permitted_gallery_ids,
146 const storage_monitor::MediaStorageUtil::DeviceIdSet* available_devices);
149 class MediaGalleriesAddUserSelectedFolderFunction
150 : public ChromeAsyncExtensionFunction {
152 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addUserSelectedFolder",
153 MEDIAGALLERIES_ADDUSERSELECTEDFOLDER)
156 virtual ~MediaGalleriesAddUserSelectedFolderFunction();
157 virtual bool RunAsync() OVERRIDE;
160 // Bottom half for RunAsync, invoked after the preferences is initialized.
161 void OnPreferencesInit();
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);
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);
177 // A helper method that calls
178 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
179 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
182 class MediaGalleriesDropPermissionForMediaFileSystemFunction
183 : public ChromeAsyncExtensionFunction {
185 DECLARE_EXTENSION_FUNCTION("mediaGalleries.dropPermissionForMediaFileSystem",
186 MEDIAGALLERIES_DROPPERMISSIONFORMEDIAFILESYSTEM)
189 virtual ~MediaGalleriesDropPermissionForMediaFileSystemFunction();
190 virtual bool RunAsync() OVERRIDE;
193 // Bottom half for RunAsync, invoked after the preferences is initialized.
194 void OnPreferencesInit(MediaGalleryPrefId pref_id);
197 class MediaGalleriesStartMediaScanFunction
198 : public ChromeAsyncExtensionFunction {
200 DECLARE_EXTENSION_FUNCTION("mediaGalleries.startMediaScan",
201 MEDIAGALLERIES_STARTMEDIASCAN)
204 virtual ~MediaGalleriesStartMediaScanFunction();
205 virtual bool RunAsync() OVERRIDE;
208 // Bottom half for RunAsync, invoked after the preferences is initialized.
209 void OnPreferencesInit();
212 class MediaGalleriesCancelMediaScanFunction
213 : public ChromeAsyncExtensionFunction {
215 DECLARE_EXTENSION_FUNCTION("mediaGalleries.cancelMediaScan",
216 MEDIAGALLERIES_CANCELMEDIASCAN)
219 virtual ~MediaGalleriesCancelMediaScanFunction();
220 virtual bool RunAsync() OVERRIDE;
223 // Bottom half for RunAsync, invoked after the preferences is initialized.
224 void OnPreferencesInit();
227 class MediaGalleriesAddScanResultsFunction
228 : public ChromeAsyncExtensionFunction {
230 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addScanResults",
231 MEDIAGALLERIES_ADDSCANRESULTS)
234 virtual ~MediaGalleriesAddScanResultsFunction();
235 virtual bool RunAsync() OVERRIDE;
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);
244 // Bottom half for RunAsync, invoked after the preferences is initialized.
245 void OnPreferencesInit();
247 // Grabs galleries from the media file system registry and passes them to
248 // ReturnGalleries().
249 void GetAndReturnGalleries();
251 // Returns galleries to the caller.
252 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
255 class MediaGalleriesGetMetadataFunction : public ChromeAsyncExtensionFunction {
257 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMetadata",
258 MEDIAGALLERIES_GETMETADATA)
261 virtual ~MediaGalleriesGetMetadataFunction();
262 virtual bool RunAsync() OVERRIDE;
265 // Bottom half for RunAsync, invoked after the preferences is initialized.
266 void OnPreferencesInit(bool mime_type_only, const std::string& blob_uuid);
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);
273 void OnSafeMediaMetadataParserDone(
274 bool parse_success, base::DictionaryValue* metadata_dictionary);
277 } // namespace extensions
279 #endif // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_