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 virtual 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 virtual void OnScanStarted(const std::string& extension_id) OVERRIDE;
66 virtual void OnScanCancelled(const std::string& extension_id) OVERRIDE;
67 virtual void OnScanFinished(
68 const std::string& extension_id,
70 const MediaGalleryScanResult& file_counts) OVERRIDE;
71 virtual void OnScanError(const std::string& extension_id) OVERRIDE;
74 friend class BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter>;
76 void DispatchEventToExtension(const std::string& extension_id,
77 const std::string& event_name,
78 scoped_ptr<base::ListValue> event_args);
80 explicit MediaGalleriesEventRouter(content::BrowserContext* context);
81 virtual ~MediaGalleriesEventRouter();
83 // BrowserContextKeyedAPI implementation.
84 static const char* service_name() {
85 return "MediaGalleriesAPI";
87 static const bool kServiceIsNULLWhileTesting = true;
89 // GalleryWatchManagerObserver
90 virtual void OnGalleryChanged(const std::string& extension_id,
91 MediaGalleryPrefId gallery_id) OVERRIDE;
92 virtual void OnGalleryWatchDropped(const std::string& extension_id,
93 MediaGalleryPrefId gallery_id) OVERRIDE;
95 // extensions::EventRouter::Observer implementation.
96 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
101 base::WeakPtrFactory<MediaGalleriesEventRouter> weak_ptr_factory_;
103 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesEventRouter);
106 class MediaGalleriesGetMediaFileSystemsFunction
107 : public ChromeAsyncExtensionFunction {
109 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMediaFileSystems",
110 MEDIAGALLERIES_GETMEDIAFILESYSTEMS)
113 virtual ~MediaGalleriesGetMediaFileSystemsFunction();
114 virtual bool RunAsync() OVERRIDE;
117 // Bottom half for RunAsync, invoked after the preferences is initialized.
118 void OnPreferencesInit(
119 MediaGalleries::GetMediaFileSystemsInteractivity interactive);
121 // Always show the dialog.
122 void AlwaysShowDialog(const std::vector<MediaFileSystemInfo>& filesystems);
124 // If no galleries are found, show the dialog, otherwise return them.
125 void ShowDialogIfNoGalleries(
126 const std::vector<MediaFileSystemInfo>& filesystems);
128 // Grabs galleries from the media file system registry and passes them to
129 // |ReturnGalleries|.
130 void GetAndReturnGalleries();
132 // Returns galleries to the caller.
133 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
135 // Shows the configuration dialog to edit gallery preferences.
138 // A helper method that calls
139 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
140 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
143 class MediaGalleriesGetAllMediaFileSystemMetadataFunction
144 : public ChromeAsyncExtensionFunction {
146 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllMediaFileSystemMetadata",
147 MEDIAGALLERIES_GETALLMEDIAFILESYSTEMMETADATA)
150 virtual ~MediaGalleriesGetAllMediaFileSystemMetadataFunction();
151 virtual bool RunAsync() OVERRIDE;
154 // Bottom half for RunAsync, invoked after the preferences is initialized.
155 // Gets the list of permitted galleries and checks if they are available.
156 void OnPreferencesInit();
158 // Callback to run upon getting the list of available devices.
159 // Sends the list of media filesystem metadata back to the extension.
161 const MediaGalleryPrefIdSet& permitted_gallery_ids,
162 const storage_monitor::MediaStorageUtil::DeviceIdSet* available_devices);
165 class MediaGalleriesAddUserSelectedFolderFunction
166 : public ChromeAsyncExtensionFunction {
168 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addUserSelectedFolder",
169 MEDIAGALLERIES_ADDUSERSELECTEDFOLDER)
172 virtual ~MediaGalleriesAddUserSelectedFolderFunction();
173 virtual bool RunAsync() OVERRIDE;
176 // Bottom half for RunAsync, invoked after the preferences is initialized.
177 void OnPreferencesInit();
179 // Callback for the directory prompt request, with the input from the user.
180 // If |selected_directory| is empty, then the user canceled.
181 // Either handle the user canceled case or add the selected gallery.
182 void OnDirectorySelected(const base::FilePath& selected_directory);
184 // Callback for the directory prompt request. |pref_id| is for the gallery
185 // the user just added. |filesystems| is the entire list of file systems.
186 // The fsid for the file system that corresponds to |pref_id| will be
187 // appended to the list of file systems returned to the caller. The
188 // Javascript binding for this API will interpret the list appropriately.
189 void ReturnGalleriesAndId(
190 MediaGalleryPrefId pref_id,
191 const std::vector<MediaFileSystemInfo>& filesystems);
193 // A helper method that calls
194 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
195 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
198 class MediaGalleriesDropPermissionForMediaFileSystemFunction
199 : public ChromeAsyncExtensionFunction {
201 DECLARE_EXTENSION_FUNCTION("mediaGalleries.dropPermissionForMediaFileSystem",
202 MEDIAGALLERIES_DROPPERMISSIONFORMEDIAFILESYSTEM)
205 virtual ~MediaGalleriesDropPermissionForMediaFileSystemFunction();
206 virtual bool RunAsync() OVERRIDE;
209 // Bottom half for RunAsync, invoked after the preferences is initialized.
210 void OnPreferencesInit(MediaGalleryPrefId pref_id);
213 class MediaGalleriesStartMediaScanFunction
214 : public ChromeAsyncExtensionFunction {
216 DECLARE_EXTENSION_FUNCTION("mediaGalleries.startMediaScan",
217 MEDIAGALLERIES_STARTMEDIASCAN)
220 virtual ~MediaGalleriesStartMediaScanFunction();
221 virtual bool RunAsync() OVERRIDE;
224 // Bottom half for RunAsync, invoked after the preferences is initialized.
225 void OnPreferencesInit();
228 class MediaGalleriesCancelMediaScanFunction
229 : public ChromeAsyncExtensionFunction {
231 DECLARE_EXTENSION_FUNCTION("mediaGalleries.cancelMediaScan",
232 MEDIAGALLERIES_CANCELMEDIASCAN)
235 virtual ~MediaGalleriesCancelMediaScanFunction();
236 virtual bool RunAsync() OVERRIDE;
239 // Bottom half for RunAsync, invoked after the preferences is initialized.
240 void OnPreferencesInit();
243 class MediaGalleriesAddScanResultsFunction
244 : public ChromeAsyncExtensionFunction {
246 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addScanResults",
247 MEDIAGALLERIES_ADDSCANRESULTS)
250 virtual ~MediaGalleriesAddScanResultsFunction();
251 virtual bool RunAsync() OVERRIDE;
253 // Pulled out for testing.
254 virtual MediaGalleriesScanResultController* MakeDialog(
255 content::WebContents* web_contents,
256 const extensions::Extension& extension,
257 const base::Closure& on_finish);
260 // Bottom half for RunAsync, invoked after the preferences is initialized.
261 void OnPreferencesInit();
263 // Grabs galleries from the media file system registry and passes them to
264 // ReturnGalleries().
265 void GetAndReturnGalleries();
267 // Returns galleries to the caller.
268 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
271 class MediaGalleriesGetMetadataFunction : public ChromeAsyncExtensionFunction {
273 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMetadata",
274 MEDIAGALLERIES_GETMETADATA)
277 virtual ~MediaGalleriesGetMetadataFunction();
278 virtual bool RunAsync() OVERRIDE;
281 // Bottom half for RunAsync, invoked after the preferences is initialized.
282 void OnPreferencesInit(MediaGalleries::GetMetadataType metadata_type,
283 const std::string& blob_uuid);
285 void GetMetadata(MediaGalleries::GetMetadataType metadata_type,
286 const std::string& blob_uuid,
287 scoped_ptr<std::string> blob_header,
288 int64 total_blob_length);
290 void OnSafeMediaMetadataParserDone(
291 bool parse_success, scoped_ptr<base::DictionaryValue> result_dictionary,
292 scoped_ptr<std::vector<metadata::AttachedImage> > attached_images);
294 void ConstructNextBlob(
295 scoped_ptr<base::DictionaryValue> result_dictionary,
296 scoped_ptr<std::vector<metadata::AttachedImage> > attached_images,
297 scoped_ptr<std::vector<std::string> > blob_uuids,
298 scoped_ptr<content::BlobHandle> current_blob);
301 } // namespace extensions
303 #endif // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_