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