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/drive_api_error_codes.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 ~LocalReaderProxy() override
;
64 // ReaderProxy overrides.
65 int Read(net::IOBuffer
* buffer
,
67 const net::CompletionCallback
& callback
) override
;
68 void OnGetContent(scoped_ptr
<std::string
> data
) override
;
69 void OnCompleted(FileError error
) override
;
72 scoped_ptr
<util::LocalFileReader
> file_reader_
;
74 // Callback for the LocalFileReader::Read.
76 const net::CompletionCallback
& callback
, int read_result
);
78 // The number of remaining bytes to be read.
79 int64 remaining_length_
;
81 // This should remain the last member so it'll be destroyed first and
82 // invalidate its weak pointers before other members are destroyed.
83 base::WeakPtrFactory
<LocalReaderProxy
> weak_ptr_factory_
;
84 DISALLOW_COPY_AND_ASSIGN(LocalReaderProxy
);
87 // The read operation implementation for the file which is being downloaded.
88 class NetworkReaderProxy
: public ReaderProxy
{
90 // If the instance is deleted during the download process, it is necessary
91 // to cancel the job. |job_canceller| should be the callback to run the
92 // cancelling. |full_content_length| is necessary for determining whether the
93 // deletion is done in the middle of download process.
95 int64 offset
, int64 content_length
, int64 full_content_length
,
96 const base::Closure
& job_canceller
);
97 ~NetworkReaderProxy() override
;
99 // ReaderProxy overrides.
100 int Read(net::IOBuffer
* buffer
,
102 const net::CompletionCallback
& callback
) override
;
103 void OnGetContent(scoped_ptr
<std::string
> data
) override
;
104 void OnCompleted(FileError error
) override
;
107 // The data received from the server, but not yet read.
108 ScopedVector
<std::string
> pending_data_
;
110 // The number of bytes to be skipped.
111 int64 remaining_offset_
;
113 // The number of bytes of remaining data (including the data not yet
114 // received from the server).
115 int64 remaining_content_length_
;
117 // Flag to remember whether this read request is for reading till the end of
119 const bool is_full_download_
;
123 // To support pending Read(), it is necessary to keep its arguments.
124 scoped_refptr
<net::IOBuffer
> buffer_
;
126 net::CompletionCallback callback_
;
128 // Keeps the closure to cancel downloading job if necessary.
129 // Will be reset when the job is completed (regardless whether the job is
130 // successfully done or not).
131 base::Closure job_canceller_
;
133 DISALLOW_COPY_AND_ASSIGN(NetworkReaderProxy
);
136 } // namespace internal
138 class FileSystemInterface
;
141 // The stream reader for a file in FileSystem. Instances of this class
142 // should live on IO thread.
143 // Operations to communicate with a locally cached file will run on
144 // |file_task_runner| specified by the constructor.
145 class DriveFileStreamReader
{
147 // Callback to return the FileSystemInterface instance. This is an
148 // injecting point for testing.
149 // Note that the callback will be copied between threads (IO and UI), and
150 // will be called on UI thread.
151 typedef base::Callback
<FileSystemInterface
*()> FileSystemGetter
;
153 // Callback to return the result of Initialize().
154 // |error| is net::Error code.
155 typedef base::Callback
<void(int error
, scoped_ptr
<ResourceEntry
> entry
)>
156 InitializeCompletionCallback
;
158 DriveFileStreamReader(const FileSystemGetter
& file_system_getter
,
159 base::SequencedTaskRunner
* file_task_runner
);
160 ~DriveFileStreamReader();
162 // Returns true if the reader is initialized.
163 bool IsInitialized() const;
165 // Initializes the stream for the |drive_file_path|.
166 // |callback| must not be null.
167 void Initialize(const base::FilePath
& drive_file_path
,
168 const net::HttpByteRange
& byte_range
,
169 const InitializeCompletionCallback
& callback
);
171 // Reads the data into |buffer| at most |buffer_length|, and returns
172 // the number of bytes. If an error happened, returns an error code.
173 // If no data is available yet, returns net::ERR_IO_PENDING immediately,
174 // and when the data is available the actual Read operation is done
175 // and |callback| will be run with the result.
176 // The Read() method must not be called before the Initialize() is completed
177 // successfully, or if there is pending read operation.
178 // Neither |buffer| nor |callback| must be null.
179 int Read(net::IOBuffer
* buffer
, int buffer_length
,
180 const net::CompletionCallback
& callback
);
183 // Used to store the cancel closure returned by FileSystemInterface.
184 void StoreCancelDownloadClosure(const base::Closure
& cancel_download_closure
);
186 // Part of Initialize. Called after GetFileContent's initialization
188 void InitializeAfterGetFileContentInitialized(
189 const net::HttpByteRange
& byte_range
,
190 const InitializeCompletionCallback
& callback
,
192 const base::FilePath
& local_cache_file_path
,
193 scoped_ptr
<ResourceEntry
> entry
);
195 // Part of Initialize. Called when the local file open process is done.
196 void InitializeAfterLocalFileOpen(
198 const InitializeCompletionCallback
& callback
,
199 scoped_ptr
<ResourceEntry
> entry
,
200 scoped_ptr
<util::LocalFileReader
> file_reader
,
203 // Called when the data is received from the server.
204 void OnGetContent(google_apis::DriveApiErrorCode error_code
,
205 scoped_ptr
<std::string
> data
);
207 // Called when GetFileContent is completed.
208 void OnGetFileContentCompletion(
209 const InitializeCompletionCallback
& callback
,
212 const FileSystemGetter file_system_getter_
;
213 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner_
;
214 base::Closure cancel_download_closure_
;
215 scoped_ptr
<internal::ReaderProxy
> reader_proxy_
;
217 // This should remain the last member so it'll be destroyed first and
218 // invalidate its weak pointers before other members are destroyed.
219 base::WeakPtrFactory
<DriveFileStreamReader
> weak_ptr_factory_
;
220 DISALLOW_COPY_AND_ASSIGN(DriveFileStreamReader
);
225 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_STREAM_READER_H_