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/download_throttling_resource_handler.h" | 5 #include "chrome/browser/renderer_host/download_throttling_resource_handler.h" |
6 | 6 |
7 #include "chrome/browser/renderer_host/download_resource_handler.h" | 7 #include "chrome/browser/renderer_host/download_resource_handler.h" |
| 8 #include "net/base/io_buffer.h" |
8 | 9 |
9 DownloadThrottlingResourceHandler::DownloadThrottlingResourceHandler( | 10 DownloadThrottlingResourceHandler::DownloadThrottlingResourceHandler( |
10 ResourceDispatcherHost* host, | 11 ResourceDispatcherHost* host, |
11 URLRequest* request, | 12 URLRequest* request, |
12 const std::string& url, | 13 const std::string& url, |
13 int render_process_host_id, | 14 int render_process_host_id, |
14 int render_view_id, | 15 int render_view_id, |
15 int request_id, | 16 int request_id, |
16 bool in_complete) | 17 bool in_complete) |
17 : host_(host), | 18 : host_(host), |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 bool DownloadThrottlingResourceHandler::OnResponseStarted( | 51 bool DownloadThrottlingResourceHandler::OnResponseStarted( |
51 int request_id, | 52 int request_id, |
52 ResourceResponse* response) { | 53 ResourceResponse* response) { |
53 if (download_handler_.get()) | 54 if (download_handler_.get()) |
54 return download_handler_->OnResponseStarted(request_id, response); | 55 return download_handler_->OnResponseStarted(request_id, response); |
55 response_ = response; | 56 response_ = response; |
56 return true; | 57 return true; |
57 } | 58 } |
58 | 59 |
59 bool DownloadThrottlingResourceHandler::OnWillRead(int request_id, | 60 bool DownloadThrottlingResourceHandler::OnWillRead(int request_id, |
60 char** buf, | 61 net::IOBuffer** buf, |
61 int* buf_size, | 62 int* buf_size, |
62 int min_size) { | 63 int min_size) { |
63 if (download_handler_.get()) | 64 if (download_handler_.get()) |
64 return download_handler_->OnWillRead(request_id, buf, buf_size, min_size); | 65 return download_handler_->OnWillRead(request_id, buf, buf_size, min_size); |
65 | 66 |
66 // We should only have this invoked once, as such we only deal with one | 67 // We should only have this invoked once, as such we only deal with one |
67 // tmp buffer. | 68 // tmp buffer. |
68 DCHECK(!tmp_buffer_.get()); | 69 DCHECK(!tmp_buffer_.get()); |
69 if (min_size < 0) | 70 if (min_size < 0) |
70 min_size = 1024; | 71 min_size = 1024; |
71 tmp_buffer_.reset(new char[min_size]); | 72 tmp_buffer_ = new net::IOBuffer(min_size); |
72 *buf = tmp_buffer_.get(); | 73 *buf = tmp_buffer_.get(); |
73 *buf_size = min_size; | 74 *buf_size = min_size; |
74 return true; | 75 return true; |
75 } | 76 } |
76 | 77 |
77 bool DownloadThrottlingResourceHandler::OnReadCompleted(int request_id, | 78 bool DownloadThrottlingResourceHandler::OnReadCompleted(int request_id, |
78 int* bytes_read) { | 79 int* bytes_read) { |
79 if (ignore_on_read_complete_) { | 80 if (ignore_on_read_complete_) { |
80 // See comments above definition for details on this. | 81 // See comments above definition for details on this. |
81 ignore_on_read_complete_ = false; | 82 ignore_on_read_complete_ = false; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 | 126 |
126 if (tmp_buffer_length_) | 127 if (tmp_buffer_length_) |
127 CopyTmpBufferToDownloadHandler(); | 128 CopyTmpBufferToDownloadHandler(); |
128 | 129 |
129 // And let the request continue. | 130 // And let the request continue. |
130 host_->PauseRequest(render_process_host_id_, request_id_, false); | 131 host_->PauseRequest(render_process_host_id_, request_id_, false); |
131 } | 132 } |
132 | 133 |
133 void DownloadThrottlingResourceHandler::CopyTmpBufferToDownloadHandler() { | 134 void DownloadThrottlingResourceHandler::CopyTmpBufferToDownloadHandler() { |
134 // Copy over the tmp buffer. | 135 // Copy over the tmp buffer. |
135 char* buffer; | 136 net::IOBuffer* buffer; |
136 int buf_size; | 137 int buf_size; |
137 if (download_handler_->OnWillRead(request_id_, &buffer, &buf_size, | 138 if (download_handler_->OnWillRead(request_id_, &buffer, &buf_size, |
138 tmp_buffer_length_)) { | 139 tmp_buffer_length_)) { |
139 CHECK(buf_size >= tmp_buffer_length_); | 140 CHECK(buf_size >= tmp_buffer_length_); |
140 memcpy(buffer, tmp_buffer_.get(), tmp_buffer_length_); | 141 memcpy(buffer->data(), tmp_buffer_->data(), tmp_buffer_length_); |
141 download_handler_->OnReadCompleted(request_id_, &tmp_buffer_length_); | 142 download_handler_->OnReadCompleted(request_id_, &tmp_buffer_length_); |
142 } | 143 } |
143 tmp_buffer_length_ = 0; | 144 tmp_buffer_length_ = 0; |
144 tmp_buffer_.reset(); | 145 tmp_buffer_ = NULL; |
145 } | 146 } |
OLD | NEW |