drive_file_stream_reader.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1// Copyright 2013 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_DRIVE_DRIVE_FILE_STREAM_READER_H_
6#define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_STREAM_READER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/callback.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/scoped_vector.h"
15#include "chrome/browser/chromeos/drive/file_errors.h"
16#include "google_apis/drive/gdata_errorcode.h"
17#include "net/base/completion_callback.h"
18
19namespace base {
20class SequencedTaskRunner;
21}  // namespace base
22
23namespace net {
24class HttpByteRange;
25class IOBuffer;
26}  // namespace net
27
28namespace drive {
29namespace util {
30class LocalFileReader;
31}  // namespace util
32
33namespace internal {
34
35// An interface to dispatch the reading operation. If the file is locally
36// cached, LocalReaderProxy defined below will be used. Otherwise (i.e. the
37// file is being downloaded from the server), NetworkReaderProxy will be used.
38class ReaderProxy {
39 public:
40  virtual ~ReaderProxy() {}
41
42  // Called from DriveFileStreamReader::Read method.
43  virtual int Read(net::IOBuffer* buffer, int buffer_length,
44                   const net::CompletionCallback& callback) = 0;
45
46  // Called when the data from the server is received.
47  virtual void OnGetContent(scoped_ptr<std::string> data) = 0;
48
49  // Called when the accessing to the file system is completed.
50  virtual void OnCompleted(FileError error) = 0;
51};
52
53// The read operation implementation for the locally cached files.
54class LocalReaderProxy : public ReaderProxy {
55 public:
56  // The |file_reader| should be the instance which is already opened.
57  // This class takes its ownership.
58  // |length| is the number of bytes to be read. It must be equal or
59  // smaller than the remaining data size in the |file_reader|.
60  LocalReaderProxy(
61      scoped_ptr<util::LocalFileReader> file_reader, int64 length);
62  virtual ~LocalReaderProxy();
63
64  // ReaderProxy overrides.
65  virtual int Read(net::IOBuffer* buffer, int buffer_length,
66                   const net::CompletionCallback& callback) OVERRIDE;
67  virtual void OnGetContent(scoped_ptr<std::string> data) OVERRIDE;
68  virtual void OnCompleted(FileError error) OVERRIDE;
69
70 private:
71  scoped_ptr<util::LocalFileReader> file_reader_;
72
73  // Callback for the LocalFileReader::Read.
74  void OnReadCompleted(
75      const net::CompletionCallback& callback, int read_result);
76
77  // The number of remaining bytes to be read.
78  int64 remaining_length_;
79
80  // This should remain the last member so it'll be destroyed first and
81  // invalidate its weak pointers before other members are destroyed.
82  base::WeakPtrFactory<LocalReaderProxy> weak_ptr_factory_;
83  DISALLOW_COPY_AND_ASSIGN(LocalReaderProxy);
84};
85
86// The read operation implementation for the file which is being downloaded.
87class NetworkReaderProxy : public ReaderProxy {
88 public:
89  // If the instance is deleted during the download process, it is necessary
90  // to cancel the job. |job_canceller| should be the callback to run the
91  // cancelling.
92  NetworkReaderProxy(
93      int64 offset, int64 content_length, const base::Closure& job_canceller);
94  virtual ~NetworkReaderProxy();
95
96  // ReaderProxy overrides.
97  virtual int Read(net::IOBuffer* buffer, int buffer_length,
98                   const net::CompletionCallback& callback) OVERRIDE;
99  virtual void OnGetContent(scoped_ptr<std::string> data) OVERRIDE;
100  virtual void OnCompleted(FileError error) OVERRIDE;
101
102 private:
103  // The data received from the server, but not yet read.
104  ScopedVector<std::string> pending_data_;
105
106  // The number of bytes to be skipped.
107  int64 remaining_offset_;
108
109  // The number of bytes of remaining data (including the data not yet
110  // received from the server).
111  int64 remaining_content_length_;
112
113  int error_code_;
114
115  // To support pending Read(), it is necessary to keep its arguments.
116  scoped_refptr<net::IOBuffer> buffer_;
117  int buffer_length_;
118  net::CompletionCallback callback_;
119
120  // Keeps the closure to cancel downloading job if necessary.
121  // Will be reset when the job is completed (regardless whether the job is
122  // successfully done or not).
123  base::Closure job_canceller_;
124
125  DISALLOW_COPY_AND_ASSIGN(NetworkReaderProxy);
126};
127
128}  // namespace internal
129
130class FileSystemInterface;
131class ResourceEntry;
132
133// The stream reader for a file in FileSystem. Instances of this class
134// should live on IO thread.
135// Operations to communicate with a locally cached file will run on
136// |file_task_runner| specified by the constructor.
137class DriveFileStreamReader {
138 public:
139  // Callback to return the FileSystemInterface instance. This is an
140  // injecting point for testing.
141  // Note that the callback will be copied between threads (IO and UI), and
142  // will be called on UI thread.
143  typedef base::Callback<FileSystemInterface*()> FileSystemGetter;
144
145  // Callback to return the result of Initialize().
146  // |error| is net::Error code.
147  typedef base::Callback<void(int error, scoped_ptr<ResourceEntry> entry)>
148      InitializeCompletionCallback;
149
150  DriveFileStreamReader(const FileSystemGetter& file_system_getter,
151                        base::SequencedTaskRunner* file_task_runner);
152  ~DriveFileStreamReader();
153
154  // Returns true if the reader is initialized.
155  bool IsInitialized() const;
156
157  // Initializes the stream for the |drive_file_path|.
158  // |callback| must not be null.
159  void Initialize(const base::FilePath& drive_file_path,
160                  const net::HttpByteRange& byte_range,
161                  const InitializeCompletionCallback& callback);
162
163  // Reads the data into |buffer| at most |buffer_length|, and returns
164  // the number of bytes. If an error happened, returns an error code.
165  // If no data is available yet, returns net::ERR_IO_PENDING immediately,
166  // and when the data is available the actual Read operation is done
167  // and |callback| will be run with the result.
168  // The Read() method must not be called before the Initialize() is completed
169  // successfully, or if there is pending read operation.
170  // Neither |buffer| nor |callback| must be null.
171  int Read(net::IOBuffer* buffer, int buffer_length,
172           const net::CompletionCallback& callback);
173
174 private:
175  // Part of Initialize. Called after GetFileContent's initialization
176  // is done.
177  void InitializeAfterGetFileContentInitialized(
178      const net::HttpByteRange& in_byte_range,
179      const InitializeCompletionCallback& callback,
180      FileError error,
181      scoped_ptr<ResourceEntry> entry,
182      const base::FilePath& local_cache_file_path,
183      const base::Closure& cancel_download_closure);
184
185  // Part of Initialize. Called when the local file open process is done.
186  void InitializeAfterLocalFileOpen(
187      int64 length,
188      const InitializeCompletionCallback& callback,
189      scoped_ptr<ResourceEntry> entry,
190      scoped_ptr<util::LocalFileReader> file_reader,
191      int open_result);
192
193  // Called when the data is received from the server.
194  void OnGetContent(google_apis::GDataErrorCode error_code,
195                    scoped_ptr<std::string> data);
196
197  // Called when GetFileContent is completed.
198  void OnGetFileContentCompletion(
199      const InitializeCompletionCallback& callback,
200      FileError error);
201
202  const FileSystemGetter file_system_getter_;
203  scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
204  scoped_ptr<internal::ReaderProxy> reader_proxy_;
205
206  // This should remain the last member so it'll be destroyed first and
207  // invalidate its weak pointers before other members are destroyed.
208  base::WeakPtrFactory<DriveFileStreamReader> weak_ptr_factory_;
209  DISALLOW_COPY_AND_ASSIGN(DriveFileStreamReader);
210};
211
212}  // namespace drive
213
214#endif  // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_STREAM_READER_H_
215