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

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* GetItemInternal(Browser* browser, Profile* profile, int id) {
315 if ((browser != NULL) &&
Matt Perry 2012/05/14 22:57:11 nit: prefer "browser" to "browser != NULL"
benjhayden 2012/05/15 17:16:25 Done.
316 (browser->profile() != NULL))
317 profile = browser->profile();
318 DownloadManager* download_manager =
319 DownloadServiceFactory::GetForProfile(profile)->GetDownloadManager();
320 DownloadItem* download_item =
321 download_manager->GetActiveDownloadItem(id);
322 if (!download_item) {
323 if (profile->IsOffTheRecord()) {
324 // Try looking for the item in the original manager.
325 download_manager = DownloadServiceFactory::GetForProfile(
326 profile->GetOriginalProfile())->GetDownloadManager();
327 download_item = download_manager->GetActiveDownloadItem(id);
328 }
329 // Do not try looking for the item in the off-the-record manager if profile
330 // is on the record.
331 }
332 return download_item;
333 }
334
310 } // namespace 335 } // namespace
311 336
312 bool DownloadsFunctionInterface::RunImplImpl( 337 bool DownloadsFunctionInterface::RunImplImpl(
313 DownloadsFunctionInterface* pimpl) { 338 DownloadsFunctionInterface* pimpl) {
314 CHECK(pimpl); 339 CHECK(pimpl);
315 if (!pimpl->ParseArgs()) return false; 340 if (!pimpl->ParseArgs()) return false;
316 UMA_HISTOGRAM_ENUMERATION( 341 UMA_HISTOGRAM_ENUMERATION(
317 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); 342 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST);
318 return pimpl->RunInternal(); 343 return pimpl->RunInternal();
319 } 344 }
320 345
321 SyncDownloadsFunction::SyncDownloadsFunction( 346 SyncDownloadsFunction::SyncDownloadsFunction(
322 DownloadsFunctionInterface::DownloadsFunctionName function) 347 DownloadsFunctionInterface::DownloadsFunctionName function)
323 : function_(function) { 348 : function_(function) {
324 } 349 }
325 350
326 SyncDownloadsFunction::~SyncDownloadsFunction() {} 351 SyncDownloadsFunction::~SyncDownloadsFunction() {}
327 352
328 bool SyncDownloadsFunction::RunImpl() { 353 bool SyncDownloadsFunction::RunImpl() {
329 return DownloadsFunctionInterface::RunImplImpl(this); 354 return DownloadsFunctionInterface::RunImplImpl(this);
330 } 355 }
331 356
332 DownloadsFunctionInterface::DownloadsFunctionName 357 DownloadsFunctionInterface::DownloadsFunctionName
333 SyncDownloadsFunction::function() const { 358 SyncDownloadsFunction::function() const {
334 return function_; 359 return function_;
335 } 360 }
336 361
362 DownloadItem* SyncDownloadsFunction::GetItem(int id) {
363 return GetItemInternal(GetCurrentBrowser(), profile(), id);
364 }
365
337 AsyncDownloadsFunction::AsyncDownloadsFunction( 366 AsyncDownloadsFunction::AsyncDownloadsFunction(
338 DownloadsFunctionInterface::DownloadsFunctionName function) 367 DownloadsFunctionInterface::DownloadsFunctionName function)
339 : function_(function) { 368 : function_(function) {
340 } 369 }
341 370
342 AsyncDownloadsFunction::~AsyncDownloadsFunction() {} 371 AsyncDownloadsFunction::~AsyncDownloadsFunction() {}
343 372
344 bool AsyncDownloadsFunction::RunImpl() { 373 bool AsyncDownloadsFunction::RunImpl() {
345 return DownloadsFunctionInterface::RunImplImpl(this); 374 return DownloadsFunctionInterface::RunImplImpl(this);
346 } 375 }
347 376
348 DownloadsFunctionInterface::DownloadsFunctionName 377 DownloadsFunctionInterface::DownloadsFunctionName
349 AsyncDownloadsFunction::function() const { 378 AsyncDownloadsFunction::function() const {
350 return function_; 379 return function_;
351 } 380 }
352 381
382 DownloadItem* AsyncDownloadsFunction::GetItem(int id) {
383 return GetItemInternal(GetCurrentBrowser(), profile(), id);
384 }
385
353 DownloadsDownloadFunction::DownloadsDownloadFunction() 386 DownloadsDownloadFunction::DownloadsDownloadFunction()
354 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_DOWNLOAD) { 387 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_DOWNLOAD) {
355 } 388 }
356 389
357 DownloadsDownloadFunction::~DownloadsDownloadFunction() {} 390 DownloadsDownloadFunction::~DownloadsDownloadFunction() {}
358 391
359 DownloadsDownloadFunction::IOData::IOData() 392 DownloadsDownloadFunction::IOData::IOData()
360 : save_as(false), 393 : save_as(false),
361 extra_headers(NULL), 394 extra_headers(NULL),
362 method("GET"), 395 method("GET"),
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 result_.reset(base::Value::CreateIntegerValue(dl_id.local())); 558 result_.reset(base::Value::CreateIntegerValue(dl_id.local()));
526 } else { 559 } else {
527 error_ = net::ErrorToString(error); 560 error_ = net::ErrorToString(error);
528 } 561 }
529 SendResponse(error_.empty()); 562 SendResponse(error_.empty());
530 } 563 }
531 564
532 DownloadsSearchFunction::DownloadsSearchFunction() 565 DownloadsSearchFunction::DownloadsSearchFunction()
533 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH), 566 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH),
534 query_(new DownloadQuery()), 567 query_(new DownloadQuery()),
568 include_on_the_record_(true),
569 include_incognito_(true),
535 get_id_(0), 570 get_id_(0),
536 has_get_id_(false) { 571 has_get_id_(false) {
537 } 572 }
538 573
539 DownloadsSearchFunction::~DownloadsSearchFunction() {} 574 DownloadsSearchFunction::~DownloadsSearchFunction() {}
540 575
541 bool DownloadsSearchFunction::ParseArgs() { 576 bool DownloadsSearchFunction::ParseArgs() {
542 static base::LazyInstance<FilterTypeMap> filter_types = 577 static base::LazyInstance<FilterTypeMap> filter_types =
543 LAZY_INSTANCE_INITIALIZER; 578 LAZY_INSTANCE_INITIALIZER;
544 if (filter_types.Get().size() == 0) 579 if (filter_types.Get().size() == 0)
545 InitFilterTypeMap(filter_types.Get()); 580 InitFilterTypeMap(filter_types.Get());
546 581
547 base::DictionaryValue* query_json = NULL; 582 base::DictionaryValue* query_json = NULL;
548 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); 583 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json));
549 for (base::DictionaryValue::Iterator query_json_field(*query_json); 584 for (base::DictionaryValue::Iterator query_json_field(*query_json);
550 query_json_field.HasNext(); query_json_field.Advance()) { 585 query_json_field.HasNext(); query_json_field.Advance()) {
551 FilterTypeMap::const_iterator filter_type = 586 FilterTypeMap::const_iterator filter_type =
552 filter_types.Get().find(query_json_field.key()); 587 filter_types.Get().find(query_json_field.key());
553 588
554 if (filter_type != filter_types.Get().end()) { 589 if (filter_type != filter_types.Get().end()) {
555 if (!query_->AddFilter(filter_type->second, query_json_field.value())) { 590 if (!query_->AddFilter(filter_type->second, query_json_field.value())) {
556 error_ = download_extension_errors::kInvalidFilterError; 591 error_ = download_extension_errors::kInvalidFilterError;
557 return false; 592 return false;
558 } 593 }
559 } else if (query_json_field.key() == kIdKey) { 594 } else if (query_json_field.key() == kIdKey) {
560 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsInteger( 595 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsInteger(
561 &get_id_)); 596 &get_id_));
562 has_get_id_ = true; 597 has_get_id_ = true;
598 } else if (query_json_field.key() == kIncognito) {
599 std::string incognito;
600 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString(
601 &incognito));
602 if (incognito == kIncognitoNone) {
603 include_incognito_ = false;
604 } else if (incognito == kIncognitoOnly) {
605 include_on_the_record_ = false;
606 } else {
607 EXTENSION_FUNCTION_VALIDATE(false);
608 }
563 } else if (query_json_field.key() == kOrderByKey) { 609 } else if (query_json_field.key() == kOrderByKey) {
564 if (!ParseOrderBy(query_json_field.value())) 610 if (!ParseOrderBy(query_json_field.value()))
565 return false; 611 return false;
566 } else if (query_json_field.key() == kDangerKey) { 612 } else if (query_json_field.key() == kDangerKey) {
567 std::string danger_str; 613 std::string danger_str;
568 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString( 614 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString(
569 &danger_str)); 615 &danger_str));
570 content::DownloadDangerType danger_type = 616 content::DownloadDangerType danger_type =
571 DangerEnumFromString(danger_str); 617 DangerEnumFromString(danger_str);
572 if (danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) { 618 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; 672 error_ = download_extension_errors::kInvalidOrderByError;
627 return false; 673 return false;
628 } 674 }
629 query_->AddSorter(sorter_type->second, direction); 675 query_->AddSorter(sorter_type->second, direction);
630 } 676 }
631 return true; 677 return true;
632 } 678 }
633 679
634 bool DownloadsSearchFunction::RunInternal() { 680 bool DownloadsSearchFunction::RunInternal() {
635 DownloadQuery::DownloadVector all_items, cpp_results; 681 DownloadQuery::DownloadVector all_items, cpp_results;
636 DownloadManager* manager = DownloadServiceFactory::GetForProfile(profile()) 682 Profile* original_profile = NULL;
Matt Perry 2012/05/14 22:57:11 you can just do profile()->GetOriginalProfile() he
benjhayden 2012/05/15 17:16:25 Done.
637 ->GetDownloadManager(); 683 Profile* otr_profile = NULL;
684 if (profile()->IsOffTheRecord()) {
685 otr_profile = profile();
686 original_profile = profile()->GetOriginalProfile();
687 } else {
688 original_profile = profile();
689 }
690 DownloadManager* original_manager = DownloadServiceFactory::GetForProfile(
691 original_profile)->GetDownloadManager();
692 DownloadManager* otr_manager = NULL;
693 if (otr_profile != NULL) {
694 otr_manager = DownloadServiceFactory::GetForProfile(otr_profile)
695 ->GetDownloadManager();
Matt Perry 2012/05/14 22:57:11 wrap with the operator at the end of the preceding
benjhayden 2012/05/15 17:16:25 Done.
696 }
638 if (has_get_id_) { 697 if (has_get_id_) {
639 DownloadItem* item = manager->GetDownloadItem(get_id_); 698 DownloadItem* item = original_manager->GetDownloadItem(get_id_);
699 if ((item == NULL) && (otr_manager != NULL))
700 item = otr_manager->GetDownloadItem(get_id_);
640 if (item != NULL) 701 if (item != NULL)
641 all_items.push_back(item); 702 all_items.push_back(item);
642 } else { 703 } else {
643 manager->GetAllDownloads(FilePath(FILE_PATH_LITERAL("")), &all_items); 704 if (include_on_the_record_)
705 original_manager->GetAllDownloads(
706 FilePath(FILE_PATH_LITERAL("")), &all_items);
707 if (include_incognito_ && (otr_manager != NULL))
708 otr_manager->GetAllDownloads(
709 FilePath(FILE_PATH_LITERAL("")), &all_items);
644 } 710 }
645 query_->Search(all_items.begin(), all_items.end(), &cpp_results); 711 query_->Search(all_items.begin(), all_items.end(), &cpp_results);
646 base::ListValue* json_results = new base::ListValue(); 712 base::ListValue* json_results = new base::ListValue();
647 for (DownloadManager::DownloadVector::const_iterator it = cpp_results.begin(); 713 for (DownloadManager::DownloadVector::const_iterator it = cpp_results.begin();
648 it != cpp_results.end(); ++it) { 714 it != cpp_results.end(); ++it) {
649 scoped_ptr<base::DictionaryValue> item(DownloadItemToJSON(*it)); 715 scoped_ptr<base::DictionaryValue> item(DownloadItemToJSON(*it));
650 json_results->Append(item.release()); 716 json_results->Append(item.release());
651 } 717 }
652 result_.reset(json_results); 718 result_.reset(json_results);
653 return true; 719 return true;
654 } 720 }
655 721
656 DownloadsPauseFunction::DownloadsPauseFunction() 722 DownloadsPauseFunction::DownloadsPauseFunction()
657 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE), 723 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE),
658 download_id_(DownloadId::Invalid().local()) { 724 download_id_(DownloadId::Invalid().local()) {
659 } 725 }
660 726
661 DownloadsPauseFunction::~DownloadsPauseFunction() {} 727 DownloadsPauseFunction::~DownloadsPauseFunction() {}
662 728
663 bool DownloadsPauseFunction::ParseArgs() { 729 bool DownloadsPauseFunction::ParseArgs() {
664 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); 730 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_));
665 return true; 731 return true;
666 } 732 }
667 733
668 bool DownloadsPauseFunction::RunInternal() { 734 bool DownloadsPauseFunction::RunInternal() {
669 DownloadManager* download_manager = 735 DownloadItem* download_item = GetItem(download_id_);
670 DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); 736 if ((download_item == NULL) || !download_item->IsInProgress()) {
671 DownloadItem* download_item =
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(download_id_);
700 DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); 761 if (download_item == NULL) {
701 DownloadItem* download_item =
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 762 // This could be due to an invalid download ID, or it could be due to the
707 // download not being currently active. 763 // download not being currently active.
708 error_ = download_extension_errors::kInvalidOperationError; 764 error_ = download_extension_errors::kInvalidOperationError;
709 } else if (download_item->IsPaused()) { 765 } else if (download_item->IsPaused()) {
710 // If !download_item->IsPaused() already, then we treat it as a success. 766 // If !download_item->IsPaused() already, then we treat it as a success.
711 download_item->TogglePause(); 767 download_item->TogglePause();
712 } 768 }
713 return error_.empty(); 769 return error_.empty();
714 } 770 }
715 771
716 DownloadsCancelFunction::DownloadsCancelFunction() 772 DownloadsCancelFunction::DownloadsCancelFunction()
717 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_CANCEL), 773 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_CANCEL),
718 download_id_(DownloadId::Invalid().local()) { 774 download_id_(DownloadId::Invalid().local()) {
719 } 775 }
720 776
721 DownloadsCancelFunction::~DownloadsCancelFunction() {} 777 DownloadsCancelFunction::~DownloadsCancelFunction() {}
722 778
723 bool DownloadsCancelFunction::ParseArgs() { 779 bool DownloadsCancelFunction::ParseArgs() {
724 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); 780 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_));
725 return true; 781 return true;
726 } 782 }
727 783
728 bool DownloadsCancelFunction::RunInternal() { 784 bool DownloadsCancelFunction::RunInternal() {
729 DownloadManager* download_manager = 785 DownloadItem* download_item = GetItem(download_id_);
730 DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); 786 if (download_item != NULL)
731 DownloadItem* download_item =
732 download_manager->GetActiveDownloadItem(download_id_);
733
734 if (download_item)
735 download_item->Cancel(true); 787 download_item->Cancel(true);
736 // |download_item| can be NULL if the download ID was invalid or if the 788 // |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 789 // download is not currently active. Either way, we don't consider it a
738 // failure. 790 // failure.
739 return error_.empty(); 791 return error_.empty();
740 } 792 }
741 793
742 DownloadsEraseFunction::DownloadsEraseFunction() 794 DownloadsEraseFunction::DownloadsEraseFunction()
743 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ERASE) { 795 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ERASE) {
744 } 796 }
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
1074 ListValue args; 1126 ListValue args;
1075 args.Append(arg); 1127 args.Append(arg);
1076 std::string json_args; 1128 std::string json_args;
1077 base::JSONWriter::Write(&args, &json_args); 1129 base::JSONWriter::Write(&args, &json_args);
1078 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( 1130 profile_->GetExtensionEventRouter()->DispatchEventToRenderers(
1079 event_name, 1131 event_name,
1080 json_args, 1132 json_args,
1081 profile_, 1133 profile_,
1082 GURL()); 1134 GURL());
1083 } 1135 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698