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.
5 #ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_STREAM_READER_H_
6 #define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_STREAM_READER_H_
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"
20 class SequencedTaskRunner;
30 class LocalFileReader;
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.
40 virtual ~ReaderProxy() {}
42 // Called from DriveFileStreamReader::Read method.
43 virtual int Read(net::IOBuffer* buffer, int buffer_length,
44 const net::CompletionCallback& callback) = 0;
46 // Called when the data from the server is received.
47 virtual void OnGetContent(scoped_ptr<std::string> data) = 0;
49 // Called when the accessing to the file system is completed.
50 virtual void OnCompleted(FileError error) = 0;
53 // The read operation implementation for the locally cached files.
54 class LocalReaderProxy : public ReaderProxy {
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|.
61 scoped_ptr<util::LocalFileReader> file_reader, int64 length);
62 virtual ~LocalReaderProxy();
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;
71 scoped_ptr<util::LocalFileReader> file_reader_;
73 // Callback for the LocalFileReader::Read.
75 const net::CompletionCallback& callback, int read_result);
77 // The number of remaining bytes to be read.
78 int64 remaining_length_;
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);
86 // The read operation implementation for the file which is being downloaded.
87 class NetworkReaderProxy : public ReaderProxy {
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. |full_content_length| is necessary for determining whether the
92 // deletion is done in the middle of download process.
94 int64 offset, int64 content_length, int64 full_content_length,
95 const base::Closure& job_canceller);
96 virtual ~NetworkReaderProxy();
98 // ReaderProxy overrides.
99 virtual int Read(net::IOBuffer* buffer, int buffer_length,
100 const net::CompletionCallback& callback) override;
101 virtual void OnGetContent(scoped_ptr<std::string> data) override;
102 virtual void OnCompleted(FileError error) override;
105 // The data received from the server, but not yet read.
106 ScopedVector<std::string> pending_data_;
108 // The number of bytes to be skipped.
109 int64 remaining_offset_;
111 // The number of bytes of remaining data (including the data not yet
112 // received from the server).
113 int64 remaining_content_length_;
115 // Flag to remember whether this read request is for reading till the end of
117 const bool is_full_download_;
121 // To support pending Read(), it is necessary to keep its arguments.
122 scoped_refptr<net::IOBuffer> buffer_;
124 net::CompletionCallback callback_;
126 // Keeps the closure to cancel downloading job if necessary.
127 // Will be reset when the job is completed (regardless whether the job is
128 // successfully done or not).
129 base::Closure job_canceller_;
131 DISALLOW_COPY_AND_ASSIGN(NetworkReaderProxy);
134 } // namespace internal
136 class FileSystemInterface;
139 // The stream reader for a file in FileSystem. Instances of this class
140 // should live on IO thread.
141 // Operations to communicate with a locally cached file will run on
142 // |file_task_runner| specified by the constructor.
143 class DriveFileStreamReader {
145 // Callback to return the FileSystemInterface instance. This is an
146 // injecting point for testing.
147 // Note that the callback will be copied between threads (IO and UI), and
148 // will be called on UI thread.
149 typedef base::Callback<FileSystemInterface*()> FileSystemGetter;
151 // Callback to return the result of Initialize().
152 // |error| is net::Error code.
153 typedef base::Callback<void(int error, scoped_ptr<ResourceEntry> entry)>
154 InitializeCompletionCallback;
156 DriveFileStreamReader(const FileSystemGetter& file_system_getter,
157 base::SequencedTaskRunner* file_task_runner);
158 ~DriveFileStreamReader();
160 // Returns true if the reader is initialized.
161 bool IsInitialized() const;
163 // Initializes the stream for the |drive_file_path|.
164 // |callback| must not be null.
165 void Initialize(const base::FilePath& drive_file_path,
166 const net::HttpByteRange& byte_range,
167 const InitializeCompletionCallback& callback);
169 // Reads the data into |buffer| at most |buffer_length|, and returns
170 // the number of bytes. If an error happened, returns an error code.
171 // If no data is available yet, returns net::ERR_IO_PENDING immediately,
172 // and when the data is available the actual Read operation is done
173 // and |callback| will be run with the result.
174 // The Read() method must not be called before the Initialize() is completed
175 // successfully, or if there is pending read operation.
176 // Neither |buffer| nor |callback| must be null.
177 int Read(net::IOBuffer* buffer, int buffer_length,
178 const net::CompletionCallback& callback);
181 // Used to store the cancel closure returned by FileSystemInterface.
182 void StoreCancelDownloadClosure(const base::Closure& cancel_download_closure);
184 // Part of Initialize. Called after GetFileContent's initialization
186 void InitializeAfterGetFileContentInitialized(
187 const net::HttpByteRange& byte_range,
188 const InitializeCompletionCallback& callback,
190 const base::FilePath& local_cache_file_path,
191 scoped_ptr<ResourceEntry> entry);
193 // Part of Initialize. Called when the local file open process is done.
194 void InitializeAfterLocalFileOpen(
196 const InitializeCompletionCallback& callback,
197 scoped_ptr<ResourceEntry> entry,
198 scoped_ptr<util::LocalFileReader> file_reader,
201 // Called when the data is received from the server.
202 void OnGetContent(google_apis::GDataErrorCode error_code,
203 scoped_ptr<std::string> data);
205 // Called when GetFileContent is completed.
206 void OnGetFileContentCompletion(
207 const InitializeCompletionCallback& callback,
210 const FileSystemGetter file_system_getter_;
211 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
212 base::Closure cancel_download_closure_;
213 scoped_ptr<internal::ReaderProxy> reader_proxy_;
215 // This should remain the last member so it'll be destroyed first and
216 // invalidate its weak pointers before other members are destroyed.
217 base::WeakPtrFactory<DriveFileStreamReader> weak_ptr_factory_;
218 DISALLOW_COPY_AND_ASSIGN(DriveFileStreamReader);
223 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_STREAM_READER_H_