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

Unified Diff: components/image_fetcher/core/image_data_fetcher.cc

Issue 2781473003: Add |SetImageDownloadLimit| to ImageFetcher to limit downloaded bytes (Closed)
Patch Set: Use base::Optional<int64_t> Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: components/image_fetcher/core/image_data_fetcher.cc
diff --git a/components/image_fetcher/core/image_data_fetcher.cc b/components/image_fetcher/core/image_data_fetcher.cc
index 0ef812f6465ffe2436e7aed35d73fa781d14c0ab..39b44e26ba252ba249ffb71c1c7c6f2b3feac4f7 100644
--- a/components/image_fetcher/core/image_data_fetcher.cc
+++ b/components/image_fetcher/core/image_data_fetcher.cc
@@ -4,6 +4,8 @@
#include "components/image_fetcher/core/image_data_fetcher.h"
+#include <utility>
+
#include "net/base/load_flags.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
@@ -45,6 +47,11 @@ void ImageDataFetcher::SetDataUseServiceName(
data_use_service_name_ = data_use_service_name;
}
+void ImageDataFetcher::SetImageDownloadLimit(
+ const base::Optional<int64_t>& max_download_bytes) {
+ max_download_bytes_ = max_download_bytes;
+}
+
void ImageDataFetcher::FetchImageData(
const GURL& image_url,
const ImageDataFetcherCallback& callback) {
@@ -77,9 +84,7 @@ void ImageDataFetcher::FetchImageData(
}
void ImageDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
- auto request_iter = pending_requests_.find(source);
- DCHECK(request_iter != pending_requests_.end());
-
+ DCHECK(pending_requests_.find(source) != pending_requests_.end());
bool success = source->GetStatus().status() == net::URLRequestStatus::SUCCESS;
RequestMetadata metadata;
@@ -94,9 +99,36 @@ void ImageDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
if (success) {
source->GetResponseAsString(&image_data);
}
- request_iter->second->callback.Run(image_data, metadata);
+ FinishRequest(source, metadata, image_data);
+}
- // Remove the finished request.
+void ImageDataFetcher::OnURLFetchDownloadProgress(
+ const net::URLFetcher* source,
+ int64_t current,
+ int64_t total,
+ int64_t current_network_bytes) {
+ if (!max_download_bytes_.has_value()) {
+ return;
+ }
+ if (total <= max_download_bytes_.value() &&
+ current <= max_download_bytes_.value()) {
+ return;
+ }
+ DCHECK(pending_requests_.find(source) != pending_requests_.end());
+ LOG(WARNING) << "Image data exceeded download size limit.";
+ RequestMetadata metadata;
+ metadata.from_http_cache = false;
Marc Treib 2017/03/27 14:49:18 This is the default anyway, no?
fhorschig 2017/03/27 16:33:34 Gone. (Should be)
+ metadata.http_response_code = net::URLFetcher::RESPONSE_CODE_INVALID;
+
+ FinishRequest(source, metadata, /*image_data=*/std::string());
+}
+
+void ImageDataFetcher::FinishRequest(const net::URLFetcher* source,
+ const RequestMetadata& metadata,
+ const std::string& image_data) {
+ auto request_iter = pending_requests_.find(source);
+ DCHECK(request_iter != pending_requests_.end());
+ request_iter->second->callback.Run(image_data, metadata);
pending_requests_.erase(request_iter);
}

Powered by Google App Engine
This is Rietveld 408576698