OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file defines FileStream::Context class. | 5 // This file defines FileStream::Context class. |
6 // The general design of FileStream is as follows: file_stream.h defines | 6 // The general design of FileStream is as follows: file_stream.h defines |
7 // FileStream class which basically is just an "wrapper" not containing any | 7 // FileStream class which basically is just an "wrapper" not containing any |
8 // specific implementation details. It re-routes all its method calls to | 8 // specific implementation details. It re-routes all its method calls to |
9 // the instance of FileStream::Context (FileStream holds a scoped_ptr to | 9 // the instance of FileStream::Context (FileStream holds a scoped_ptr to |
10 // FileStream::Context instance). Context was extracted into a different class | 10 // FileStream::Context instance). Context was extracted into a different class |
11 // to be able to do and finish all async operations even when FileStream | 11 // to be able to do and finish all async operations even when FileStream |
12 // instance is deleted. So FileStream's destructor can schedule file | 12 // instance is deleted. So FileStream's destructor can schedule file |
13 // closing to be done by Context in WorkerPool (or the TaskRunner passed to | 13 // closing to be done by Context in WorkerPool (or the TaskRunner passed to |
14 // constructor) and then just return (releasing Context pointer from | 14 // constructor) and then just return (releasing Context pointer from |
15 // scoped_ptr) without waiting for actual closing to complete. | 15 // scoped_ptr) without waiting for actual closing to complete. |
16 // Implementation of FileStream::Context is divided in two parts: some methods | 16 // Implementation of FileStream::Context is divided in two parts: some methods |
17 // and members are platform-independent and some depend on the platform. This | 17 // and members are platform-independent and some depend on the platform. This |
18 // header file contains the complete definition of Context class including all | 18 // header file contains the complete definition of Context class including all |
19 // platform-dependent parts (because of that it has a lot of #if-#else | 19 // platform-dependent parts (because of that it has a lot of #if-#else |
20 // branching). Implementations of all platform-independent methods are | 20 // branching). Implementations of all platform-independent methods are |
21 // located in file_stream_context.cc, and all platform-dependent methods are | 21 // located in file_stream_context.cc, and all platform-dependent methods are |
22 // in file_stream_context_{win,posix}.cc. This separation provides better | 22 // in file_stream_context_{win,posix}.cc. This separation provides better |
23 // readability of Context's code. And we tried to make as much Context code | 23 // readability of Context's code. And we tried to make as much Context code |
24 // platform-independent as possible. So file_stream_context_{win,posix}.cc are | 24 // platform-independent as possible. So file_stream_context_{win,posix}.cc are |
25 // much smaller than file_stream_context.cc now. | 25 // much smaller than file_stream_context.cc now. |
26 | 26 |
27 #ifndef NET_BASE_FILE_STREAM_CONTEXT_H_ | 27 #ifndef NET_BASE_FILE_STREAM_CONTEXT_H_ |
28 #define NET_BASE_FILE_STREAM_CONTEXT_H_ | 28 #define NET_BASE_FILE_STREAM_CONTEXT_H_ |
29 | 29 |
| 30 #include "base/files/file.h" |
30 #include "base/message_loop/message_loop.h" | 31 #include "base/message_loop/message_loop.h" |
31 #include "base/platform_file.h" | 32 #include "base/move.h" |
32 #include "base/task_runner.h" | 33 #include "base/task_runner.h" |
33 #include "net/base/completion_callback.h" | 34 #include "net/base/completion_callback.h" |
34 #include "net/base/file_stream.h" | 35 #include "net/base/file_stream.h" |
35 #include "net/base/file_stream_metrics.h" | 36 #include "net/base/file_stream_metrics.h" |
36 #include "net/base/file_stream_whence.h" | 37 #include "net/base/file_stream_whence.h" |
37 #include "net/base/net_log.h" | 38 #include "net/base/net_log.h" |
38 | 39 |
39 #if defined(OS_POSIX) | 40 #if defined(OS_POSIX) |
40 #include <errno.h> | 41 #include <errno.h> |
41 #endif | 42 #endif |
(...skipping 12 matching lines...) Expand all Loading... |
54 class FileStream::Context { | 55 class FileStream::Context { |
55 #endif | 56 #endif |
56 public: | 57 public: |
57 //////////////////////////////////////////////////////////////////////////// | 58 //////////////////////////////////////////////////////////////////////////// |
58 // Platform-dependent methods implemented in | 59 // Platform-dependent methods implemented in |
59 // file_stream_context_{win,posix}.cc. | 60 // file_stream_context_{win,posix}.cc. |
60 //////////////////////////////////////////////////////////////////////////// | 61 //////////////////////////////////////////////////////////////////////////// |
61 | 62 |
62 Context(const BoundNetLog& bound_net_log, | 63 Context(const BoundNetLog& bound_net_log, |
63 const scoped_refptr<base::TaskRunner>& task_runner); | 64 const scoped_refptr<base::TaskRunner>& task_runner); |
64 Context(base::PlatformFile file, | 65 Context(base::File file, |
65 const BoundNetLog& bound_net_log, | 66 const BoundNetLog& bound_net_log, |
66 int open_flags, | 67 const scoped_refptr<base::TaskRunner>& task_runner); |
| 68 |
| 69 // This is a deprecated constructor intended only to support callers that |
| 70 // provide a PlatformFile. Such callers are to be updated to provide a File. |
| 71 Context(base::File file, |
| 72 int flags, |
| 73 const BoundNetLog& bound_net_log, |
67 const scoped_refptr<base::TaskRunner>& task_runner); | 74 const scoped_refptr<base::TaskRunner>& task_runner); |
68 #if defined(OS_WIN) | 75 #if defined(OS_WIN) |
69 virtual ~Context(); | 76 virtual ~Context(); |
70 #elif defined(OS_POSIX) | 77 #elif defined(OS_POSIX) |
71 ~Context(); | 78 ~Context(); |
72 #endif | 79 #endif |
73 | 80 |
74 int64 GetFileSize() const; | 81 int64 GetFileSize() const; |
75 | 82 |
76 int ReadAsync(IOBuffer* buf, | 83 int ReadAsync(IOBuffer* buf, |
77 int buf_len, | 84 int buf_len, |
78 const CompletionCallback& callback); | 85 const CompletionCallback& callback); |
79 int ReadSync(char* buf, int buf_len); | 86 int ReadSync(char* buf, int buf_len); |
80 | 87 |
81 int WriteAsync(IOBuffer* buf, | 88 int WriteAsync(IOBuffer* buf, |
82 int buf_len, | 89 int buf_len, |
83 const CompletionCallback& callback); | 90 const CompletionCallback& callback); |
84 int WriteSync(const char* buf, int buf_len); | 91 int WriteSync(const char* buf, int buf_len); |
85 | 92 |
86 int Truncate(int64 bytes); | 93 int Truncate(int64 bytes); |
87 | 94 |
88 //////////////////////////////////////////////////////////////////////////// | 95 //////////////////////////////////////////////////////////////////////////// |
89 // Inline methods. | 96 // Inline methods. |
90 //////////////////////////////////////////////////////////////////////////// | 97 //////////////////////////////////////////////////////////////////////////// |
91 | 98 |
92 void set_record_uma(bool value) { record_uma_ = value; } | 99 void set_record_uma(bool value) { record_uma_ = value; } |
93 base::PlatformFile file() const { return file_; } | 100 const base::File& file() const { return file_; } |
94 bool async_in_progress() const { return async_in_progress_; } | 101 bool async_in_progress() const { return async_in_progress_; } |
| 102 bool async() const { return async_in_progress_ || async_ || file_.async(); } |
95 | 103 |
96 //////////////////////////////////////////////////////////////////////////// | 104 //////////////////////////////////////////////////////////////////////////// |
97 // Platform-independent methods implemented in file_stream_context.cc. | 105 // Platform-independent methods implemented in file_stream_context.cc. |
98 //////////////////////////////////////////////////////////////////////////// | 106 //////////////////////////////////////////////////////////////////////////// |
99 | 107 |
100 // Destroys the context. It can be deleted in the method or deletion can be | 108 // Destroys the context. It can be deleted in the method or deletion can be |
101 // deferred if some asynchronous operation is now in progress or if file is | 109 // deferred if some asynchronous operation is now in progress or if file is |
102 // not closed yet. | 110 // not closed yet. |
103 void Orphan(); | 111 void Orphan(); |
104 | 112 |
(...skipping 22 matching lines...) Expand all Loading... |
127 struct IOResult { | 135 struct IOResult { |
128 IOResult(); | 136 IOResult(); |
129 IOResult(int64 result, int os_error); | 137 IOResult(int64 result, int os_error); |
130 static IOResult FromOSError(int64 os_error); | 138 static IOResult FromOSError(int64 os_error); |
131 | 139 |
132 int64 result; | 140 int64 result; |
133 int os_error; // Set only when result < 0. | 141 int os_error; // Set only when result < 0. |
134 }; | 142 }; |
135 | 143 |
136 struct OpenResult { | 144 struct OpenResult { |
| 145 MOVE_ONLY_TYPE_FOR_CPP_03(OpenResult, RValue) |
| 146 public: |
137 OpenResult(); | 147 OpenResult(); |
138 OpenResult(base::PlatformFile file, IOResult error_code); | 148 OpenResult(base::File file, IOResult error_code); |
139 base::PlatformFile file; | 149 // C++03 move emulation of this type. |
| 150 OpenResult(RValue other); |
| 151 OpenResult& operator=(RValue other); |
| 152 |
| 153 base::File file; |
140 IOResult error_code; | 154 IOResult error_code; |
141 }; | 155 }; |
142 | 156 |
143 // Log the error from |result| to |bound_net_log_|. | 157 // Log the error from |result| to |bound_net_log_|. |
144 void RecordError(const IOResult& result, FileErrorSource source) const; | 158 void RecordError(const IOResult& result, FileErrorSource source) const; |
145 | 159 |
146 void BeginOpenEvent(const base::FilePath& path); | 160 void BeginOpenEvent(const base::FilePath& path); |
147 | 161 |
148 OpenResult OpenFileImpl(const base::FilePath& path, int open_flags); | 162 OpenResult OpenFileImpl(const base::FilePath& path, int open_flags); |
149 | 163 |
| 164 IOResult CloseFileImpl(); |
| 165 |
150 void ProcessOpenError(const IOResult& result); | 166 void ProcessOpenError(const IOResult& result); |
151 void OnOpenCompleted(const CompletionCallback& callback, | 167 void OnOpenCompleted(const CompletionCallback& callback, |
152 OpenResult open_result); | 168 OpenResult open_result); |
153 | 169 |
154 void CloseAndDelete(); | 170 void CloseAndDelete(); |
155 void OnCloseCompleted(); | 171 void OnCloseCompleted(); |
156 | 172 |
157 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); | 173 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); |
158 | 174 |
159 // Called when asynchronous Seek() is completed. | 175 // Called when asynchronous Seek() is completed. |
(...skipping 26 matching lines...) Expand all Loading... |
186 // Platform-dependent methods implemented in | 202 // Platform-dependent methods implemented in |
187 // file_stream_context_{win,posix}.cc. | 203 // file_stream_context_{win,posix}.cc. |
188 //////////////////////////////////////////////////////////////////////////// | 204 //////////////////////////////////////////////////////////////////////////// |
189 | 205 |
190 // Adjusts the position from where the data is read. | 206 // Adjusts the position from where the data is read. |
191 IOResult SeekFileImpl(Whence whence, int64 offset); | 207 IOResult SeekFileImpl(Whence whence, int64 offset); |
192 | 208 |
193 // Flushes all data written to the stream. | 209 // Flushes all data written to the stream. |
194 IOResult FlushFileImpl(); | 210 IOResult FlushFileImpl(); |
195 | 211 |
196 // Closes the file. | |
197 IOResult CloseFileImpl(); | |
198 | |
199 #if defined(OS_WIN) | 212 #if defined(OS_WIN) |
200 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); | 213 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); |
201 | 214 |
202 // Implementation of MessageLoopForIO::IOHandler. | 215 // Implementation of MessageLoopForIO::IOHandler. |
203 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, | 216 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, |
204 DWORD bytes_read, | 217 DWORD bytes_read, |
205 DWORD error) OVERRIDE; | 218 DWORD error) OVERRIDE; |
206 #elif defined(OS_POSIX) | 219 #elif defined(OS_POSIX) |
207 // ReadFileImpl() is a simple wrapper around read() that handles EINTR | 220 // ReadFileImpl() is a simple wrapper around read() that handles EINTR |
208 // signals and calls RecordAndMapError() to map errno to net error codes. | 221 // signals and calls RecordAndMapError() to map errno to net error codes. |
209 IOResult ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | 222 IOResult ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
210 | 223 |
211 // WriteFileImpl() is a simple wrapper around write() that handles EINTR | 224 // WriteFileImpl() is a simple wrapper around write() that handles EINTR |
212 // signals and calls MapSystemError() to map errno to net error codes. | 225 // signals and calls MapSystemError() to map errno to net error codes. |
213 // It tries to write to completion. | 226 // It tries to write to completion. |
214 IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | 227 IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
215 #endif | 228 #endif |
216 | 229 |
217 base::PlatformFile file_; | 230 base::File file_; |
218 bool record_uma_; | 231 bool record_uma_; |
219 bool async_in_progress_; | 232 bool async_in_progress_; |
220 bool orphaned_; | 233 bool orphaned_; |
| 234 bool async_; // To be removed when flags are removed from the constructor. |
221 BoundNetLog bound_net_log_; | 235 BoundNetLog bound_net_log_; |
222 scoped_refptr<base::TaskRunner> task_runner_; | 236 scoped_refptr<base::TaskRunner> task_runner_; |
223 | 237 |
224 #if defined(OS_WIN) | 238 #if defined(OS_WIN) |
225 base::MessageLoopForIO::IOContext io_context_; | 239 base::MessageLoopForIO::IOContext io_context_; |
226 CompletionCallback callback_; | 240 CompletionCallback callback_; |
227 scoped_refptr<IOBuffer> in_flight_buf_; | 241 scoped_refptr<IOBuffer> in_flight_buf_; |
228 FileErrorSource error_source_; | 242 FileErrorSource error_source_; |
229 #endif | 243 #endif |
230 | 244 |
231 DISALLOW_COPY_AND_ASSIGN(Context); | 245 DISALLOW_COPY_AND_ASSIGN(Context); |
232 }; | 246 }; |
233 | 247 |
234 } // namespace net | 248 } // namespace net |
235 | 249 |
236 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ | 250 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ |
237 | 251 |
OLD | NEW |