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_jsons_(&item_jsons_) { | |
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); |
426 for (ItemMap::const_iterator iter = downloads_.begin(); | |
427 iter != downloads_.end(); ++iter) { | |
428 iter->second->RemoveObserver(this); | |
429 } | |
430 } | |
431 | |
432 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { | |
433 if (item->state() == DownloadItem::REMOVING) | |
434 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.
| |
435 base::DictionaryValue* current_json = item_jsons_[item->id()]; | |
436 scoped_ptr<base::DictionaryValue> new_json(DownloadItemToJSON(item)); | |
437 scoped_ptr<base::DictionaryValue> delta(new base::DictionaryValue()); | |
438 delta->SetInteger(constants::kIdKey, item->id()); | |
439 bool changed = false; | |
440 for (base::DictionaryValue::key_iterator key = new_json->begin_keys(); | |
441 key != new_json->end_keys(); ++key) { | |
442 if (*key != constants::kBytesReceivedKey) { | |
443 base::Value* new_value = NULL; | |
444 base::Value* old_value = NULL; | |
445 if (new_json->Get(*key, &new_value) && | |
446 (!current_json->HasKey(*key) || | |
447 (current_json->Get(*key, &old_value) && | |
448 !new_value->Equals(old_value)))) { | |
449 delta->Set(*key + ".old", (old_value ? old_value->DeepCopy() | |
450 : base::Value::CreateNullValue())); | |
451 delta->Set(*key + ".new", new_value->DeepCopy()); | |
452 changed = true; | |
453 } | |
454 } | |
455 } | |
456 if (changed) | |
457 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); | |
458 current_json->Swap(new_json.get()); | |
459 } | |
460 | |
461 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) { | |
427 } | 462 } |
428 | 463 |
429 void ExtensionDownloadsEventRouter::ModelChanged() { | 464 void ExtensionDownloadsEventRouter::ModelChanged() { |
430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 465 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
431 if (manager_ == NULL) | 466 if (manager_ == NULL) |
432 return; | 467 return; |
433 DownloadManager::DownloadVector current_vec; | 468 DownloadManager::DownloadVector current_vec; |
434 manager_->SearchDownloads(string16(), ¤t_vec); | 469 manager_->SearchDownloads(string16(), ¤t_vec); |
435 DownloadIdSet current_set; | 470 DownloadIdSet current_set, prev_set; |
471 for (ItemMap::const_iterator iter = downloads_.begin(); | |
472 iter != downloads_.end(); ++iter) { | |
473 prev_set.insert(iter->first); | |
474 } | |
436 ItemMap current_map; | 475 ItemMap current_map; |
437 for (DownloadManager::DownloadVector::const_iterator iter = | 476 for (DownloadManager::DownloadVector::const_iterator iter = |
438 current_vec.begin(); | 477 current_vec.begin(); |
439 iter != current_vec.end(); ++iter) { | 478 iter != current_vec.end(); ++iter) { |
440 DownloadItem* item = *iter; | 479 DownloadItem* item = *iter; |
441 int item_id = item->id(); | 480 int item_id = item->id(); |
442 // TODO(benjhayden): Remove the following line when every item's id >= 0, | 481 // TODO(benjhayden): Remove the following line when every item's id >= 0, |
443 // which will allow firing onErased events for items from the history. | 482 // which will allow firing onErased events for items from the history. |
444 if (item_id < 0) continue; | 483 if (item_id < 0) continue; |
445 DCHECK(current_map.find(item_id) == current_map.end()); | 484 DCHECK(current_map.find(item_id) == current_map.end()); |
446 current_set.insert(item_id); | 485 current_set.insert(item_id); |
447 current_map[item_id] = item; | 486 current_map[item_id] = item; |
448 } | 487 } |
449 DownloadIdSet new_set; // current_set - downloads_; | 488 DownloadIdSet new_set; // current_set - prev_set; |
450 DownloadIdSet erased_set; // downloads_ - current_set; | 489 DownloadIdSet erased_set; // prev_set - current_set; |
451 std::insert_iterator<DownloadIdSet> new_insertor(new_set, new_set.begin()); | 490 std::insert_iterator<DownloadIdSet> new_insertor(new_set, new_set.begin()); |
452 std::insert_iterator<DownloadIdSet> erased_insertor( | 491 std::insert_iterator<DownloadIdSet> erased_insertor( |
453 erased_set, erased_set.begin()); | 492 erased_set, erased_set.begin()); |
454 std::set_difference(current_set.begin(), current_set.end(), | 493 std::set_difference(current_set.begin(), current_set.end(), |
455 downloads_.begin(), downloads_.end(), | 494 prev_set.begin(), prev_set.end(), |
456 new_insertor); | 495 new_insertor); |
457 std::set_difference(downloads_.begin(), downloads_.end(), | 496 std::set_difference(prev_set.begin(), prev_set.end(), |
458 current_set.begin(), current_set.end(), | 497 current_set.begin(), current_set.end(), |
459 erased_insertor); | 498 erased_insertor); |
460 for (DownloadIdSet::const_iterator iter = new_set.begin(); | 499 for (DownloadIdSet::const_iterator iter = new_set.begin(); |
461 iter != new_set.end(); ++iter) { | 500 iter != new_set.end(); ++iter) { |
462 DispatchEvent(extension_event_names::kOnDownloadCreated, | 501 DispatchEvent(extension_event_names::kOnDownloadCreated, |
463 DownloadItemToJSON(current_map[*iter])); | 502 DownloadItemToJSON(current_map[*iter])); |
503 DCHECK(item_jsons_.find(*iter) == item_jsons_.end()); | |
504 item_jsons_[*iter] = DownloadItemToJSON(current_map[*iter]); | |
505 current_map[*iter]->AddObserver(this); | |
464 } | 506 } |
465 for (DownloadIdSet::const_iterator iter = erased_set.begin(); | 507 for (DownloadIdSet::const_iterator iter = erased_set.begin(); |
466 iter != erased_set.end(); ++iter) { | 508 iter != erased_set.end(); ++iter) { |
467 DispatchEvent(extension_event_names::kOnDownloadErased, | 509 DispatchEvent(extension_event_names::kOnDownloadErased, |
468 base::Value::CreateIntegerValue(*iter)); | 510 base::Value::CreateIntegerValue(*iter)); |
511 delete item_jsons_[*iter]; | |
512 item_jsons_.erase(*iter); | |
469 } | 513 } |
470 downloads_.swap(current_set); | 514 downloads_.swap(current_map); |
471 } | 515 } |
472 | 516 |
473 void ExtensionDownloadsEventRouter::ManagerGoingDown() { | 517 void ExtensionDownloadsEventRouter::ManagerGoingDown() { |
474 manager_->RemoveObserver(this); | 518 manager_->RemoveObserver(this); |
475 manager_ = NULL; | 519 manager_ = NULL; |
476 } | 520 } |
477 | 521 |
478 void ExtensionDownloadsEventRouter::DispatchEvent( | 522 void ExtensionDownloadsEventRouter::DispatchEvent( |
479 const char* event_name, base::Value* arg) { | 523 const char* event_name, base::Value* arg) { |
480 ListValue args; | 524 ListValue args; |
481 args.Append(arg); | 525 args.Append(arg); |
482 std::string json_args; | 526 std::string json_args; |
483 base::JSONWriter::Write(&args, false, &json_args); | 527 base::JSONWriter::Write(&args, false, &json_args); |
484 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 528 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
485 event_name, | 529 event_name, |
486 json_args, | 530 json_args, |
487 profile_, | 531 profile_, |
488 GURL()); | 532 GURL()); |
489 } | 533 } |
OLD | NEW |