| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 13 #include "content/browser/loader/resource_controller.h" | 13 #include "content/browser/loader/resource_controller.h" |
| 14 #include "content/browser/loader/resource_request_info_impl.h" | 14 #include "content/browser/loader/resource_request_info_impl.h" |
| 15 #include "content/browser/loader/temporary_file_stream.h" | 15 #include "content/browser/loader/temporary_file_stream.h" |
| 16 #include "content/public/common/resource_response.h" | 16 #include "content/public/common/resource_response.h" |
| 17 #include "net/base/file_stream.h" | 17 #include "net/base/file_stream.h" |
| 18 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
| 19 #include "net/base/mime_sniffer.h" | |
| 20 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 21 #include "storage/browser/blob/shareable_file_reference.h" | 20 #include "storage/browser/blob/shareable_file_reference.h" |
| 22 | 21 |
| 23 using storage::ShareableFileReference; | 22 using storage::ShareableFileReference; |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 // This class is similar to identically named classes in AsyncResourceHandler | 26 // This class is similar to identically named classes in AsyncResourceHandler |
| 28 // and MimeTypeResourceHandler, but not quite. | 27 // and MimeTypeResourceHandler, but not quite. |
| 29 // TODO(ncbray): generalize and unify these cases? | 28 // TODO(ncbray): generalize and unify these cases? |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 } | 185 } |
| 187 | 186 |
| 188 void RedirectToFileResourceHandler::OnWillRead( | 187 void RedirectToFileResourceHandler::OnWillRead( |
| 189 scoped_refptr<net::IOBuffer>* buf, | 188 scoped_refptr<net::IOBuffer>* buf, |
| 190 int* buf_size, | 189 int* buf_size, |
| 191 std::unique_ptr<ResourceController> controller) { | 190 std::unique_ptr<ResourceController> controller) { |
| 192 if (buf_->capacity() < next_buffer_size_) | 191 if (buf_->capacity() < next_buffer_size_) |
| 193 buf_->SetCapacity(next_buffer_size_); | 192 buf_->SetCapacity(next_buffer_size_); |
| 194 | 193 |
| 195 // We should have paused this network request already if the buffer is full. | 194 // We should have paused this network request already if the buffer is full. |
| 196 DCHECK(!BufIsFull()); | 195 DCHECK_GT(buf_->RemainingCapacity(), 0); |
| 197 | 196 |
| 198 *buf = buf_.get(); | 197 *buf = buf_.get(); |
| 199 *buf_size = buf_->RemainingCapacity(); | 198 *buf_size = buf_->RemainingCapacity(); |
| 200 | 199 |
| 201 buf_write_pending_ = true; | 200 buf_write_pending_ = true; |
| 202 controller->Resume(); | 201 controller->Resume(); |
| 203 } | 202 } |
| 204 | 203 |
| 205 void RedirectToFileResourceHandler::OnReadCompleted( | 204 void RedirectToFileResourceHandler::OnReadCompleted( |
| 206 int bytes_read, | 205 int bytes_read, |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 if (rv == net::ERR_IO_PENDING) | 347 if (rv == net::ERR_IO_PENDING) |
| 349 break; | 348 break; |
| 350 if (rv <= 0) | 349 if (rv <= 0) |
| 351 return false; | 350 return false; |
| 352 next_handler_->OnDataDownloaded(rv); | 351 next_handler_->OnDataDownloaded(rv); |
| 353 write_cursor_ += rv; | 352 write_cursor_ += rv; |
| 354 } | 353 } |
| 355 | 354 |
| 356 // If the request was defered for a reason other than having been completed, | 355 // If the request was defered for a reason other than having been completed, |
| 357 // and the buffer has space, resume the request. | 356 // and the buffer has space, resume the request. |
| 358 if (has_controller() && !completed_during_write_ && !BufIsFull()) { | 357 if (has_controller() && !completed_during_write_ && |
| 358 buf_->RemainingCapacity() > 0) { |
| 359 request()->LogUnblocked(); | 359 request()->LogUnblocked(); |
| 360 Resume(); | 360 Resume(); |
| 361 } | 361 } |
| 362 return true; | 362 return true; |
| 363 } | 363 } |
| 364 | 364 |
| 365 bool RedirectToFileResourceHandler::BufIsFull() const { | |
| 366 // This is a hack to workaround MimeTypeResourceHandler's inability to | |
| 367 // deal with a ResourceHandler that returns a buffer size of less than | |
| 368 // 2 * net::kMaxBytesToSniff from its OnWillRead method. | |
| 369 // TODO(darin): Fix this retardation! | |
| 370 return buf_->RemainingCapacity() <= (2 * net::kMaxBytesToSniff); | |
| 371 } | |
| 372 | |
| 373 } // namespace content | 365 } // namespace content |
| OLD | NEW |