| 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 10 matching lines...) Expand all Loading... |
| 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/files/file.h" |
| 31 #include "base/memory/weak_ptr.h" |
| 31 #include "base/message_loop/message_loop.h" | 32 #include "base/message_loop/message_loop.h" |
| 32 #include "base/move.h" | 33 #include "base/move.h" |
| 33 #include "base/task_runner.h" | 34 #include "base/task_runner.h" |
| 34 #include "net/base/completion_callback.h" | 35 #include "net/base/completion_callback.h" |
| 35 #include "net/base/file_stream.h" | 36 #include "net/base/file_stream.h" |
| 36 | 37 |
| 37 #if defined(OS_POSIX) | 38 #if defined(OS_POSIX) |
| 38 #include <errno.h> | 39 #include <errno.h> |
| 39 #endif | 40 #endif |
| 40 | 41 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 | 153 |
| 153 void OnFileOpened(); | 154 void OnFileOpened(); |
| 154 | 155 |
| 155 #if defined(OS_WIN) | 156 #if defined(OS_WIN) |
| 156 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); | 157 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); |
| 157 | 158 |
| 158 // Implementation of MessageLoopForIO::IOHandler. | 159 // Implementation of MessageLoopForIO::IOHandler. |
| 159 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, | 160 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, |
| 160 DWORD bytes_read, | 161 DWORD bytes_read, |
| 161 DWORD error) override; | 162 DWORD error) override; |
| 163 |
| 164 // The ReadFile call on Windows can execute synchonously at times. |
| 165 // http://support.microsoft.com/kb/156932. This ends up blocking the calling |
| 166 // thread which is undesirable. To avoid this we execute the ReadFile call |
| 167 // on a worker thread. |
| 168 // The |context| parameter is a weak pointer instance passed to the worker |
| 169 // pool. |
| 170 // The |file| parameter is the handle to the file being read. |
| 171 // The |buf| parameter is the buffer where we want the ReadFile to read the |
| 172 // data into. |
| 173 // The |buf_len| parameter contains the number of bytes to be read. |
| 174 // The |overlapped| parameter is a pointer to the OVERLAPPED structure being |
| 175 // used. |
| 176 // The |origin_thread_loop| is a MessageLoopProxy instance used to post tasks |
| 177 // back to the originating thread. |
| 178 static void ReadAsync( |
| 179 const base::WeakPtr<FileStream::Context>& context, |
| 180 HANDLE file, |
| 181 scoped_refptr<net::IOBuffer> buf, |
| 182 int buf_len, |
| 183 OVERLAPPED* overlapped, |
| 184 scoped_refptr<base::MessageLoopProxy> origin_thread_loop); |
| 185 |
| 186 // This callback executes on the main calling thread. It informs the caller |
| 187 // about the result of the ReadFile call. |
| 188 // The |os_error| parameter contains the value of the last error returned by |
| 189 // the ReadFile API. |
| 190 void ReadAsyncResult(DWORD os_error); |
| 191 |
| 162 #elif defined(OS_POSIX) | 192 #elif defined(OS_POSIX) |
| 163 // ReadFileImpl() is a simple wrapper around read() that handles EINTR | 193 // ReadFileImpl() is a simple wrapper around read() that handles EINTR |
| 164 // signals and calls RecordAndMapError() to map errno to net error codes. | 194 // signals and calls RecordAndMapError() to map errno to net error codes. |
| 165 IOResult ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | 195 IOResult ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
| 166 | 196 |
| 167 // WriteFileImpl() is a simple wrapper around write() that handles EINTR | 197 // WriteFileImpl() is a simple wrapper around write() that handles EINTR |
| 168 // signals and calls MapSystemError() to map errno to net error codes. | 198 // signals and calls MapSystemError() to map errno to net error codes. |
| 169 // It tries to write to completion. | 199 // It tries to write to completion. |
| 170 IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | 200 IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
| 171 #endif | 201 #endif |
| 172 | 202 |
| 173 base::File file_; | 203 base::File file_; |
| 174 bool async_in_progress_; | 204 bool async_in_progress_; |
| 175 bool orphaned_; | 205 bool orphaned_; |
| 176 scoped_refptr<base::TaskRunner> task_runner_; | 206 scoped_refptr<base::TaskRunner> task_runner_; |
| 177 | 207 |
| 178 #if defined(OS_WIN) | 208 #if defined(OS_WIN) |
| 179 base::MessageLoopForIO::IOContext io_context_; | 209 base::MessageLoopForIO::IOContext io_context_; |
| 180 CompletionCallback callback_; | 210 CompletionCallback callback_; |
| 181 scoped_refptr<IOBuffer> in_flight_buf_; | 211 scoped_refptr<IOBuffer> in_flight_buf_; |
| 212 // WeakPtrFactory for posting tasks back to |this|. |
| 213 base::WeakPtrFactory<Context> weak_ptr_factory_; |
| 182 #endif | 214 #endif |
| 183 | 215 |
| 184 DISALLOW_COPY_AND_ASSIGN(Context); | 216 DISALLOW_COPY_AND_ASSIGN(Context); |
| 185 }; | 217 }; |
| 186 | 218 |
| 187 } // namespace net | 219 } // namespace net |
| 188 | 220 |
| 189 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ | 221 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ |
| OLD | NEW |