| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/renderer_host/async_resource_handler.h" | 5 #include "content/browser/renderer_host/async_resource_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 return; | 113 return; |
| 114 } | 114 } |
| 115 | 115 |
| 116 if (has_new_first_party_for_cookies) | 116 if (has_new_first_party_for_cookies) |
| 117 request_->set_first_party_for_cookies(new_first_party_for_cookies); | 117 request_->set_first_party_for_cookies(new_first_party_for_cookies); |
| 118 | 118 |
| 119 ResumeIfDeferred(); | 119 ResumeIfDeferred(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 void AsyncResourceHandler::OnDataReceivedACK() { | 122 void AsyncResourceHandler::OnDataReceivedACK() { |
| 123 // If the pending data count was higher than the max, resume the request. | 123 if (pending_data_count_-- == kMaxPendingDataMessages) |
| 124 if (--pending_data_count_ == kMaxPendingDataMessages) { | |
| 125 // Decrement the pending data count one more time because we also | |
| 126 // incremented it before deferring the request. | |
| 127 --pending_data_count_; | |
| 128 | |
| 129 // Resume the request. | |
| 130 ResumeIfDeferred(); | 124 ResumeIfDeferred(); |
| 131 } | |
| 132 } | 125 } |
| 133 | 126 |
| 134 bool AsyncResourceHandler::OnUploadProgress(int request_id, | 127 bool AsyncResourceHandler::OnUploadProgress(int request_id, |
| 135 uint64 position, | 128 uint64 position, |
| 136 uint64 size) { | 129 uint64 size) { |
| 137 return filter_->Send(new ResourceMsg_UploadProgress(routing_id_, request_id, | 130 return filter_->Send(new ResourceMsg_UploadProgress(routing_id_, request_id, |
| 138 position, size)); | 131 position, size)); |
| 139 } | 132 } |
| 140 | 133 |
| 141 bool AsyncResourceHandler::OnRequestRedirected(int request_id, | 134 bool AsyncResourceHandler::OnRequestRedirected(int request_id, |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 return false; | 220 return false; |
| 228 } | 221 } |
| 229 DCHECK(read_buffer_->data()); | 222 DCHECK(read_buffer_->data()); |
| 230 *buf = read_buffer_.get(); | 223 *buf = read_buffer_.get(); |
| 231 *buf_size = next_buffer_size_; | 224 *buf_size = next_buffer_size_; |
| 232 } | 225 } |
| 233 | 226 |
| 234 return true; | 227 return true; |
| 235 } | 228 } |
| 236 | 229 |
| 237 bool AsyncResourceHandler::OnReadCompleted(int request_id, int* bytes_read, | 230 bool AsyncResourceHandler::OnReadCompleted(int request_id, int bytes_read, |
| 238 bool* defer) { | 231 bool* defer) { |
| 239 if (!*bytes_read) | 232 if (!bytes_read) |
| 240 return true; | 233 return true; |
| 241 DCHECK(read_buffer_.get()); | 234 DCHECK(read_buffer_.get()); |
| 242 | 235 |
| 243 if (read_buffer_->buffer_size() == *bytes_read) { | 236 if (read_buffer_->buffer_size() == bytes_read) { |
| 244 // The network layer has saturated our buffer. Next time, we should give it | 237 // The network layer has saturated our buffer. Next time, we should give it |
| 245 // a bigger buffer for it to fill, to minimize the number of round trips we | 238 // a bigger buffer for it to fill, to minimize the number of round trips we |
| 246 // do with the renderer process. | 239 // do with the renderer process. |
| 247 next_buffer_size_ = std::min(next_buffer_size_ * 2, kMaxReadBufSize); | 240 next_buffer_size_ = std::min(next_buffer_size_ * 2, kMaxReadBufSize); |
| 248 } | 241 } |
| 249 | 242 |
| 250 if (!WillSendData(defer)) { | 243 WillSendData(defer); |
| 251 // We should not send this data now, we have too many pending requests. | |
| 252 return true; | |
| 253 } | |
| 254 | 244 |
| 255 base::SharedMemoryHandle handle; | 245 base::SharedMemoryHandle handle; |
| 256 if (!read_buffer_->shared_memory()->GiveToProcess( | 246 if (!read_buffer_->shared_memory()->GiveToProcess( |
| 257 filter_->peer_handle(), &handle)) { | 247 filter_->peer_handle(), &handle)) { |
| 258 // We wrongfully incremented the pending data count. Fake an ACK message | 248 // We wrongfully incremented the pending data count. Fake an ACK message |
| 259 // to fix this. We can't move this call above the WillSendData because | 249 // to fix this. We can't move this call above the WillSendData because |
| 260 // it's killing our read_buffer_, and we don't want that when we pause | 250 // it's killing our read_buffer_, and we don't want that when we pause |
| 261 // the request. | 251 // the request. |
| 262 OnDataReceivedACK(); | 252 OnDataReceivedACK(); |
| 263 | 253 |
| 264 // We just unmapped the memory. | 254 // We just unmapped the memory. |
| 265 read_buffer_ = NULL; | 255 read_buffer_ = NULL; |
| 266 return false; | 256 return false; |
| 267 } | 257 } |
| 268 // We just unmapped the memory. | 258 // We just unmapped the memory. |
| 269 read_buffer_ = NULL; | 259 read_buffer_ = NULL; |
| 270 | 260 |
| 271 int encoded_data_length = | 261 int encoded_data_length = |
| 272 DevToolsNetLogObserver::GetAndResetEncodedDataLength(request_); | 262 DevToolsNetLogObserver::GetAndResetEncodedDataLength(request_); |
| 273 filter_->Send(new ResourceMsg_DataReceived( | 263 filter_->Send(new ResourceMsg_DataReceived( |
| 274 routing_id_, request_id, handle, *bytes_read, encoded_data_length)); | 264 routing_id_, request_id, handle, bytes_read, encoded_data_length)); |
| 275 | 265 |
| 276 return true; | 266 return true; |
| 277 } | 267 } |
| 278 | 268 |
| 279 void AsyncResourceHandler::OnDataDownloaded( | 269 void AsyncResourceHandler::OnDataDownloaded( |
| 280 int request_id, int bytes_downloaded) { | 270 int request_id, int bytes_downloaded) { |
| 281 filter_->Send(new ResourceMsg_DataDownloaded( | 271 filter_->Send(new ResourceMsg_DataDownloaded( |
| 282 routing_id_, request_id, bytes_downloaded)); | 272 routing_id_, request_id, bytes_downloaded)); |
| 283 } | 273 } |
| 284 | 274 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 315 // static | 305 // static |
| 316 void AsyncResourceHandler::GlobalCleanup() { | 306 void AsyncResourceHandler::GlobalCleanup() { |
| 317 if (g_spare_read_buffer) { | 307 if (g_spare_read_buffer) { |
| 318 // Avoid the CHECK in SharedIOBuffer::~SharedIOBuffer(). | 308 // Avoid the CHECK in SharedIOBuffer::~SharedIOBuffer(). |
| 319 SharedIOBuffer* tmp = g_spare_read_buffer; | 309 SharedIOBuffer* tmp = g_spare_read_buffer; |
| 320 g_spare_read_buffer = NULL; | 310 g_spare_read_buffer = NULL; |
| 321 tmp->Release(); | 311 tmp->Release(); |
| 322 } | 312 } |
| 323 } | 313 } |
| 324 | 314 |
| 325 bool AsyncResourceHandler::WillSendData(bool* defer) { | 315 void AsyncResourceHandler::WillSendData(bool* defer) { |
| 326 if (++pending_data_count_ > kMaxPendingDataMessages) { | 316 if (++pending_data_count_ >= kMaxPendingDataMessages) { |
| 327 // We reached the max number of data messages that can be sent to | 317 // We reached the max number of data messages that can be sent to |
| 328 // the renderer for a given request. Pause the request and wait for | 318 // the renderer for a given request. Pause the request and wait for |
| 329 // the renderer to start processing them before resuming it. | 319 // the renderer to start processing them before resuming it. |
| 330 *defer = did_defer_ = true; | 320 *defer = did_defer_ = true; |
| 331 return false; | |
| 332 } | 321 } |
| 333 | |
| 334 return true; | |
| 335 } | 322 } |
| 336 | 323 |
| 337 void AsyncResourceHandler::ResumeIfDeferred() { | 324 void AsyncResourceHandler::ResumeIfDeferred() { |
| 338 if (did_defer_) { | 325 if (did_defer_) { |
| 339 did_defer_ = false; | 326 did_defer_ = false; |
| 340 controller()->Resume(); | 327 controller()->Resume(); |
| 341 } | 328 } |
| 342 } | 329 } |
| 343 | 330 |
| 344 } // namespace content | 331 } // namespace content |
| OLD | NEW |