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