OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_
H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_
H_ |
6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_
H_ | 6 #define CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READER_
H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "webkit/browser/blob/file_stream_reader.h" | 12 #include "webkit/browser/blob/file_stream_reader.h" |
13 #include "webkit/browser/fileapi/file_system_url.h" | 13 #include "webkit/browser/fileapi/file_system_url.h" |
14 | 14 |
15 namespace chromeos { | 15 namespace chromeos { |
16 namespace file_system_provider { | 16 namespace file_system_provider { |
17 | 17 |
18 struct EntryMetadata; | 18 struct EntryMetadata; |
19 class ProvidedFileSystemInterface; | 19 class ProvidedFileSystemInterface; |
20 | 20 |
21 // Implements a streamed file reader. It is lazily initialized by the first call | 21 // Implements a streamed file reader. It is lazily initialized by the first call |
22 // to Read(). | 22 // to Read(). |
23 class FileStreamReader : public webkit_blob::FileStreamReader { | 23 class FileStreamReader : public webkit_blob::FileStreamReader { |
24 public: | 24 public: |
25 typedef base::Callback< | |
26 void(base::WeakPtr<ProvidedFileSystemInterface> file_system, | |
27 const base::FilePath& file_path, | |
28 int file_handle, | |
29 base::File::Error result)> OpenFileCompletedCallback; | |
30 | |
31 FileStreamReader(fileapi::FileSystemContext* context, | 25 FileStreamReader(fileapi::FileSystemContext* context, |
32 const fileapi::FileSystemURL& url, | 26 const fileapi::FileSystemURL& url, |
33 int64 initial_offset, | 27 int64 initial_offset, |
34 const base::Time& expected_modification_time); | 28 const base::Time& expected_modification_time); |
35 | 29 |
36 virtual ~FileStreamReader(); | 30 virtual ~FileStreamReader(); |
37 | 31 |
38 // webkit_blob::FileStreamReader overrides. | 32 // webkit_blob::FileStreamReader overrides. |
39 virtual int Read(net::IOBuffer* buf, | 33 virtual int Read(net::IOBuffer* buf, |
40 int buf_len, | 34 int buf_len, |
41 const net::CompletionCallback& callback) OVERRIDE; | 35 const net::CompletionCallback& callback) OVERRIDE; |
42 virtual int64 GetLength( | 36 virtual int64 GetLength( |
43 const net::Int64CompletionCallback& callback) OVERRIDE; | 37 const net::Int64CompletionCallback& callback) OVERRIDE; |
44 | 38 |
45 private: | 39 private: |
| 40 // Helper class for executing operations on the provided file system. All |
| 41 // of its methods must be called on UI thread. Callbacks are called on IO |
| 42 // thread. |
| 43 class OperationRunner; |
| 44 |
46 // State of the file stream reader. | 45 // State of the file stream reader. |
47 enum State { NOT_INITIALIZED, INITIALIZING, INITIALIZED, FAILED }; | 46 enum State { NOT_INITIALIZED, INITIALIZING, INITIALIZED, FAILED }; |
48 | 47 |
49 // Called when Read() operation is completed with either a success of an | 48 // Called when Read() operation is completed with either a success of an |
50 // error. | 49 // error. |
51 void OnReadCompleted(net::CompletionCallback callback, int result); | 50 void OnReadCompleted(net::CompletionCallback callback, int result); |
52 | 51 |
53 // Initializes the reader by opening the file. When completed with success, | 52 // Initializes the reader by opening the file. When completed with success, |
54 // runs the |pending_closure|. Otherwise, calls the |error_callback|. | 53 // runs the |pending_closure|. Otherwise, calls the |error_callback|. |
55 void Initialize(const base::Closure& pending_closure, | 54 void Initialize(const base::Closure& pending_closure, |
56 const net::Int64CompletionCallback& error_callback); | 55 const net::Int64CompletionCallback& error_callback); |
57 | 56 |
58 // Called when opening a file is completed with either a success or an error. | 57 // Called when opening a file is completed with either a success or an error. |
59 void OnOpenFileCompleted( | 58 void OnOpenFileCompleted( |
60 const base::Closure& pending_closure, | 59 const base::Closure& pending_closure, |
61 const net::Int64CompletionCallback& error_callback, | 60 const net::Int64CompletionCallback& error_callback, |
62 base::WeakPtr<ProvidedFileSystemInterface> file_system, | |
63 const base::FilePath& file_path, | |
64 int file_handle, | |
65 base::File::Error result); | 61 base::File::Error result); |
66 | 62 |
67 // Called when initialization is completed with either a success or an error. | 63 // Called when initialization is completed with either a success or an error. |
68 void OnInitializeCompleted(const base::Closure& pending_closure, | 64 void OnInitializeCompleted(const base::Closure& pending_closure, |
69 const net::Int64CompletionCallback& error_callback, | 65 const net::Int64CompletionCallback& error_callback, |
70 const EntryMetadata& metadata, | 66 const EntryMetadata& metadata, |
71 base::File::Error result); | 67 base::File::Error result); |
72 | 68 |
73 // Called when a file system provider returns chunk of read data. Note, that | 69 // Called when a file system provider returns chunk of read data. Note, that |
74 // this may be called multiple times per single Read() call, as long as | 70 // this may be called multiple times per single Read() call, as long as |
(...skipping 16 matching lines...) Expand all Loading... |
91 int buffer_length, | 87 int buffer_length, |
92 const net::CompletionCallback& callback); | 88 const net::CompletionCallback& callback); |
93 | 89 |
94 // Same as GetLength(), but called after initializing is completed. | 90 // Same as GetLength(), but called after initializing is completed. |
95 void GetLengthAfterInitialized(const net::Int64CompletionCallback& callback); | 91 void GetLengthAfterInitialized(const net::Int64CompletionCallback& callback); |
96 | 92 |
97 fileapi::FileSystemURL url_; | 93 fileapi::FileSystemURL url_; |
98 int64 current_offset_; | 94 int64 current_offset_; |
99 int64 current_length_; | 95 int64 current_length_; |
100 base::Time expected_modification_time_; | 96 base::Time expected_modification_time_; |
| 97 scoped_refptr<OperationRunner> runner_; |
101 State state_; | 98 State state_; |
102 | 99 |
103 // Set during initialization (in case of a success). | |
104 base::WeakPtr<ProvidedFileSystemInterface> file_system_; | |
105 base::FilePath file_path_; | |
106 int file_handle_; | |
107 | |
108 base::WeakPtrFactory<FileStreamReader> weak_ptr_factory_; | 100 base::WeakPtrFactory<FileStreamReader> weak_ptr_factory_; |
109 DISALLOW_COPY_AND_ASSIGN(FileStreamReader); | 101 DISALLOW_COPY_AND_ASSIGN(FileStreamReader); |
110 }; | 102 }; |
111 | 103 |
112 } // namespace file_system_provider | 104 } // namespace file_system_provider |
113 } // namespace chromeos | 105 } // namespace chromeos |
114 | 106 |
115 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READ
ER_H_ | 107 #endif // CHROME_BROWSER_CHROMEOS_FILE_SYSTEM_PROVIDER_FILEAPI_FILE_STREAM_READ
ER_H_ |
OLD | NEW |