| OLD | NEW |
| 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 Loading... |
| 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 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 Loading... |
| 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 DLOG(WARNING) << "Image data exceeded download size limit."; |
| 119 RequestMetadata metadata; |
| 120 metadata.http_response_code = net::URLFetcher::RESPONSE_CODE_INVALID; |
| 121 |
| 122 FinishRequest(source, metadata, /*image_data=*/std::string()); |
| 123 } |
| 124 |
| 125 void ImageDataFetcher::FinishRequest(const net::URLFetcher* source, |
| 126 const RequestMetadata& metadata, |
| 127 const std::string& image_data) { |
| 128 auto request_iter = pending_requests_.find(source); |
| 129 DCHECK(request_iter != pending_requests_.end()); |
| 97 request_iter->second->callback.Run(image_data, metadata); | 130 request_iter->second->callback.Run(image_data, metadata); |
| 98 | |
| 99 // Remove the finished request. | |
| 100 pending_requests_.erase(request_iter); | 131 pending_requests_.erase(request_iter); |
| 101 } | 132 } |
| 102 | 133 |
| 103 } // namespace image_fetcher | 134 } // namespace image_fetcher |
| OLD | NEW |