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

Unified Diff: chrome/browser/extensions/extension_browser_event_router.cc

Issue 7192016: chrome.experimental.downloads (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: merged db_handle, id; onCreated, onErased Created 9 years, 5 months 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/extension_browser_event_router.cc
diff --git a/chrome/browser/extensions/extension_browser_event_router.cc b/chrome/browser/extensions/extension_browser_event_router.cc
index 66a2cf01501549ad6be4a7c921d3533c1dbc688d..8c37ed5cbcd784e3c4fb8b52bf249b52b9e2c648 100644
--- a/chrome/browser/extensions/extension_browser_event_router.cc
+++ b/chrome/browser/extensions/extension_browser_event_router.cc
@@ -20,6 +20,8 @@
#include "content/browser/tab_contents/navigation_entry.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/common/notification_service.h"
+#include "chrome/browser/download/download_manager.h"
+#include "chrome/browser/download/download_query.h"
namespace events = extension_event_names;
namespace tab_keys = extension_tabs_module_constants;
@@ -62,6 +64,80 @@ DictionaryValue* ExtensionBrowserEventRouter::TabEntry::DidNavigate(
return changed_properties;
}
+class ExtensionBrowserEventRouter::DownloadManagerObserver
+ : public DownloadManager::Observer {
+ public:
+ DownloadManagerObserver(ExtensionBrowserEventRouter* router)
+ : router_(router) {
+ DVLOG(1) << __PRETTY_FUNCTION__ << " " << this << " " << router_;
+ DCHECK(router_);
+ manager_ = router_->profile_->GetDownloadManager();
+ DCHECK(manager_);
+ manager_->AddObserver(this);
+ }
+ virtual ~DownloadManagerObserver() {
+ DVLOG(1) << __PRETTY_FUNCTION__ << " " << this << " " << router_;
+ if (manager_) manager_->RemoveObserver(this);
+ }
+
+ virtual void ModelChanged() {
+ DCHECK(manager_);
+ DVLOG(1) << __FUNCTION__ << " " << manager_ << " " << router_;
+ std::vector<DownloadItem*> items;
+ ListValue json_items;
+ manager_->Search(download_util::DownloadQuery(), &items, true, NULL, &json_items);
+ DVLOG(1) << __FUNCTION__ << " " << items.size();
+ base::hash_map<int64, DownloadItem*> updated;
+ for (std::vector<DownloadItem*>::const_iterator iter = items.begin();
+ iter != items.end(); ++iter) {
+ updated[(*iter)->id()] = *iter;
+ }
+
+ // An item is new if it's in items but not downloads_.
+ for (ListValue::const_iterator iter = json_items.begin();
+ iter != json_items.end(); ++iter) {
+ int item_id = -1;
+ DCHECK(((DictionaryValue*)(*iter))->GetInteger("id", &item_id));
+ base::hash_map<int64, DownloadItem*>::const_iterator found = downloads_.find(item_id);
+ if (found == downloads_.end()) {
+ DispatchEvent(events::kOnDownloadCreated, (*iter));
+ downloads_[item_id] = updated[item_id];
+ }
+ }
+
+ // An item was erased if it's in downloads_ but not updated.
+ for (base::hash_map<int64, DownloadItem*>::iterator iter = downloads_.begin();
+ iter != downloads_.end(); ++iter) {
+ if (updated.find(iter->first) == updated.end()) {
+ DispatchEvent(events::kOnDownloadErased, Value::CreateIntegerValue(iter->first));
+ downloads_.erase(iter);
+ }
+ }
+
+ // TODO assert that updated == downloads_
+ }
+
+ virtual void ManagerGoingDown() {
+ DVLOG(1) << __PRETTY_FUNCTION__ << " " << this << " " << router_ << " " << manager_;
+ manager_ = NULL;
+ }
+
+ private:
+ void DispatchEvent(const char* const event, Value* arg) {
+ ListValue args;
+ args.Append(arg);
+ std::string json_args;
+ base::JSONWriter::Write(&args, false, &json_args);
+ router_->DispatchEvent(router_->profile_, event, json_args);
+ }
+
+ ExtensionBrowserEventRouter* router_;
+ DownloadManager* manager_;
+ base::hash_map<int64, DownloadItem*> downloads_;
+
+ DISALLOW_COPY_AND_ASSIGN(DownloadManagerObserver);
+};
+
void ExtensionBrowserEventRouter::Init() {
if (initialized_)
return;
@@ -93,6 +169,7 @@ void ExtensionBrowserEventRouter::Init() {
}
}
}
+ download_manager_observer_.reset(new DownloadManagerObserver(this));
initialized_ = true;
}
« no previous file with comments | « chrome/browser/extensions/extension_browser_event_router.h ('k') | chrome/browser/extensions/extension_downloads.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698