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.
4
5#ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_H_
6#define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_H_
7
8#include "base/basictypes.h"
9#include "base/files/file_path.h"
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/memory/weak_ptr.h"
13#include "storage/browser/blob/file_stream_reader.h"
14#include "storage/browser/fileapi/file_system_url.h"
15
16namespace chromeos {
17namespace file_system_provider {
18
19struct EntryMetadata;
20class ProvidedFileSystemInterface;
21
22// Implements a streamed file reader. It is lazily initialized by the first call
23// to Read().
24class FileStreamReader : public storage::FileStreamReader {
25 public:
26  typedef base::Callback<
27      void(base::WeakPtr<ProvidedFileSystemInterface> file_system,
28           const base::FilePath& file_path,
29           int file_handle,
30           base::File::Error result)> OpenFileCompletedCallback;
31
32  FileStreamReader(storage::FileSystemContext* context,
33                   const storage::FileSystemURL& url,
34                   int64 initial_offset,
35                   const base::Time& expected_modification_time);
36
37  virtual ~FileStreamReader();
38
39  // storage::FileStreamReader overrides.
40  virtual int Read(net::IOBuffer* buf,
41                   int buf_len,
42                   const net::CompletionCallback& callback) OVERRIDE;
43  virtual int64 GetLength(
44      const net::Int64CompletionCallback& callback) OVERRIDE;
45
46 private:
47  // Helper class for executing operations on the provided file system. All
48  // of its methods must be called on UI thread. Callbacks are called on IO
49  // thread.
50  class OperationRunner;
51
52  // State of the file stream reader.
53  enum State { NOT_INITIALIZED, INITIALIZING, INITIALIZED, FAILED };
54
55  // Called when Read() operation is completed with either a success of an
56  // error.
57  void OnReadCompleted(net::CompletionCallback callback, int result);
58
59  // Initializes the reader by opening the file. When completed with success,
60  // runs the |pending_closure|. Otherwise, calls the |error_callback|.
61  void Initialize(const base::Closure& pending_closure,
62                  const net::Int64CompletionCallback& error_callback);
63
64  // Called when opening a file is completed with either a success or an error.
65  void OnOpenFileCompleted(
66      const base::Closure& pending_closure,
67      const net::Int64CompletionCallback& error_callback,
68      base::File::Error result);
69
70  // Called when initialization is completed with either a success or an error.
71  void OnInitializeCompleted(const base::Closure& pending_closure,
72                             const net::Int64CompletionCallback& error_callback,
73                             scoped_ptr<EntryMetadata> metadata,
74                             base::File::Error result);
75
76  // Called when a file system provider returns chunk of read data. Note, that
77  // this may be called multiple times per single Read() call, as long as
78  // |has_more| is set to true. |result| is set to success only if reading is
79  // successful, and the file has not changed while reading.
80  void OnReadChunkReceived(const net::CompletionCallback& callback,
81                           int chunk_length,
82                           bool has_more,
83                           base::File::Error result);
84
85  // Called when fetching length of the file is completed with either a success
86  // or an error.
87  void OnGetMetadataForGetLengthReceived(
88      const net::Int64CompletionCallback& callback,
89      scoped_ptr<EntryMetadata> metadata,
90      base::File::Error result);
91
92  // Same as Read(), but called after initializing is completed.
93  void ReadAfterInitialized(scoped_refptr<net::IOBuffer> buffer,
94                            int buffer_length,
95                            const net::CompletionCallback& callback);
96
97  // Same as GetLength(), but called after initializing is completed.
98  void GetLengthAfterInitialized(const net::Int64CompletionCallback& callback);
99
100  storage::FileSystemURL url_;
101  int64 current_offset_;
102  int64 current_length_;
103  base::Time expected_modification_time_;
104  scoped_refptr<OperationRunner> runner_;
105  State state_;
106
107  base::WeakPtrFactory<FileStreamReader> weak_ptr_factory_;
108  DISALLOW_COPY_AND_ASSIGN(FileStreamReader);
109};
110
111}  // namespace file_system_provider
112}  // namespace chromeos
113
114#endif  // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_H_
115