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/common/extensions/api/media_galleries.h"
21 #include "components/storage_monitor/media_storage_util.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,
50 const MediaGalleryScanResult& file_counts) OVERRIDE;
51 virtual void OnScanError(const std::string& extension_id) OVERRIDE;
54 friend class ProfileKeyedAPIFactory<MediaGalleriesEventRouter>;
56 void DispatchEventToExtension(const std::string& extension_id,
57 const std::string& event_name,
58 scoped_ptr<base::ListValue> event_args);
60 explicit MediaGalleriesEventRouter(Profile* profile);
61 virtual ~MediaGalleriesEventRouter();
63 // ProfileKeyedAPI implementation.
64 static const char* service_name() {
65 return "MediaGalleriesAPI";
67 static const bool kServiceIsNULLWhileTesting = true;
72 base::WeakPtrFactory<MediaGalleriesEventRouter> weak_ptr_factory_;
74 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesEventRouter);
77 class MediaGalleriesGetMediaFileSystemsFunction
78 : public ChromeAsyncExtensionFunction {
80 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMediaFileSystems",
81 MEDIAGALLERIES_GETMEDIAFILESYSTEMS)
84 virtual ~MediaGalleriesGetMediaFileSystemsFunction();
85 virtual bool RunImpl() OVERRIDE;
88 // Bottom half for RunImpl, invoked after the preferences is initialized.
89 void OnPreferencesInit(
90 MediaGalleries::GetMediaFileSystemsInteractivity interactive);
92 // Always show the dialog.
93 void AlwaysShowDialog(const std::vector<MediaFileSystemInfo>& filesystems);
95 // If no galleries are found, show the dialog, otherwise return them.
96 void ShowDialogIfNoGalleries(
97 const std::vector<MediaFileSystemInfo>& filesystems);
99 // Grabs galleries from the media file system registry and passes them to
100 // |ReturnGalleries|.
101 void GetAndReturnGalleries();
103 // Returns galleries to the caller.
104 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
106 // Shows the configuration dialog to edit gallery preferences.
109 // A helper method that calls
110 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
111 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
114 class MediaGalleriesGetAllMediaFileSystemMetadataFunction
115 : public ChromeAsyncExtensionFunction {
117 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllMediaFileSystemMetadata",
118 MEDIAGALLERIES_GETALLMEDIAFILESYSTEMMETADATA)
121 virtual ~MediaGalleriesGetAllMediaFileSystemMetadataFunction();
122 virtual bool RunImpl() OVERRIDE;
125 // Bottom half for RunImpl, invoked after the preferences is initialized.
126 // Gets the list of permitted galleries and checks if they are available.
127 void OnPreferencesInit();
129 // Callback to run upon getting the list of available devices.
130 // Sends the list of media filesystem metadata back to the extension.
131 void OnGetGalleries(const MediaGalleryPrefIdSet& permitted_gallery_ids,
132 const MediaStorageUtil::DeviceIdSet* available_devices);
135 class MediaGalleriesAddUserSelectedFolderFunction
136 : public ChromeAsyncExtensionFunction {
138 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addUserSelectedFolder",
139 MEDIAGALLERIES_ADDUSERSELECTEDFOLDER)
142 virtual ~MediaGalleriesAddUserSelectedFolderFunction();
143 virtual bool RunImpl() OVERRIDE;
146 // Bottom half for RunImpl, invoked after the preferences is initialized.
147 void OnPreferencesInit();
149 // Callback for the directory prompt request, with the input from the user.
150 // If |selected_directory| is empty, then the user canceled.
151 // Either handle the user canceled case or add the selected gallery.
152 void OnDirectorySelected(const base::FilePath& selected_directory);
154 // Callback for the directory prompt request. |pref_id| is for the gallery
155 // the user just added. |filesystems| is the entire list of file systems.
156 // The fsid for the file system that corresponds to |pref_id| will be
157 // appended to the list of file systems returned to the caller. The
158 // Javascript binding for this API will interpret the list appropriately.
159 void ReturnGalleriesAndId(
160 MediaGalleryPrefId pref_id,
161 const std::vector<MediaFileSystemInfo>& filesystems);
163 // A helper method that calls
164 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension().
165 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb);
168 class MediaGalleriesStartMediaScanFunction
169 : public ChromeAsyncExtensionFunction {
171 DECLARE_EXTENSION_FUNCTION("mediaGalleries.startMediaScan",
172 MEDIAGALLERIES_STARTMEDIASCAN)
175 virtual ~MediaGalleriesStartMediaScanFunction();
176 virtual bool RunImpl() OVERRIDE;
179 // Bottom half for RunImpl, invoked after the preferences is initialized.
180 void OnPreferencesInit();
183 class MediaGalleriesCancelMediaScanFunction
184 : public ChromeAsyncExtensionFunction {
186 DECLARE_EXTENSION_FUNCTION("mediaGalleries.cancelMediaScan",
187 MEDIAGALLERIES_CANCELMEDIASCAN)
190 virtual ~MediaGalleriesCancelMediaScanFunction();
191 virtual bool RunImpl() OVERRIDE;
194 // Bottom half for RunImpl, invoked after the preferences is initialized.
195 void OnPreferencesInit();
198 class MediaGalleriesAddScanResultsFunction
199 : public ChromeAsyncExtensionFunction {
201 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addScanResults",
202 MEDIAGALLERIES_ADDSCANRESULTS)
205 virtual ~MediaGalleriesAddScanResultsFunction();
206 virtual bool RunImpl() OVERRIDE;
209 // Bottom half for RunImpl, invoked after the preferences is initialized.
210 void OnPreferencesInit();
212 // Grabs galleries from the media file system registry and passes them to
213 // ReturnGalleries().
214 void GetAndReturnGalleries();
216 // Returns galleries to the caller.
217 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems);
220 class MediaGalleriesGetMetadataFunction : public ChromeAsyncExtensionFunction {
222 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMetadata",
223 MEDIAGALLERIES_GETMETADATA)
226 virtual ~MediaGalleriesGetMetadataFunction();
227 virtual bool RunImpl() OVERRIDE;
230 // Bottom half for RunImpl, invoked after the preferences is initialized.
231 void OnPreferencesInit(bool mime_type_only, const std::string& blob_uuid);
233 void SniffMimeType(bool mime_type_only, scoped_ptr<std::string> blob_header,
234 int64 total_blob_length);
237 } // namespace extensions
239 #endif // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_