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 |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 void SeekAsync(Whence whence, | 109 void SeekAsync(Whence whence, |
110 int64 offset, | 110 int64 offset, |
111 const Int64CompletionCallback& callback); | 111 const Int64CompletionCallback& callback); |
112 int64 SeekSync(Whence whence, int64 offset); | 112 int64 SeekSync(Whence whence, int64 offset); |
113 | 113 |
114 void FlushAsync(const CompletionCallback& callback); | 114 void FlushAsync(const CompletionCallback& callback); |
115 int FlushSync(); | 115 int FlushSync(); |
116 | 116 |
117 private: | 117 private: |
118 //////////////////////////////////////////////////////////////////////////// | 118 //////////////////////////////////////////////////////////////////////////// |
119 // Error code that is platform-dependent but is used in the platform- | |
120 // independent code implemented in file_stream_context.cc. | |
121 //////////////////////////////////////////////////////////////////////////// | |
122 enum { | |
123 #if defined(OS_WIN) | |
124 ERROR_BAD_FILE = ERROR_INVALID_HANDLE | |
125 #elif defined(OS_POSIX) | |
126 ERROR_BAD_FILE = EBADF | |
127 #endif | |
128 }; | |
129 | |
130 //////////////////////////////////////////////////////////////////////////// | |
131 // Platform-independent methods implemented in file_stream_context.cc. | 119 // Platform-independent methods implemented in file_stream_context.cc. |
132 //////////////////////////////////////////////////////////////////////////// | 120 //////////////////////////////////////////////////////////////////////////// |
133 | 121 |
134 struct OpenResult { | 122 struct IOResult { |
135 base::PlatformFile file; | 123 IOResult(); |
136 int error_code; | 124 IOResult(int64 result, int os_error); |
| 125 static IOResult FromOSError(int64 os_error); |
| 126 |
| 127 int64 result; |
| 128 int os_error; // Set only when result < 0. |
137 }; | 129 }; |
138 | 130 |
139 // Map system error into network error code and log it with |bound_net_log_|. | 131 struct OpenResult { |
140 int RecordAndMapError(int error, FileErrorSource source) const; | 132 OpenResult(); |
| 133 OpenResult(base::PlatformFile file, IOResult error_code); |
| 134 base::PlatformFile file; |
| 135 IOResult error_code; |
| 136 }; |
| 137 |
| 138 // Log the error from |result| to |bound_net_log_|. |
| 139 void RecordError(const IOResult& result, FileErrorSource source) const; |
141 | 140 |
142 void BeginOpenEvent(const base::FilePath& path); | 141 void BeginOpenEvent(const base::FilePath& path); |
143 | 142 |
144 OpenResult OpenFileImpl(const base::FilePath& path, int open_flags); | 143 OpenResult OpenFileImpl(const base::FilePath& path, int open_flags); |
145 | 144 |
146 int ProcessOpenError(int error_code); | 145 void ProcessOpenError(const IOResult& result); |
147 void OnOpenCompleted(const CompletionCallback& callback, OpenResult result); | 146 void OnOpenCompleted(const CompletionCallback& callback, OpenResult result); |
148 | 147 |
149 void CloseAndDelete(); | 148 void CloseAndDelete(); |
150 void OnCloseCompleted(); | 149 void OnCloseCompleted(); |
151 | 150 |
152 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); | 151 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); |
153 | 152 |
154 // Checks for IO error that probably happened in async methods. | |
155 // If there was error reports it. | |
156 void CheckForIOError(int64* result, FileErrorSource source); | |
157 | |
158 // Called when asynchronous Seek() is completed. | 153 // Called when asynchronous Seek() is completed. |
159 // Reports error if needed and calls callback. | 154 // Reports error if needed and calls callback. |
160 void ProcessAsyncResult(const Int64CompletionCallback& callback, | 155 void ProcessAsyncResult(const Int64CompletionCallback& callback, |
161 FileErrorSource source, | 156 FileErrorSource source, |
162 int64 result); | 157 const IOResult& result); |
163 | 158 |
164 // Called when asynchronous Open() or Seek() | 159 // Called when asynchronous Open() or Seek() |
165 // is completed. |result| contains the result or a network error code. | 160 // is completed. |result| contains the result or a network error code. |
166 void OnAsyncCompleted(const Int64CompletionCallback& callback, int64 result); | 161 void OnAsyncCompleted(const Int64CompletionCallback& callback, int64 result); |
167 | 162 |
168 //////////////////////////////////////////////////////////////////////////// | 163 //////////////////////////////////////////////////////////////////////////// |
169 // Helper stuff which is platform-dependent but is used in the platform- | 164 // Helper stuff which is platform-dependent but is used in the platform- |
170 // independent code implemented in file_stream_context.cc. These helpers were | 165 // independent code implemented in file_stream_context.cc. These helpers were |
171 // introduced solely to implement as much of the Context methods as | 166 // introduced solely to implement as much of the Context methods as |
172 // possible independently from platform. | 167 // possible independently from platform. |
173 //////////////////////////////////////////////////////////////////////////// | 168 //////////////////////////////////////////////////////////////////////////// |
174 | 169 |
175 #if defined(OS_WIN) | 170 #if defined(OS_WIN) |
176 int GetLastErrno() { return GetLastError(); } | 171 int GetLastErrno() { return GetLastError(); } |
177 void OnAsyncFileOpened(); | 172 void OnAsyncFileOpened(); |
178 #elif defined(OS_POSIX) | 173 #elif defined(OS_POSIX) |
179 int GetLastErrno() { return errno; } | 174 int GetLastErrno() { return errno; } |
180 void OnAsyncFileOpened() {} | 175 void OnAsyncFileOpened() {} |
181 void CancelIo(base::PlatformFile) {} | 176 void CancelIo(base::PlatformFile) {} |
182 #endif | 177 #endif |
183 | 178 |
184 //////////////////////////////////////////////////////////////////////////// | 179 //////////////////////////////////////////////////////////////////////////// |
185 // Platform-dependent methods implemented in | 180 // Platform-dependent methods implemented in |
186 // file_stream_context_{win,posix}.cc. | 181 // file_stream_context_{win,posix}.cc. |
187 //////////////////////////////////////////////////////////////////////////// | 182 //////////////////////////////////////////////////////////////////////////// |
188 | 183 |
189 // Adjusts the position from where the data is read. | 184 // Adjusts the position from where the data is read. |
190 int64 SeekFileImpl(Whence whence, int64 offset); | 185 IOResult SeekFileImpl(Whence whence, int64 offset); |
191 | 186 |
192 // Flushes all data written to the stream. | 187 // Flushes all data written to the stream. |
193 int64 FlushFileImpl(); | 188 IOResult FlushFileImpl(); |
194 | 189 |
195 #if defined(OS_WIN) | 190 #if defined(OS_WIN) |
196 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); | 191 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); |
197 | 192 |
198 // Implementation of MessageLoopForIO::IOHandler | 193 // Implementation of MessageLoopForIO::IOHandler. |
199 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 194 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
200 DWORD bytes_read, | 195 DWORD bytes_read, |
201 DWORD error) OVERRIDE; | 196 DWORD error) OVERRIDE; |
202 #elif defined(OS_POSIX) | 197 #elif defined(OS_POSIX) |
203 // ReadFileImpl() is a simple wrapper around read() that handles EINTR | 198 // ReadFileImpl() is a simple wrapper around read() that handles EINTR |
204 // signals and calls RecordAndMapError() to map errno to net error codes. | 199 // signals and calls RecordAndMapError() to map errno to net error codes. |
205 int64 ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | 200 IOResult ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
206 | 201 |
207 // WriteFileImpl() is a simple wrapper around write() that handles EINTR | 202 // WriteFileImpl() is a simple wrapper around write() that handles EINTR |
208 // signals and calls MapSystemError() to map errno to net error codes. | 203 // signals and calls MapSystemError() to map errno to net error codes. |
209 // It tries to write to completion. | 204 // It tries to write to completion. |
210 int64 WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | 205 IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
211 #endif | 206 #endif |
212 | 207 |
213 base::PlatformFile file_; | 208 base::PlatformFile file_; |
214 bool record_uma_; | 209 bool record_uma_; |
215 bool async_in_progress_; | 210 bool async_in_progress_; |
216 bool orphaned_; | 211 bool orphaned_; |
217 BoundNetLog bound_net_log_; | 212 BoundNetLog bound_net_log_; |
218 | 213 |
219 #if defined(OS_WIN) | 214 #if defined(OS_WIN) |
220 MessageLoopForIO::IOContext io_context_; | 215 MessageLoopForIO::IOContext io_context_; |
221 CompletionCallback callback_; | 216 CompletionCallback callback_; |
222 scoped_refptr<IOBuffer> in_flight_buf_; | 217 scoped_refptr<IOBuffer> in_flight_buf_; |
223 FileErrorSource error_source_; | 218 FileErrorSource error_source_; |
224 #endif | 219 #endif |
225 | 220 |
226 DISALLOW_COPY_AND_ASSIGN(Context); | 221 DISALLOW_COPY_AND_ASSIGN(Context); |
227 }; | 222 }; |
228 | 223 |
229 } // namespace net | 224 } // namespace net |
230 | 225 |
231 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ | 226 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ |
232 | 227 |
OLD | NEW |