| 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, |
| 147 OpenResult open_result); |
| 148 | 148 |
| 149 void CloseAndDelete(); | 149 void CloseAndDelete(); |
| 150 void OnCloseCompleted(); | 150 void OnCloseCompleted(); |
| 151 | 151 |
| 152 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); | 152 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); |
| 153 | 153 |
| 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. | 154 // Called when asynchronous Seek() is completed. |
| 159 // Reports error if needed and calls callback. | 155 // Reports error if needed and calls callback. |
| 160 void ProcessAsyncResult(const Int64CompletionCallback& callback, | 156 void ProcessAsyncResult(const Int64CompletionCallback& callback, |
| 161 FileErrorSource source, | 157 FileErrorSource source, |
| 162 int64 result); | 158 const IOResult& result); |
| 163 | 159 |
| 164 // Called when asynchronous Open() or Seek() | 160 // Called when asynchronous Open() or Seek() |
| 165 // is completed. |result| contains the result or a network error code. | 161 // is completed. |result| contains the result or a network error code. |
| 166 void OnAsyncCompleted(const Int64CompletionCallback& callback, int64 result); | 162 void OnAsyncCompleted(const Int64CompletionCallback& callback, int64 result); |
| 167 | 163 |
| 168 //////////////////////////////////////////////////////////////////////////// | 164 //////////////////////////////////////////////////////////////////////////// |
| 169 // Helper stuff which is platform-dependent but is used in the platform- | 165 // Helper stuff which is platform-dependent but is used in the platform- |
| 170 // independent code implemented in file_stream_context.cc. These helpers were | 166 // independent code implemented in file_stream_context.cc. These helpers were |
| 171 // introduced solely to implement as much of the Context methods as | 167 // introduced solely to implement as much of the Context methods as |
| 172 // possible independently from platform. | 168 // possible independently from platform. |
| 173 //////////////////////////////////////////////////////////////////////////// | 169 //////////////////////////////////////////////////////////////////////////// |
| 174 | 170 |
| 175 #if defined(OS_WIN) | 171 #if defined(OS_WIN) |
| 176 int GetLastErrno() { return GetLastError(); } | 172 int GetLastErrno() { return GetLastError(); } |
| 177 void OnAsyncFileOpened(); | 173 void OnAsyncFileOpened(); |
| 178 #elif defined(OS_POSIX) | 174 #elif defined(OS_POSIX) |
| 179 int GetLastErrno() { return errno; } | 175 int GetLastErrno() { return errno; } |
| 180 void OnAsyncFileOpened() {} | 176 void OnAsyncFileOpened() {} |
| 181 void CancelIo(base::PlatformFile) {} | 177 void CancelIo(base::PlatformFile) {} |
| 182 #endif | 178 #endif |
| 183 | 179 |
| 184 //////////////////////////////////////////////////////////////////////////// | 180 //////////////////////////////////////////////////////////////////////////// |
| 185 // Platform-dependent methods implemented in | 181 // Platform-dependent methods implemented in |
| 186 // file_stream_context_{win,posix}.cc. | 182 // file_stream_context_{win,posix}.cc. |
| 187 //////////////////////////////////////////////////////////////////////////// | 183 //////////////////////////////////////////////////////////////////////////// |
| 188 | 184 |
| 189 // Adjusts the position from where the data is read. | 185 // Adjusts the position from where the data is read. |
| 190 int64 SeekFileImpl(Whence whence, int64 offset); | 186 IOResult SeekFileImpl(Whence whence, int64 offset); |
| 191 | 187 |
| 192 // Flushes all data written to the stream. | 188 // Flushes all data written to the stream. |
| 193 int64 FlushFileImpl(); | 189 IOResult FlushFileImpl(); |
| 194 | 190 |
| 195 #if defined(OS_WIN) | 191 #if defined(OS_WIN) |
| 196 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); | 192 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); |
| 197 | 193 |
| 198 // Implementation of MessageLoopForIO::IOHandler | 194 // Implementation of MessageLoopForIO::IOHandler. |
| 199 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | 195 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
| 200 DWORD bytes_read, | 196 DWORD bytes_read, |
| 201 DWORD error) OVERRIDE; | 197 DWORD error) OVERRIDE; |
| 202 #elif defined(OS_POSIX) | 198 #elif defined(OS_POSIX) |
| 203 // ReadFileImpl() is a simple wrapper around read() that handles EINTR | 199 // ReadFileImpl() is a simple wrapper around read() that handles EINTR |
| 204 // signals and calls RecordAndMapError() to map errno to net error codes. | 200 // signals and calls RecordAndMapError() to map errno to net error codes. |
| 205 int64 ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | 201 IOResult ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
| 206 | 202 |
| 207 // WriteFileImpl() is a simple wrapper around write() that handles EINTR | 203 // WriteFileImpl() is a simple wrapper around write() that handles EINTR |
| 208 // signals and calls MapSystemError() to map errno to net error codes. | 204 // signals and calls MapSystemError() to map errno to net error codes. |
| 209 // It tries to write to completion. | 205 // It tries to write to completion. |
| 210 int64 WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | 206 IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
| 211 #endif | 207 #endif |
| 212 | 208 |
| 213 base::PlatformFile file_; | 209 base::PlatformFile file_; |
| 214 bool record_uma_; | 210 bool record_uma_; |
| 215 bool async_in_progress_; | 211 bool async_in_progress_; |
| 216 bool orphaned_; | 212 bool orphaned_; |
| 217 BoundNetLog bound_net_log_; | 213 BoundNetLog bound_net_log_; |
| 218 | 214 |
| 219 #if defined(OS_WIN) | 215 #if defined(OS_WIN) |
| 220 MessageLoopForIO::IOContext io_context_; | 216 MessageLoopForIO::IOContext io_context_; |
| 221 CompletionCallback callback_; | 217 CompletionCallback callback_; |
| 222 scoped_refptr<IOBuffer> in_flight_buf_; | 218 scoped_refptr<IOBuffer> in_flight_buf_; |
| 223 FileErrorSource error_source_; | 219 FileErrorSource error_source_; |
| 224 #endif | 220 #endif |
| 225 | 221 |
| 226 DISALLOW_COPY_AND_ASSIGN(Context); | 222 DISALLOW_COPY_AND_ASSIGN(Context); |
| 227 }; | 223 }; |
| 228 | 224 |
| 229 } // namespace net | 225 } // namespace net |
| 230 | 226 |
| 231 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ | 227 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ |
| 232 | 228 |
| OLD | NEW |