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
93 int64 offset
, int64 content_length
, const base::Closure
& job_canceller
);
94 virtual ~NetworkReaderProxy();
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
;
103 // The data received from the server, but not yet read.
104 ScopedVector
<std::string
> pending_data_
;
106 // The number of bytes to be skipped.
107 int64 remaining_offset_
;
109 // The number of bytes of remaining data (including the data not yet
110 // received from the server).
111 int64 remaining_content_length_
;
115 // To support pending Read(), it is necessary to keep its arguments.
116 scoped_refptr
<net::IOBuffer
> buffer_
;
118 net::CompletionCallback callback_
;
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_
;
125 DISALLOW_COPY_AND_ASSIGN(NetworkReaderProxy
);
128 } // namespace internal
130 class FileSystemInterface
;
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.
137 class DriveFileStreamReader
{
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
;
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
;
150 DriveFileStreamReader(const FileSystemGetter
& file_system_getter
,
151 base::SequencedTaskRunner
* file_task_runner
);
152 ~DriveFileStreamReader();
154 // Returns true if the reader is initialized.
155 bool IsInitialized() const;
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
);
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
);
175 // Part of Initialize. Called after GetFileContent's initialization
177 void InitializeAfterGetFileContentInitialized(
178 const net::HttpByteRange
& byte_range
,
179 const InitializeCompletionCallback
& callback
,
181 scoped_ptr
<ResourceEntry
> entry
,
182 const base::FilePath
& local_cache_file_path
,
183 const base::Closure
& cancel_download_closure
);
185 // Part of Initialize. Called when the local file open process is done.
186 void InitializeAfterLocalFileOpen(
188 const InitializeCompletionCallback
& callback
,
189 scoped_ptr
<ResourceEntry
> entry
,
190 scoped_ptr
<util::LocalFileReader
> file_reader
,
193 // Called when the data is received from the server.
194 void OnGetContent(google_apis::GDataErrorCode error_code
,
195 scoped_ptr
<std::string
> data
);
197 // Called when GetFileContent is completed.
198 void OnGetFileContentCompletion(
199 const InitializeCompletionCallback
& callback
,
202 const FileSystemGetter file_system_getter_
;
203 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner_
;
204 scoped_ptr
<internal::ReaderProxy
> reader_proxy_
;
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
);
214 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVE_FILE_STREAM_READER_H_