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 15 matching lines...) Expand all Loading... | |
| 26 #include "chrome/browser/download/download_util.h" | 26 #include "chrome/browser/download/download_util.h" |
| 27 #include "chrome/browser/extensions/extension_downloads_api_constants.h" | 27 #include "chrome/browser/extensions/extension_downloads_api_constants.h" |
| 28 #include "chrome/browser/extensions/extension_event_names.h" | 28 #include "chrome/browser/extensions/extension_event_names.h" |
| 29 #include "chrome/browser/extensions/extension_event_router.h" | 29 #include "chrome/browser/extensions/extension_event_router.h" |
| 30 #include "chrome/browser/icon_loader.h" | 30 #include "chrome/browser/icon_loader.h" |
| 31 #include "chrome/browser/icon_manager.h" | 31 #include "chrome/browser/icon_manager.h" |
| 32 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" | 32 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
| 33 #include "chrome/browser/ui/browser_list.h" | 33 #include "chrome/browser/ui/browser_list.h" |
| 34 #include "content/browser/download/download_file_manager.h" | 34 #include "content/browser/download/download_file_manager.h" |
| 35 #include "content/browser/download/download_item.h" | 35 #include "content/browser/download/download_item.h" |
| 36 #include "content/browser/download/download_stats.h" | |
| 36 #include "content/browser/download/download_types.h" | 37 #include "content/browser/download/download_types.h" |
| 37 #include "content/browser/download/interrupt_reasons.h" | 38 #include "content/browser/download/interrupt_reasons.h" |
| 38 #include "content/browser/renderer_host/render_process_host.h" | 39 #include "content/browser/renderer_host/render_process_host.h" |
| 39 #include "content/browser/renderer_host/render_view_host.h" | 40 #include "content/browser/renderer_host/render_view_host.h" |
| 40 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 41 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
| 41 #include "net/http/http_util.h" | 42 #include "net/http/http_util.h" |
| 42 #include "net/url_request/url_request.h" | |
| 43 | 43 |
| 44 namespace constants = extension_downloads_api_constants; | 44 namespace constants = extension_downloads_api_constants; |
| 45 | 45 |
| 46 bool DownloadsFunctionInterface::RunImplImpl( | 46 bool DownloadsFunctionInterface::RunImplImpl( |
| 47 DownloadsFunctionInterface* pimpl) { | 47 DownloadsFunctionInterface* pimpl) { |
| 48 CHECK(pimpl); | 48 CHECK(pimpl); |
| 49 if (!pimpl->ParseArgs()) return false; | 49 if (!pimpl->ParseArgs()) return false; |
| 50 UMA_HISTOGRAM_ENUMERATION( | 50 UMA_HISTOGRAM_ENUMERATION( |
| 51 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); | 51 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); |
| 52 pimpl->RunInternal(); | 52 pimpl->RunInternal(); |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 315 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_SET_DESTINATION) { | 315 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_SET_DESTINATION) { |
| 316 } | 316 } |
| 317 | 317 |
| 318 DownloadsSetDestinationFunction::~DownloadsSetDestinationFunction() {} | 318 DownloadsSetDestinationFunction::~DownloadsSetDestinationFunction() {} |
| 319 | 319 |
| 320 bool DownloadsSetDestinationFunction::ParseArgs() { | 320 bool DownloadsSetDestinationFunction::ParseArgs() { |
| 321 int dl_id = 0; | 321 int dl_id = 0; |
| 322 std::string path; | 322 std::string path; |
| 323 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); | 323 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &dl_id)); |
| 324 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &path)); | 324 EXTENSION_FUNCTION_VALIDATE(args_->GetString(1, &path)); |
| 325 VLOG(1) << __FUNCTION__ << " " << dl_id << " " << &path; | 325 VLOG(1) << __FUNCTION__ << " " << dl_id << " " << path; |
| 326 error_ = constants::kNotImplemented; | 326 error_ = constants::kNotImplemented; |
| 327 return false; | 327 return false; |
| 328 } | 328 } |
| 329 | 329 |
| 330 void DownloadsSetDestinationFunction::RunInternal() { | 330 void DownloadsSetDestinationFunction::RunInternal() { |
| 331 NOTIMPLEMENTED(); | 331 NOTIMPLEMENTED(); |
| 332 } | 332 } |
| 333 | 333 |
| 334 DownloadsAcceptDangerFunction::DownloadsAcceptDangerFunction() | 334 DownloadsAcceptDangerFunction::DownloadsAcceptDangerFunction() |
| 335 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ACCEPT_DANGER) { | 335 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ACCEPT_DANGER) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 json->SetInteger(constants::kBytesReceivedKey, item->received_bytes()); | 405 json->SetInteger(constants::kBytesReceivedKey, item->received_bytes()); |
| 406 json->SetInteger(constants::kTotalBytesKey, item->total_bytes()); | 406 json->SetInteger(constants::kTotalBytesKey, item->total_bytes()); |
| 407 if (item->state() == DownloadItem::INTERRUPTED) | 407 if (item->state() == DownloadItem::INTERRUPTED) |
| 408 json->SetInteger(constants::kErrorKey, | 408 json->SetInteger(constants::kErrorKey, |
| 409 static_cast<int>(item->last_reason())); | 409 static_cast<int>(item->last_reason())); |
| 410 // TODO(benjhayden): Implement endTime and fileSize. | 410 // TODO(benjhayden): Implement endTime and fileSize. |
| 411 // json->SetInteger(constants::kEndTimeKey, -1); | 411 // json->SetInteger(constants::kEndTimeKey, -1); |
| 412 json->SetInteger(constants::kFileSizeKey, item->total_bytes()); | 412 json->SetInteger(constants::kFileSizeKey, item->total_bytes()); |
| 413 return json; | 413 return json; |
| 414 } | 414 } |
| 415 | |
| 416 typedef std::set<int> DownloadIdSet; | |
| 417 | |
| 415 } // anonymous namespace | 418 } // anonymous namespace |
| 416 | 419 |
| 417 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( | 420 ExtensionDownloadsEventRouter::ExtensionDownloadsEventRouter( |
| 418 Profile* profile) | 421 Profile* profile) |
| 419 : profile_(profile), | 422 : profile_(profile), |
| 420 manager_( | 423 manager_( |
| 421 profile ? | 424 profile ? |
| 422 DownloadServiceFactory::GetForProfile(profile)->GetDownloadManager() : | 425 DownloadServiceFactory::GetForProfile(profile)->GetDownloadManager() : |
| 423 NULL) { | 426 NULL), |
| 427 delete_item_jsons_(&item_jsons_), | |
| 428 delete_on_changed_stats_(&on_changed_stats_) { | |
| 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 429 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 425 DCHECK(profile_); | |
| 426 DCHECK(manager_); | |
| 427 manager_->AddObserver(this); | 430 manager_->AddObserver(this); |
| 428 } | 431 } |
| 429 | 432 |
| 430 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { | 433 ExtensionDownloadsEventRouter::~ExtensionDownloadsEventRouter() { |
| 431 if (manager_ != NULL) | 434 if (manager_ != NULL) |
| 432 manager_->RemoveObserver(this); | 435 manager_->RemoveObserver(this); |
| 436 for (ItemMap::const_iterator iter = downloads_.begin(); | |
| 437 iter != downloads_.end(); ++iter) { | |
| 438 if (iter->second != NULL) | |
| 439 iter->second->RemoveObserver(this); | |
| 440 } | |
| 441 } | |
| 442 | |
| 443 ExtensionDownloadsEventRouter::OnChangedStat::OnChangedStat() | |
|
Randy Smith (Not in Mondays)
2011/10/18 21:15:32
I don't have a strong feeling on this, but I think
| |
| 444 : fires(0), total(0) { | |
| 445 } | |
| 446 | |
| 447 ExtensionDownloadsEventRouter::OnChangedStat::~OnChangedStat() { | |
| 448 download_stats::RecordOnChanged(fires * 100 / total); | |
| 449 } | |
| 450 | |
| 451 void ExtensionDownloadsEventRouter::OnDownloadUpdated(DownloadItem* item) { | |
| 452 if (item->state() == DownloadItem::REMOVING) { | |
| 453 // Remove item from downloads_, but keep the id in downloads_ so that we | |
| 454 // notice its absence in ModelChanged() and fire an onErased. | |
| 455 ItemMap::const_iterator it = downloads_.find(item->id()); | |
| 456 DCHECK(it != downloads_.end()); | |
| 457 DCHECK(it->second == item); | |
| 458 downloads_[item->id()] = NULL; | |
| 459 item->RemoveObserver(this); | |
| 460 return; | |
| 461 } | |
| 462 base::DictionaryValue* current_json = item_jsons_[item->id()]; | |
| 463 scoped_ptr<base::DictionaryValue> new_json(DownloadItemToJSON(item)); | |
| 464 scoped_ptr<base::DictionaryValue> delta(new base::DictionaryValue()); | |
| 465 delta->SetInteger(constants::kIdKey, item->id()); | |
| 466 bool changed = false; | |
| 467 for (base::DictionaryValue::key_iterator key = new_json->begin_keys(); | |
| 468 key != new_json->end_keys(); ++key) { | |
| 469 if (*key != constants::kBytesReceivedKey) { | |
| 470 base::Value* new_value = NULL; | |
| 471 base::Value* old_value = NULL; | |
| 472 if (new_json->Get(*key, &new_value) && | |
| 473 (!current_json->HasKey(*key) || | |
| 474 (current_json->Get(*key, &old_value) && | |
| 475 !new_value->Equals(old_value)))) { | |
| 476 delta->Set(*key + ".old", (old_value ? old_value->DeepCopy() | |
| 477 : base::Value::CreateNullValue())); | |
| 478 delta->Set(*key + ".new", new_value->DeepCopy()); | |
| 479 changed = true; | |
| 480 } | |
| 481 } | |
| 482 } | |
| 483 ++(on_changed_stats_[item->id()]->total); | |
| 484 if (changed) { | |
| 485 DispatchEvent(extension_event_names::kOnDownloadChanged, delta.release()); | |
| 486 ++(on_changed_stats_[item->id()]->fires); | |
| 487 } | |
| 488 current_json->Swap(new_json.get()); | |
| 489 } | |
| 490 | |
| 491 void ExtensionDownloadsEventRouter::OnDownloadOpened(DownloadItem* item) { | |
| 433 } | 492 } |
| 434 | 493 |
| 435 void ExtensionDownloadsEventRouter::ModelChanged() { | 494 void ExtensionDownloadsEventRouter::ModelChanged() { |
| 436 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 495 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 437 if (manager_ == NULL) | 496 if (manager_ == NULL) |
| 438 return; | 497 return; |
| 439 DownloadManager::DownloadVector current_vec; | 498 DownloadManager::DownloadVector current_vec; |
| 440 manager_->SearchDownloads(string16(), ¤t_vec); | 499 manager_->SearchDownloads(string16(), ¤t_vec); |
| 441 DownloadIdSet current_set; | 500 DownloadIdSet current_set, prev_set; |
| 501 for (ItemMap::const_iterator iter = downloads_.begin(); | |
| 502 iter != downloads_.end(); ++iter) { | |
| 503 prev_set.insert(iter->first); | |
| 504 } | |
| 442 ItemMap current_map; | 505 ItemMap current_map; |
| 443 for (DownloadManager::DownloadVector::const_iterator iter = | 506 for (DownloadManager::DownloadVector::const_iterator iter = |
| 444 current_vec.begin(); | 507 current_vec.begin(); |
| 445 iter != current_vec.end(); ++iter) { | 508 iter != current_vec.end(); ++iter) { |
| 446 DownloadItem* item = *iter; | 509 DownloadItem* item = *iter; |
| 447 int item_id = item->id(); | 510 int item_id = item->id(); |
| 448 // TODO(benjhayden): Remove the following line when every item's id >= 0, | 511 // TODO(benjhayden): Remove the following line when every item's id >= 0, |
| 449 // which will allow firing onErased events for items from the history. | 512 // which will allow firing onErased events for items from the history. |
| 450 if (item_id < 0) continue; | 513 if (item_id < 0) continue; |
| 451 DCHECK(current_map.find(item_id) == current_map.end()); | 514 DCHECK(current_map.find(item_id) == current_map.end()); |
| 452 current_set.insert(item_id); | 515 current_set.insert(item_id); |
| 453 current_map[item_id] = item; | 516 current_map[item_id] = item; |
| 454 } | 517 } |
| 455 DownloadIdSet new_set; // current_set - downloads_; | 518 DownloadIdSet new_set; // current_set - prev_set; |
| 456 DownloadIdSet erased_set; // downloads_ - current_set; | 519 DownloadIdSet erased_set; // prev_set - current_set; |
| 457 std::insert_iterator<DownloadIdSet> new_insertor(new_set, new_set.begin()); | 520 std::insert_iterator<DownloadIdSet> new_insertor(new_set, new_set.begin()); |
| 458 std::insert_iterator<DownloadIdSet> erased_insertor( | 521 std::insert_iterator<DownloadIdSet> erased_insertor( |
| 459 erased_set, erased_set.begin()); | 522 erased_set, erased_set.begin()); |
| 460 std::set_difference(current_set.begin(), current_set.end(), | 523 std::set_difference(current_set.begin(), current_set.end(), |
| 461 downloads_.begin(), downloads_.end(), | 524 prev_set.begin(), prev_set.end(), |
| 462 new_insertor); | 525 new_insertor); |
| 463 std::set_difference(downloads_.begin(), downloads_.end(), | 526 std::set_difference(prev_set.begin(), prev_set.end(), |
| 464 current_set.begin(), current_set.end(), | 527 current_set.begin(), current_set.end(), |
| 465 erased_insertor); | 528 erased_insertor); |
| 466 for (DownloadIdSet::const_iterator iter = new_set.begin(); | 529 for (DownloadIdSet::const_iterator iter = new_set.begin(); |
| 467 iter != new_set.end(); ++iter) { | 530 iter != new_set.end(); ++iter) { |
| 468 DispatchEvent(extension_event_names::kOnDownloadCreated, | 531 DispatchEvent(extension_event_names::kOnDownloadCreated, |
| 469 DownloadItemToJSON(current_map[*iter])); | 532 DownloadItemToJSON(current_map[*iter])); |
| 533 DCHECK(item_jsons_.find(*iter) == item_jsons_.end()); | |
| 534 on_changed_stats_[*iter] = new OnChangedStat(); | |
| 535 item_jsons_[*iter] = DownloadItemToJSON(current_map[*iter]); | |
| 536 current_map[*iter]->AddObserver(this); | |
| 470 } | 537 } |
| 471 for (DownloadIdSet::const_iterator iter = erased_set.begin(); | 538 for (DownloadIdSet::const_iterator iter = erased_set.begin(); |
| 472 iter != erased_set.end(); ++iter) { | 539 iter != erased_set.end(); ++iter) { |
| 473 DispatchEvent(extension_event_names::kOnDownloadErased, | 540 DispatchEvent(extension_event_names::kOnDownloadErased, |
| 474 base::Value::CreateIntegerValue(*iter)); | 541 base::Value::CreateIntegerValue(*iter)); |
| 542 delete item_jsons_[*iter]; | |
| 543 item_jsons_.erase(*iter); | |
| 544 delete on_changed_stats_[*iter]; | |
| 545 on_changed_stats_.erase(*iter); | |
| 475 } | 546 } |
| 476 downloads_.swap(current_set); | 547 downloads_.swap(current_map); |
| 477 } | 548 } |
| 478 | 549 |
| 479 void ExtensionDownloadsEventRouter::ManagerGoingDown() { | 550 void ExtensionDownloadsEventRouter::ManagerGoingDown() { |
| 480 manager_->RemoveObserver(this); | 551 manager_->RemoveObserver(this); |
| 481 manager_ = NULL; | 552 manager_ = NULL; |
| 482 } | 553 } |
| 483 | 554 |
| 484 void ExtensionDownloadsEventRouter::DispatchEvent( | 555 void ExtensionDownloadsEventRouter::DispatchEvent( |
| 485 const char* event_name, base::Value* arg) { | 556 const char* event_name, base::Value* arg) { |
| 486 ListValue args; | 557 ListValue args; |
| 487 args.Append(arg); | 558 args.Append(arg); |
| 488 std::string json_args; | 559 std::string json_args; |
| 489 base::JSONWriter::Write(&args, false, &json_args); | 560 base::JSONWriter::Write(&args, false, &json_args); |
| 490 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 561 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 491 event_name, | 562 event_name, |
| 492 json_args, | 563 json_args, |
| 493 profile_, | 564 profile_, |
| 494 GURL()); | 565 GURL()); |
| 495 } | 566 } |
| OLD | NEW |