Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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"; | |
|
Matt Perry
2012/05/15 19:02:23
This can go away.
benjhayden
2012/05/15 21:05:05
Done.
| |
| 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 Loading... | |
| 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 Loading... | |
| 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* GetActiveItemInternal( | |
| 315 Profile* profile, | |
| 316 bool spanning_mode, | |
| 317 bool include_incognito, | |
| 318 int id) { | |
| 319 // Spanning-mode extensions should set profile() to the on-record profile, but | |
| 320 // this does not appear to be the case for extension_function_test_utils, so | |
|
Matt Perry
2012/05/15 19:02:23
I'd rather fix extension_function_test_utils if th
benjhayden
2012/05/15 21:05:05
Found another way around; see download_extension_t
| |
| 321 // grab the on-record profile manually in this case. | |
| 322 Profile* current_profile = (spanning_mode && | |
| 323 profile->IsOffTheRecord()) ? | |
| 324 profile->GetOriginalProfile() : profile; | |
| 325 DownloadManager* manager = DownloadServiceFactory::GetForProfile( | |
| 326 current_profile)->GetDownloadManager(); | |
| 327 DownloadItem* download_item = manager->GetActiveDownloadItem(id); | |
| 328 DownloadManager* incognito_manager = (include_incognito && | |
| 329 profile->HasOffTheRecordProfile()) ? | |
| 330 DownloadServiceFactory::GetForProfile( | |
| 331 profile->GetOffTheRecordProfile())->GetDownloadManager() : NULL; | |
| 332 if (!download_item && incognito_manager) | |
| 333 download_item = incognito_manager->GetActiveDownloadItem(id); | |
| 334 return download_item; | |
| 335 } | |
| 336 | |
| 310 } // namespace | 337 } // namespace |
| 311 | 338 |
| 312 bool DownloadsFunctionInterface::RunImplImpl( | 339 bool DownloadsFunctionInterface::RunImplImpl( |
| 313 DownloadsFunctionInterface* pimpl) { | 340 DownloadsFunctionInterface* pimpl) { |
| 314 CHECK(pimpl); | 341 CHECK(pimpl); |
| 315 if (!pimpl->ParseArgs()) return false; | 342 if (!pimpl->ParseArgs()) return false; |
| 316 UMA_HISTOGRAM_ENUMERATION( | 343 UMA_HISTOGRAM_ENUMERATION( |
| 317 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); | 344 "Download.ApiFunctions", pimpl->function(), DOWNLOADS_FUNCTION_LAST); |
| 318 return pimpl->RunInternal(); | 345 return pimpl->RunInternal(); |
| 319 } | 346 } |
| 320 | 347 |
| 321 SyncDownloadsFunction::SyncDownloadsFunction( | 348 SyncDownloadsFunction::SyncDownloadsFunction( |
| 322 DownloadsFunctionInterface::DownloadsFunctionName function) | 349 DownloadsFunctionInterface::DownloadsFunctionName function) |
| 323 : function_(function) { | 350 : function_(function) { |
| 324 } | 351 } |
| 325 | 352 |
| 326 SyncDownloadsFunction::~SyncDownloadsFunction() {} | 353 SyncDownloadsFunction::~SyncDownloadsFunction() {} |
| 327 | 354 |
| 328 bool SyncDownloadsFunction::RunImpl() { | 355 bool SyncDownloadsFunction::RunImpl() { |
| 329 return DownloadsFunctionInterface::RunImplImpl(this); | 356 return DownloadsFunctionInterface::RunImplImpl(this); |
| 330 } | 357 } |
| 331 | 358 |
| 332 DownloadsFunctionInterface::DownloadsFunctionName | 359 DownloadsFunctionInterface::DownloadsFunctionName |
| 333 SyncDownloadsFunction::function() const { | 360 SyncDownloadsFunction::function() const { |
| 334 return function_; | 361 return function_; |
| 335 } | 362 } |
| 336 | 363 |
| 364 DownloadItem* SyncDownloadsFunction::GetActiveItem(int download_id) { | |
| 365 return GetActiveItemInternal( | |
| 366 profile(), | |
| 367 !GetExtension() || !GetExtension()->incognito_split_mode(), | |
| 368 include_incognito(), | |
| 369 download_id); | |
| 370 } | |
| 371 | |
| 337 AsyncDownloadsFunction::AsyncDownloadsFunction( | 372 AsyncDownloadsFunction::AsyncDownloadsFunction( |
| 338 DownloadsFunctionInterface::DownloadsFunctionName function) | 373 DownloadsFunctionInterface::DownloadsFunctionName function) |
| 339 : function_(function) { | 374 : function_(function) { |
| 340 } | 375 } |
| 341 | 376 |
| 342 AsyncDownloadsFunction::~AsyncDownloadsFunction() {} | 377 AsyncDownloadsFunction::~AsyncDownloadsFunction() {} |
| 343 | 378 |
| 344 bool AsyncDownloadsFunction::RunImpl() { | 379 bool AsyncDownloadsFunction::RunImpl() { |
| 345 return DownloadsFunctionInterface::RunImplImpl(this); | 380 return DownloadsFunctionInterface::RunImplImpl(this); |
| 346 } | 381 } |
| 347 | 382 |
| 348 DownloadsFunctionInterface::DownloadsFunctionName | 383 DownloadsFunctionInterface::DownloadsFunctionName |
| 349 AsyncDownloadsFunction::function() const { | 384 AsyncDownloadsFunction::function() const { |
| 350 return function_; | 385 return function_; |
| 351 } | 386 } |
| 352 | 387 |
| 388 DownloadItem* AsyncDownloadsFunction::GetActiveItem(int download_id) { | |
| 389 return GetActiveItemInternal( | |
| 390 profile(), | |
| 391 !GetExtension() || !GetExtension()->incognito_split_mode(), | |
| 392 include_incognito(), | |
| 393 download_id); | |
| 394 } | |
| 395 | |
| 353 DownloadsDownloadFunction::DownloadsDownloadFunction() | 396 DownloadsDownloadFunction::DownloadsDownloadFunction() |
| 354 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_DOWNLOAD) { | 397 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_DOWNLOAD) { |
| 355 } | 398 } |
| 356 | 399 |
| 357 DownloadsDownloadFunction::~DownloadsDownloadFunction() {} | 400 DownloadsDownloadFunction::~DownloadsDownloadFunction() {} |
| 358 | 401 |
| 359 DownloadsDownloadFunction::IOData::IOData() | 402 DownloadsDownloadFunction::IOData::IOData() |
| 360 : save_as(false), | 403 : save_as(false), |
| 361 extra_headers(NULL), | 404 extra_headers(NULL), |
| 362 method("GET"), | 405 method("GET"), |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 525 result_.reset(base::Value::CreateIntegerValue(dl_id.local())); | 568 result_.reset(base::Value::CreateIntegerValue(dl_id.local())); |
| 526 } else { | 569 } else { |
| 527 error_ = net::ErrorToString(error); | 570 error_ = net::ErrorToString(error); |
| 528 } | 571 } |
| 529 SendResponse(error_.empty()); | 572 SendResponse(error_.empty()); |
| 530 } | 573 } |
| 531 | 574 |
| 532 DownloadsSearchFunction::DownloadsSearchFunction() | 575 DownloadsSearchFunction::DownloadsSearchFunction() |
| 533 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH), | 576 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_SEARCH), |
| 534 query_(new DownloadQuery()), | 577 query_(new DownloadQuery()), |
| 578 include_on_the_record_(true), | |
| 579 include_incognito_(true), | |
| 535 get_id_(0), | 580 get_id_(0), |
| 536 has_get_id_(false) { | 581 has_get_id_(false) { |
| 537 } | 582 } |
| 538 | 583 |
| 539 DownloadsSearchFunction::~DownloadsSearchFunction() {} | 584 DownloadsSearchFunction::~DownloadsSearchFunction() {} |
| 540 | 585 |
| 541 bool DownloadsSearchFunction::ParseArgs() { | 586 bool DownloadsSearchFunction::ParseArgs() { |
| 542 static base::LazyInstance<FilterTypeMap> filter_types = | 587 static base::LazyInstance<FilterTypeMap> filter_types = |
| 543 LAZY_INSTANCE_INITIALIZER; | 588 LAZY_INSTANCE_INITIALIZER; |
| 544 if (filter_types.Get().size() == 0) | 589 if (filter_types.Get().size() == 0) |
| 545 InitFilterTypeMap(filter_types.Get()); | 590 InitFilterTypeMap(filter_types.Get()); |
| 546 | 591 |
| 547 base::DictionaryValue* query_json = NULL; | 592 base::DictionaryValue* query_json = NULL; |
| 548 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); | 593 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query_json)); |
| 549 for (base::DictionaryValue::Iterator query_json_field(*query_json); | 594 for (base::DictionaryValue::Iterator query_json_field(*query_json); |
| 550 query_json_field.HasNext(); query_json_field.Advance()) { | 595 query_json_field.HasNext(); query_json_field.Advance()) { |
| 551 FilterTypeMap::const_iterator filter_type = | 596 FilterTypeMap::const_iterator filter_type = |
| 552 filter_types.Get().find(query_json_field.key()); | 597 filter_types.Get().find(query_json_field.key()); |
| 553 | 598 |
| 554 if (filter_type != filter_types.Get().end()) { | 599 if (filter_type != filter_types.Get().end()) { |
| 555 if (!query_->AddFilter(filter_type->second, query_json_field.value())) { | 600 if (!query_->AddFilter(filter_type->second, query_json_field.value())) { |
| 556 error_ = download_extension_errors::kInvalidFilterError; | 601 error_ = download_extension_errors::kInvalidFilterError; |
| 557 return false; | 602 return false; |
| 558 } | 603 } |
| 559 } else if (query_json_field.key() == kIdKey) { | 604 } else if (query_json_field.key() == kIdKey) { |
| 560 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsInteger( | 605 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsInteger( |
| 561 &get_id_)); | 606 &get_id_)); |
| 562 has_get_id_ = true; | 607 has_get_id_ = true; |
| 608 } else if (query_json_field.key() == kIncognito) { | |
|
Matt Perry
2012/05/15 19:02:23
This is obsolete.
benjhayden
2012/05/15 21:05:05
Done.
| |
| 609 std::string incognito; | |
| 610 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString( | |
| 611 &incognito)); | |
| 612 if (incognito == kIncognitoNone) { | |
| 613 include_incognito_ = false; | |
| 614 } else if (incognito == kIncognitoOnly) { | |
| 615 include_on_the_record_ = false; | |
| 616 } else { | |
| 617 EXTENSION_FUNCTION_VALIDATE(false); | |
| 618 } | |
| 563 } else if (query_json_field.key() == kOrderByKey) { | 619 } else if (query_json_field.key() == kOrderByKey) { |
| 564 if (!ParseOrderBy(query_json_field.value())) | 620 if (!ParseOrderBy(query_json_field.value())) |
| 565 return false; | 621 return false; |
| 566 } else if (query_json_field.key() == kDangerKey) { | 622 } else if (query_json_field.key() == kDangerKey) { |
| 567 std::string danger_str; | 623 std::string danger_str; |
| 568 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString( | 624 EXTENSION_FUNCTION_VALIDATE(query_json_field.value().GetAsString( |
| 569 &danger_str)); | 625 &danger_str)); |
| 570 content::DownloadDangerType danger_type = | 626 content::DownloadDangerType danger_type = |
| 571 DangerEnumFromString(danger_str); | 627 DangerEnumFromString(danger_str); |
| 572 if (danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) { | 628 if (danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 625 if (sorter_type == sorter_types.Get().end()) { | 681 if (sorter_type == sorter_types.Get().end()) { |
| 626 error_ = download_extension_errors::kInvalidOrderByError; | 682 error_ = download_extension_errors::kInvalidOrderByError; |
| 627 return false; | 683 return false; |
| 628 } | 684 } |
| 629 query_->AddSorter(sorter_type->second, direction); | 685 query_->AddSorter(sorter_type->second, direction); |
| 630 } | 686 } |
| 631 return true; | 687 return true; |
| 632 } | 688 } |
| 633 | 689 |
| 634 bool DownloadsSearchFunction::RunInternal() { | 690 bool DownloadsSearchFunction::RunInternal() { |
| 691 // Spanning-mode extensions should set profile() to the on-record profile, but | |
| 692 // this does not appear to be the case for extension_function_test_utils, so | |
| 693 // grab the on-record profile manually in this case. | |
| 694 Profile* current_profile = ((!GetExtension() || | |
| 695 !GetExtension()->incognito_split_mode()) && | |
| 696 profile()->IsOffTheRecord()) ? | |
| 697 profile()->GetOriginalProfile() : profile(); | |
| 698 DownloadManager* manager = DownloadServiceFactory::GetForProfile( | |
| 699 current_profile)->GetDownloadManager(); | |
| 700 DownloadManager* incognito_manager = (include_incognito() && | |
| 701 profile()->HasOffTheRecordProfile()) ? | |
| 702 DownloadServiceFactory::GetForProfile( | |
| 703 profile()->GetOffTheRecordProfile())->GetDownloadManager() : NULL; | |
| 635 DownloadQuery::DownloadVector all_items, cpp_results; | 704 DownloadQuery::DownloadVector all_items, cpp_results; |
| 636 DownloadManager* manager = DownloadServiceFactory::GetForProfile(profile()) | |
| 637 ->GetDownloadManager(); | |
| 638 if (has_get_id_) { | 705 if (has_get_id_) { |
| 639 DownloadItem* item = manager->GetDownloadItem(get_id_); | 706 DownloadItem* item = manager->GetDownloadItem(get_id_); |
| 640 if (item != NULL) | 707 if (!item && incognito_manager) |
| 708 item = incognito_manager->GetDownloadItem(get_id_); | |
| 709 if (item) | |
| 641 all_items.push_back(item); | 710 all_items.push_back(item); |
| 642 } else { | 711 } else { |
| 643 manager->GetAllDownloads(FilePath(FILE_PATH_LITERAL("")), &all_items); | 712 manager->GetAllDownloads(FilePath(FILE_PATH_LITERAL("")), &all_items); |
| 713 if (incognito_manager) | |
| 714 incognito_manager->GetAllDownloads( | |
| 715 FilePath(FILE_PATH_LITERAL("")), &all_items); | |
| 644 } | 716 } |
| 645 query_->Search(all_items.begin(), all_items.end(), &cpp_results); | 717 query_->Search(all_items.begin(), all_items.end(), &cpp_results); |
| 646 base::ListValue* json_results = new base::ListValue(); | 718 base::ListValue* json_results = new base::ListValue(); |
| 647 for (DownloadManager::DownloadVector::const_iterator it = cpp_results.begin(); | 719 for (DownloadManager::DownloadVector::const_iterator it = cpp_results.begin(); |
| 648 it != cpp_results.end(); ++it) { | 720 it != cpp_results.end(); ++it) { |
| 649 scoped_ptr<base::DictionaryValue> item(DownloadItemToJSON(*it)); | 721 scoped_ptr<base::DictionaryValue> item(DownloadItemToJSON(*it)); |
| 650 json_results->Append(item.release()); | 722 json_results->Append(item.release()); |
| 651 } | 723 } |
| 652 result_.reset(json_results); | 724 result_.reset(json_results); |
| 653 return true; | 725 return true; |
| 654 } | 726 } |
| 655 | 727 |
| 656 DownloadsPauseFunction::DownloadsPauseFunction() | 728 DownloadsPauseFunction::DownloadsPauseFunction() |
| 657 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE), | 729 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_PAUSE), |
| 658 download_id_(DownloadId::Invalid().local()) { | 730 download_id_(DownloadId::Invalid().local()) { |
| 659 } | 731 } |
| 660 | 732 |
| 661 DownloadsPauseFunction::~DownloadsPauseFunction() {} | 733 DownloadsPauseFunction::~DownloadsPauseFunction() {} |
| 662 | 734 |
| 663 bool DownloadsPauseFunction::ParseArgs() { | 735 bool DownloadsPauseFunction::ParseArgs() { |
| 664 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); | 736 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); |
| 665 return true; | 737 return true; |
| 666 } | 738 } |
| 667 | 739 |
| 668 bool DownloadsPauseFunction::RunInternal() { | 740 bool DownloadsPauseFunction::RunInternal() { |
| 669 DownloadManager* download_manager = | 741 DownloadItem* download_item = GetActiveItem(download_id_); |
| 670 DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); | 742 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 | 743 // This could be due to an invalid download ID, or it could be due to the |
| 677 // download not being currently active. | 744 // download not being currently active. |
| 678 error_ = download_extension_errors::kInvalidOperationError; | 745 error_ = download_extension_errors::kInvalidOperationError; |
| 679 } else if (!download_item->IsPaused()) { | 746 } else if (!download_item->IsPaused()) { |
| 680 // If download_item->IsPaused() already then we treat it as a success. | 747 // If download_item->IsPaused() already then we treat it as a success. |
| 681 download_item->TogglePause(); | 748 download_item->TogglePause(); |
| 682 } | 749 } |
| 683 return error_.empty(); | 750 return error_.empty(); |
| 684 } | 751 } |
| 685 | 752 |
| 686 DownloadsResumeFunction::DownloadsResumeFunction() | 753 DownloadsResumeFunction::DownloadsResumeFunction() |
| 687 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_RESUME), | 754 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_RESUME), |
| 688 download_id_(DownloadId::Invalid().local()) { | 755 download_id_(DownloadId::Invalid().local()) { |
| 689 } | 756 } |
| 690 | 757 |
| 691 DownloadsResumeFunction::~DownloadsResumeFunction() {} | 758 DownloadsResumeFunction::~DownloadsResumeFunction() {} |
| 692 | 759 |
| 693 bool DownloadsResumeFunction::ParseArgs() { | 760 bool DownloadsResumeFunction::ParseArgs() { |
| 694 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); | 761 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); |
| 695 return true; | 762 return true; |
| 696 } | 763 } |
| 697 | 764 |
| 698 bool DownloadsResumeFunction::RunInternal() { | 765 bool DownloadsResumeFunction::RunInternal() { |
| 699 DownloadManager* download_manager = | 766 DownloadItem* download_item = GetActiveItem(download_id_); |
| 700 DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); | 767 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 | 768 // This could be due to an invalid download ID, or it could be due to the |
| 707 // download not being currently active. | 769 // download not being currently active. |
| 708 error_ = download_extension_errors::kInvalidOperationError; | 770 error_ = download_extension_errors::kInvalidOperationError; |
| 709 } else if (download_item->IsPaused()) { | 771 } else if (download_item->IsPaused()) { |
| 710 // If !download_item->IsPaused() already, then we treat it as a success. | 772 // If !download_item->IsPaused() already, then we treat it as a success. |
| 711 download_item->TogglePause(); | 773 download_item->TogglePause(); |
| 712 } | 774 } |
| 713 return error_.empty(); | 775 return error_.empty(); |
| 714 } | 776 } |
| 715 | 777 |
| 716 DownloadsCancelFunction::DownloadsCancelFunction() | 778 DownloadsCancelFunction::DownloadsCancelFunction() |
| 717 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_CANCEL), | 779 : SyncDownloadsFunction(DOWNLOADS_FUNCTION_CANCEL), |
| 718 download_id_(DownloadId::Invalid().local()) { | 780 download_id_(DownloadId::Invalid().local()) { |
| 719 } | 781 } |
| 720 | 782 |
| 721 DownloadsCancelFunction::~DownloadsCancelFunction() {} | 783 DownloadsCancelFunction::~DownloadsCancelFunction() {} |
| 722 | 784 |
| 723 bool DownloadsCancelFunction::ParseArgs() { | 785 bool DownloadsCancelFunction::ParseArgs() { |
| 724 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); | 786 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &download_id_)); |
| 725 return true; | 787 return true; |
| 726 } | 788 } |
| 727 | 789 |
| 728 bool DownloadsCancelFunction::RunInternal() { | 790 bool DownloadsCancelFunction::RunInternal() { |
| 729 DownloadManager* download_manager = | 791 DownloadItem* download_item = GetActiveItem(download_id_); |
| 730 DownloadServiceFactory::GetForProfile(profile())->GetDownloadManager(); | 792 if (download_item != NULL) |
| 731 DownloadItem* download_item = | |
| 732 download_manager->GetActiveDownloadItem(download_id_); | |
| 733 | |
| 734 if (download_item) | |
| 735 download_item->Cancel(true); | 793 download_item->Cancel(true); |
| 736 // |download_item| can be NULL if the download ID was invalid or if the | 794 // |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 | 795 // download is not currently active. Either way, we don't consider it a |
| 738 // failure. | 796 // failure. |
| 739 return error_.empty(); | 797 return error_.empty(); |
| 740 } | 798 } |
| 741 | 799 |
| 742 DownloadsEraseFunction::DownloadsEraseFunction() | 800 DownloadsEraseFunction::DownloadsEraseFunction() |
| 743 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ERASE) { | 801 : AsyncDownloadsFunction(DOWNLOADS_FUNCTION_ERASE) { |
| 744 } | 802 } |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1074 ListValue args; | 1132 ListValue args; |
| 1075 args.Append(arg); | 1133 args.Append(arg); |
| 1076 std::string json_args; | 1134 std::string json_args; |
| 1077 base::JSONWriter::Write(&args, &json_args); | 1135 base::JSONWriter::Write(&args, &json_args); |
| 1078 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | 1136 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( |
| 1079 event_name, | 1137 event_name, |
| 1080 json_args, | 1138 json_args, |
| 1081 profile_, | 1139 profile_, |
| 1082 GURL()); | 1140 GURL()); |
| 1083 } | 1141 } |
| OLD | NEW |