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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/image_fetcher/core/image_data_fetcher.h" 5 #include "components/image_fetcher/core/image_data_fetcher.h"
6 6
7 #include <utility>
8
7 #include "net/base/load_flags.h" 9 #include "net/base/load_flags.h"
8 #include "net/http/http_response_headers.h" 10 #include "net/http/http_response_headers.h"
9 #include "net/http/http_status_code.h" 11 #include "net/http/http_status_code.h"
10 #include "net/url_request/url_fetcher.h" 12 #include "net/url_request/url_fetcher.h"
11 #include "net/url_request/url_request_context_getter.h" 13 #include "net/url_request/url_request_context_getter.h"
12 #include "net/url_request/url_request_status.h" 14 #include "net/url_request/url_request_status.h"
13 #include "url/gurl.h" 15 #include "url/gurl.h"
14 16
15 using data_use_measurement::DataUseUserData; 17 using data_use_measurement::DataUseUserData;
16 18
(...skipping 21 matching lines...) Expand all
38 data_use_service_name_(DataUseUserData::IMAGE_FETCHER_UNTAGGED), 40 data_use_service_name_(DataUseUserData::IMAGE_FETCHER_UNTAGGED),
39 next_url_fetcher_id_(0) {} 41 next_url_fetcher_id_(0) {}
40 42
41 ImageDataFetcher::~ImageDataFetcher() {} 43 ImageDataFetcher::~ImageDataFetcher() {}
42 44
43 void ImageDataFetcher::SetDataUseServiceName( 45 void ImageDataFetcher::SetDataUseServiceName(
44 DataUseServiceName data_use_service_name) { 46 DataUseServiceName data_use_service_name) {
45 data_use_service_name_ = data_use_service_name; 47 data_use_service_name_ = data_use_service_name;
46 } 48 }
47 49
50 void ImageDataFetcher::SetImageDownloadLimit(
51 const base::Optional<int64_t>& max_download_bytes) {
52 max_download_bytes_ = max_download_bytes;
53 }
54
48 void ImageDataFetcher::FetchImageData( 55 void ImageDataFetcher::FetchImageData(
49 const GURL& image_url, 56 const GURL& image_url,
50 const ImageDataFetcherCallback& callback) { 57 const ImageDataFetcherCallback& callback) {
51 FetchImageData( 58 FetchImageData(
52 image_url, callback, /*referrer=*/std::string(), 59 image_url, callback, /*referrer=*/std::string(),
53 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE); 60 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE);
54 } 61 }
55 62
56 void ImageDataFetcher::FetchImageData( 63 void ImageDataFetcher::FetchImageData(
57 const GURL& image_url, 64 const GURL& image_url,
(...skipping 12 matching lines...) Expand all
70 request->url_fetcher->SetReferrerPolicy(referrer_policy); 77 request->url_fetcher->SetReferrerPolicy(referrer_policy);
71 request->url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 78 request->url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
72 net::LOAD_DO_NOT_SAVE_COOKIES | 79 net::LOAD_DO_NOT_SAVE_COOKIES |
73 net::LOAD_DO_NOT_SEND_AUTH_DATA); 80 net::LOAD_DO_NOT_SEND_AUTH_DATA);
74 request->url_fetcher->Start(); 81 request->url_fetcher->Start();
75 82
76 pending_requests_[request->url_fetcher.get()] = std::move(request); 83 pending_requests_[request->url_fetcher.get()] = std::move(request);
77 } 84 }
78 85
79 void ImageDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) { 86 void ImageDataFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
80 auto request_iter = pending_requests_.find(source); 87 DCHECK(pending_requests_.find(source) != pending_requests_.end());
81 DCHECK(request_iter != pending_requests_.end());
82
83 bool success = source->GetStatus().status() == net::URLRequestStatus::SUCCESS; 88 bool success = source->GetStatus().status() == net::URLRequestStatus::SUCCESS;
84 89
85 RequestMetadata metadata; 90 RequestMetadata metadata;
86 if (success && source->GetResponseHeaders()) { 91 if (success && source->GetResponseHeaders()) {
87 source->GetResponseHeaders()->GetMimeType(&metadata.mime_type); 92 source->GetResponseHeaders()->GetMimeType(&metadata.mime_type);
88 metadata.http_response_code = source->GetResponseHeaders()->response_code(); 93 metadata.http_response_code = source->GetResponseHeaders()->response_code();
89 success &= (metadata.http_response_code == net::HTTP_OK); 94 success &= (metadata.http_response_code == net::HTTP_OK);
90 } 95 }
91 metadata.from_http_cache = source->WasCached(); 96 metadata.from_http_cache = source->WasCached();
92 97
93 std::string image_data; 98 std::string image_data;
94 if (success) { 99 if (success) {
95 source->GetResponseAsString(&image_data); 100 source->GetResponseAsString(&image_data);
96 } 101 }
102 FinishRequest(source, metadata, image_data);
103 }
104
105 void ImageDataFetcher::OnURLFetchDownloadProgress(
106 const net::URLFetcher* source,
107 int64_t current,
108 int64_t total,
109 int64_t current_network_bytes) {
110 if (!max_download_bytes_.has_value()) {
111 return;
112 }
113 if (total <= max_download_bytes_.value() &&
114 current <= max_download_bytes_.value()) {
115 return;
116 }
117 DCHECK(pending_requests_.find(source) != pending_requests_.end());
118 LOG(WARNING) << "Image data exceeded download size limit.";
119 RequestMetadata metadata;
120 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)
121 metadata.http_response_code = net::URLFetcher::RESPONSE_CODE_INVALID;
122
123 FinishRequest(source, metadata, /*image_data=*/std::string());
124 }
125
126 void ImageDataFetcher::FinishRequest(const net::URLFetcher* source,
127 const RequestMetadata& metadata,
128 const std::string& image_data) {
129 auto request_iter = pending_requests_.find(source);
130 DCHECK(request_iter != pending_requests_.end());
97 request_iter->second->callback.Run(image_data, metadata); 131 request_iter->second->callback.Run(image_data, metadata);
98
99 // Remove the finished request.
100 pending_requests_.erase(request_iter); 132 pending_requests_.erase(request_iter);
101 } 133 }
102 134
103 } // namespace image_fetcher 135 } // namespace image_fetcher
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698