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.
5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_STREAM_READER_H_
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_STREAM_READER_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "storage/browser/blob/file_stream_reader.h"
19 namespace file_system_provider {
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
23 // |preloading_buffer_length| bytes (or less for the last chunk, because of
24 // EOF). Up to |max_bytes_to_read| of bytes can be requested in total.
26 // The underlying internal file stream reader *must not* return any values
27 // synchronously. Instead, results must be returned by a callback, including
29 class BufferingFileStreamReader : public storage::FileStreamReader {
31 BufferingFileStreamReader(
32 scoped_ptr<storage::FileStreamReader> file_stream_reader,
33 int preloading_buffer_length,
34 int64 max_bytes_to_read);
36 virtual ~BufferingFileStreamReader();
38 // storage::FileStreamReader overrides.
39 virtual int Read(net::IOBuffer* buf,
41 const net::CompletionCallback& callback) OVERRIDE;
42 virtual int64 GetLength(
43 const net::Int64CompletionCallback& callback) OVERRIDE;
46 // Copies data from the preloading buffer and updates the internal iterator.
47 // Returns number of bytes successfully copied.
48 int CopyFromPreloadingBuffer(scoped_refptr<net::IOBuffer> buffer,
51 // Preloads data from the internal stream reader and calls the |callback|.
52 void Preload(const net::CompletionCallback& callback);
54 void OnReadCompleted(const net::CompletionCallback& callback, int result);
56 // Called when preloading of a buffer chunk is finished. Updates state of the
57 // preloading buffer and copied requested data to the |buffer|.
58 void OnPreloadCompleted(scoped_refptr<net::IOBuffer> buffer,
60 const net::CompletionCallback& callback,
63 scoped_ptr<storage::FileStreamReader> file_stream_reader_;
64 int preloading_buffer_length_;
65 int64 max_bytes_to_read_;
67 scoped_refptr<net::IOBuffer> preloading_buffer_;
68 int preloading_buffer_offset_;
71 base::WeakPtrFactory<BufferingFileStreamReader> weak_ptr_factory_;
72 DISALLOW_COPY_AND_ASSIGN(BufferingFileStreamReader);
75 } // namespace file_system_provider
76 } // namespace chromeos
78 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_BUFFERING_FILE_STREAM_READER_H_