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/loader/redirect_to_file_resource_handler.h" | 5 #include "content/browser/loader/redirect_to_file_resource_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_util_proxy.h" | 8 #include "base/files/file_util_proxy.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 | 72 |
73 RedirectToFileResourceHandler::~RedirectToFileResourceHandler() { | 73 RedirectToFileResourceHandler::~RedirectToFileResourceHandler() { |
74 } | 74 } |
75 | 75 |
76 bool RedirectToFileResourceHandler::OnResponseStarted( | 76 bool RedirectToFileResourceHandler::OnResponseStarted( |
77 int request_id, | 77 int request_id, |
78 ResourceResponse* response, | 78 ResourceResponse* response, |
79 bool* defer) { | 79 bool* defer) { |
80 if (response->head.error_code == net::OK || | 80 if (response->head.error_code == net::OK || |
81 response->head.error_code == net::ERR_IO_PENDING) { | 81 response->head.error_code == net::ERR_IO_PENDING) { |
82 DCHECK(deletable_file_ && !deletable_file_->path().empty()); | 82 DCHECK(deletable_file_.get() && !deletable_file_->path().empty()); |
83 response->head.download_file_path = deletable_file_->path(); | 83 response->head.download_file_path = deletable_file_->path(); |
84 } | 84 } |
85 return next_handler_->OnResponseStarted(request_id, response, defer); | 85 return next_handler_->OnResponseStarted(request_id, response, defer); |
86 } | 86 } |
87 | 87 |
88 bool RedirectToFileResourceHandler::OnWillStart(int request_id, | 88 bool RedirectToFileResourceHandler::OnWillStart(int request_id, |
89 const GURL& url, | 89 const GURL& url, |
90 bool* defer) { | 90 bool* defer) { |
91 request_id_ = request_id; | 91 request_id_ = request_id; |
92 if (!deletable_file_) { | 92 if (!deletable_file_.get()) { |
93 // Defer starting the request until we have created the temporary file. | 93 // Defer starting the request until we have created the temporary file. |
94 // TODO(darin): This is sub-optimal. We should not delay starting the | 94 // TODO(darin): This is sub-optimal. We should not delay starting the |
95 // network request like this. | 95 // network request like this. |
96 did_defer_ = *defer = true; | 96 did_defer_ = *defer = true; |
97 base::FileUtilProxy::CreateTemporary( | 97 base::FileUtilProxy::CreateTemporary( |
98 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), | 98 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE), |
99 base::PLATFORM_FILE_ASYNC, | 99 base::PLATFORM_FILE_ASYNC, |
100 base::Bind(&RedirectToFileResourceHandler::DidCreateTemporaryFile, | 100 base::Bind(&RedirectToFileResourceHandler::DidCreateTemporaryFile, |
101 weak_factory_.GetWeakPtr())); | 101 weak_factory_.GetWeakPtr())); |
102 return true; | 102 return true; |
103 } | 103 } |
104 return next_handler_->OnWillStart(request_id, url, defer); | 104 return next_handler_->OnWillStart(request_id, url, defer); |
105 } | 105 } |
106 | 106 |
107 bool RedirectToFileResourceHandler::OnWillRead(int request_id, | 107 bool RedirectToFileResourceHandler::OnWillRead(int request_id, |
108 net::IOBuffer** buf, | 108 net::IOBuffer** buf, |
109 int* buf_size, | 109 int* buf_size, |
110 int min_size) { | 110 int min_size) { |
111 DCHECK_EQ(-1, min_size); | 111 DCHECK_EQ(-1, min_size); |
112 | 112 |
113 if (buf_->capacity() < next_buffer_size_) | 113 if (buf_->capacity() < next_buffer_size_) |
114 buf_->SetCapacity(next_buffer_size_); | 114 buf_->SetCapacity(next_buffer_size_); |
115 | 115 |
116 // We should have paused this network request already if the buffer is full. | 116 // We should have paused this network request already if the buffer is full. |
117 DCHECK(!BufIsFull()); | 117 DCHECK(!BufIsFull()); |
118 | 118 |
119 *buf = buf_; | 119 *buf = buf_.get(); |
120 *buf_size = buf_->RemainingCapacity(); | 120 *buf_size = buf_->RemainingCapacity(); |
121 | 121 |
122 buf_write_pending_ = true; | 122 buf_write_pending_ = true; |
123 return true; | 123 return true; |
124 } | 124 } |
125 | 125 |
126 bool RedirectToFileResourceHandler::OnReadCompleted(int request_id, | 126 bool RedirectToFileResourceHandler::OnReadCompleted(int request_id, |
127 int bytes_read, | 127 int bytes_read, |
128 bool* defer) { | 128 bool* defer) { |
129 DCHECK(buf_write_pending_); | 129 DCHECK(buf_write_pending_); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 // Note that buf_ is also shared with the code that writes data into the | 224 // Note that buf_ is also shared with the code that writes data into the |
225 // cache, so modifying it can cause some pretty subtle race conditions: | 225 // cache, so modifying it can cause some pretty subtle race conditions: |
226 // https://code.google.com/p/chromium/issues/detail?id=152076 | 226 // https://code.google.com/p/chromium/issues/detail?id=152076 |
227 // We're using DependentIOBuffer instead of DrainableIOBuffer to dodge some | 227 // We're using DependentIOBuffer instead of DrainableIOBuffer to dodge some |
228 // of these issues, for the moment. | 228 // of these issues, for the moment. |
229 // TODO(ncbray) make this code less crazy scary. | 229 // TODO(ncbray) make this code less crazy scary. |
230 // Also note that Write may increase the refcount of "wrapped" deep in the | 230 // Also note that Write may increase the refcount of "wrapped" deep in the |
231 // bowels of its implementation, the use of scoped_refptr here is not | 231 // bowels of its implementation, the use of scoped_refptr here is not |
232 // spurious. | 232 // spurious. |
233 scoped_refptr<DependentIOBuffer> wrapped = new DependentIOBuffer( | 233 scoped_refptr<DependentIOBuffer> wrapped = new DependentIOBuffer( |
234 buf_, buf_->StartOfBuffer() + write_cursor_); | 234 buf_.get(), buf_->StartOfBuffer() + write_cursor_); |
235 int write_len = buf_->offset() - write_cursor_; | 235 int write_len = buf_->offset() - write_cursor_; |
236 | 236 |
237 int rv = file_stream_->Write( | 237 int rv = file_stream_->Write( |
238 wrapped, | 238 wrapped.get(), |
239 write_len, | 239 write_len, |
240 base::Bind(&RedirectToFileResourceHandler::DidWriteToFile, | 240 base::Bind(&RedirectToFileResourceHandler::DidWriteToFile, |
241 base::Unretained(this))); | 241 base::Unretained(this))); |
242 if (rv == net::ERR_IO_PENDING) { | 242 if (rv == net::ERR_IO_PENDING) { |
243 write_callback_pending_ = true; | 243 write_callback_pending_ = true; |
244 return true; | 244 return true; |
245 } | 245 } |
246 if (rv <= 0) | 246 if (rv <= 0) |
247 return false; | 247 return false; |
248 next_handler_->OnDataDownloaded(request_id_, rv); | 248 next_handler_->OnDataDownloaded(request_id_, rv); |
(...skipping 10 matching lines...) Expand all Loading... |
259 } | 259 } |
260 | 260 |
261 void RedirectToFileResourceHandler::ResumeIfDeferred() { | 261 void RedirectToFileResourceHandler::ResumeIfDeferred() { |
262 if (did_defer_) { | 262 if (did_defer_) { |
263 did_defer_ = false; | 263 did_defer_ = false; |
264 controller()->Resume(); | 264 controller()->Resume(); |
265 } | 265 } |
266 } | 266 } |
267 | 267 |
268 } // namespace content | 268 } // namespace content |
OLD | NEW |