| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/renderer_host/async_resource_handler.h" | 5 #include "chrome/browser/renderer_host/async_resource_handler.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/process.h" | 8 #include "base/process.h" |
| 9 #include "base/shared_memory.h" | 9 #include "base/shared_memory.h" |
| 10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // When reading, we don't know if we are going to get EOF (0 bytes read), so | 15 // When reading, we don't know if we are going to get EOF (0 bytes read), so |
| 16 // we typically have a buffer that we allocated but did not use. We keep | 16 // we typically have a buffer that we allocated but did not use. We keep |
| 17 // this buffer around for the next read as a small optimization. | 17 // this buffer around for the next read as a small optimization. |
| 18 SharedIOBuffer* g_spare_read_buffer = NULL; | 18 SharedIOBuffer* g_spare_read_buffer = NULL; |
| 19 | 19 |
| 20 // The initial size of the shared memory buffer. (32 kilobytes). | 20 // The initial size of the shared memory buffer. (32 kilobytes). |
| 21 const int kReadBufSize = 32768; | 21 const int kInitialReadBufSize = 32768; |
| 22 | 22 |
| 23 // The maximum size of the shared memory buffer. (512 kilobytes). | 23 // The maximum size of the shared memory buffer. (512 kilobytes). |
| 24 const int kMaxBufSize = 524288; | 24 const int kMaxReadBufSize = 524288; |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 // Our version of IOBuffer that uses shared memory. | 28 // Our version of IOBuffer that uses shared memory. |
| 29 class SharedIOBuffer : public net::IOBuffer { | 29 class SharedIOBuffer : public net::IOBuffer { |
| 30 public: | 30 public: |
| 31 SharedIOBuffer(int buffer_size) | 31 SharedIOBuffer(int buffer_size) |
| 32 : net::IOBuffer(), | 32 : net::IOBuffer(), |
| 33 ok_(false), | 33 ok_(false), |
| 34 buffer_size_(buffer_size) { | 34 buffer_size_(buffer_size) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 59 int process_id, | 59 int process_id, |
| 60 int routing_id, | 60 int routing_id, |
| 61 base::ProcessHandle process_handle, | 61 base::ProcessHandle process_handle, |
| 62 const GURL& url, | 62 const GURL& url, |
| 63 ResourceDispatcherHost* resource_dispatcher_host) | 63 ResourceDispatcherHost* resource_dispatcher_host) |
| 64 : receiver_(receiver), | 64 : receiver_(receiver), |
| 65 process_id_(process_id), | 65 process_id_(process_id), |
| 66 routing_id_(routing_id), | 66 routing_id_(routing_id), |
| 67 process_handle_(process_handle), | 67 process_handle_(process_handle), |
| 68 rdh_(resource_dispatcher_host), | 68 rdh_(resource_dispatcher_host), |
| 69 next_buffer_size_(kReadBufSize) { | 69 next_buffer_size_(kInitialReadBufSize) { |
| 70 } | 70 } |
| 71 | 71 |
| 72 bool AsyncResourceHandler::OnUploadProgress(int request_id, | 72 bool AsyncResourceHandler::OnUploadProgress(int request_id, |
| 73 uint64 position, | 73 uint64 position, |
| 74 uint64 size) { | 74 uint64 size) { |
| 75 return receiver_->Send(new ViewMsg_Resource_UploadProgress(routing_id_, | 75 return receiver_->Send(new ViewMsg_Resource_UploadProgress(routing_id_, |
| 76 request_id, | 76 request_id, |
| 77 position, size)); | 77 position, size)); |
| 78 } | 78 } |
| 79 | 79 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 97 int* buf_size, int min_size) { | 97 int* buf_size, int min_size) { |
| 98 DCHECK(min_size == -1); | 98 DCHECK(min_size == -1); |
| 99 | 99 |
| 100 if (g_spare_read_buffer) { | 100 if (g_spare_read_buffer) { |
| 101 DCHECK(!read_buffer_); | 101 DCHECK(!read_buffer_); |
| 102 read_buffer_.swap(&g_spare_read_buffer); | 102 read_buffer_.swap(&g_spare_read_buffer); |
| 103 // TODO(willchan): Remove after debugging bug 16371. | 103 // TODO(willchan): Remove after debugging bug 16371. |
| 104 CHECK(read_buffer_->data()); | 104 CHECK(read_buffer_->data()); |
| 105 | 105 |
| 106 *buf = read_buffer_.get(); | 106 *buf = read_buffer_.get(); |
| 107 *buf_size = read_buffer_.get()->buffer_size(); | 107 *buf_size = read_buffer_->buffer_size(); |
| 108 } else { | 108 } else { |
| 109 read_buffer_ = new SharedIOBuffer(next_buffer_size_); | 109 read_buffer_ = new SharedIOBuffer(next_buffer_size_); |
| 110 if (!read_buffer_->ok()) { | 110 if (!read_buffer_->ok()) { |
| 111 DLOG(ERROR) << "Couldn't allocate shared io buffer"; | 111 DLOG(ERROR) << "Couldn't allocate shared io buffer"; |
| 112 return false; | 112 return false; |
| 113 } | 113 } |
| 114 // TODO(willchan): Remove after debugging bug 16371. | 114 // TODO(willchan): Remove after debugging bug 16371. |
| 115 CHECK(read_buffer_->data()); | 115 CHECK(read_buffer_->data()); |
| 116 *buf = read_buffer_.get(); | 116 *buf = read_buffer_.get(); |
| 117 *buf_size = next_buffer_size_; | 117 *buf_size = next_buffer_size_; |
| 118 } | 118 } |
| 119 | 119 |
| 120 next_buffer_size_ = std::min(next_buffer_size_ * 2, kMaxBufSize); | |
| 121 | |
| 122 return true; | 120 return true; |
| 123 } | 121 } |
| 124 | 122 |
| 125 bool AsyncResourceHandler::OnReadCompleted(int request_id, int* bytes_read) { | 123 bool AsyncResourceHandler::OnReadCompleted(int request_id, int* bytes_read) { |
| 126 if (!*bytes_read) | 124 if (!*bytes_read) |
| 127 return true; | 125 return true; |
| 128 DCHECK(read_buffer_.get()); | 126 DCHECK(read_buffer_.get()); |
| 129 | 127 |
| 128 if (read_buffer_->buffer_size() == *bytes_read) { |
| 129 // The network layer has saturated our buffer. Next time, we should give it |
| 130 // a bigger buffer for it to fill, to minimize the number of round trips we |
| 131 // do with the renderer process. |
| 132 next_buffer_size_ = std::min(next_buffer_size_ * 2, kMaxReadBufSize); |
| 133 } |
| 134 |
| 130 if (!rdh_->WillSendData(process_id_, request_id)) { | 135 if (!rdh_->WillSendData(process_id_, request_id)) { |
| 131 // We should not send this data now, we have too many pending requests. | 136 // We should not send this data now, we have too many pending requests. |
| 132 return true; | 137 return true; |
| 133 } | 138 } |
| 134 | 139 |
| 135 base::SharedMemoryHandle handle; | 140 base::SharedMemoryHandle handle; |
| 136 if (!read_buffer_->shared_memory()->GiveToProcess(process_handle_, &handle)) { | 141 if (!read_buffer_->shared_memory()->GiveToProcess(process_handle_, &handle)) { |
| 137 // We wrongfully incremented the pending data count. Fake an ACK message | 142 // We wrongfully incremented the pending data count. Fake an ACK message |
| 138 // to fix this. We can't move this call above the WillSendData because | 143 // to fix this. We can't move this call above the WillSendData because |
| 139 // it's killing our read_buffer_, and we don't want that when we pause | 144 // it's killing our read_buffer_, and we don't want that when we pause |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 179 |
| 175 // static | 180 // static |
| 176 void AsyncResourceHandler::GlobalCleanup() { | 181 void AsyncResourceHandler::GlobalCleanup() { |
| 177 if (g_spare_read_buffer) { | 182 if (g_spare_read_buffer) { |
| 178 // Avoid the CHECK in SharedIOBuffer::~SharedIOBuffer(). | 183 // Avoid the CHECK in SharedIOBuffer::~SharedIOBuffer(). |
| 179 SharedIOBuffer* tmp = g_spare_read_buffer; | 184 SharedIOBuffer* tmp = g_spare_read_buffer; |
| 180 g_spare_read_buffer = NULL; | 185 g_spare_read_buffer = NULL; |
| 181 tmp->Release(); | 186 tmp->Release(); |
| 182 } | 187 } |
| 183 } | 188 } |
| OLD | NEW |