Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/file_system_provider/fileapi/buffering_file_st ream_reader.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/message_loop/message_loop.h" | |
|
hashimoto
2014/07/08 11:12:55
nit: No need to include this.
mtomasz
2014/07/09 02:05:22
Done.
| |
| 10 #include "net/base/io_buffer.h" | |
| 11 #include "net/base/net_errors.h" | |
| 12 | |
| 13 namespace chromeos { | |
| 14 namespace file_system_provider { | |
| 15 | |
| 16 BufferingFileStreamReader::BufferingFileStreamReader( | |
| 17 scoped_ptr<webkit_blob::FileStreamReader> file_stream_reader, | |
| 18 int buffer_size) | |
| 19 : file_stream_reader_(file_stream_reader.Pass()), | |
| 20 buffer_size_(buffer_size), | |
| 21 preloading_buffer_(new net::IOBuffer(buffer_size_)), | |
| 22 preloading_buffer_offset_(0), | |
| 23 buffered_bytes_(0), | |
| 24 weak_ptr_factory_(this) { | |
| 25 } | |
| 26 | |
| 27 BufferingFileStreamReader::~BufferingFileStreamReader() { | |
| 28 } | |
| 29 | |
| 30 int BufferingFileStreamReader::Read(net::IOBuffer* buffer, | |
| 31 int buffer_length, | |
| 32 const net::CompletionCallback& callback) { | |
| 33 // Return as much as available in the internal buffer. It may be less than | |
| 34 // |buffer_length|, what is valid. | |
| 35 const int bytes_read = | |
| 36 CopyFromPreloadingBuffer(make_scoped_refptr(buffer), buffer_length); | |
| 37 if (bytes_read) | |
| 38 return bytes_read; | |
| 39 | |
| 40 // If the internal buffer is empty, and more bytes than the internal buffer | |
| 41 // size is requested, then call the internal file stream reader directly. | |
| 42 if (buffer_length >= buffer_size_) { | |
| 43 const int result = | |
| 44 file_stream_reader_->Read(buffer, buffer_length, callback); | |
| 45 DCHECK_EQ(result, net::ERR_IO_PENDING); | |
| 46 return result; | |
| 47 } | |
| 48 | |
| 49 // Nothing copied, so contents have to be preloaded. | |
| 50 Preload(base::Bind(&BufferingFileStreamReader::OnPreloadCompleted, | |
| 51 weak_ptr_factory_.GetWeakPtr(), | |
| 52 make_scoped_refptr(buffer), | |
| 53 buffer_length, | |
| 54 callback)); | |
| 55 | |
| 56 return net::ERR_IO_PENDING; | |
| 57 } | |
| 58 | |
| 59 int64 BufferingFileStreamReader::GetLength( | |
| 60 const net::Int64CompletionCallback& callback) { | |
| 61 const int64 result = file_stream_reader_->GetLength(callback); | |
| 62 DCHECK_EQ(net::ERR_IO_PENDING, result); | |
| 63 | |
| 64 return result; | |
| 65 } | |
| 66 | |
| 67 int BufferingFileStreamReader::CopyFromPreloadingBuffer( | |
| 68 scoped_refptr<net::IOBuffer> buffer, | |
| 69 int buffer_length) { | |
| 70 const int read_bytes = std::min(buffer_length, buffered_bytes_); | |
| 71 | |
| 72 memcpy(buffer->data(), | |
| 73 preloading_buffer_->data() + preloading_buffer_offset_, | |
| 74 read_bytes); | |
| 75 preloading_buffer_offset_ += read_bytes; | |
| 76 buffered_bytes_ -= read_bytes; | |
| 77 | |
| 78 return read_bytes; | |
| 79 } | |
| 80 | |
| 81 void BufferingFileStreamReader::Preload( | |
| 82 const net::CompletionCallback& callback) { | |
| 83 // TODO(mtomasz): Dynamically calculate the chunk size. Start from a small | |
| 84 // one, then increase for consecutive requests. That would improve performance | |
| 85 // when reading just small chunks, instead of the entire file. | |
| 86 const int preload_bytes = buffer_size_; | |
| 87 | |
| 88 const int result = | |
| 89 file_stream_reader_->Read(preloading_buffer_, preload_bytes, callback); | |
| 90 DCHECK_EQ(result, net::ERR_IO_PENDING); | |
| 91 } | |
| 92 | |
| 93 void BufferingFileStreamReader::OnPreloadCompleted( | |
| 94 scoped_refptr<net::IOBuffer> buffer, | |
| 95 int buffer_length, | |
| 96 const net::CompletionCallback& callback, | |
| 97 int result) { | |
| 98 if (result < 0) { | |
| 99 callback.Run(result); | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 preloading_buffer_offset_ = 0; | |
| 104 buffered_bytes_ = result; | |
| 105 | |
| 106 callback.Run(CopyFromPreloadingBuffer(buffer, buffer_length)); | |
| 107 } | |
| 108 | |
| 109 } // namespace file_system_provider | |
| 110 } // namespace chromeos | |
| OLD | NEW |