summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media_galleries/fileapi/readahead_file_stream_reader.h
blob: 65d4ff23116648c4f394684391233cf15b9f5e1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_READAHEAD_FILE_STREAM_READER_H_
#define CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_READAHEAD_FILE_STREAM_READER_H_

#include <stdint.h>

#include <queue>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "net/base/io_buffer.h"
#include "storage/browser/fileapi/file_stream_reader.h"

// Wraps a source FileStreamReader with a readahead buffer.
class ReadaheadFileStreamReader
    : public NON_EXPORTED_BASE(storage::FileStreamReader) {
 public:
  // Takes ownership of |source|.
  explicit ReadaheadFileStreamReader(storage::FileStreamReader* source);

  ~ReadaheadFileStreamReader() override;

  // FileStreamReader overrides.
  int Read(net::IOBuffer* buf,
           int buf_len,
           const net::CompletionCallback& callback) override;
  int64_t GetLength(const net::Int64CompletionCallback& callback) override;

 private:
  // Returns the number of bytes consumed from the internal cache into |sink|.
  // Returns an error code if we are out of cache, hit an error, or hit EOF.
  int FinishReadFromCacheOrStoredError(net::DrainableIOBuffer* sink);

  // Reads into a new buffer from the source reader. This calls
  // OnFinishReadFromSource when it completes (either synchronously or
  // asynchronously).
  void ReadFromSourceIfNeeded();
  void OnFinishReadFromSource(net::IOBuffer* buffer, int result);

  // This is reset to NULL upon encountering a read error or EOF.
  scoped_ptr<storage::FileStreamReader> source_;

  // This stores the error or EOF from the source FileStreamReader. Its
  // value is undefined if |source_| is non-NULL.
  int source_error_;
  bool source_has_pending_read_;

  // This contains a queue of buffers filled from |source_|, waiting to be
  // consumed.
  std::queue<scoped_refptr<net::DrainableIOBuffer> > buffers_;

  // The read buffer waiting for the source FileStreamReader to finish
  // reading and fill the cache.
  scoped_refptr<net::DrainableIOBuffer> pending_sink_buffer_;
  net::CompletionCallback pending_read_callback_;

  base::WeakPtrFactory<ReadaheadFileStreamReader> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(ReadaheadFileStreamReader);
};

#endif  // CHROME_BROWSER_MEDIA_GALLERIES_FILEAPI_READAHEAD_FILE_STREAM_READER_H_