| 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 15 matching lines...) Expand all Loading... |
| 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/files/file.h" |
| 31 #include "base/message_loop/message_loop.h" | 31 #include "base/message_loop/message_loop.h" |
| 32 #include "base/move.h" | 32 #include "base/move.h" |
| 33 #include "base/task_runner.h" | 33 #include "base/task_runner.h" |
| 34 #include "net/base/completion_callback.h" | 34 #include "net/base/completion_callback.h" |
| 35 #include "net/base/file_stream.h" | 35 #include "net/base/file_stream.h" |
| 36 #include "net/base/file_stream_whence.h" | |
| 37 | 36 |
| 38 #if defined(OS_POSIX) | 37 #if defined(OS_POSIX) |
| 39 #include <errno.h> | 38 #include <errno.h> |
| 40 #endif | 39 #endif |
| 41 | 40 |
| 42 namespace base { | 41 namespace base { |
| 43 class FilePath; | 42 class FilePath; |
| 44 } | 43 } |
| 45 | 44 |
| 46 namespace net { | 45 namespace net { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 59 //////////////////////////////////////////////////////////////////////////// | 58 //////////////////////////////////////////////////////////////////////////// |
| 60 | 59 |
| 61 explicit Context(const scoped_refptr<base::TaskRunner>& task_runner); | 60 explicit Context(const scoped_refptr<base::TaskRunner>& task_runner); |
| 62 Context(base::File file, const scoped_refptr<base::TaskRunner>& task_runner); | 61 Context(base::File file, const scoped_refptr<base::TaskRunner>& task_runner); |
| 63 #if defined(OS_WIN) | 62 #if defined(OS_WIN) |
| 64 virtual ~Context(); | 63 virtual ~Context(); |
| 65 #elif defined(OS_POSIX) | 64 #elif defined(OS_POSIX) |
| 66 ~Context(); | 65 ~Context(); |
| 67 #endif | 66 #endif |
| 68 | 67 |
| 69 int ReadAsync(IOBuffer* buf, | 68 int Read(IOBuffer* buf, |
| 70 int buf_len, | 69 int buf_len, |
| 71 const CompletionCallback& callback); | 70 const CompletionCallback& callback); |
| 72 | 71 |
| 73 int WriteAsync(IOBuffer* buf, | 72 int Write(IOBuffer* buf, |
| 74 int buf_len, | 73 int buf_len, |
| 75 const CompletionCallback& callback); | 74 const CompletionCallback& callback); |
| 76 | |
| 77 //////////////////////////////////////////////////////////////////////////// | |
| 78 // Inline methods. | |
| 79 //////////////////////////////////////////////////////////////////////////// | |
| 80 | 75 |
| 81 const base::File& file() const { return file_; } | 76 const base::File& file() const { return file_; } |
| 82 bool async_in_progress() const { return async_in_progress_; } | 77 bool async_in_progress() const { return async_in_progress_; } |
| 83 | 78 |
| 84 //////////////////////////////////////////////////////////////////////////// | 79 //////////////////////////////////////////////////////////////////////////// |
| 85 // Platform-independent methods implemented in file_stream_context.cc. | 80 // Platform-independent methods implemented in file_stream_context.cc. |
| 86 //////////////////////////////////////////////////////////////////////////// | 81 //////////////////////////////////////////////////////////////////////////// |
| 87 | 82 |
| 88 // Destroys the context. It can be deleted in the method or deletion can be | 83 // Destroys the context. It can be deleted in the method or deletion can be |
| 89 // deferred if some asynchronous operation is now in progress or if file is | 84 // deferred if some asynchronous operation is now in progress or if file is |
| 90 // not closed yet. | 85 // not closed yet. |
| 91 void Orphan(); | 86 void Orphan(); |
| 92 | 87 |
| 93 void OpenAsync(const base::FilePath& path, | 88 void Open(const base::FilePath& path, |
| 94 int open_flags, | 89 int open_flags, |
| 95 const CompletionCallback& callback); | 90 const CompletionCallback& callback); |
| 96 | 91 |
| 97 void CloseAsync(const CompletionCallback& callback); | 92 void Close(const CompletionCallback& callback); |
| 98 | 93 |
| 99 void SeekAsync(Whence whence, | 94 void Seek(base::File::Whence whence, |
| 100 int64 offset, | 95 int64 offset, |
| 101 const Int64CompletionCallback& callback); | 96 const Int64CompletionCallback& callback); |
| 102 | 97 |
| 103 void FlushAsync(const CompletionCallback& callback); | 98 void Flush(const CompletionCallback& callback); |
| 104 | 99 |
| 105 private: | 100 private: |
| 106 //////////////////////////////////////////////////////////////////////////// | |
| 107 // Platform-independent methods implemented in file_stream_context.cc. | |
| 108 //////////////////////////////////////////////////////////////////////////// | |
| 109 | |
| 110 struct IOResult { | 101 struct IOResult { |
| 111 IOResult(); | 102 IOResult(); |
| 112 IOResult(int64 result, int os_error); | 103 IOResult(int64 result, int os_error); |
| 113 static IOResult FromOSError(int64 os_error); | 104 static IOResult FromOSError(int64 os_error); |
| 114 | 105 |
| 115 int64 result; | 106 int64 result; |
| 116 int os_error; // Set only when result < 0. | 107 int os_error; // Set only when result < 0. |
| 117 }; | 108 }; |
| 118 | 109 |
| 119 struct OpenResult { | 110 struct OpenResult { |
| 120 MOVE_ONLY_TYPE_FOR_CPP_03(OpenResult, RValue) | 111 MOVE_ONLY_TYPE_FOR_CPP_03(OpenResult, RValue) |
| 121 public: | 112 public: |
| 122 OpenResult(); | 113 OpenResult(); |
| 123 OpenResult(base::File file, IOResult error_code); | 114 OpenResult(base::File file, IOResult error_code); |
| 124 // C++03 move emulation of this type. | 115 // C++03 move emulation of this type. |
| 125 OpenResult(RValue other); | 116 OpenResult(RValue other); |
| 126 OpenResult& operator=(RValue other); | 117 OpenResult& operator=(RValue other); |
| 127 | 118 |
| 128 base::File file; | 119 base::File file; |
| 129 IOResult error_code; | 120 IOResult error_code; |
| 130 }; | 121 }; |
| 131 | 122 |
| 123 //////////////////////////////////////////////////////////////////////////// |
| 124 // Platform-independent methods implemented in file_stream_context.cc. |
| 125 //////////////////////////////////////////////////////////////////////////// |
| 126 |
| 132 OpenResult OpenFileImpl(const base::FilePath& path, int open_flags); | 127 OpenResult OpenFileImpl(const base::FilePath& path, int open_flags); |
| 133 | 128 |
| 134 IOResult CloseFileImpl(); | 129 IOResult CloseFileImpl(); |
| 135 | 130 |
| 131 IOResult FlushFileImpl(); |
| 132 |
| 136 void OnOpenCompleted(const CompletionCallback& callback, | 133 void OnOpenCompleted(const CompletionCallback& callback, |
| 137 OpenResult open_result); | 134 OpenResult open_result); |
| 138 | 135 |
| 139 void CloseAndDelete(); | 136 void CloseAndDelete(); |
| 140 | 137 |
| 141 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); | 138 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); |
| 142 | 139 |
| 143 // Called when asynchronous Open() or Seek() | 140 // Called when Open() or Seek() completes. |result| contains the result or a |
| 144 // is completed. |result| contains the result or a network error code. | 141 // network error code. |
| 145 void OnAsyncCompleted(const Int64CompletionCallback& callback, | 142 void OnAsyncCompleted(const Int64CompletionCallback& callback, |
| 146 const IOResult& result); | 143 const IOResult& result); |
| 147 | 144 |
| 148 //////////////////////////////////////////////////////////////////////////// | 145 //////////////////////////////////////////////////////////////////////////// |
| 149 // Helper stuff which is platform-dependent but is used in the platform- | |
| 150 // independent code implemented in file_stream_context.cc. These helpers were | |
| 151 // introduced solely to implement as much of the Context methods as | |
| 152 // possible independently from platform. | |
| 153 //////////////////////////////////////////////////////////////////////////// | |
| 154 | |
| 155 #if defined(OS_WIN) | |
| 156 int GetLastErrno() { return GetLastError(); } | |
| 157 void OnAsyncFileOpened(); | |
| 158 #elif defined(OS_POSIX) | |
| 159 int GetLastErrno() { return errno; } | |
| 160 void OnAsyncFileOpened() {} | |
| 161 void CancelIo(base::PlatformFile) {} | |
| 162 #endif | |
| 163 | |
| 164 //////////////////////////////////////////////////////////////////////////// | |
| 165 // Platform-dependent methods implemented in | 146 // Platform-dependent methods implemented in |
| 166 // file_stream_context_{win,posix}.cc. | 147 // file_stream_context_{win,posix}.cc. |
| 167 //////////////////////////////////////////////////////////////////////////// | 148 //////////////////////////////////////////////////////////////////////////// |
| 168 | 149 |
| 169 // Adjusts the position from where the data is read. | 150 // Adjusts the position from where the data is read. |
| 170 IOResult SeekFileImpl(Whence whence, int64 offset); | 151 IOResult SeekFileImpl(base::File::Whence whence, int64 offset); |
| 171 | 152 |
| 172 // Flushes all data written to the stream. | 153 void OnFileOpened(); |
| 173 IOResult FlushFileImpl(); | |
| 174 | 154 |
| 175 #if defined(OS_WIN) | 155 #if defined(OS_WIN) |
| 176 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); | 156 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); |
| 177 | 157 |
| 178 // Implementation of MessageLoopForIO::IOHandler. | 158 // Implementation of MessageLoopForIO::IOHandler. |
| 179 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, | 159 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, |
| 180 DWORD bytes_read, | 160 DWORD bytes_read, |
| 181 DWORD error) OVERRIDE; | 161 DWORD error) OVERRIDE; |
| 182 #elif defined(OS_POSIX) | 162 #elif defined(OS_POSIX) |
| 183 // ReadFileImpl() is a simple wrapper around read() that handles EINTR | 163 // ReadFileImpl() is a simple wrapper around read() that handles EINTR |
| (...skipping 16 matching lines...) Expand all Loading... |
| 200 CompletionCallback callback_; | 180 CompletionCallback callback_; |
| 201 scoped_refptr<IOBuffer> in_flight_buf_; | 181 scoped_refptr<IOBuffer> in_flight_buf_; |
| 202 #endif | 182 #endif |
| 203 | 183 |
| 204 DISALLOW_COPY_AND_ASSIGN(Context); | 184 DISALLOW_COPY_AND_ASSIGN(Context); |
| 205 }; | 185 }; |
| 206 | 186 |
| 207 } // namespace net | 187 } // namespace net |
| 208 | 188 |
| 209 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ | 189 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ |
| OLD | NEW |