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