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. 4 5 // Defines the Chrome Extensions Media Galleries API functions for accessing 6 // user's media files, as specified in the extension API IDL. 7 8 #ifndef CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_ 9 #define CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_ 10 11 #include <string> 12 #include <vector> 13 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 "chrome/common/media_galleries/metadata_types.h" 22 #include "components/storage_monitor/media_storage_util.h" 23 #include "extensions/browser/browser_context_keyed_api_factory.h" 24 25 namespace MediaGalleries = extensions::api::media_galleries; 26 27 class MediaGalleriesScanResultController; 28 29 namespace content { 30 class BlobHandle; 31 class WebContents; 32 } 33 34 namespace metadata { 35 class SafeMediaMetadataParser; 36 } 37 38 namespace extensions { 39 40 class Extension; 41 42 // The profile-keyed service that manages the media galleries extension API. 43 // Created at the same time as the Profile. This is also the event router. 44 class MediaGalleriesEventRouter : public BrowserContextKeyedAPI, 45 public MediaScanManagerObserver { 46 public: 47 // KeyedService implementation. 48 virtual void Shutdown() OVERRIDE; 49 50 // BrowserContextKeyedAPI implementation. 51 static BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter>* 52 GetFactoryInstance(); 53 54 // Convenience method to get the MediaGalleriesAPI for a profile. 55 static MediaGalleriesEventRouter* Get(content::BrowserContext* context); 56 57 bool ExtensionHasScanProgressListener(const std::string& extension_id) const; 58 59 // MediaScanManagerObserver implementation. 60 virtual void OnScanStarted(const std::string& extension_id) OVERRIDE; 61 virtual void OnScanCancelled(const std::string& extension_id) OVERRIDE; 62 virtual void OnScanFinished( 63 const std::string& extension_id, 64 int gallery_count, 65 const MediaGalleryScanResult& file_counts) OVERRIDE; 66 virtual void OnScanError(const std::string& extension_id) OVERRIDE; 67 68 private: 69 friend class BrowserContextKeyedAPIFactory<MediaGalleriesEventRouter>; 70 71 void DispatchEventToExtension(const std::string& extension_id, 72 const std::string& event_name, 73 scoped_ptr<base::ListValue> event_args); 74 75 explicit MediaGalleriesEventRouter(content::BrowserContext* context); 76 virtual ~MediaGalleriesEventRouter(); 77 78 // BrowserContextKeyedAPI implementation. 79 static const char* service_name() { 80 return "MediaGalleriesAPI"; 81 } 82 static const bool kServiceIsNULLWhileTesting = true; 83 84 // Current profile. 85 Profile* profile_; 86 87 base::WeakPtrFactory<MediaGalleriesEventRouter> weak_ptr_factory_; 88 89 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesEventRouter); 90 }; 91 92 class MediaGalleriesGetMediaFileSystemsFunction 93 : public ChromeAsyncExtensionFunction { 94 public: 95 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMediaFileSystems", 96 MEDIAGALLERIES_GETMEDIAFILESYSTEMS) 97 98 protected: 99 virtual ~MediaGalleriesGetMediaFileSystemsFunction(); 100 virtual bool RunAsync() OVERRIDE; 101 102 private: 103 // Bottom half for RunAsync, invoked after the preferences is initialized. 104 void OnPreferencesInit( 105 MediaGalleries::GetMediaFileSystemsInteractivity interactive); 106 107 // Always show the dialog. 108 void AlwaysShowDialog(const std::vector<MediaFileSystemInfo>& filesystems); 109 110 // If no galleries are found, show the dialog, otherwise return them. 111 void ShowDialogIfNoGalleries( 112 const std::vector<MediaFileSystemInfo>& filesystems); 113 114 // Grabs galleries from the media file system registry and passes them to 115 // |ReturnGalleries|. 116 void GetAndReturnGalleries(); 117 118 // Returns galleries to the caller. 119 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems); 120 121 // Shows the configuration dialog to edit gallery preferences. 122 void ShowDialog(); 123 124 // A helper method that calls 125 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension(). 126 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb); 127 }; 128 129 class MediaGalleriesGetAllMediaFileSystemMetadataFunction 130 : public ChromeAsyncExtensionFunction { 131 public: 132 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getAllMediaFileSystemMetadata", 133 MEDIAGALLERIES_GETALLMEDIAFILESYSTEMMETADATA) 134 135 protected: 136 virtual ~MediaGalleriesGetAllMediaFileSystemMetadataFunction(); 137 virtual bool RunAsync() OVERRIDE; 138 139 private: 140 // Bottom half for RunAsync, invoked after the preferences is initialized. 141 // Gets the list of permitted galleries and checks if they are available. 142 void OnPreferencesInit(); 143 144 // Callback to run upon getting the list of available devices. 145 // Sends the list of media filesystem metadata back to the extension. 146 void OnGetGalleries( 147 const MediaGalleryPrefIdSet& permitted_gallery_ids, 148 const storage_monitor::MediaStorageUtil::DeviceIdSet* available_devices); 149 }; 150 151 class MediaGalleriesAddUserSelectedFolderFunction 152 : public ChromeAsyncExtensionFunction { 153 public: 154 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addUserSelectedFolder", 155 MEDIAGALLERIES_ADDUSERSELECTEDFOLDER) 156 157 protected: 158 virtual ~MediaGalleriesAddUserSelectedFolderFunction(); 159 virtual bool RunAsync() OVERRIDE; 160 161 private: 162 // Bottom half for RunAsync, invoked after the preferences is initialized. 163 void OnPreferencesInit(); 164 165 // Callback for the directory prompt request, with the input from the user. 166 // If |selected_directory| is empty, then the user canceled. 167 // Either handle the user canceled case or add the selected gallery. 168 void OnDirectorySelected(const base::FilePath& selected_directory); 169 170 // Callback for the directory prompt request. |pref_id| is for the gallery 171 // the user just added. |filesystems| is the entire list of file systems. 172 // The fsid for the file system that corresponds to |pref_id| will be 173 // appended to the list of file systems returned to the caller. The 174 // Javascript binding for this API will interpret the list appropriately. 175 void ReturnGalleriesAndId( 176 MediaGalleryPrefId pref_id, 177 const std::vector<MediaFileSystemInfo>& filesystems); 178 179 // A helper method that calls 180 // MediaFileSystemRegistry::GetMediaFileSystemsForExtension(). 181 void GetMediaFileSystemsForExtension(const MediaFileSystemsCallback& cb); 182 }; 183 184 class MediaGalleriesDropPermissionForMediaFileSystemFunction 185 : public ChromeAsyncExtensionFunction { 186 public: 187 DECLARE_EXTENSION_FUNCTION("mediaGalleries.dropPermissionForMediaFileSystem", 188 MEDIAGALLERIES_DROPPERMISSIONFORMEDIAFILESYSTEM) 189 190 protected: 191 virtual ~MediaGalleriesDropPermissionForMediaFileSystemFunction(); 192 virtual bool RunAsync() OVERRIDE; 193 194 private: 195 // Bottom half for RunAsync, invoked after the preferences is initialized. 196 void OnPreferencesInit(MediaGalleryPrefId pref_id); 197 }; 198 199 class MediaGalleriesStartMediaScanFunction 200 : public ChromeAsyncExtensionFunction { 201 public: 202 DECLARE_EXTENSION_FUNCTION("mediaGalleries.startMediaScan", 203 MEDIAGALLERIES_STARTMEDIASCAN) 204 205 protected: 206 virtual ~MediaGalleriesStartMediaScanFunction(); 207 virtual bool RunAsync() OVERRIDE; 208 209 private: 210 // Bottom half for RunAsync, invoked after the preferences is initialized. 211 void OnPreferencesInit(); 212 }; 213 214 class MediaGalleriesCancelMediaScanFunction 215 : public ChromeAsyncExtensionFunction { 216 public: 217 DECLARE_EXTENSION_FUNCTION("mediaGalleries.cancelMediaScan", 218 MEDIAGALLERIES_CANCELMEDIASCAN) 219 220 protected: 221 virtual ~MediaGalleriesCancelMediaScanFunction(); 222 virtual bool RunAsync() OVERRIDE; 223 224 private: 225 // Bottom half for RunAsync, invoked after the preferences is initialized. 226 void OnPreferencesInit(); 227 }; 228 229 class MediaGalleriesAddScanResultsFunction 230 : public ChromeAsyncExtensionFunction { 231 public: 232 DECLARE_EXTENSION_FUNCTION("mediaGalleries.addScanResults", 233 MEDIAGALLERIES_ADDSCANRESULTS) 234 235 protected: 236 virtual ~MediaGalleriesAddScanResultsFunction(); 237 virtual bool RunAsync() OVERRIDE; 238 239 // Pulled out for testing. 240 virtual MediaGalleriesScanResultController* MakeDialog( 241 content::WebContents* web_contents, 242 const extensions::Extension& extension, 243 const base::Closure& on_finish); 244 245 private: 246 // Bottom half for RunAsync, invoked after the preferences is initialized. 247 void OnPreferencesInit(); 248 249 // Grabs galleries from the media file system registry and passes them to 250 // ReturnGalleries(). 251 void GetAndReturnGalleries(); 252 253 // Returns galleries to the caller. 254 void ReturnGalleries(const std::vector<MediaFileSystemInfo>& filesystems); 255 }; 256 257 class MediaGalleriesGetMetadataFunction : public ChromeAsyncExtensionFunction { 258 public: 259 DECLARE_EXTENSION_FUNCTION("mediaGalleries.getMetadata", 260 MEDIAGALLERIES_GETMETADATA) 261 262 protected: 263 virtual ~MediaGalleriesGetMetadataFunction(); 264 virtual bool RunAsync() OVERRIDE; 265 266 private: 267 // Bottom half for RunAsync, invoked after the preferences is initialized. 268 void OnPreferencesInit(MediaGalleries::GetMetadataType metadata_type, 269 const std::string& blob_uuid); 270 271 void GetMetadata(MediaGalleries::GetMetadataType metadata_type, 272 const std::string& blob_uuid, 273 scoped_ptr<std::string> blob_header, 274 int64 total_blob_length); 275 276 void OnSafeMediaMetadataParserDone( 277 bool parse_success, scoped_ptr<base::DictionaryValue> result_dictionary, 278 scoped_ptr<std::vector<metadata::AttachedImage> > attached_images); 279 280 void ConstructNextBlob( 281 scoped_ptr<base::DictionaryValue> result_dictionary, 282 scoped_ptr<std::vector<metadata::AttachedImage> > attached_images, 283 scoped_ptr<std::vector<std::string> > blob_uuids, 284 scoped_ptr<content::BlobHandle> current_blob); 285 }; 286 287 } // namespace extensions 288 289 #endif // CHROME_BROWSER_EXTENSIONS_API_MEDIA_GALLERIES_MEDIA_GALLERIES_API_H_ 290