Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / media_galleries / fileapi / readahead_file_stream_reader.h
blob90afc96d916cf4bcdcddf30d94a739778c66caf1
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_MEDIA_GALLERIES_FILEAPI_READAHEAD_FILE_STREAM_READER_H_
6 #define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_READAHEAD_FILE_STREAM_READER_H_
8 #include <queue>
10 #include "base/memory/weak_ptr.h"
11 #include "net/base/io_buffer.h"
12 #include "storage/browser/fileapi/file_stream_reader.h"
14 // Wraps a source FileStreamReader with a readahead buffer.
15 class ReadaheadFileStreamReader
16 : public NON_EXPORTED_BASE(storage::FileStreamReader) {
17 public:
18 // Takes ownership of |source|.
19 explicit ReadaheadFileStreamReader(storage::FileStreamReader* source);
21 ~ReadaheadFileStreamReader() override;
23 // FileStreamReader overrides.
24 int Read(net::IOBuffer* buf,
25 int buf_len,
26 const net::CompletionCallback& callback) override;
27 int64 GetLength(const net::Int64CompletionCallback& callback) override;
29 private:
30 // Returns the number of bytes consumed from the internal cache into |sink|.
31 // Returns an error code if we are out of cache, hit an error, or hit EOF.
32 int FinishReadFromCacheOrStoredError(net::DrainableIOBuffer* sink);
34 // Reads into a new buffer from the source reader. This calls
35 // OnFinishReadFromSource when it completes (either synchronously or
36 // asynchronously).
37 void ReadFromSourceIfNeeded();
38 void OnFinishReadFromSource(net::IOBuffer* buffer, int result);
40 // This is reset to NULL upon encountering a read error or EOF.
41 scoped_ptr<storage::FileStreamReader> source_;
43 // This stores the error or EOF from the source FileStreamReader. Its
44 // value is undefined if |source_| is non-NULL.
45 int source_error_;
46 bool source_has_pending_read_;
48 // This contains a queue of buffers filled from |source_|, waiting to be
49 // consumed.
50 std::queue<scoped_refptr<net::DrainableIOBuffer> > buffers_;
52 // The read buffer waiting for the source FileStreamReader to finish
53 // reading and fill the cache.
54 scoped_refptr<net::DrainableIOBuffer> pending_sink_buffer_;
55 net::CompletionCallback pending_read_callback_;
57 base::WeakPtrFactory<ReadaheadFileStreamReader> weak_factory_;
59 DISALLOW_COPY_AND_ASSIGN(ReadaheadFileStreamReader);
62 #endif // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_READAHEAD_FILE_STREAM_READER_H_