| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_STRE
AM_READER_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_STRE
AM_READER_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_STRE
AM_READER_H_ | 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_STRE
AM_READER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> |
| 11 |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 14 #include "storage/browser/fileapi/file_stream_reader.h" | 15 #include "storage/browser/fileapi/file_stream_reader.h" |
| 15 | 16 |
| 16 namespace net { | 17 namespace net { |
| 17 class IOBuffer; | 18 class IOBuffer; |
| 18 } // namespace net | 19 } // namespace net |
| 19 | 20 |
| 20 namespace chromeos { | 21 namespace chromeos { |
| 21 namespace file_system_provider { | 22 namespace file_system_provider { |
| 22 | 23 |
| 23 // Wraps the file stream reader implementation with a prefetching buffer. | 24 // Wraps the file stream reader implementation with a prefetching buffer. |
| 24 // Reads data from the internal file stream reader in chunks of size at least | 25 // Reads data from the internal file stream reader in chunks of size at least |
| 25 // |preloading_buffer_length| bytes (or less for the last chunk, because of | 26 // |preloading_buffer_length| bytes (or less for the last chunk, because of |
| 26 // EOF). Up to |max_bytes_to_read| of bytes can be requested in total. | 27 // EOF). Up to |max_bytes_to_read| of bytes can be requested in total. |
| 27 // | 28 // |
| 28 // The underlying internal file stream reader *must not* return any values | 29 // The underlying internal file stream reader *must not* return any values |
| 29 // synchronously. Instead, results must be returned by a callback, including | 30 // synchronously. Instead, results must be returned by a callback, including |
| 30 // errors. | 31 // errors. |
| 31 class BufferingFileStreamReader : public storage::FileStreamReader { | 32 class BufferingFileStreamReader : public storage::FileStreamReader { |
| 32 public: | 33 public: |
| 33 BufferingFileStreamReader( | 34 BufferingFileStreamReader( |
| 34 scoped_ptr<storage::FileStreamReader> file_stream_reader, | 35 std::unique_ptr<storage::FileStreamReader> file_stream_reader, |
| 35 int preloading_buffer_length, | 36 int preloading_buffer_length, |
| 36 int64_t max_bytes_to_read); | 37 int64_t max_bytes_to_read); |
| 37 | 38 |
| 38 ~BufferingFileStreamReader() override; | 39 ~BufferingFileStreamReader() override; |
| 39 | 40 |
| 40 // storage::FileStreamReader overrides. | 41 // storage::FileStreamReader overrides. |
| 41 int Read(net::IOBuffer* buf, | 42 int Read(net::IOBuffer* buf, |
| 42 int buf_len, | 43 int buf_len, |
| 43 const net::CompletionCallback& callback) override; | 44 const net::CompletionCallback& callback) override; |
| 44 int64_t GetLength(const net::Int64CompletionCallback& callback) override; | 45 int64_t GetLength(const net::Int64CompletionCallback& callback) override; |
| 45 | 46 |
| 46 private: | 47 private: |
| 47 // Copies data from the preloading buffer and updates the internal iterator. | 48 // Copies data from the preloading buffer and updates the internal iterator. |
| 48 // Returns number of bytes successfully copied. | 49 // Returns number of bytes successfully copied. |
| 49 int CopyFromPreloadingBuffer(scoped_refptr<net::IOBuffer> buffer, | 50 int CopyFromPreloadingBuffer(scoped_refptr<net::IOBuffer> buffer, |
| 50 int buffer_length); | 51 int buffer_length); |
| 51 | 52 |
| 52 // Preloads data from the internal stream reader and calls the |callback|. | 53 // Preloads data from the internal stream reader and calls the |callback|. |
| 53 void Preload(const net::CompletionCallback& callback); | 54 void Preload(const net::CompletionCallback& callback); |
| 54 | 55 |
| 55 void OnReadCompleted(const net::CompletionCallback& callback, int result); | 56 void OnReadCompleted(const net::CompletionCallback& callback, int result); |
| 56 | 57 |
| 57 // Called when preloading of a buffer chunk is finished. Updates state of the | 58 // Called when preloading of a buffer chunk is finished. Updates state of the |
| 58 // preloading buffer and copied requested data to the |buffer|. | 59 // preloading buffer and copied requested data to the |buffer|. |
| 59 void OnPreloadCompleted(scoped_refptr<net::IOBuffer> buffer, | 60 void OnPreloadCompleted(scoped_refptr<net::IOBuffer> buffer, |
| 60 int buffer_length, | 61 int buffer_length, |
| 61 const net::CompletionCallback& callback, | 62 const net::CompletionCallback& callback, |
| 62 int result); | 63 int result); |
| 63 | 64 |
| 64 scoped_ptr<storage::FileStreamReader> file_stream_reader_; | 65 std::unique_ptr<storage::FileStreamReader> file_stream_reader_; |
| 65 int preloading_buffer_length_; | 66 int preloading_buffer_length_; |
| 66 int64_t max_bytes_to_read_; | 67 int64_t max_bytes_to_read_; |
| 67 int64_t bytes_read_; | 68 int64_t bytes_read_; |
| 68 scoped_refptr<net::IOBuffer> preloading_buffer_; | 69 scoped_refptr<net::IOBuffer> preloading_buffer_; |
| 69 int preloading_buffer_offset_; | 70 int preloading_buffer_offset_; |
| 70 int preloaded_bytes_; | 71 int preloaded_bytes_; |
| 71 | 72 |
| 72 base::WeakPtrFactory<BufferingFileStreamReader> weak_ptr_factory_; | 73 base::WeakPtrFactory<BufferingFileStreamReader> weak_ptr_factory_; |
| 73 DISALLOW_COPY_AND_ASSIGN(BufferingFileStreamReader); | 74 DISALLOW_COPY_AND_ASSIGN(BufferingFileStreamReader); |
| 74 }; | 75 }; |
| 75 | 76 |
| 76 } // namespace file_system_provider | 77 } // namespace file_system_provider |
| 77 } // namespace chromeos | 78 } // namespace chromeos |
| 78 | 79 |
| 79 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_S
TREAM_READER_H_ | 80 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_S
TREAM_READER_H_ |
| OLD | NEW |