Chromium Code Reviews| Index: chrome/browser/extensions/api/media_galleries_private/gallery_watch_manager.cc |
| diff --git a/chrome/browser/extensions/api/media_galleries_private/gallery_watch_manager.cc b/chrome/browser/extensions/api/media_galleries_private/gallery_watch_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e48405bbc4c5cfb81bae76e7675600a01907fb90 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/media_galleries_private/gallery_watch_manager.cc |
| @@ -0,0 +1,223 @@ |
| +// 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. |
| +// |
| +// GalleryWatchManager implementation. |
| + |
| +#include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_manager.h" |
| + |
| +#include <list> |
| + |
| +#include "base/bind.h" |
| +#include "base/compiler_specific.h" |
| +#include "base/location.h" |
| +#include "base/stl_util.h" |
| +#include "base/time.h" |
| +#include "chrome/browser/extensions/api/media_galleries_private/gallery_watch_manager_factory.h" |
| +#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 "chrome/browser/extensions/api/media_galleries_private/media_galleries_private_event_router.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| + |
| +using content::BrowserThread; |
| + |
| +// Dispatches the gallery changed event on the UI thread. |
| +void SendGalleryChangedEventOnUIThread( |
| + const Profile* profile, |
| + const std::string& gallery_id, |
| + const std::set<std::string>& extension_ids) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + DCHECK(profile); |
| + MediaGalleriesPrivateEventRouter* router = |
| + MediaGalleriesPrivateAPIFactory::GetForProfile( |
| + const_cast<Profile*>(profile))->event_router(); |
| + if (!router) |
| + return; |
| + router->OnGalleryChanged(gallery_id, extension_ids); |
| +} |
| + |
| +} // namespace |
| + |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| +// GalleryWatchManager // |
| +/////////////////////////////////////////////////////////////////////////////// |
| + |
| +GalleryWatchManager::GalleryWatchManager(const Profile* profile) |
| + : profile_(profile) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| +} |
| + |
| +GalleryWatchManager::~GalleryWatchManager() { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + if (!gallery_watchers_.empty()) { |
| + // Profile is in shutdown mode. |
| + STLDeleteValues(&gallery_watchers_); |
| + } |
| +} |
| + |
| +bool GalleryWatchManager::StartGalleryWatch( |
| + const std::string& gallery_id, |
| + const FilePath& watch_path, |
| + const std::string& extension_id) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + WatcherMap::const_iterator iter = gallery_watchers_.find(watch_path); |
|
Lei Zhang
2012/12/15 04:13:21
It's easier to understand if you write the code as
kmadhusu
2012/12/17 23:58:05
Done.
|
| + if (iter == gallery_watchers_.end()) { |
| + scoped_ptr<GalleryFilePathWatcher> watch( |
| + new GalleryFilePathWatcher(profile_, gallery_id, watch_path, |
| + extension_id)); |
| + |
| + if (watch->SetupWatch()) |
| + gallery_watchers_[watch_path] = watch.release(); |
| + else |
| + return false; |
| + } else { |
| + iter->second->AddExtension(extension_id); |
| + } |
| + return true; |
| +} |
| + |
| +void GalleryWatchManager::StopGalleryWatch( |
| + const FilePath& watch_path, |
| + const std::string& extension_id) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + WatcherMap::iterator iter = gallery_watchers_.find(watch_path); |
| + if (iter == gallery_watchers_.end()) |
| + return; |
| + // Remove the renderer process for this watch. |
| + iter->second->RemoveExtension(extension_id); |
| + if (iter->second->GetRefCount() == 0) { |
| + delete iter->second; |
| + gallery_watchers_.erase(iter); |
| + } |
| +} |
| + |
| +void GalleryWatchManager::OnExtensionDestroyed( |
| + const std::string& extension_id) { |
| + DCHECK(thread_checker_.CalledOnValidThread()); |
| + std::list<FilePath> watchers_to_erase; |
| + for (WatcherMap::iterator iter = gallery_watchers_.begin(); |
| + iter != gallery_watchers_.end(); ++iter) { |
| + // Remove the renderer process for this watch. |
| + iter->second->OnExtensionDestroyed(extension_id); |
| + if (iter->second->GetRefCount() == 0) |
| + watchers_to_erase.push_back(iter->first); |
| + } |
| + |
| + for (std::list<FilePath>::const_iterator path = watchers_to_erase.begin(); |
| + path != watchers_to_erase.end(); ++path) { |
| + WatcherMap::iterator iter = gallery_watchers_.find(*path); |
| + DCHECK(iter != gallery_watchers_.end()); |
| + delete iter->second; |
| + gallery_watchers_.erase(iter); |
| + } |
| +} |
| + |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| +// GalleryWatchManager::GalleryFilePathWatcher // |
| +/////////////////////////////////////////////////////////////////////////////// |
| + |
| +GalleryWatchManager::GalleryFilePathWatcher::GalleryFilePathWatcher( |
| + const Profile* profile, |
| + const std::string& gallery_id, |
| + const FilePath& path, |
| + const std::string& extension_id) |
| + : profile_(profile), |
| + gallery_id_(gallery_id), |
| + ref_count_(0), |
| + weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + file_watcher_.reset(new base::files::FilePathWatcher()); |
| + gallery_path_ = path; |
| + AddExtension(extension_id); |
| +} |
| + |
| +GalleryWatchManager::GalleryFilePathWatcher::~GalleryFilePathWatcher() { |
| + DCHECK(thread_check_.CalledOnValidThread()); |
| +} |
| + |
| +void GalleryWatchManager::GalleryFilePathWatcher::AddExtension( |
| + const std::string& extension_id) { |
| + DCHECK(thread_check_.CalledOnValidThread()); |
| + ExtensionUsageRegistry::iterator it = extensions_.find(extension_id); |
| + if (it != extensions_.end()) |
| + it->second++; |
| + else |
| + extensions_.insert(ExtensionUsageRegistry::value_type(extension_id, 1)); |
| + ref_count_++; |
| +} |
| + |
| +void GalleryWatchManager::GalleryFilePathWatcher::RemoveExtension( |
| + const std::string& extension_id) { |
| + DCHECK(thread_check_.CalledOnValidThread()); |
| + ExtensionUsageRegistry::iterator it = extensions_.find(extension_id); |
| + if (it == extensions_.end()) |
| + return; |
| + // If entry found - decrease it's count and remove if necessary |
| + it->second--; |
| + if (0 == it->second) |
| + extensions_.erase(it); |
| + ref_count_--; |
| +} |
| + |
| +void GalleryWatchManager::GalleryFilePathWatcher::OnExtensionDestroyed( |
| + const std::string& extension_id) { |
| + DCHECK(thread_check_.CalledOnValidThread()); |
| + ExtensionUsageRegistry::iterator it = extensions_.find(extension_id); |
| + if (it == extensions_.end()) |
| + return; |
| + ref_count_ -= it->second; |
| + extensions_.erase(it); |
| +} |
| + |
| +unsigned int GalleryWatchManager::GalleryFilePathWatcher::GetRefCount() const { |
| + DCHECK(thread_check_.CalledOnValidThread()); |
| + return ref_count_; |
| +} |
| + |
| +bool GalleryWatchManager::GalleryFilePathWatcher::SetupWatch() { |
| + DCHECK(thread_check_.CalledOnValidThread()); |
| + return file_watcher_->Watch( |
| + gallery_path_, true, |
| + base::Bind( |
| + &GalleryWatchManager::GalleryFilePathWatcher::OnFilePathChanged, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void GalleryWatchManager::GalleryFilePathWatcher::OnFilePathChanged( |
| + const FilePath& path, |
| + bool error) { |
| + DCHECK(thread_check_.CalledOnValidThread()); |
| + if (error || (path != gallery_path_)) |
| + return; |
| + |
| + if (!last_gallery_changed_event_.is_null()) { |
| + // Ignore gallery change event if it is received too frequently. |
| + // For example, when an user copies/deletes 1000 media files from a gallery, |
| + // this callback is called 1000 times within a span of 10ms. |
| + // GalleryWatchManager should not send 1000 gallery changed events to |
| + // the watching extension. |
| + const int kMinSecondsToIgnoreGalleryChangedEvent = 3; |
| + base::TimeDelta diff = base::Time::Now() - last_gallery_changed_event_; |
| + if (diff.InSeconds() < kMinSecondsToIgnoreGalleryChangedEvent) |
| + return; |
| + } |
| + |
| + last_gallery_changed_event_ = base::Time::Now(); |
| + ExtensionIdSet extension_ids; |
| + for (ExtensionUsageRegistry::const_iterator iter = extensions_.begin(); |
| + iter != extensions_.end(); ++iter) |
| + extension_ids.insert(iter->first); |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(SendGalleryChangedEventOnUIThread, profile_, gallery_id_, |
| + extension_ids)); |
| +} |
| + |
| +} // namespace extensions |