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 "base/threading/thread_checker.h"
16 #include "components/drive/file_errors.h"
17 #include "google_apis/drive/drive_api_error_codes.h"
18 #include "net/base/completion_callback.h"
21 class SequencedTaskRunner
;
31 class LocalFileReader
;
36 // An interface to dispatch the reading operation. If the file is locally
37 // cached, LocalReaderProxy defined below will be used. Otherwise (i.e. the
38 // file is being downloaded from the server), NetworkReaderProxy will be used.
41 virtual ~ReaderProxy() {}
43 // Called from DriveFileStreamReader::Read method.
44 virtual int Read(net::IOBuffer
* buffer
, int buffer_length
,
45 const net::CompletionCallback
& callback
) = 0;
47 // Called when the data from the server is received.
48 virtual void OnGetContent(scoped_ptr
<std::string
> data
) = 0;
50 // Called when the accessing to the file system is completed.
51 virtual void OnCompleted(FileError error
) = 0;
54 // The read operation implementation for the locally cached files.
55 class LocalReaderProxy
: public ReaderProxy
{
57 // The |file_reader| should be the instance which is already opened.
58 // This class takes its ownership.
59 // |length| is the number of bytes to be read. It must be equal or
60 // smaller than the remaining data size in the |file_reader|.
62 scoped_ptr
<util::LocalFileReader
> file_reader
, int64 length
);
63 ~LocalReaderProxy() override
;
65 // ReaderProxy overrides.
66 int Read(net::IOBuffer
* buffer
,
68 const net::CompletionCallback
& callback
) override
;
69 void OnGetContent(scoped_ptr
<std::string
> data
) override
;
70 void OnCompleted(FileError error
) override
;
73 scoped_ptr
<util::LocalFileReader
> file_reader_
;
75 // Callback for the LocalFileReader::Read.
77 const net::CompletionCallback
& callback
, int read_result
);
79 // The number of remaining bytes to be read.
80 int64 remaining_length_
;
82 base::ThreadChecker thread_checker_
;
84 // This should remain the last member so it'll be destroyed first and
85 // invalidate its weak pointers before other members are destroyed.
86 base::WeakPtrFactory
<LocalReaderProxy
> weak_ptr_factory_
;
87 DISALLOW_COPY_AND_ASSIGN(LocalReaderProxy
);
90 // The read operation implementation for the file which is being downloaded.
91 class NetworkReaderProxy
: public ReaderProxy
{
93 // If the instance is deleted during the download process, it is necessary
94 // to cancel the job. |job_canceller| should be the callback to run the
95 // cancelling. |full_content_length| is necessary for determining whether the
96 // deletion is done in the middle of download process.
98 int64 offset
, int64 content_length
, int64 full_content_length
,
99 const base::Closure
& job_canceller
);
100 ~NetworkReaderProxy() override
;
102 // ReaderProxy overrides.
103 int Read(net::IOBuffer
* buffer
,
105 const net::CompletionCallback
& callback
) override
;
106 void OnGetContent(scoped_ptr
<std::string
> data
) override
;
107 void OnCompleted(FileError error
) override
;
110 // The data received from the server, but not yet read.
111 ScopedVector
<std::string
> pending_data_
;
113 // The number of bytes to be skipped.
114 int64 remaining_offset_
;
116 // The number of bytes of remaining data (including the data not yet
117 // received from the server).
118 int64 remaining_content_length_
;
120 // Flag to remember whether this read request is for reading till the end of
122 const bool is_full_download_
;
126 // To support pending Read(), it is necessary to keep its arguments.
127 scoped_refptr
<net::IOBuffer
> buffer_
;
129 net::CompletionCallback callback_
;
131 base::ThreadChecker thread_checker_
;
133 // Keeps the closure to cancel downloading job if necessary.
134 // Will be reset when the job is completed (regardless whether the job is
135 // successfully done or not).
136 base::Closure job_canceller_
;
138 DISALLOW_COPY_AND_ASSIGN(NetworkReaderProxy
);
141 } // namespace internal
143 class FileSystemInterface
;
146 // The stream reader for a file in FileSystem. Instances of this class
147 // should live on IO thread.
148 // Operations to communicate with a locally cached file will run on
149 // |file_task_runner| specified by the constructor.
150 class DriveFileStreamReader
{
152 // Callback to return the FileSystemInterface instance. This is an
153 // injecting point for testing.
154 // Note that the callback will be copied between threads (IO and UI), and
155 // will be called on UI thread.
156 typedef base::Callback
<FileSystemInterface
*()> FileSystemGetter
;
158 // Callback to return the result of Initialize().
159 // |error| is net::Error code.
160 typedef base::Callback
<void(int error
, scoped_ptr
<ResourceEntry
> entry
)>
161 InitializeCompletionCallback
;
163 DriveFileStreamReader(const FileSystemGetter
& file_system_getter
,
164 base::SequencedTaskRunner
* file_task_runner
);
165 ~DriveFileStreamReader();
167 // Returns true if the reader is initialized.
168 bool IsInitialized() const;
170 // Initializes the stream for the |drive_file_path|.
171 // |callback| must not be null.
172 void Initialize(const base::FilePath
& drive_file_path
,
173 const net::HttpByteRange
& byte_range
,
174 const InitializeCompletionCallback
& callback
);
176 // Reads the data into |buffer| at most |buffer_length|, and returns
177 // the number of bytes. If an error happened, returns an error code.
178 // If no data is available yet, returns net::ERR_IO_PENDING immediately,
179 // and when the data is available the actual Read operation is done
180 // and |callback| will be run with the result.
181 // The Read() method must not be called before the Initialize() is completed
182 // successfully, or if there is pending read operation.
183 // Neither |buffer| nor |callback| must be null.
184 int Read(net::IOBuffer
* buffer
, int buffer_length
,
185 const net::CompletionCallback
& callback
);
188 // Used to store the cancel closure returned by FileSystemInterface.
189 void StoreCancelDownloadClosure(const base::Closure
& cancel_download_closure
);
191 // Part of Initialize. Called after GetFileContent's initialization
193 void InitializeAfterGetFileContentInitialized(
194 const net::HttpByteRange
& byte_range
,
195 const InitializeCompletionCallback
& callback
,
197 const base::FilePath
& local_cache_file_path
,
198 scoped_ptr
<ResourceEntry
> entry
);
200 // Part of Initialize. Called when the local file open process is done.
201 void InitializeAfterLocalFileOpen(
203 const InitializeCompletionCallback
& callback
,
204 scoped_ptr
<ResourceEntry
> entry
,
205 scoped_ptr
<util::LocalFileReader
> file_reader
,
208 // Called when the data is received from the server.
209 void OnGetContent(google_apis::DriveApiErrorCode error_code
,
210 scoped_ptr
<std::string
> data
);
212 // Called when GetFileContent is completed.
213 void OnGetFileContentCompletion(
214 const InitializeCompletionCallback
& callback
,
217 const FileSystemGetter file_system_getter_
;
218 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner_
;
219 base::Closure cancel_download_closure_
;
220 scoped_ptr
<internal::ReaderProxy
> reader_proxy_
;
222 base::ThreadChecker thread_checker_
;
224 // This should remain the last member so it'll be destroyed first and
225 // invalidate its weak pointers before other members are destroyed.
226 base::WeakPtrFactory
<DriveFileStreamReader
> weak_ptr_factory_
;
227 DISALLOW_COPY_AND_ASSIGN(DriveFileStreamReader
);
232 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_STREAM_READER_H_