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/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/browser/storage_monitor/media_storage_util.h"
21 #include "chrome/common/extensions/api/media_galleries.h"
23 namespace MediaGalleries = extensions::api::media_galleries;
25 namespace extensions {
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 {
32 // BrowserContextKeyedService implementation.
33 virtual void Shutdown() OVERRIDE;
35 // ProfileKeyedAPI implementation.
36 static ProfileKeyedAPIFactory<MediaGalleriesEventRouter>*
39 // Convenience method to get the MediaGalleriesAPI for a profile.
40 static MediaGalleriesEventRouter* Get(Profile* profile);
42 bool ExtensionHasScanProgressListener(const std::string& extension_id) const;
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, int gallery_count, int image_count,
49 int audio_count, int video_count) OVERRIDE;
52 friend class ProfileKeyedAPIFactory<MediaGalleriesEventRouter>;
54 void DispatchEventToExtension(const std::string& extension_id,
55 const std::string& event_name,
56 scoped_ptr<base::ListValue> event_args);
58 explicit MediaGalleriesEventRouter(Profile* profile);
59 virtual ~MediaGalleriesEventRouter();
61 // ProfileKeyedAPI implementation.
62 static const char* service_name() {
63 return "MediaGalleriesAPI";
65 static const bool kServiceIsNULLWhileTesting = true;
70 base::WeakPtrFactory<MediaGalleriesEventRouter> weak_ptr_factory_;
72 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesEventRouter);
75 class MediaGalleriesGetMediaFileSystemsFunction
76 : public ChromeAsyncExtensionFunction {
78 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMediaFileSystems",
79 MEDIAGALLERIES_GETMEDIAFILESYSTEMS)
82 virtual ~MediaGalleriesGetMediaFileSystemsFunction();
83 virtual bool RunImpl() OVERRIDE;
86 // Bottom half for RunImpl, invoked after the preferences is initialized.
87 void OnPreferencesInit(
88 MediaGalleries::GetMediaFileSystemsInteractivity interactive);
90 // Always show the dialog.
91 void AlwaysShowDialog(const std::vector<MediaFileSystemInfo>& filesystems);
93 // If no galleries are found, show the dialog, otherwise return them.
94 void ShowDialogIfNoGalleries(
95 const std::vector<MediaFileSystemInfo>& filesystems);
97 // Grabs galleries from the media file system registry and passes them to
99 void GetAndReturnGalleries();
101 // Returns galleries to the caller.
102 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
104 // Shows the configuration dialog to edit gallery preferences.
107 // A helper method that calls
108 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
109 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
112 class MediaGalleriesGetAllMediaFileSystemMetadataFunction
113 : public ChromeAsyncExtensionFunction {
115 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllMediaFileSystemMetadata",
116 MEDIAGALLERIES_GETALLMEDIAFILESYSTEMMETADATA)
119 virtual ~MediaGalleriesGetAllMediaFileSystemMetadataFunction();
120 virtual bool RunImpl() OVERRIDE;
123 // Bottom half for RunImpl, invoked after the preferences is initialized.
124 // Gets the list of permitted galleries and checks if they are available.
125 void OnPreferencesInit();
127 // Callback to run upon getting the list of available devices.
128 // Sends the list of media filesystem metadata back to the extension.
129 void OnGetGalleries(const MediaGalleryPrefIdSet& permitted_gallery_ids,
130 const MediaStorageUtil::DeviceIdSet* available_devices);
133 class MediaGalleriesAddUserSelectedFolderFunction
134 : public ChromeAsyncExtensionFunction {
136 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addUserSelectedFolder",
137 MEDIAGALLERIES_ADDUSERSELECTEDFOLDER)
140 virtual ~MediaGalleriesAddUserSelectedFolderFunction();
141 virtual bool RunImpl() OVERRIDE;
144 // Bottom half for RunImpl, invoked after the preferences is initialized.
145 void OnPreferencesInit();
147 // Callback for the directory prompt request, with the input from the user.
148 // If |selected_directory| is empty, then the user canceled.
149 // Either handle the user canceled case or add the selected gallery.
150 void OnDirectorySelected(const base::FilePath& selected_directory);
152 // Callback for the directory prompt request. |pref_id| is for the gallery
153 // the user just added. |filesystems| is the entire list of file systems.
154 // The fsid for the file system that corresponds to |pref_id| will be
155 // appended to the list of file systems returned to the caller. The
156 // Javascript binding for this API will interpret the list appropriately.
157 void ReturnGalleriesAndId(
158 MediaGalleryPrefId pref_id,
159 const std::vector<MediaFileSystemInfo>& filesystems);
161 // A helper method that calls
162 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
163 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
166 class MediaGalleriesStartMediaScanFunction
167 : public ChromeAsyncExtensionFunction {
169 DECLARE_EXTENSION_FUNCTION("mediaGalleries.startMediaScan",
170 MEDIAGALLERIES_STARTMEDIASCAN)
173 virtual ~MediaGalleriesStartMediaScanFunction();
174 virtual bool RunImpl() OVERRIDE;
177 // Bottom half for RunImpl, invoked after the preferences is initialized.
178 void OnPreferencesInit();
181 class MediaGalleriesCancelMediaScanFunction
182 : public ChromeAsyncExtensionFunction {
184 DECLARE_EXTENSION_FUNCTION("mediaGalleries.cancelMediaScan",
185 MEDIAGALLERIES_CANCELMEDIASCAN)
188 virtual ~MediaGalleriesCancelMediaScanFunction();
189 virtual bool RunImpl() OVERRIDE;
192 // Bottom half for RunImpl, invoked after the preferences is initialized.
193 void OnPreferencesInit();
196 class MediaGalleriesAddScanResultsFunction
197 : public ChromeAsyncExtensionFunction {
199 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addScanResults",
200 MEDIAGALLERIES_ADDSCANRESULTS)
203 virtual ~MediaGalleriesAddScanResultsFunction();
204 virtual bool RunImpl() OVERRIDE;
207 // Bottom half for RunImpl, invoked after the preferences is initialized.
208 void OnPreferencesInit();
210 // Grabs galleries from the media file system registry and passes them to
211 // ReturnGalleries().
212 void GetAndReturnGalleries();
214 // Returns galleries to the caller.
215 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
218 class MediaGalleriesGetMetadataFunction : public ChromeAsyncExtensionFunction {
220 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMetadata",
221 MEDIAGALLERIES_GETMETADATA)
224 virtual ~MediaGalleriesGetMetadataFunction();
225 virtual bool RunImpl() OVERRIDE;
228 // Bottom half for RunImpl, invoked after the preferences is initialized.
229 void OnPreferencesInit(bool mime_type_only, const std::string& blob_uuid);
231 void SniffMimeType(bool mime_type_only, scoped_ptr<std::string> blob_header,
232 int64 total_blob_length);
235 } // namespace extensions
237 #endif // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_