Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/extension_downloads_api.h" | 5 #include "chrome/browser/extensions/extension_downloads_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cctype> | 8 #include <cctype> |
| 9 #include <iterator> | 9 #include <iterator> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 // TODO(benjhayden): Implement endTime and fileSize. | 407 // TODO(benjhayden): Implement endTime and fileSize. |
| 408 // json->SetInteger(constants::kEndTimeKey, -1); | 408 // json->SetInteger(constants::kEndTimeKey, -1); |
| 409 json->SetInteger(constants::kFileSizeKey, item->total_bytes()); | 409 json->SetInteger(constants::kFileSizeKey, item->total_bytes()); |
| 410 return json; | 410 return json; |
| 411 } | 411 } |
| 412 } // anonymous namespace | 412 } // anonymous namespace |
| 413 | 413 |
| 414 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( | 414 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( |
| 415 Profile* profile) | 415 Profile* profile) |
| 416 : profile_(profile), | 416 : profile_(profile), |
| 417 manager_(profile ? profile->GetDownloadManager() : NULL) { | 417 manager_(profile ? profile->GetDownloadManager() : NULL), |
| 418 delete_item_observers_(&item_observers_) { | |
| 418 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 419 DCHECK(profile_); | |
| 420 DCHECK(manager_); | |
| 421 manager_->AddObserver(this); | 420 manager_->AddObserver(this); |
| 422 } | 421 } |
| 423 | 422 |
| 424 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { | 423 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { |
| 425 if (manager_ != NULL) | 424 if (manager_ != NULL) |
| 426 manager_->RemoveObserver(this); | 425 manager_->RemoveObserver(this); |
| 427 } | 426 } |
| 428 | 427 |
| 428 ExtensionDownloadsEventRouter::ItemObserver::ItemObserver( | |
| 429 const EventDispatcher& dispatcher, DownloadItem* item) | |
| 430 : dispatcher_(dispatcher), | |
| 431 item_(item), | |
| 432 json_(DownloadItemToJSON(item_)) { | |
| 433 item_->AddObserver(this); | |
| 434 } | |
| 435 | |
| 436 ExtensionDownloadsEventRouter::ItemObserver::~ItemObserver() { | |
| 437 if (item_ != NULL) | |
| 438 item_->RemoveObserver(this); | |
| 439 } | |
| 440 | |
| 441 void ExtensionDownloadsEventRouter::ItemObserver::OnDownloadUpdated( | |
|
Randy Smith (Not in Mondays)
2011/10/12 18:46:30
This amount of work on each DownloadItem updated s
benjhayden
2011/10/13 16:30:29
This isn't quite as much work as it looks like. Th
Randy Smith (Not in Mondays)
2011/10/13 23:42:59
Yep, that's what I meant.
benjhayden
2011/10/17 19:14:00
I just added a uma stat to count the number of OnD
Randy Smith (Not in Mondays)
2011/10/18 21:15:32
Sounds good; thanks.
| |
| 442 DownloadItem* item) { | |
| 443 CHECK_EQ(item, item_); | |
| 444 if (item_->state() == DownloadItem::REMOVING) | |
| 445 return; | |
| 446 scoped_ptr<base::DictionaryValue> new_json(DownloadItemToJSON(item_)); | |
| 447 base::DictionaryValue* delta = new base::DictionaryValue(); | |
| 448 delta->SetInteger(constants::kIdKey, item_->id()); | |
| 449 bool changed = false; | |
| 450 for (base::DictionaryValue::key_iterator key = new_json->begin_keys(); | |
| 451 key != new_json->end_keys(); ++key) { | |
| 452 if (*key != constants::kBytesReceivedKey) { | |
| 453 base::Value* new_value = NULL; | |
| 454 base::Value* old_value = NULL; | |
| 455 if (new_json->Get(*key, &new_value) && | |
| 456 (!json_->HasKey(*key) || | |
| 457 (json_->Get(*key, &old_value) && | |
| 458 !new_value->Equals(old_value)))) { | |
| 459 delta->Set(*key + ".old", (old_value ? old_value->DeepCopy() | |
| 460 : base::Value::CreateNullValue())); | |
| 461 delta->Set(*key + ".new", new_value->DeepCopy()); | |
| 462 changed = true; | |
| 463 } | |
| 464 } | |
| 465 } | |
| 466 if (changed) | |
| 467 dispatcher_.Run(delta); | |
| 468 json_.swap(new_json); | |
| 469 } | |
| 470 | |
| 471 void ExtensionDownloadsEventRouter::ItemObserver::OnDownloadOpened( | |
| 472 DownloadItem* item) { | |
| 473 CHECK_EQ(item, item_); | |
| 474 } | |
| 475 | |
| 429 void ExtensionDownloadsEventRouter::ModelChanged() { | 476 void ExtensionDownloadsEventRouter::ModelChanged() { |
| 430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 431 if (manager_ == NULL) | 478 if (manager_ == NULL) |
| 432 return; | 479 return; |
| 433 DownloadManager::DownloadVector current_vec; | 480 DownloadManager::DownloadVector current_vec; |
| 434 manager_->SearchDownloads(string16(), ¤t_vec); | 481 manager_->SearchDownloads(string16(), ¤t_vec); |
| 435 DownloadIdSet current_set; | 482 DownloadIdSet current_set; |
| 436 ItemMap current_map; | 483 ItemMap current_map; |
| 437 for (DownloadManager::DownloadVector::const_iterator iter = | 484 for (DownloadManager::DownloadVector::const_iterator iter = |
| 438 current_vec.begin(); | 485 current_vec.begin(); |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 454 std::set_difference(current_set.begin(), current_set.end(), | 501 std::set_difference(current_set.begin(), current_set.end(), |
| 455 downloads_.begin(), downloads_.end(), | 502 downloads_.begin(), downloads_.end(), |
| 456 new_insertor); | 503 new_insertor); |
| 457 std::set_difference(downloads_.begin(), downloads_.end(), | 504 std::set_difference(downloads_.begin(), downloads_.end(), |
| 458 current_set.begin(), current_set.end(), | 505 current_set.begin(), current_set.end(), |
| 459 erased_insertor); | 506 erased_insertor); |
| 460 for (DownloadIdSet::const_iterator iter = new_set.begin(); | 507 for (DownloadIdSet::const_iterator iter = new_set.begin(); |
| 461 iter != new_set.end(); ++iter) { | 508 iter != new_set.end(); ++iter) { |
| 462 DispatchEvent(extension_event_names::kOnDownloadCreated, | 509 DispatchEvent(extension_event_names::kOnDownloadCreated, |
| 463 DownloadItemToJSON(current_map[*iter])); | 510 DownloadItemToJSON(current_map[*iter])); |
| 511 DCHECK(item_observers_.find(*iter) == item_observers_.end()); | |
| 512 item_observers_[*iter] = new ItemObserver(base::Bind( | |
| 513 &ExtensionDownloadsEventRouter::DispatchEvent, base::Unretained(this), | |
| 514 extension_event_names::kOnDownloadChanged), current_map[*iter]); | |
| 464 } | 515 } |
| 465 for (DownloadIdSet::const_iterator iter = erased_set.begin(); | 516 for (DownloadIdSet::const_iterator iter = erased_set.begin(); |
| 466 iter != erased_set.end(); ++iter) { | 517 iter != erased_set.end(); ++iter) { |
| 467 DispatchEvent(extension_event_names::kOnDownloadErased, | 518 DispatchEvent(extension_event_names::kOnDownloadErased, |
| 468 base::Value::CreateIntegerValue(*iter)); | 519 base::Value::CreateIntegerValue(*iter)); |
| 520 delete item_observers_[*iter]; | |
| 521 item_observers_.erase(*iter); | |
| 469 } | 522 } |
| 470 downloads_.swap(current_set); | 523 downloads_.swap(current_set); |
| 471 } | 524 } |
| 472 | 525 |
| 473 void ExtensionDownloadsEventRouter::ManagerGoingDown() { | 526 void ExtensionDownloadsEventRouter::ManagerGoingDown() { |
| 474 manager_->RemoveObserver(this); | 527 manager_->RemoveObserver(this); |
| 475 manager_ = NULL; | 528 manager_ = NULL; |
| 476 } | 529 } |
| 477 | 530 |
| 478 void ExtensionDownloadsEventRouter::DispatchEvent( | 531 void ExtensionDownloadsEventRouter::DispatchEvent( |
| 479 const char* event_name, base::Value* arg) { | 532 const char* event_name, base::Value* arg) { |
| 480 ListValue args; | 533 ListValue args; |
| 481 args.Append(arg); | 534 args.Append(arg); |
| 482 std::string json_args; | 535 std::string json_args; |
| 483 base::JSONWriter::Write(&args, false, &json_args); | 536 base::JSONWriter::Write(&args, false, &json_args); |
| 484 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 537 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 485 event_name, | 538 event_name, |
| 486 json_args, | 539 json_args, |
| 487 profile_, | 540 profile_, |
| 488 GURL()); | 541 GURL()); |
| 489 } | 542 } |
| OLD | NEW |