| 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 |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 86 |
| 87 ThrottlingURLLoader::~ThrottlingURLLoader() {} | 87 ThrottlingURLLoader::~ThrottlingURLLoader() {} |
| 88 | 88 |
| 89 void ThrottlingURLLoader::FollowRedirect() { | 89 void ThrottlingURLLoader::FollowRedirect() { |
| 90 if (url_loader_) | 90 if (url_loader_) |
| 91 url_loader_->FollowRedirect(); | 91 url_loader_->FollowRedirect(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void ThrottlingURLLoader::SetPriority(net::RequestPriority priority, | 94 void ThrottlingURLLoader::SetPriority(net::RequestPriority priority, |
| 95 int32_t intra_priority_value) { | 95 int32_t intra_priority_value) { |
| 96 if (!url_loader_ && !cancelled_by_throttle_) { | 96 if (!url_loader_ && !loader_cancelled_) { |
| 97 DCHECK_EQ(DEFERRED_START, deferred_stage_); | 97 DCHECK_EQ(DEFERRED_START, deferred_stage_); |
| 98 priority_info_ = | 98 priority_info_ = |
| 99 base::MakeUnique<PriorityInfo>(priority, intra_priority_value); | 99 base::MakeUnique<PriorityInfo>(priority, intra_priority_value); |
| 100 return; | 100 return; |
| 101 } | 101 } |
| 102 | 102 |
| 103 url_loader_->SetPriority(priority, intra_priority_value); | 103 url_loader_->SetPriority(priority, intra_priority_value); |
| 104 } | 104 } |
| 105 | 105 |
| 106 void ThrottlingURLLoader::DisconnectClient() { |
| 107 client_binding_.Close(); |
| 108 url_loader_ = nullptr; |
| 109 loader_cancelled_ = true; |
| 110 } |
| 111 |
| 106 ThrottlingURLLoader::ThrottlingURLLoader( | 112 ThrottlingURLLoader::ThrottlingURLLoader( |
| 107 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, | 113 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, |
| 108 mojom::URLLoaderClient* client, | 114 mojom::URLLoaderClient* client, |
| 109 const net::NetworkTrafficAnnotationTag& traffic_annotation) | 115 const net::NetworkTrafficAnnotationTag& traffic_annotation) |
| 110 : forwarding_client_(client), | 116 : forwarding_client_(client), |
| 111 client_binding_(this), | 117 client_binding_(this), |
| 112 traffic_annotation_(traffic_annotation) { | 118 traffic_annotation_(traffic_annotation) { |
| 113 if (throttles.size() > 0) { | 119 if (throttles.size() > 0) { |
| 114 // TODO(yzshen): Implement a URLLoaderThrottle subclass which handles a list | 120 // TODO(yzshen): Implement a URLLoaderThrottle subclass which handles a list |
| 115 // of URLLoaderThrottles. | 121 // of URLLoaderThrottles. |
| 116 CHECK_EQ(1u, throttles.size()); | 122 CHECK_EQ(1u, throttles.size()); |
| 117 throttle_ = std::move(throttles[0]); | 123 throttle_ = std::move(throttles[0]); |
| 118 throttle_->set_delegate(this); | 124 throttle_->set_delegate(this); |
| 119 } | 125 } |
| 120 } | 126 } |
| 121 | 127 |
| 122 void ThrottlingURLLoader::Start( | 128 void ThrottlingURLLoader::Start( |
| 123 mojom::URLLoaderFactory* factory, | 129 mojom::URLLoaderFactory* factory, |
| 124 int32_t routing_id, | 130 int32_t routing_id, |
| 125 int32_t request_id, | 131 int32_t request_id, |
| 126 uint32_t options, | 132 uint32_t options, |
| 127 StartLoaderCallback start_loader_callback, | 133 StartLoaderCallback start_loader_callback, |
| 128 const ResourceRequest& url_request, | 134 const ResourceRequest& url_request, |
| 129 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | 135 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 130 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); | 136 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); |
| 131 DCHECK(!cancelled_by_throttle_); | 137 DCHECK(!loader_cancelled_); |
| 132 | 138 |
| 133 if (throttle_) { | 139 if (throttle_) { |
| 134 bool deferred = false; | 140 bool deferred = false; |
| 135 throttle_->WillStartRequest(url_request, &deferred); | 141 throttle_->WillStartRequest(url_request, &deferred); |
| 136 if (cancelled_by_throttle_) | 142 if (loader_cancelled_) |
| 137 return; | 143 return; |
| 138 | 144 |
| 139 if (deferred) { | 145 if (deferred) { |
| 140 deferred_stage_ = DEFERRED_START; | 146 deferred_stage_ = DEFERRED_START; |
| 141 start_info_ = | 147 start_info_ = |
| 142 base::MakeUnique<StartInfo>(factory, routing_id, request_id, options, | 148 base::MakeUnique<StartInfo>(factory, routing_id, request_id, options, |
| 143 std::move(start_loader_callback), | 149 std::move(start_loader_callback), |
| 144 url_request, std::move(task_runner)); | 150 url_request, std::move(task_runner)); |
| 145 return; | 151 return; |
| 146 } | 152 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 std::move(start_loader_callback) | 185 std::move(start_loader_callback) |
| 180 .Run(std::move(url_loader_request), std::move(client)); | 186 .Run(std::move(url_loader_request), std::move(client)); |
| 181 } | 187 } |
| 182 } | 188 } |
| 183 | 189 |
| 184 void ThrottlingURLLoader::OnReceiveResponse( | 190 void ThrottlingURLLoader::OnReceiveResponse( |
| 185 const ResourceResponseHead& response_head, | 191 const ResourceResponseHead& response_head, |
| 186 const base::Optional<net::SSLInfo>& ssl_info, | 192 const base::Optional<net::SSLInfo>& ssl_info, |
| 187 mojom::DownloadedTempFilePtr downloaded_file) { | 193 mojom::DownloadedTempFilePtr downloaded_file) { |
| 188 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); | 194 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); |
| 189 DCHECK(!cancelled_by_throttle_); | 195 DCHECK(!loader_cancelled_); |
| 190 | 196 |
| 191 if (throttle_) { | 197 if (throttle_) { |
| 192 bool deferred = false; | 198 bool deferred = false; |
| 193 throttle_->WillProcessResponse(&deferred); | 199 throttle_->WillProcessResponse(&deferred); |
| 194 if (cancelled_by_throttle_) | 200 if (loader_cancelled_) |
| 195 return; | 201 return; |
| 196 | 202 |
| 197 if (deferred) { | 203 if (deferred) { |
| 198 deferred_stage_ = DEFERRED_RESPONSE; | 204 deferred_stage_ = DEFERRED_RESPONSE; |
| 199 response_info_ = base::MakeUnique<ResponseInfo>( | 205 response_info_ = base::MakeUnique<ResponseInfo>( |
| 200 response_head, ssl_info, std::move(downloaded_file)); | 206 response_head, ssl_info, std::move(downloaded_file)); |
| 201 client_binding_.PauseIncomingMethodCallProcessing(); | 207 client_binding_.PauseIncomingMethodCallProcessing(); |
| 202 return; | 208 return; |
| 203 } | 209 } |
| 204 } | 210 } |
| 205 | 211 |
| 206 forwarding_client_->OnReceiveResponse(response_head, ssl_info, | 212 forwarding_client_->OnReceiveResponse(response_head, ssl_info, |
| 207 std::move(downloaded_file)); | 213 std::move(downloaded_file)); |
| 208 } | 214 } |
| 209 | 215 |
| 210 void ThrottlingURLLoader::OnReceiveRedirect( | 216 void ThrottlingURLLoader::OnReceiveRedirect( |
| 211 const net::RedirectInfo& redirect_info, | 217 const net::RedirectInfo& redirect_info, |
| 212 const ResourceResponseHead& response_head) { | 218 const ResourceResponseHead& response_head) { |
| 213 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); | 219 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); |
| 214 DCHECK(!cancelled_by_throttle_); | 220 DCHECK(!loader_cancelled_); |
| 215 | 221 |
| 216 if (throttle_) { | 222 if (throttle_) { |
| 217 bool deferred = false; | 223 bool deferred = false; |
| 218 throttle_->WillRedirectRequest(redirect_info, &deferred); | 224 throttle_->WillRedirectRequest(redirect_info, &deferred); |
| 219 if (cancelled_by_throttle_) | 225 if (loader_cancelled_) |
| 220 return; | 226 return; |
| 221 | 227 |
| 222 if (deferred) { | 228 if (deferred) { |
| 223 deferred_stage_ = DEFERRED_REDIRECT; | 229 deferred_stage_ = DEFERRED_REDIRECT; |
| 224 redirect_info_ = | 230 redirect_info_ = |
| 225 base::MakeUnique<RedirectInfo>(redirect_info, response_head); | 231 base::MakeUnique<RedirectInfo>(redirect_info, response_head); |
| 226 client_binding_.PauseIncomingMethodCallProcessing(); | 232 client_binding_.PauseIncomingMethodCallProcessing(); |
| 227 return; | 233 return; |
| 228 } | 234 } |
| 229 } | 235 } |
| 230 | 236 |
| 231 forwarding_client_->OnReceiveRedirect(redirect_info, response_head); | 237 forwarding_client_->OnReceiveRedirect(redirect_info, response_head); |
| 232 } | 238 } |
| 233 | 239 |
| 234 void ThrottlingURLLoader::OnDataDownloaded(int64_t data_len, | 240 void ThrottlingURLLoader::OnDataDownloaded(int64_t data_len, |
| 235 int64_t encoded_data_len) { | 241 int64_t encoded_data_len) { |
| 236 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); | 242 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); |
| 237 DCHECK(!cancelled_by_throttle_); | 243 DCHECK(!loader_cancelled_); |
| 238 | 244 |
| 239 forwarding_client_->OnDataDownloaded(data_len, encoded_data_len); | 245 forwarding_client_->OnDataDownloaded(data_len, encoded_data_len); |
| 240 } | 246 } |
| 241 | 247 |
| 242 void ThrottlingURLLoader::OnUploadProgress( | 248 void ThrottlingURLLoader::OnUploadProgress( |
| 243 int64_t current_position, | 249 int64_t current_position, |
| 244 int64_t total_size, | 250 int64_t total_size, |
| 245 OnUploadProgressCallback ack_callback) { | 251 OnUploadProgressCallback ack_callback) { |
| 246 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); | 252 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); |
| 247 DCHECK(!cancelled_by_throttle_); | 253 DCHECK(!loader_cancelled_); |
| 248 | 254 |
| 249 forwarding_client_->OnUploadProgress(current_position, total_size, | 255 forwarding_client_->OnUploadProgress(current_position, total_size, |
| 250 std::move(ack_callback)); | 256 std::move(ack_callback)); |
| 251 } | 257 } |
| 252 | 258 |
| 253 void ThrottlingURLLoader::OnReceiveCachedMetadata( | 259 void ThrottlingURLLoader::OnReceiveCachedMetadata( |
| 254 const std::vector<uint8_t>& data) { | 260 const std::vector<uint8_t>& data) { |
| 255 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); | 261 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); |
| 256 DCHECK(!cancelled_by_throttle_); | 262 DCHECK(!loader_cancelled_); |
| 257 | 263 |
| 258 forwarding_client_->OnReceiveCachedMetadata(data); | 264 forwarding_client_->OnReceiveCachedMetadata(data); |
| 259 } | 265 } |
| 260 | 266 |
| 261 void ThrottlingURLLoader::OnTransferSizeUpdated(int32_t transfer_size_diff) { | 267 void ThrottlingURLLoader::OnTransferSizeUpdated(int32_t transfer_size_diff) { |
| 262 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); | 268 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); |
| 263 DCHECK(!cancelled_by_throttle_); | 269 DCHECK(!loader_cancelled_); |
| 264 | 270 |
| 265 forwarding_client_->OnTransferSizeUpdated(transfer_size_diff); | 271 forwarding_client_->OnTransferSizeUpdated(transfer_size_diff); |
| 266 } | 272 } |
| 267 | 273 |
| 268 void ThrottlingURLLoader::OnStartLoadingResponseBody( | 274 void ThrottlingURLLoader::OnStartLoadingResponseBody( |
| 269 mojo::ScopedDataPipeConsumerHandle body) { | 275 mojo::ScopedDataPipeConsumerHandle body) { |
| 270 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); | 276 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); |
| 271 DCHECK(!cancelled_by_throttle_); | 277 DCHECK(!loader_cancelled_); |
| 272 | 278 |
| 273 forwarding_client_->OnStartLoadingResponseBody(std::move(body)); | 279 forwarding_client_->OnStartLoadingResponseBody(std::move(body)); |
| 274 } | 280 } |
| 275 | 281 |
| 276 void ThrottlingURLLoader::OnComplete( | 282 void ThrottlingURLLoader::OnComplete( |
| 277 const ResourceRequestCompletionStatus& status) { | 283 const ResourceRequestCompletionStatus& status) { |
| 278 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); | 284 DCHECK_EQ(DEFERRED_NONE, deferred_stage_); |
| 279 DCHECK(!cancelled_by_throttle_); | 285 DCHECK(!loader_cancelled_); |
| 280 | 286 |
| 281 forwarding_client_->OnComplete(status); | 287 forwarding_client_->OnComplete(status); |
| 282 } | 288 } |
| 283 | 289 |
| 284 void ThrottlingURLLoader::CancelWithError(int error_code) { | 290 void ThrottlingURLLoader::CancelWithError(int error_code) { |
| 285 // TODO(yzshen): Support a mode that cancellation also deletes the disk cache | 291 // TODO(yzshen): Support a mode that cancellation also deletes the disk cache |
| 286 // entry. | 292 // entry. |
| 287 if (cancelled_by_throttle_) | 293 if (loader_cancelled_) |
| 288 return; | 294 return; |
| 289 | 295 |
| 290 cancelled_by_throttle_ = true; | |
| 291 | |
| 292 ResourceRequestCompletionStatus request_complete_data; | 296 ResourceRequestCompletionStatus request_complete_data; |
| 293 request_complete_data.error_code = error_code; | 297 request_complete_data.error_code = error_code; |
| 294 request_complete_data.completion_time = base::TimeTicks::Now(); | 298 request_complete_data.completion_time = base::TimeTicks::Now(); |
| 295 | 299 |
| 296 deferred_stage_ = DEFERRED_NONE; | 300 deferred_stage_ = DEFERRED_NONE; |
| 297 client_binding_.Close(); | 301 DisconnectClient(); |
| 298 url_loader_ = nullptr; | |
| 299 | |
| 300 forwarding_client_->OnComplete(request_complete_data); | 302 forwarding_client_->OnComplete(request_complete_data); |
| 301 } | 303 } |
| 302 | 304 |
| 303 void ThrottlingURLLoader::Resume() { | 305 void ThrottlingURLLoader::Resume() { |
| 304 if (cancelled_by_throttle_ || deferred_stage_ == DEFERRED_NONE) | 306 if (loader_cancelled_ || deferred_stage_ == DEFERRED_NONE) |
| 305 return; | 307 return; |
| 306 | 308 |
| 307 switch (deferred_stage_) { | 309 switch (deferred_stage_) { |
| 308 case DEFERRED_START: { | 310 case DEFERRED_START: { |
| 309 StartNow(start_info_->url_loader_factory, start_info_->routing_id, | 311 StartNow(start_info_->url_loader_factory, start_info_->routing_id, |
| 310 start_info_->request_id, start_info_->options, | 312 start_info_->request_id, start_info_->options, |
| 311 std::move(start_info_->start_loader_callback), | 313 std::move(start_info_->start_loader_callback), |
| 312 start_info_->url_request, std::move(start_info_->task_runner)); | 314 start_info_->url_request, std::move(start_info_->task_runner)); |
| 313 | 315 |
| 314 if (priority_info_) { | 316 if (priority_info_) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 332 break; | 334 break; |
| 333 } | 335 } |
| 334 default: | 336 default: |
| 335 NOTREACHED(); | 337 NOTREACHED(); |
| 336 break; | 338 break; |
| 337 } | 339 } |
| 338 deferred_stage_ = DEFERRED_NONE; | 340 deferred_stage_ = DEFERRED_NONE; |
| 339 } | 341 } |
| 340 | 342 |
| 341 } // namespace content | 343 } // namespace content |
| OLD | NEW |