| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/common/throttling_url_loader.h" | 5 #include "content/common/throttling_url_loader.h" |
| 6 | 6 |
| 7 #include "base/single_thread_task_runner.h" | 7 #include "base/single_thread_task_runner.h" |
| 8 | 8 |
| 9 namespace content { | 9 namespace content { |
| 10 | 10 |
| 11 ThrottlingURLLoader::StartInfo::StartInfo( | 11 ThrottlingURLLoader::StartInfo::StartInfo( |
| 12 mojom::URLLoaderFactory* in_url_loader_factory, | 12 mojom::URLLoaderFactory* in_url_loader_factory, |
| 13 int32_t in_routing_id, | 13 int32_t in_routing_id, |
| 14 int32_t in_request_id, | 14 int32_t in_request_id, |
| 15 uint32_t in_options, | 15 uint32_t in_options, |
| 16 std::unique_ptr<ResourceRequest> in_url_request, | 16 StartLoaderCallback in_start_loader_callback, |
| 17 const ResourceRequest& in_url_request, |
| 17 scoped_refptr<base::SingleThreadTaskRunner> in_task_runner) | 18 scoped_refptr<base::SingleThreadTaskRunner> in_task_runner) |
| 18 : url_loader_factory(in_url_loader_factory), | 19 : url_loader_factory(in_url_loader_factory), |
| 19 routing_id(in_routing_id), | 20 routing_id(in_routing_id), |
| 20 request_id(in_request_id), | 21 request_id(in_request_id), |
| 21 options(in_options), | 22 options(in_options), |
| 22 url_request(std::move(in_url_request)), | 23 start_loader_callback(std::move(in_start_loader_callback)), |
| 24 url_request(in_url_request), |
| 23 task_runner(std::move(in_task_runner)) {} | 25 task_runner(std::move(in_task_runner)) {} |
| 24 | 26 |
| 25 ThrottlingURLLoader::StartInfo::~StartInfo() = default; | 27 ThrottlingURLLoader::StartInfo::~StartInfo() = default; |
| 26 | 28 |
| 27 ThrottlingURLLoader::ResponseInfo::ResponseInfo( | 29 ThrottlingURLLoader::ResponseInfo::ResponseInfo( |
| 28 const ResourceResponseHead& in_response_head, | 30 const ResourceResponseHead& in_response_head, |
| 29 const base::Optional<net::SSLInfo>& in_ssl_info, | 31 const base::Optional<net::SSLInfo>& in_ssl_info, |
| 30 mojom::DownloadedTempFilePtr in_downloaded_file) | 32 mojom::DownloadedTempFilePtr in_downloaded_file) |
| 31 : response_head(in_response_head), | 33 : response_head(in_response_head), |
| 32 ssl_info(in_ssl_info), | 34 ssl_info(in_ssl_info), |
| (...skipping 15 matching lines...) Expand all Loading... |
| 48 | 50 |
| 49 ThrottlingURLLoader::PriorityInfo::~PriorityInfo() = default; | 51 ThrottlingURLLoader::PriorityInfo::~PriorityInfo() = default; |
| 50 | 52 |
| 51 // static | 53 // static |
| 52 std::unique_ptr<ThrottlingURLLoader> ThrottlingURLLoader::CreateLoaderAndStart( | 54 std::unique_ptr<ThrottlingURLLoader> ThrottlingURLLoader::CreateLoaderAndStart( |
| 53 mojom::URLLoaderFactory* factory, | 55 mojom::URLLoaderFactory* factory, |
| 54 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, | 56 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, |
| 55 int32_t routing_id, | 57 int32_t routing_id, |
| 56 int32_t request_id, | 58 int32_t request_id, |
| 57 uint32_t options, | 59 uint32_t options, |
| 58 std::unique_ptr<ResourceRequest> url_request, | 60 const ResourceRequest& url_request, |
| 59 mojom::URLLoaderClient* client, | 61 mojom::URLLoaderClient* client, |
| 60 const net::MutableNetworkTrafficAnnotationTag& traffic_annotation, | 62 const net::MutableNetworkTrafficAnnotationTag& traffic_annotation, |
| 61 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 63 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 62 std::unique_ptr<ThrottlingURLLoader> loader(new ThrottlingURLLoader( | 64 std::unique_ptr<ThrottlingURLLoader> loader(new ThrottlingURLLoader( |
| 63 std::move(throttles), client, traffic_annotation)); | 65 std::move(throttles), client, traffic_annotation)); |
| 64 loader->Start(factory, routing_id, request_id, options, | 66 loader->Start(factory, routing_id, request_id, options, StartLoaderCallback(), |
| 65 std::move(url_request), std::move(task_runner)); | 67 url_request, std::move(task_runner)); |
| 66 return loader; | 68 return loader; |
| 67 } | 69 } |
| 68 | 70 |
| 71 // static |
| 72 std::unique_ptr<ThrottlingURLLoader> ThrottlingURLLoader::CreateLoaderAndStart( |
| 73 StartLoaderCallback start_loader_callback, |
| 74 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, |
| 75 const ResourceRequest& url_request, |
| 76 mojom::URLLoaderClient* client, |
| 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 78 std::unique_ptr<ThrottlingURLLoader> loader(new ThrottlingURLLoader( |
| 79 std::move(throttles), client, net::MutableNetworkTrafficAnnotationTag())); |
| 80 loader->Start(nullptr, 0, 0, mojom::kURLLoadOptionNone, |
| 81 std::move(start_loader_callback), url_request, |
| 82 std::move(task_runner)); |
| 83 return loader; |
| 84 } |
| 85 |
| 69 ThrottlingURLLoader::~ThrottlingURLLoader() {} | 86 ThrottlingURLLoader::~ThrottlingURLLoader() {} |
| 70 | 87 |
| 71 void ThrottlingURLLoader::FollowRedirect() { | 88 void ThrottlingURLLoader::FollowRedirect() { |
| 72 if (url_loader_) | 89 if (url_loader_) |
| 73 url_loader_->FollowRedirect(); | 90 url_loader_->FollowRedirect(); |
| 74 } | 91 } |
| 75 | 92 |
| 76 void ThrottlingURLLoader::SetPriority(net::RequestPriority priority, | 93 void ThrottlingURLLoader::SetPriority(net::RequestPriority priority, |
| 77 int32_t intra_priority_value) { | 94 int32_t intra_priority_value) { |
| 78 if (!url_loader_ && !cancelled_by_throttle_) { | 95 if (!url_loader_ && !cancelled_by_throttle_) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 99 throttle_ = std::move(throttles[0]); | 116 throttle_ = std::move(throttles[0]); |
| 100 throttle_->set_delegate(this); | 117 throttle_->set_delegate(this); |
| 101 } | 118 } |
| 102 } | 119 } |
| 103 | 120 |
| 104 void ThrottlingURLLoader::Start( | 121 void ThrottlingURLLoader::Start( |
| 105 mojom::URLLoaderFactory* factory, | 122 mojom::URLLoaderFactory* factory, |
| 106 int32_t routing_id, | 123 int32_t routing_id, |
| 107 int32_t request_id, | 124 int32_t request_id, |
| 108 uint32_t options, | 125 uint32_t options, |
| 109 std::unique_ptr<ResourceRequest> url_request, | 126 StartLoaderCallback start_loader_callback, |
| 127 const ResourceRequest& url_request, |
| 110 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 128 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 111 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); | 129 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); |
| 112 DCHECK(!cancelled_by_throttle_); | 130 DCHECK(!cancelled_by_throttle_); |
| 113 | 131 |
| 114 if (throttle_) { | 132 if (throttle_) { |
| 115 bool deferred = false; | 133 bool deferred = false; |
| 116 throttle_->WillStartRequest(url_request->url, url_request->load_flags, | 134 throttle_->WillStartRequest(url_request.url, url_request.load_flags, |
| 117 url_request->resource_type, &deferred); | 135 url_request.resource_type, &deferred); |
| 118 if (cancelled_by_throttle_) | 136 if (cancelled_by_throttle_) |
| 119 return; | 137 return; |
| 120 | 138 |
| 121 if (deferred) { | 139 if (deferred) { |
| 122 deferred_stage_ = DEFERRED_START; | 140 deferred_stage_ = DEFERRED_START; |
| 123 start_info_ = base::MakeUnique<StartInfo>(factory, routing_id, request_id, | 141 start_info_ = |
| 124 options, std::move(url_request), | 142 base::MakeUnique<StartInfo>(factory, routing_id, request_id, options, |
| 125 std::move(task_runner)); | 143 std::move(start_loader_callback), |
| 144 url_request, std::move(task_runner)); |
| 126 return; | 145 return; |
| 127 } | 146 } |
| 128 } | 147 } |
| 129 | 148 |
| 149 StartNow(factory, routing_id, request_id, options, |
| 150 std::move(start_loader_callback), url_request, |
| 151 std::move(task_runner)); |
| 152 } |
| 153 |
| 154 void ThrottlingURLLoader::StartNow( |
| 155 mojom::URLLoaderFactory* factory, |
| 156 int32_t routing_id, |
| 157 int32_t request_id, |
| 158 uint32_t options, |
| 159 StartLoaderCallback start_loader_callback, |
| 160 const ResourceRequest& url_request, |
| 161 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 130 mojom::URLLoaderClientPtr client; | 162 mojom::URLLoaderClientPtr client; |
| 131 client_binding_.Bind(mojo::MakeRequest(&client), std::move(task_runner)); | 163 client_binding_.Bind(mojo::MakeRequest(&client), std::move(task_runner)); |
| 132 factory->CreateLoaderAndStart(mojo::MakeRequest(&url_loader_), routing_id, | 164 |
| 133 request_id, options, *url_request, | 165 if (factory) { |
| 134 std::move(client), traffic_annotation_); | 166 DCHECK(!start_loader_callback); |
| 167 |
| 168 mojom::URLLoaderAssociatedPtr url_loader; |
| 169 auto url_loader_request = mojo::MakeRequest(&url_loader); |
| 170 url_loader_ = std::move(url_loader); |
| 171 factory->CreateLoaderAndStart(std::move(url_loader_request), routing_id, |
| 172 request_id, options, url_request, |
| 173 std::move(client), traffic_annotation_); |
| 174 } else { |
| 175 mojom::URLLoaderPtr url_loader; |
| 176 auto url_loader_request = mojo::MakeRequest(&url_loader); |
| 177 url_loader_ = std::move(url_loader); |
| 178 std::move(start_loader_callback) |
| 179 .Run(std::move(url_loader_request), std::move(client)); |
| 180 } |
| 135 } | 181 } |
| 136 | 182 |
| 137 void ThrottlingURLLoader::OnReceiveResponse( | 183 void ThrottlingURLLoader::OnReceiveResponse( |
| 138 const ResourceResponseHead& response_head, | 184 const ResourceResponseHead& response_head, |
| 139 const base::Optional<net::SSLInfo>& ssl_info, | 185 const base::Optional<net::SSLInfo>& ssl_info, |
| 140 mojom::DownloadedTempFilePtr downloaded_file) { | 186 mojom::DownloadedTempFilePtr downloaded_file) { |
| 141 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); | 187 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); |
| 142 DCHECK(!cancelled_by_throttle_); | 188 DCHECK(!cancelled_by_throttle_); |
| 143 | 189 |
| 144 if (throttle_) { | 190 if (throttle_) { |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 return; | 287 return; |
| 242 | 288 |
| 243 cancelled_by_throttle_ = true; | 289 cancelled_by_throttle_ = true; |
| 244 | 290 |
| 245 ResourceRequestCompletionStatus request_complete_data; | 291 ResourceRequestCompletionStatus request_complete_data; |
| 246 request_complete_data.error_code = error_code; | 292 request_complete_data.error_code = error_code; |
| 247 request_complete_data.completion_time = base::TimeTicks::Now(); | 293 request_complete_data.completion_time = base::TimeTicks::Now(); |
| 248 | 294 |
| 249 deferred_stage_ = DEFERRED_NONE; | 295 deferred_stage_ = DEFERRED_NONE; |
| 250 client_binding_.Close(); | 296 client_binding_.Close(); |
| 251 url_loader_.reset(); | 297 url_loader_ = nullptr; |
| 252 | 298 |
| 253 forwarding_client_->OnComplete(request_complete_data); | 299 forwarding_client_->OnComplete(request_complete_data); |
| 254 } | 300 } |
| 255 | 301 |
| 256 void ThrottlingURLLoader::Resume() { | 302 void ThrottlingURLLoader::Resume() { |
| 257 if (cancelled_by_throttle_ || deferred_stage_ == DEFERRED_NONE) | 303 if (cancelled_by_throttle_ || deferred_stage_ == DEFERRED_NONE) |
| 258 return; | 304 return; |
| 259 | 305 |
| 260 switch (deferred_stage_) { | 306 switch (deferred_stage_) { |
| 261 case DEFERRED_START: { | 307 case DEFERRED_START: { |
| 262 mojom::URLLoaderClientPtr client; | 308 StartNow(start_info_->url_loader_factory, start_info_->routing_id, |
| 263 client_binding_.Bind( | 309 start_info_->request_id, start_info_->options, |
| 264 mojo::MakeRequest(&client), std::move(start_info_->task_runner)); | 310 std::move(start_info_->start_loader_callback), |
| 265 start_info_->url_loader_factory->CreateLoaderAndStart( | 311 start_info_->url_request, std::move(start_info_->task_runner)); |
| 266 mojo::MakeRequest(&url_loader_), start_info_->routing_id, | |
| 267 start_info_->request_id, start_info_->options, | |
| 268 *start_info_->url_request, std::move(client), traffic_annotation_); | |
| 269 | 312 |
| 270 if (priority_info_) { | 313 if (priority_info_) { |
| 271 auto priority_info = std::move(priority_info_); | 314 auto priority_info = std::move(priority_info_); |
| 272 url_loader_->SetPriority(priority_info->priority, | 315 url_loader_->SetPriority(priority_info->priority, |
| 273 priority_info->intra_priority_value); | 316 priority_info->intra_priority_value); |
| 274 } | 317 } |
| 275 break; | 318 break; |
| 276 } | 319 } |
| 277 case DEFERRED_REDIRECT: { | 320 case DEFERRED_REDIRECT: { |
| 278 client_binding_.ResumeIncomingMethodCallProcessing(); | 321 client_binding_.ResumeIncomingMethodCallProcessing(); |
| 279 forwarding_client_->OnReceiveRedirect(redirect_info_->redirect_info, | 322 forwarding_client_->OnReceiveRedirect(redirect_info_->redirect_info, |
| 280 redirect_info_->response_head); | 323 redirect_info_->response_head); |
| 281 break; | 324 break; |
| 282 } | 325 } |
| 283 case DEFERRED_RESPONSE: { | 326 case DEFERRED_RESPONSE: { |
| 284 client_binding_.ResumeIncomingMethodCallProcessing(); | 327 client_binding_.ResumeIncomingMethodCallProcessing(); |
| 285 forwarding_client_->OnReceiveResponse( | 328 forwarding_client_->OnReceiveResponse( |
| 286 response_info_->response_head, response_info_->ssl_info, | 329 response_info_->response_head, response_info_->ssl_info, |
| 287 std::move(response_info_->downloaded_file)); | 330 std::move(response_info_->downloaded_file)); |
| 288 break; | 331 break; |
| 289 } | 332 } |
| 290 default: | 333 default: |
| 291 NOTREACHED(); | 334 NOTREACHED(); |
| 292 break; | 335 break; |
| 293 } | 336 } |
| 294 deferred_stage_ = DEFERRED_NONE; | 337 deferred_stage_ = DEFERRED_NONE; |
| 295 } | 338 } |
| 296 | 339 |
| 297 } // namespace content | 340 } // namespace content |
| OLD | NEW |