| 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/extensions/api/downloads/downloads_api.h" | 5 #include "chrome/browser/extensions/api/downloads/downloads_api.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 // comparisons. | 506 // comparisons. |
| 507 static base::LazyInstance<FilterTypeMap> filter_types = | 507 static base::LazyInstance<FilterTypeMap> filter_types = |
| 508 LAZY_INSTANCE_INITIALIZER; | 508 LAZY_INSTANCE_INITIALIZER; |
| 509 if (filter_types.Get().empty()) | 509 if (filter_types.Get().empty()) |
| 510 InitFilterTypeMap(filter_types.Pointer()); | 510 InitFilterTypeMap(filter_types.Pointer()); |
| 511 | 511 |
| 512 DownloadQuery query_out; | 512 DownloadQuery query_out; |
| 513 | 513 |
| 514 size_t limit = 1000; | 514 size_t limit = 1000; |
| 515 if (query_in.limit.get()) { | 515 if (query_in.limit.get()) { |
| 516 if (*query_in.limit.get() < 0) { | 516 if (*query_in.limit < 0) { |
| 517 *error = errors::kInvalidQueryLimit; | 517 *error = errors::kInvalidQueryLimit; |
| 518 return; | 518 return; |
| 519 } | 519 } |
| 520 limit = *query_in.limit.get(); | 520 limit = *query_in.limit; |
| 521 } | 521 } |
| 522 if (limit > 0) { | 522 if (limit > 0) { |
| 523 query_out.Limit(limit); | 523 query_out.Limit(limit); |
| 524 } | 524 } |
| 525 | 525 |
| 526 std::string state_string = downloads::ToString(query_in.state); | 526 std::string state_string = downloads::ToString(query_in.state); |
| 527 if (!state_string.empty()) { | 527 if (!state_string.empty()) { |
| 528 DownloadItem::DownloadState state = StateEnumFromString(state_string); | 528 DownloadItem::DownloadState state = StateEnumFromString(state_string); |
| 529 if (state == DownloadItem::MAX_DOWNLOAD_STATE) { | 529 if (state == DownloadItem::MAX_DOWNLOAD_STATE) { |
| 530 *error = errors::kInvalidState; | 530 *error = errors::kInvalidState; |
| 531 return; | 531 return; |
| 532 } | 532 } |
| 533 query_out.AddFilter(state); | 533 query_out.AddFilter(state); |
| 534 } | 534 } |
| 535 std::string danger_string = | 535 std::string danger_string = |
| 536 downloads::ToString(query_in.danger); | 536 downloads::ToString(query_in.danger); |
| 537 if (!danger_string.empty()) { | 537 if (!danger_string.empty()) { |
| 538 content::DownloadDangerType danger_type = DangerEnumFromString( | 538 content::DownloadDangerType danger_type = DangerEnumFromString( |
| 539 danger_string); | 539 danger_string); |
| 540 if (danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) { | 540 if (danger_type == content::DOWNLOAD_DANGER_TYPE_MAX) { |
| 541 *error = errors::kInvalidDangerType; | 541 *error = errors::kInvalidDangerType; |
| 542 return; | 542 return; |
| 543 } | 543 } |
| 544 query_out.AddFilter(danger_type); | 544 query_out.AddFilter(danger_type); |
| 545 } | 545 } |
| 546 if (query_in.order_by.get()) { | 546 if (query_in.order_by.get()) { |
| 547 CompileDownloadQueryOrderBy(*query_in.order_by.get(), error, &query_out); | 547 CompileDownloadQueryOrderBy(*query_in.order_by, error, &query_out); |
| 548 if (!error->empty()) | 548 if (!error->empty()) |
| 549 return; | 549 return; |
| 550 } | 550 } |
| 551 | 551 |
| 552 std::unique_ptr<base::DictionaryValue> query_in_value(query_in.ToValue()); | 552 std::unique_ptr<base::DictionaryValue> query_in_value(query_in.ToValue()); |
| 553 for (base::DictionaryValue::Iterator query_json_field(*query_in_value.get()); | 553 for (base::DictionaryValue::Iterator query_json_field(*query_in_value); |
| 554 !query_json_field.IsAtEnd(); query_json_field.Advance()) { | 554 !query_json_field.IsAtEnd(); query_json_field.Advance()) { |
| 555 FilterTypeMap::const_iterator filter_type = | 555 FilterTypeMap::const_iterator filter_type = |
| 556 filter_types.Get().find(query_json_field.key()); | 556 filter_types.Get().find(query_json_field.key()); |
| 557 if (filter_type != filter_types.Get().end()) { | 557 if (filter_type != filter_types.Get().end()) { |
| 558 if (!query_out.AddFilter(filter_type->second, query_json_field.value())) { | 558 if (!query_out.AddFilter(filter_type->second, query_json_field.value())) { |
| 559 *error = errors::kInvalidFilter; | 559 *error = errors::kInvalidFilter; |
| 560 return; | 560 return; |
| 561 } | 561 } |
| 562 } | 562 } |
| 563 } | 563 } |
| 564 | 564 |
| 565 DownloadQuery::DownloadVector all_items; | 565 DownloadQuery::DownloadVector all_items; |
| 566 if (query_in.id.get()) { | 566 if (query_in.id.get()) { |
| 567 DownloadItem* download_item = manager->GetDownload(*query_in.id.get()); | 567 DownloadItem* download_item = manager->GetDownload(*query_in.id); |
| 568 if (!download_item && incognito_manager) | 568 if (!download_item && incognito_manager) |
| 569 download_item = incognito_manager->GetDownload(*query_in.id.get()); | 569 download_item = incognito_manager->GetDownload(*query_in.id); |
| 570 if (download_item) | 570 if (download_item) |
| 571 all_items.push_back(download_item); | 571 all_items.push_back(download_item); |
| 572 } else { | 572 } else { |
| 573 manager->GetAllDownloads(&all_items); | 573 manager->GetAllDownloads(&all_items); |
| 574 if (incognito_manager) | 574 if (incognito_manager) |
| 575 incognito_manager->GetAllDownloads(&all_items); | 575 incognito_manager->GetAllDownloads(&all_items); |
| 576 } | 576 } |
| 577 query_out.AddFilter(base::Bind(&IsNotTemporaryDownloadFilter)); | 577 query_out.AddFilter(base::Bind(&IsNotTemporaryDownloadFilter)); |
| 578 query_out.Search(all_items.begin(), all_items.end(), results); | 578 query_out.Search(all_items.begin(), all_items.end(), results); |
| 579 } | 579 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 download_item->SetUserData(kKey, this); | 618 download_item->SetUserData(kKey, this); |
| 619 } | 619 } |
| 620 | 620 |
| 621 ~ExtensionDownloadsEventRouterData() override { | 621 ~ExtensionDownloadsEventRouterData() override { |
| 622 if (updated_ > 0) { | 622 if (updated_ > 0) { |
| 623 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", | 623 UMA_HISTOGRAM_PERCENTAGE("Download.OnChanged", |
| 624 (changed_fired_ * 100 / updated_)); | 624 (changed_fired_ * 100 / updated_)); |
| 625 } | 625 } |
| 626 } | 626 } |
| 627 | 627 |
| 628 const base::DictionaryValue& json() const { return *json_.get(); } | 628 const base::DictionaryValue& json() const { return *json_; } |
| 629 void set_json(std::unique_ptr<base::DictionaryValue> json_item) { | 629 void set_json(std::unique_ptr<base::DictionaryValue> json_item) { |
| 630 json_ = std::move(json_item); | 630 json_ = std::move(json_item); |
| 631 } | 631 } |
| 632 | 632 |
| 633 void OnItemUpdated() { ++updated_; } | 633 void OnItemUpdated() { ++updated_; } |
| 634 void OnChangedFired() { ++changed_fired_; } | 634 void OnChangedFired() { ++changed_fired_; } |
| 635 | 635 |
| 636 static void SetDetermineFilenameTimeoutSecondsForTesting(int s) { | 636 static void SetDetermineFilenameTimeoutSecondsForTesting(int s) { |
| 637 determine_filename_timeout_s_ = s; | 637 determine_filename_timeout_s_ = s; |
| 638 } | 638 } |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 983 #if defined(OS_WIN) | 983 #if defined(OS_WIN) |
| 984 // Can't get filename16 from options.ToValue() because that converts it from | 984 // Can't get filename16 from options.ToValue() because that converts it from |
| 985 // std::string. | 985 // std::string. |
| 986 base::DictionaryValue* options_value = NULL; | 986 base::DictionaryValue* options_value = NULL; |
| 987 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options_value)); | 987 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options_value)); |
| 988 base::string16 filename16; | 988 base::string16 filename16; |
| 989 EXTENSION_FUNCTION_VALIDATE(options_value->GetString( | 989 EXTENSION_FUNCTION_VALIDATE(options_value->GetString( |
| 990 kFilenameKey, &filename16)); | 990 kFilenameKey, &filename16)); |
| 991 creator_suggested_filename = base::FilePath(filename16); | 991 creator_suggested_filename = base::FilePath(filename16); |
| 992 #elif defined(OS_POSIX) | 992 #elif defined(OS_POSIX) |
| 993 creator_suggested_filename = base::FilePath(*options.filename.get()); | 993 creator_suggested_filename = base::FilePath(*options.filename); |
| 994 #endif | 994 #endif |
| 995 if (!net::IsSafePortableRelativePath(creator_suggested_filename)) { | 995 if (!net::IsSafePortableRelativePath(creator_suggested_filename)) { |
| 996 error_ = errors::kInvalidFilename; | 996 error_ = errors::kInvalidFilename; |
| 997 return false; | 997 return false; |
| 998 } | 998 } |
| 999 } | 999 } |
| 1000 | 1000 |
| 1001 if (options.save_as.get()) | 1001 if (options.save_as.get()) |
| 1002 download_params->set_prompt(*options.save_as.get()); | 1002 download_params->set_prompt(*options.save_as); |
| 1003 | 1003 |
| 1004 if (options.headers.get()) { | 1004 if (options.headers.get()) { |
| 1005 for (const downloads::HeaderNameValuePair& name_value : *options.headers) { | 1005 for (const downloads::HeaderNameValuePair& name_value : *options.headers) { |
| 1006 if (!net::HttpUtil::IsValidHeaderName(name_value.name)) { | 1006 if (!net::HttpUtil::IsValidHeaderName(name_value.name)) { |
| 1007 error_ = errors::kInvalidHeaderName; | 1007 error_ = errors::kInvalidHeaderName; |
| 1008 return false; | 1008 return false; |
| 1009 } | 1009 } |
| 1010 if (!net::HttpUtil::IsSafeHeader(name_value.name)) { | 1010 if (!net::HttpUtil::IsSafeHeader(name_value.name)) { |
| 1011 error_ = errors::kInvalidHeaderUnsafe; | 1011 error_ = errors::kInvalidHeaderUnsafe; |
| 1012 return false; | 1012 return false; |
| 1013 } | 1013 } |
| 1014 if (!net::HttpUtil::IsValidHeaderValue(name_value.value)) { | 1014 if (!net::HttpUtil::IsValidHeaderValue(name_value.value)) { |
| 1015 error_ = errors::kInvalidHeaderValue; | 1015 error_ = errors::kInvalidHeaderValue; |
| 1016 return false; | 1016 return false; |
| 1017 } | 1017 } |
| 1018 download_params->add_request_header(name_value.name, name_value.value); | 1018 download_params->add_request_header(name_value.name, name_value.value); |
| 1019 } | 1019 } |
| 1020 } | 1020 } |
| 1021 | 1021 |
| 1022 std::string method_string = | 1022 std::string method_string = |
| 1023 downloads::ToString(options.method); | 1023 downloads::ToString(options.method); |
| 1024 if (!method_string.empty()) | 1024 if (!method_string.empty()) |
| 1025 download_params->set_method(method_string); | 1025 download_params->set_method(method_string); |
| 1026 if (options.body.get()) | 1026 if (options.body.get()) |
| 1027 download_params->set_post_body(*options.body.get()); | 1027 download_params->set_post_body(*options.body); |
| 1028 download_params->set_callback(base::Bind( | 1028 download_params->set_callback(base::Bind( |
| 1029 &DownloadsDownloadFunction::OnStarted, this, | 1029 &DownloadsDownloadFunction::OnStarted, this, |
| 1030 creator_suggested_filename, options.conflict_action)); | 1030 creator_suggested_filename, options.conflict_action)); |
| 1031 // Prevent login prompts for 401/407 responses. | 1031 // Prevent login prompts for 401/407 responses. |
| 1032 download_params->set_do_not_prompt_for_login(true); | 1032 download_params->set_do_not_prompt_for_login(true); |
| 1033 | 1033 |
| 1034 DownloadManager* manager = BrowserContext::GetDownloadManager( | 1034 DownloadManager* manager = BrowserContext::GetDownloadManager( |
| 1035 current_profile); | 1035 current_profile); |
| 1036 manager->DownloadUrl(std::move(download_params)); | 1036 manager->DownloadUrl(std::move(download_params)); |
| 1037 RecordDownloadSource(DOWNLOAD_INITIATED_BY_EXTENSION); | 1037 RecordDownloadSource(DOWNLOAD_INITIATED_BY_EXTENSION); |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1475 } | 1475 } |
| 1476 | 1476 |
| 1477 bool DownloadsGetFileIconFunction::RunAsync() { | 1477 bool DownloadsGetFileIconFunction::RunAsync() { |
| 1478 std::unique_ptr<downloads::GetFileIcon::Params> params( | 1478 std::unique_ptr<downloads::GetFileIcon::Params> params( |
| 1479 downloads::GetFileIcon::Params::Create(*args_)); | 1479 downloads::GetFileIcon::Params::Create(*args_)); |
| 1480 EXTENSION_FUNCTION_VALIDATE(params.get()); | 1480 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 1481 const downloads::GetFileIconOptions* options = | 1481 const downloads::GetFileIconOptions* options = |
| 1482 params->options.get(); | 1482 params->options.get(); |
| 1483 int icon_size = kDefaultIconSize; | 1483 int icon_size = kDefaultIconSize; |
| 1484 if (options && options->size.get()) | 1484 if (options && options->size.get()) |
| 1485 icon_size = *options->size.get(); | 1485 icon_size = *options->size; |
| 1486 DownloadItem* download_item = | 1486 DownloadItem* download_item = |
| 1487 GetDownload(GetProfile(), include_incognito(), params->download_id); | 1487 GetDownload(GetProfile(), include_incognito(), params->download_id); |
| 1488 if (InvalidId(download_item, &error_) || | 1488 if (InvalidId(download_item, &error_) || |
| 1489 Fault(download_item->GetTargetFilePath().empty(), | 1489 Fault(download_item->GetTargetFilePath().empty(), |
| 1490 errors::kEmptyFile, &error_)) | 1490 errors::kEmptyFile, &error_)) |
| 1491 return false; | 1491 return false; |
| 1492 // In-progress downloads return the intermediate filename for GetFullPath() | 1492 // In-progress downloads return the intermediate filename for GetFullPath() |
| 1493 // which doesn't have the final extension. Therefore a good file icon can't be | 1493 // which doesn't have the final extension. Therefore a good file icon can't be |
| 1494 // found, so use GetTargetFilePath() instead. | 1494 // found, so use GetTargetFilePath() instead. |
| 1495 DCHECK(icon_extractor_.get()); | 1495 DCHECK(icon_extractor_.get()); |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1800 DownloadItemToJSON(download_item, profile_)); | 1800 DownloadItemToJSON(download_item, profile_)); |
| 1801 std::unique_ptr<base::DictionaryValue> delta(new base::DictionaryValue()); | 1801 std::unique_ptr<base::DictionaryValue> delta(new base::DictionaryValue()); |
| 1802 delta->SetInteger(kIdKey, download_item->GetId()); | 1802 delta->SetInteger(kIdKey, download_item->GetId()); |
| 1803 std::set<std::string> new_fields; | 1803 std::set<std::string> new_fields; |
| 1804 bool changed = false; | 1804 bool changed = false; |
| 1805 | 1805 |
| 1806 // For each field in the new json representation of the download_item except | 1806 // For each field in the new json representation of the download_item except |
| 1807 // the bytesReceived field, if the field has changed from the previous old | 1807 // the bytesReceived field, if the field has changed from the previous old |
| 1808 // json, set the differences in the |delta| object and remember that something | 1808 // json, set the differences in the |delta| object and remember that something |
| 1809 // significant changed. | 1809 // significant changed. |
| 1810 for (base::DictionaryValue::Iterator iter(*new_json.get()); | 1810 for (base::DictionaryValue::Iterator iter(*new_json); !iter.IsAtEnd(); |
| 1811 !iter.IsAtEnd(); iter.Advance()) { | 1811 iter.Advance()) { |
| 1812 new_fields.insert(iter.key()); | 1812 new_fields.insert(iter.key()); |
| 1813 if (IsDownloadDeltaField(iter.key())) { | 1813 if (IsDownloadDeltaField(iter.key())) { |
| 1814 const base::Value* old_value = NULL; | 1814 const base::Value* old_value = NULL; |
| 1815 if (!data->json().HasKey(iter.key()) || | 1815 if (!data->json().HasKey(iter.key()) || |
| 1816 (data->json().Get(iter.key(), &old_value) && | 1816 (data->json().Get(iter.key(), &old_value) && |
| 1817 !iter.value().Equals(old_value))) { | 1817 !iter.value().Equals(old_value))) { |
| 1818 delta->Set(iter.key() + ".current", iter.value().DeepCopy()); | 1818 delta->Set(iter.key() + ".current", iter.value().DeepCopy()); |
| 1819 if (old_value) | 1819 if (old_value) |
| 1820 delta->Set(iter.key() + ".previous", old_value->DeepCopy()); | 1820 delta->Set(iter.key() + ".previous", old_value->DeepCopy()); |
| 1821 changed = true; | 1821 changed = true; |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1911 return; | 1911 return; |
| 1912 base::Time now(base::Time::Now()); | 1912 base::Time now(base::Time::Now()); |
| 1913 int delta = now.ToTimeT() - last_checked_removal_.ToTimeT(); | 1913 int delta = now.ToTimeT() - last_checked_removal_.ToTimeT(); |
| 1914 if (delta <= kFileExistenceRateLimitSeconds) | 1914 if (delta <= kFileExistenceRateLimitSeconds) |
| 1915 return; | 1915 return; |
| 1916 last_checked_removal_ = now; | 1916 last_checked_removal_ = now; |
| 1917 manager->CheckForHistoryFilesRemoval(); | 1917 manager->CheckForHistoryFilesRemoval(); |
| 1918 } | 1918 } |
| 1919 | 1919 |
| 1920 } // namespace extensions | 1920 } // namespace extensions |
| OLD | NEW |