Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1631)

Unified Diff: chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api.cc

Issue 11535008: Implement mediaGalleriesPrivate api to notify extensions about gallery changed events. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review comments Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api.cc
diff --git a/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api.cc b/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api.cc
index 7b83d03e1965dfab2ae8b9a50270de5671c84f2b..2fd029f9da8b5ed08de405b7f08580ae7732f171 100644
--- a/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api.cc
+++ b/chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api.cc
@@ -4,21 +4,121 @@
#include "chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api.h"
-#include "chrome/browser/extensions/api/media_galleries_private/media_galleries_private_api_factory.h"
+#include "base/bind.h"
+#include "base/file_path.h"
+#include "base/location.h"
+#include "base/string_number_conversions.h"
+#include "chrome/browser/browser_process.h"
+#include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_manager.h"
#include "chrome/browser/extensions/api/media_galleries_private/media_galleries_private_event_router.h"
+#include "chrome/browser/extensions/api/media_galleries_private/media_gallery_extension_notification_observer.h"
#include "chrome/browser/extensions/event_names.h"
#include "chrome/browser/extensions/event_router.h"
#include "chrome/browser/extensions/extension_system.h"
+#include "chrome/browser/media_gallery/media_file_system_registry.h"
+#include "chrome/browser/media_gallery/media_galleries_preferences.h"
+#include "chrome/browser/system_monitor/media_storage_util.h"
+#include "chrome/common/extensions/api/media_galleries_private.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/render_view_host.h"
namespace extensions {
+namespace {
+
+// Returns the absolute file path of the gallery specified by the |gallery_id|.
+// Returns an empty file path if the |gallery_id| is invalid.
+FilePath GetGalleryAbsolutePath(chrome::MediaGalleryPrefId gallery_id,
+ const Profile* profile,
+ const Extension* extension) {
+ DCHECK(profile);
+ DCHECK(extension);
+ chrome::MediaFileSystemRegistry* registry =
+ g_browser_process->media_file_system_registry();
+ DCHECK(registry);
+ chrome::MediaGalleriesPreferences* preferences =
+ registry->GetPreferences(const_cast<Profile*>(profile));
Lei Zhang 2012/12/18 00:47:01 Why do you take a Profile*, pass it in as a const
kmadhusu 2012/12/18 21:32:39 Fixed. Passing Profile* as param.
+ if (!preferences)
+ return FilePath();
+
+ const chrome::MediaGalleryPrefIdSet permitted_galleries =
+ preferences->GalleriesForExtension(*extension);
+ if (permitted_galleries.empty() ||
+ permitted_galleries.find(gallery_id) == permitted_galleries.end())
+ return FilePath();
+
+ const chrome::MediaGalleriesPrefInfoMap& galleries_info =
+ preferences->known_galleries();
+ return chrome::MediaStorageUtil::FindDevicePathById(
+ galleries_info.find(gallery_id)->second.device_id);
+}
+
+// Returns GalleryWatchManager associated with the specified |profile|. Set
+// |create| to true, to create a GalleryWatchManager if it does not exists.
+GalleryWatchManager* GetGalleryWatchManagerOnFileThread(const Profile* profile,
Lei Zhang 2012/12/18 00:47:01 Is this really needed? Add can just do: GalleryWa
kmadhusu 2012/12/18 21:32:39 Removed.
+ bool create) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
+ if (!create && !GalleryWatchManager::HasForProfile(profile))
+ return NULL;
+ return GalleryWatchManager::GetForProfile(profile);
+}
+
+// Handles the profile shutdown event on the file thread to clean up
+// GalleryWatchManager.
+void HandleProfileShutdownOnFileThread(const Profile* profile) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
+ GalleryWatchManager::OnProfileShutdown(profile);
+}
+
+// Sets up a gallery watch on the file thread for the extension specified by the
+// |extension_id|.
+// |profile| specifies the extension profile.
+// |gallery_id| specifies the gallery identifier.
+// |gallery_file_path| specifies the absolute gallery path.
+// Returns true, if the watch setup operation was successful.
+bool SetupGalleryWatchOnFileThread(const Profile* profile,
+ const std::string& gallery_id,
+ const FilePath& gallery_file_path,
+ const std::string& extension_id) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
+ GalleryWatchManager* manager = GetGalleryWatchManagerOnFileThread(profile,
+ true);
+ DCHECK(manager);
+ return manager->StartGalleryWatch(gallery_id, gallery_file_path,
+ extension_id);
+}
+
+// Cancels the gallery watch for the extension specified by the |extension_id|
+// on the file thread.
+void RemoveGalleryWatchOnFileThread(const Profile* profile,
+ const FilePath& gallery_file_path,
+ const std::string& extension_id) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
+ GalleryWatchManager* manager = GetGalleryWatchManagerOnFileThread(profile,
+ false);
+ if (!manager)
+ return;
+ manager->StopGalleryWatch(gallery_file_path, extension_id);
+}
+
+} // namespace
+
+
+///////////////////////////////////////////////////////////////////////////////
+// MediaGalleriesPrivateAPI //
+///////////////////////////////////////////////////////////////////////////////
+
MediaGalleriesPrivateAPI::MediaGalleriesPrivateAPI(Profile* profile)
: profile_(profile) {
+ DCHECK(profile_);
+ extension_notification_observer_.reset(
+ new MediaGalleryExtensionNotificationObserver(profile_));
ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
this, event_names::kOnAttachEventName);
- ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
+ ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
this, event_names::kOnDetachEventName);
-
+ ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
+ this, event_names::kOnGalleryChangedEventName);
}
MediaGalleriesPrivateAPI::~MediaGalleriesPrivateAPI() {
@@ -26,13 +126,111 @@ MediaGalleriesPrivateAPI::~MediaGalleriesPrivateAPI() {
void MediaGalleriesPrivateAPI::Shutdown() {
ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
+ content::BrowserThread::PostTask(
+ content::BrowserThread::FILE, FROM_HERE,
+ base::Bind(&HandleProfileShutdownOnFileThread, profile_));
}
void MediaGalleriesPrivateAPI::OnListenerAdded(
- const extensions::EventListenerInfo& details) {
+ const EventListenerInfo& details) {
media_galleries_private_event_router_.reset(
new MediaGalleriesPrivateEventRouter(profile_));
- ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+// MediaGalleriesPrivateAddGalleryWatchFunction //
+///////////////////////////////////////////////////////////////////////////////
+
+bool MediaGalleriesPrivateAddGalleryWatchFunction::RunImpl() {
+ DCHECK(profile_);
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ if (!render_view_host() || !render_view_host()->GetProcess())
+ return false;
+
+ // First param is the gallery identifier to watch.
+ std::string gallery_id;
+ if (!args_->GetString(0, &gallery_id) || (gallery_id.empty()))
+ return false;
+
+ chrome::MediaGalleryPrefId gallery_pref_id = 0;
+ if (!base::StringToUint64(gallery_id, &gallery_pref_id)) {
+ HandleResponse(gallery_id, false);
+ return true;
+ }
+
+ FilePath gallery_file_path(
+ GetGalleryAbsolutePath(gallery_pref_id, profile_, GetExtension()));
+ if (gallery_file_path.empty()) {
+ HandleResponse(gallery_id, false);
+ return true;
+ }
+
+#if defined(OS_WIN)
+ content::BrowserThread::PostTaskAndReplyWithResult(
+ content::BrowserThread::FILE,
+ FROM_HERE,
+ base::Bind(&SetupGalleryWatchOnFileThread,
+ profile_,
+ gallery_id,
+ gallery_file_path,
+ extension_id()),
+ base::Bind(&MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse,
+ this,
+ gallery_id));
+#else
+ // Recursive gallery watch operation is not currently supported on
+ // non-windows platforms. Please refer to crbug.com/144491 for more details.
+ HandleResponse(gallery_id, false);
+#endif
+ return true;
+}
+
+void MediaGalleriesPrivateAddGalleryWatchFunction::HandleResponse(
+ const std::string& gallery_id,
+ bool success) {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ extensions::api::media_galleries_private::AddGalleryWatchResult result;
+ result.gallery_id = gallery_id;
+ result.success = success;
+ SetResult(result.ToValue().release());
+ SendResponse(true);
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+// MediaGalleriesPrivateRemoveGalleryWatchFunction //
+///////////////////////////////////////////////////////////////////////////////
+
+bool MediaGalleriesPrivateRemoveGalleryWatchFunction::RunImpl() {
+ DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+#if defined(OS_WIN)
+ // Remove gallery watch operation is currently supported on windows platforms.
+ // Please refer to crbug.com/144491 for more details.
+ if (!render_view_host() || !render_view_host()->GetProcess())
+ return false;
+
+ // First param is the gallery identifier.
+ std::string gallery_id;
+ if (!args_->GetString(0, &gallery_id) || (gallery_id.empty()))
Lei Zhang 2012/12/18 00:47:01 Again, look in out/Debug/gen/chrome/common/extensi
kmadhusu 2012/12/18 21:32:39 Done.
+ return false;
+
+ chrome::MediaGalleryPrefId gallery_pref_id = 0;
+ if (!base::StringToUint64(gallery_id, &gallery_pref_id))
+ return false;
+
+ FilePath gallery_file_path(
+ GetGalleryAbsolutePath(gallery_pref_id, profile_, GetExtension()));
+ if (gallery_file_path.empty())
+ return false;
+
+ content::BrowserThread::PostTask(
+ content::BrowserThread::FILE, FROM_HERE,
+ base::Bind(&RemoveGalleryWatchOnFileThread,
+ profile_,
+ gallery_file_path,
+ extension_id()));
+#endif
+ return true;
}
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698