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

Side by Side Diff: chrome/browser/download/download_extension_api.cc

Issue 9425014: Fix most of the downloads api in incognito profiles (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 8 years, 7 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/download/download_extension_api.h" 5 #include "chrome/browser/download/download_extension_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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 const char kEndTimeKey[] = "endTime"; 86 const char kEndTimeKey[] = "endTime";
87 const char kErrorKey[] = "error"; 87 const char kErrorKey[] = "error";
88 const char kFileSizeKey[] = "fileSize"; 88 const char kFileSizeKey[] = "fileSize";
89 const char kFilenameKey[] = "filename"; 89 const char kFilenameKey[] = "filename";
90 const char kFilenameRegexKey[] = "filenameRegex"; 90 const char kFilenameRegexKey[] = "filenameRegex";
91 const char kHeaderNameKey[] = "name"; 91 const char kHeaderNameKey[] = "name";
92 const char kHeaderValueKey[] = "value"; 92 const char kHeaderValueKey[] = "value";
93 const char kHeaderBinaryValueKey[] = "binaryValue"; 93 const char kHeaderBinaryValueKey[] = "binaryValue";
94 const char kHeadersKey[] = "headers"; 94 const char kHeadersKey[] = "headers";
95 const char kIdKey[] = "id"; 95 const char kIdKey[] = "id";
96 const char kIncognito[] = "incognito";
97 const char kIncognitoNone[] = "none";
98 const char kIncognitoOnly[] = "only";
96 const char kLimitKey[] = "limit"; 99 const char kLimitKey[] = "limit";
97 const char kMethodKey[] = "method"; 100 const char kMethodKey[] = "method";
98 const char kMimeKey[] = "mime"; 101 const char kMimeKey[] = "mime";
99 const char kOrderByKey[] = "orderBy"; 102 const char kOrderByKey[] = "orderBy";
100 const char kPausedKey[] = "paused"; 103 const char kPausedKey[] = "paused";
101 const char kQueryKey[] = "query"; 104 const char kQueryKey[] = "query";
102 const char kSaveAsKey[] = "saveAs"; 105 const char kSaveAsKey[] = "saveAs";
103 const char kSizeKey[] = "size"; 106 const char kSizeKey[] = "size";
104 const char kStartTimeKey[] = "startTime"; 107 const char kStartTimeKey[] = "startTime";
105 const char kStartedAfterKey[] = "startedAfter"; 108 const char kStartedAfterKey[] = "startedAfter";
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 if (item->GetDangerType() != content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) 192 if (item->GetDangerType() != content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)
190 json->SetBoolean(kDangerAcceptedKey, 193 json->SetBoolean(kDangerAcceptedKey,
191 item->GetSafetyState() == DownloadItem::DANGEROUS_BUT_VALIDATED); 194 item->GetSafetyState() == DownloadItem::DANGEROUS_BUT_VALIDATED);
192 json->SetString(kStateKey, StateString(item->GetState())); 195 json->SetString(kStateKey, StateString(item->GetState()));
193 json->SetBoolean(kPausedKey, item->IsPaused()); 196 json->SetBoolean(kPausedKey, item->IsPaused());
194 json->SetString(kMimeKey, item->GetMimeType()); 197 json->SetString(kMimeKey, item->GetMimeType());
195 json->SetInteger(kStartTimeKey, 198 json->SetInteger(kStartTimeKey,
196 (item->GetStartTime() - base::Time::UnixEpoch()).InMilliseconds()); 199 (item->GetStartTime() - base::Time::UnixEpoch()).InMilliseconds());
197 json->SetInteger(kBytesReceivedKey, item->GetReceivedBytes()); 200 json->SetInteger(kBytesReceivedKey, item->GetReceivedBytes());
198 json->SetInteger(kTotalBytesKey, item->GetTotalBytes()); 201 json->SetInteger(kTotalBytesKey, item->GetTotalBytes());
202 json->SetBoolean(kIncognito, item->IsOtr());
199 if (item->GetState() == DownloadItem::INTERRUPTED) { 203 if (item->GetState() == DownloadItem::INTERRUPTED) {
200 json->SetInteger(kErrorKey, static_cast<int>(item->GetLastReason())); 204 json->SetInteger(kErrorKey, static_cast<int>(item->GetLastReason()));
201 } else if (item->GetState() == DownloadItem::CANCELLED) { 205 } else if (item->GetState() == DownloadItem::CANCELLED) {
202 json->SetInteger(kErrorKey, static_cast<int>( 206 json->SetInteger(kErrorKey, static_cast<int>(
203 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED)); 207 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED));
204 } 208 }
205 // TODO(benjhayden): Implement endTime and fileSize. 209 // TODO(benjhayden): Implement endTime and fileSize.
206 // json->SetInteger(kEndTimeKey, -1); 210 // json->SetInteger(kEndTimeKey, -1);
207 json->SetInteger(kFileSizeKey, item->GetTotalBytes()); 211 json->SetInteger(kFileSizeKey, item->GetTotalBytes());
208 return scoped_ptr<base::DictionaryValue>(json); 212 return scoped_ptr<base::DictionaryValue>(json);
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
300 sorter_types[kStartTimeKey] = DownloadQuery::SORT_START_TIME; 304 sorter_types[kStartTimeKey] = DownloadQuery::SORT_START_TIME;
301 sorter_types[kStateKey] = DownloadQuery::SORT_STATE; 305 sorter_types[kStateKey] = DownloadQuery::SORT_STATE;
302 sorter_types[kTotalBytesKey] = DownloadQuery::SORT_TOTAL_BYTES; 306 sorter_types[kTotalBytesKey] = DownloadQuery::SORT_TOTAL_BYTES;
303 sorter_types[kUrlKey] = DownloadQuery::SORT_URL; 307 sorter_types[kUrlKey] = DownloadQuery::SORT_URL;
304 } 308 }
305 309
306 bool IsNotTemporaryDownloadFilter(const DownloadItem& item) { 310 bool IsNotTemporaryDownloadFilter(const DownloadItem& item) {
307 return !item.IsTemporary(); 311 return !item.IsTemporary();
308 } 312 }
309 313
314 DownloadItem* GetItem(Profile* profile, int id) {
315 DownloadManager* download_manager =
316 DownloadServiceFactory::GetForProfile(profile)->GetDownloadManager();
317 DownloadItem* download_item =
318 download_manager->GetActiveDownloadItem(id);
319 if (!download_item) {
320 if (profile->IsOffTheRecord()) {
321 // Try looking for the item in the original manager.
322 download_manager = DownloadServiceFactory::GetForProfile(
323 profile->GetOriginalProfile())->GetDownloadManager();
324 download_item = download_manager->GetActiveDownloadItem(id);
325 }
326 }
327 return download_item;
328 }
329
310 } // namespace 330 } // namespace
311 331
312 bool DownloadsFunctionInterface::RunImplImpl( 332 bool DownloadsFunctionInterface::RunImplImpl(
313 DownloadsFunctionInterface* pimpl) { 333 DownloadsFunctionInterface* pimpl) {
314 CHECK(pimpl); 334 CHECK(pimpl);
315 if (!pimpl->ParseArgs()) return false; 335 if (!pimpl->ParseArgs()) return false;
316 UMA_HISTOGRAM_ENUMERATION( 336 UMA_HISTOGRAM_ENUMERATION(
317 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); 337 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST);
318 return pimpl->RunInternal(); 338 return pimpl->RunInternal();
319 } 339 }
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 EXTENSION_FUNCTION_VALIDATE(header->GetString( 454 EXTENSION_FUNCTION_VALIDATE(header->GetString(
435 kHeaderValueKey, &value)); 455 kHeaderValueKey, &value));
436 } 456 }
437 if (!net::HttpUtil::IsSafeHeader(name)) { 457 if (!net::HttpUtil::IsSafeHeader(name)) {
438 error_ = download_extension_errors::kGenericError; 458 error_ = download_extension_errors::kGenericError;
439 return false; 459 return false;
440 } 460 }
441 } 461 }
442 } 462 }
443 iodata_->rdh = content::ResourceDispatcherHost::Get(); 463 iodata_->rdh = content::ResourceDispatcherHost::Get();
464 // If the extension is running an OTR window, then
465 // GetCurrentBrowser()->profile() will be OTR, if there is a current browser
466 // at all. this->profile() will always be the original profile.
467 // Unfortunately, DownloadRequestHandle derives the DownloadManager* from
468 // render_process_host_id and render_view_host_routing_id, which don't seem to
469 // understand OTR, so this change will depend on DownloadManager::DownloadUrl.
Randy Smith (Not in Mondays) 2012/05/02 18:57:01 I'm confused; render_process_host_id and render_vi
benjhayden 2012/05/03 13:54:39 Done.
470 // Profile* profile = profile();
471 // if ((GetCurrentBrowser() != NULL) &&
472 // (GetCurrentBrowser()->profile() != NULL))
473 // profile = GetCurrentBrowser()->profile();
444 iodata_->resource_context = profile()->GetResourceContext(); 474 iodata_->resource_context = profile()->GetResourceContext();
445 iodata_->render_process_host_id = render_view_host()->GetProcess()->GetID(); 475 iodata_->render_process_host_id = render_view_host()->GetProcess()->GetID();
446 iodata_->render_view_host_routing_id = render_view_host()->GetRoutingID(); 476 iodata_->render_view_host_routing_id = render_view_host()->GetRoutingID();
447 return true; 477 return true;
448 } 478 }
449 479
450 bool DownloadsDownloadFunction::RunInternal() { 480 bool DownloadsDownloadFunction::RunInternal() {
451 VLOG(1) << __FUNCTION__ << " " << iodata_->url.spec(); 481 VLOG(1) << __FUNCTION__ << " " << iodata_->url.spec();
452 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( 482 if (!BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
453 &DownloadsDownloadFunction::BeginDownloadOnIOThread, this))) { 483 &DownloadsDownloadFunction::BeginDownloadOnIOThread, this))) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 result_.reset(base::Value::CreateIntegerValue(dl_id.local())); 555 result_.reset(base::Value::CreateIntegerValue(dl_id.local()));
526 } else { 556 } else {
527 error_ = net::ErrorToString(error); 557 error_ = net::ErrorToString(error);
528 } 558 }
529 SendResponse(error_.empty()); 559 SendResponse(error_.empty());
530 } 560 }
531 561
532 DownloadsSearchFunction::DownloadsSearchFunction() 562 DownloadsSearchFunction::DownloadsSearchFunction()
533 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH), 563 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH),
534 query_(new DownloadQuery()), 564 query_(new DownloadQuery()),
565 include_on_the_record_(true),
566 include_incognito_(true),
535 get_id_(0), 567 get_id_(0),
536 has_get_id_(false) { 568 has_get_id_(false) {
537 } 569 }
538 570
539 DownloadsSearchFunction::~DownloadsSearchFunction() {} 571 DownloadsSearchFunction::~DownloadsSearchFunction() {}
540 572
541 bool DownloadsSearchFunction::ParseArgs() { 573 bool DownloadsSearchFunction::ParseArgs() {
542 static base::LazyInstance<FilterTypeMap> filter_types = 574 static base::LazyInstance<FilterTypeMap> filter_types =
543 LAZY_INSTANCE_INITIALIZER; 575 LAZY_INSTANCE_INITIALIZER;
544 if (filter_types.Get().size() == 0) 576 if (filter_types.Get().size() == 0)
545 InitFilterTypeMap(filter_types.Get()); 577 InitFilterTypeMap(filter_types.Get());
546 578
547 base::DictionaryValue* query_json = NULL; 579 base::DictionaryValue* query_json = NULL;
548 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); 580 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json));
549 for (base::DictionaryValue::Iterator query_json_field(*query_json); 581 for (base::DictionaryValue::Iterator query_json_field(*query_json);
550 query_json_field.HasNext(); query_json_field.Advance()) { 582 query_json_field.HasNext(); query_json_field.Advance()) {
551 FilterTypeMap::const_iterator filter_type = 583 FilterTypeMap::const_iterator filter_type =
552 filter_types.Get().find(query_json_field.key()); 584 filter_types.Get().find(query_json_field.key());
553 585
554 if (filter_type != filter_types.Get().end()) { 586 if (filter_type != filter_types.Get().end()) {
555 if (!query_->AddFilter(filter_type->second, query_json_field.value())) { 587 if (!query_->AddFilter(filter_type->second, query_json_field.value())) {
556 error_ = download_extension_errors::kInvalidFilterError; 588 error_ = download_extension_errors::kInvalidFilterError;
557 return false; 589 return false;
558 } 590 }
559 } else if (query_json_field.key() == kIdKey) { 591 } else if (query_json_field.key() == kIdKey) {
560 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsInteger( 592 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsInteger(
561 &get_id_)); 593 &get_id_));
562 has_get_id_ = true; 594 has_get_id_ = true;
595 } else if (query_json_field.key() == kIncognito) {
596 std::string incognito;
597 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString(
598 &incognito));
599 if (incognito == kIncognitoNone) {
600 include_incognito_ = false;
601 } else if (incognito == kIncognitoOnly) {
602 include_on_the_record_ = false;
603 } else {
604 EXTENSION_FUNCTION_VALIDATE(false);
605 }
563 } else if (query_json_field.key() == kOrderByKey) { 606 } else if (query_json_field.key() == kOrderByKey) {
564 if (!ParseOrderBy(query_json_field.value())) 607 if (!ParseOrderBy(query_json_field.value()))
565 return false; 608 return false;
566 } else if (query_json_field.key() == kDangerKey) { 609 } else if (query_json_field.key() == kDangerKey) {
567 std::string danger_str; 610 std::string danger_str;
568 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString( 611 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString(
569 &danger_str)); 612 &danger_str));
570 content::DownloadDangerType danger_type = 613 content::DownloadDangerType danger_type =
571 DangerEnumFromString(danger_str); 614 DangerEnumFromString(danger_str);
572 if (danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) { 615 if (danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
626 error_ = download_extension_errors::kInvalidOrderByError; 669 error_ = download_extension_errors::kInvalidOrderByError;
627 return false; 670 return false;
628 } 671 }
629 query_->AddSorter(sorter_type->second, direction); 672 query_->AddSorter(sorter_type->second, direction);
630 } 673 }
631 return true; 674 return true;
632 } 675 }
633 676
634 bool DownloadsSearchFunction::RunInternal() { 677 bool DownloadsSearchFunction::RunInternal() {
635 DownloadQuery::DownloadVector all_items, cpp_results; 678 DownloadQuery::DownloadVector all_items, cpp_results;
636 DownloadManager* manager = DownloadServiceFactory::GetForProfile(profile()) 679 Profile* original_profile = NULL;
637 ->GetDownloadManager(); 680 Profile* otr_profile = NULL;
681 if (profile()->IsOffTheRecord()) {
682 otr_profile = profile();
683 original_profile = profile()->GetOriginalProfile();
684 } else {
685 original_profile = profile();
686 if (profile()->HasOffTheRecordProfile())
687 otr_profile = profile()->GetOffTheRecordProfile();
Randy Smith (Not in Mondays) 2012/05/02 18:57:01 I don't think it's cool to allow access to OTR dow
benjhayden 2012/05/03 13:54:39 Done.
688 }
689 DownloadManager* original_manager = DownloadServiceFactory::GetForProfile(
690 original_profile)->GetDownloadManager();
691 DownloadManager* otr_manager = NULL;
692 if (otr_profile != NULL) {
693 otr_manager = DownloadServiceFactory::GetForProfile(otr_profile)
694 ->GetDownloadManager();
695 }
638 if (has_get_id_) { 696 if (has_get_id_) {
639 DownloadItem* item = manager->GetDownloadItem(get_id_); 697 DownloadItem* item = original_manager->GetDownloadItem(get_id_);
698 if ((item == NULL) && (otr_manager != NULL))
699 item = otr_manager->GetDownloadItem(get_id_);
640 if (item != NULL) 700 if (item != NULL)
641 all_items.push_back(item); 701 all_items.push_back(item);
642 } else { 702 } else {
643 manager->GetAllDownloads(FilePath(FILE_PATH_LITERAL("")), &all_items); 703 if (include_on_the_record_)
704 original_manager->GetAllDownloads(
705 FilePath(FILE_PATH_LITERAL("")), &all_items);
706 if (include_incognito_ && (otr_manager != NULL))
707 otr_manager->GetAllDownloads(
708 FilePath(FILE_PATH_LITERAL("")), &all_items);
644 } 709 }
645 query_->Search(all_items.begin(), all_items.end(), &cpp_results); 710 query_->Search(all_items.begin(), all_items.end(), &cpp_results);
646 base::ListValue* json_results = new base::ListValue(); 711 base::ListValue* json_results = new base::ListValue();
647 for (DownloadManager::DownloadVector::const_iterator it = cpp_results.begin(); 712 for (DownloadManager::DownloadVector::const_iterator it = cpp_results.begin();
648 it != cpp_results.end(); ++it) { 713 it != cpp_results.end(); ++it) {
649 scoped_ptr<base::DictionaryValue> item(DownloadItemToJSON(*it)); 714 scoped_ptr<base::DictionaryValue> item(DownloadItemToJSON(*it));
650 json_results->Append(item.release()); 715 json_results->Append(item.release());
651 } 716 }
652 result_.reset(json_results); 717 result_.reset(json_results);
653 return true; 718 return true;
654 } 719 }
655 720
656 DownloadsPauseFunction::DownloadsPauseFunction() 721 DownloadsPauseFunction::DownloadsPauseFunction()
657 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE), 722 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE),
658 download_id_(DownloadId::Invalid().local()) { 723 download_id_(DownloadId::Invalid().local()) {
659 } 724 }
660 725
661 DownloadsPauseFunction::~DownloadsPauseFunction() {} 726 DownloadsPauseFunction::~DownloadsPauseFunction() {}
662 727
663 bool DownloadsPauseFunction::ParseArgs() { 728 bool DownloadsPauseFunction::ParseArgs() {
664 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); 729 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_));
665 return true; 730 return true;
666 } 731 }
667 732
668 bool DownloadsPauseFunction::RunInternal() { 733 bool DownloadsPauseFunction::RunInternal() {
669 DownloadManager* download_manager = 734 DownloadItem* download_item = GetItem(
670 DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); 735 GetCurrentBrowser()->profile(), download_id_);
671 DownloadItem* download_item = 736 if ((download_item == NULL) || !download_item->IsInProgress()) {
672 download_manager->GetActiveDownloadItem(download_id_);
673 DCHECK(!download_item || download_item->IsInProgress());
674
675 if (!download_item) {
676 // This could be due to an invalid download ID, or it could be due to the 737 // This could be due to an invalid download ID, or it could be due to the
677 // download not being currently active. 738 // download not being currently active.
678 error_ = download_extension_errors::kInvalidOperationError; 739 error_ = download_extension_errors::kInvalidOperationError;
679 } else if (!download_item->IsPaused()) { 740 } else if (!download_item->IsPaused()) {
680 // If download_item->IsPaused() already then we treat it as a success. 741 // If download_item->IsPaused() already then we treat it as a success.
681 download_item->TogglePause(); 742 download_item->TogglePause();
682 } 743 }
683 return error_.empty(); 744 return error_.empty();
684 } 745 }
685 746
686 DownloadsResumeFunction::DownloadsResumeFunction() 747 DownloadsResumeFunction::DownloadsResumeFunction()
687 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_RESUME), 748 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_RESUME),
688 download_id_(DownloadId::Invalid().local()) { 749 download_id_(DownloadId::Invalid().local()) {
689 } 750 }
690 751
691 DownloadsResumeFunction::~DownloadsResumeFunction() {} 752 DownloadsResumeFunction::~DownloadsResumeFunction() {}
692 753
693 bool DownloadsResumeFunction::ParseArgs() { 754 bool DownloadsResumeFunction::ParseArgs() {
694 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); 755 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_));
695 return true; 756 return true;
696 } 757 }
697 758
698 bool DownloadsResumeFunction::RunInternal() { 759 bool DownloadsResumeFunction::RunInternal() {
699 DownloadManager* download_manager = 760 DownloadItem* download_item = GetItem(
700 DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); 761 GetCurrentBrowser()->profile(), download_id_);
701 DownloadItem* download_item = 762 if (download_item == NULL) {
702 download_manager->GetActiveDownloadItem(download_id_);
703 DCHECK(!download_item || download_item->IsInProgress());
704
705 if (!download_item) {
706 // This could be due to an invalid download ID, or it could be due to the 763 // This could be due to an invalid download ID, or it could be due to the
707 // download not being currently active. 764 // download not being currently active.
708 error_ = download_extension_errors::kInvalidOperationError; 765 error_ = download_extension_errors::kInvalidOperationError;
709 } else if (download_item->IsPaused()) { 766 } else if (download_item->IsPaused()) {
710 // If !download_item->IsPaused() already, then we treat it as a success. 767 // If !download_item->IsPaused() already, then we treat it as a success.
711 download_item->TogglePause(); 768 download_item->TogglePause();
712 } 769 }
713 return error_.empty(); 770 return error_.empty();
714 } 771 }
715 772
716 DownloadsCancelFunction::DownloadsCancelFunction() 773 DownloadsCancelFunction::DownloadsCancelFunction()
717 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_CANCEL), 774 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_CANCEL),
718 download_id_(DownloadId::Invalid().local()) { 775 download_id_(DownloadId::Invalid().local()) {
719 } 776 }
720 777
721 DownloadsCancelFunction::~DownloadsCancelFunction() {} 778 DownloadsCancelFunction::~DownloadsCancelFunction() {}
722 779
723 bool DownloadsCancelFunction::ParseArgs() { 780 bool DownloadsCancelFunction::ParseArgs() {
724 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); 781 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_));
725 return true; 782 return true;
726 } 783 }
727 784
728 bool DownloadsCancelFunction::RunInternal() { 785 bool DownloadsCancelFunction::RunInternal() {
729 DownloadManager* download_manager = 786 DownloadItem* download_item = GetItem(
730 DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); 787 GetCurrentBrowser()->profile(), download_id_);
731 DownloadItem* download_item = 788 if (download_item != NULL)
732 download_manager->GetActiveDownloadItem(download_id_);
733
734 if (download_item)
735 download_item->Cancel(true); 789 download_item->Cancel(true);
736 // |download_item| can be NULL if the download ID was invalid or if the 790 // |download_item| can be NULL if the download ID was invalid or if the
737 // download is not currently active. Either way, we don't consider it a 791 // download is not currently active. Either way, we don't consider it a
738 // failure. 792 // failure.
739 return error_.empty(); 793 return error_.empty();
740 } 794 }
741 795
742 DownloadsEraseFunction::DownloadsEraseFunction() 796 DownloadsEraseFunction::DownloadsEraseFunction()
743 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ERASE) { 797 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ERASE) {
744 } 798 }
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 ListValue args; 1128 ListValue args;
1075 args.Append(arg); 1129 args.Append(arg);
1076 std::string json_args; 1130 std::string json_args;
1077 base::JSONWriter::Write(&args, &json_args); 1131 base::JSONWriter::Write(&args, &json_args);
1078 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( 1132 profile_->GetExtensionEventRouter()->DispatchEventToRenderers(
1079 event_name, 1133 event_name,
1080 json_args, 1134 json_args,
1081 profile_, 1135 profile_,
1082 GURL()); 1136 GURL());
1083 } 1137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698