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

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

Issue 8203005: Implement chrome.experimental.downloads.onChanged (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: mixin Created 9 years, 2 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_downloads_api.cc
diff --git a/chrome/browser/extensions/extension_downloads_api.cc b/chrome/browser/extensions/extension_downloads_api.cc
index 2182b000fdcf62ef0ff1d153824ef259c545a0f2..541ff546c2389d40088ed0cd4b79144ab2a72b4c 100644
--- a/chrome/browser/extensions/extension_downloads_api.cc
+++ b/chrome/browser/extensions/extension_downloads_api.cc
@@ -414,16 +414,51 @@ base::DictionaryValue* DownloadItemToJSON(DownloadItem* item) {
ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter(
Profile* profile)
: profile_(profile),
- manager_(profile ? profile->GetDownloadManager() : NULL) {
+ manager_(profile ? profile->GetDownloadManager() : NULL),
+ delete_item_jsons_(&item_jsons_) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- DCHECK(profile_);
- DCHECK(manager_);
manager_->AddObserver(this);
}
ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() {
if (manager_ != NULL)
manager_->RemoveObserver(this);
+ for (ItemMap::const_iterator iter = downloads_.begin();
+ iter != downloads_.end(); ++iter) {
+ iter->second->RemoveObserver(this);
+ }
+}
+
+void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) {
+ if (item->state() == DownloadItem::REMOVING)
+ return;
Randy Smith (Not in Mondays) 2011/10/13 23:42:59 I feel a bit uncomfortable about keeping a Downloa
benjhayden 2011/10/17 19:14:00 Done.
+ base::DictionaryValue* current_json = item_jsons_[item->id()];
+ scoped_ptr<base::DictionaryValue> new_json(DownloadItemToJSON(item));
+ scoped_ptr<base::DictionaryValue> delta(new base::DictionaryValue());
+ delta->SetInteger(constants::kIdKey, item->id());
+ bool changed = false;
+ for (base::DictionaryValue::key_iterator key = new_json->begin_keys();
+ key != new_json->end_keys(); ++key) {
+ if (*key != constants::kBytesReceivedKey) {
+ base::Value* new_value = NULL;
+ base::Value* old_value = NULL;
+ if (new_json->Get(*key, &new_value) &&
+ (!current_json->HasKey(*key) ||
+ (current_json->Get(*key, &old_value) &&
+ !new_value->Equals(old_value)))) {
+ delta->Set(*key + ".old", (old_value ? old_value->DeepCopy()
+ : base::Value::CreateNullValue()));
+ delta->Set(*key + ".new", new_value->DeepCopy());
+ changed = true;
+ }
+ }
+ }
+ if (changed)
+ DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release());
+ current_json->Swap(new_json.get());
+}
+
+void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) {
}
void ExtensionDownloadsEventRouter::ModelChanged() {
@@ -432,7 +467,11 @@ void ExtensionDownloadsEventRouter::ModelChanged() {
return;
DownloadManager::DownloadVector current_vec;
manager_->SearchDownloads(string16(), &current_vec);
- DownloadIdSet current_set;
+ DownloadIdSet current_set, prev_set;
+ for (ItemMap::const_iterator iter = downloads_.begin();
+ iter != downloads_.end(); ++iter) {
+ prev_set.insert(iter->first);
+ }
ItemMap current_map;
for (DownloadManager::DownloadVector::const_iterator iter =
current_vec.begin();
@@ -446,28 +485,33 @@ void ExtensionDownloadsEventRouter::ModelChanged() {
current_set.insert(item_id);
current_map[item_id] = item;
}
- DownloadIdSet new_set; // current_set - downloads_;
- DownloadIdSet erased_set; // downloads_ - current_set;
+ DownloadIdSet new_set; // current_set - prev_set;
+ DownloadIdSet erased_set; // prev_set - current_set;
std::insert_iterator<DownloadIdSet> new_insertor(new_set, new_set.begin());
std::insert_iterator<DownloadIdSet> erased_insertor(
erased_set, erased_set.begin());
std::set_difference(current_set.begin(), current_set.end(),
- downloads_.begin(), downloads_.end(),
+ prev_set.begin(), prev_set.end(),
new_insertor);
- std::set_difference(downloads_.begin(), downloads_.end(),
+ std::set_difference(prev_set.begin(), prev_set.end(),
current_set.begin(), current_set.end(),
erased_insertor);
for (DownloadIdSet::const_iterator iter = new_set.begin();
iter != new_set.end(); ++iter) {
DispatchEvent(extension_event_names::kOnDownloadCreated,
DownloadItemToJSON(current_map[*iter]));
+ DCHECK(item_jsons_.find(*iter) == item_jsons_.end());
+ item_jsons_[*iter] = DownloadItemToJSON(current_map[*iter]);
+ current_map[*iter]->AddObserver(this);
}
for (DownloadIdSet::const_iterator iter = erased_set.begin();
iter != erased_set.end(); ++iter) {
DispatchEvent(extension_event_names::kOnDownloadErased,
base::Value::CreateIntegerValue(*iter));
+ delete item_jsons_[*iter];
+ item_jsons_.erase(*iter);
}
- downloads_.swap(current_set);
+ downloads_.swap(current_map);
}
void ExtensionDownloadsEventRouter::ManagerGoingDown() {

Powered by Google App Engine
This is Rietveld 408576698