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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 void OnFileOpened(); | 154 void OnFileOpened(); |
155 | 155 |
156 #if defined(OS_WIN) | 156 #if defined(OS_WIN) |
157 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); | 157 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); |
158 | 158 |
159 // Implementation of MessageLoopForIO::IOHandler. | 159 // Implementation of MessageLoopForIO::IOHandler. |
160 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, | 160 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, |
161 DWORD bytes_read, | 161 DWORD bytes_read, |
162 DWORD error) override; | 162 DWORD error) override; |
163 | 163 |
| 164 // Invokes the user callback. |
| 165 void InvokeUserCallback(); |
| 166 |
164 // The ReadFile call on Windows can execute synchonously at times. | 167 // The ReadFile call on Windows can execute synchonously at times. |
165 // http://support.microsoft.com/kb/156932. This ends up blocking the calling | 168 // 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 | 169 // thread which is undesirable. To avoid this we execute the ReadFile call |
167 // on a worker thread. | 170 // on a worker thread. |
168 // The |context| parameter is a weak pointer instance passed to the worker | 171 // The |context| parameter is a pointer to the current Context instance. It |
169 // pool. | 172 // is safe to pass this as is to the pool as the Context instance should |
| 173 // remain valid until the pending Read operation completes. |
170 // The |file| parameter is the handle to the file being read. | 174 // 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 | 175 // The |buf| parameter is the buffer where we want the ReadFile to read the |
172 // data into. | 176 // data into. |
173 // The |buf_len| parameter contains the number of bytes to be read. | 177 // The |buf_len| parameter contains the number of bytes to be read. |
174 // The |overlapped| parameter is a pointer to the OVERLAPPED structure being | 178 // The |overlapped| parameter is a pointer to the OVERLAPPED structure being |
175 // used. | 179 // used. |
176 // The |origin_thread_loop| is a MessageLoopProxy instance used to post tasks | 180 // The |origin_thread_loop| is a MessageLoopProxy instance used to post tasks |
177 // back to the originating thread. | 181 // back to the originating thread. |
178 static void ReadAsync( | 182 static void ReadAsync( |
179 const base::WeakPtr<FileStream::Context>& context, | 183 FileStream::Context* context, |
180 HANDLE file, | 184 HANDLE file, |
181 scoped_refptr<net::IOBuffer> buf, | 185 scoped_refptr<net::IOBuffer> buf, |
182 int buf_len, | 186 int buf_len, |
183 OVERLAPPED* overlapped, | 187 OVERLAPPED* overlapped, |
184 scoped_refptr<base::MessageLoopProxy> origin_thread_loop); | 188 scoped_refptr<base::MessageLoopProxy> origin_thread_loop); |
185 | 189 |
186 // This callback executes on the main calling thread. It informs the caller | 190 // This callback executes on the main calling thread. It informs the caller |
187 // about the result of the ReadFile call. | 191 // about the result of the ReadFile call. |
| 192 // The |bytes_read| contains the number of bytes read from the file, if |
| 193 // ReadFile succeeds. |
188 // The |os_error| parameter contains the value of the last error returned by | 194 // The |os_error| parameter contains the value of the last error returned by |
189 // the ReadFile API. | 195 // the ReadFile API. |
190 void ReadAsyncResult(DWORD os_error); | 196 void ReadAsyncResult(DWORD bytes_read, DWORD os_error); |
191 | 197 |
192 #elif defined(OS_POSIX) | 198 #elif defined(OS_POSIX) |
193 // ReadFileImpl() is a simple wrapper around read() that handles EINTR | 199 // ReadFileImpl() is a simple wrapper around read() that handles EINTR |
194 // signals and calls RecordAndMapError() to map errno to net error codes. | 200 // signals and calls RecordAndMapError() to map errno to net error codes. |
195 IOResult ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | 201 IOResult ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
196 | 202 |
197 // WriteFileImpl() is a simple wrapper around write() that handles EINTR | 203 // WriteFileImpl() is a simple wrapper around write() that handles EINTR |
198 // signals and calls MapSystemError() to map errno to net error codes. | 204 // signals and calls MapSystemError() to map errno to net error codes. |
199 // It tries to write to completion. | 205 // It tries to write to completion. |
200 IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | 206 IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
201 #endif | 207 #endif |
202 | 208 |
203 base::File file_; | 209 base::File file_; |
204 bool async_in_progress_; | 210 bool async_in_progress_; |
205 bool orphaned_; | 211 bool orphaned_; |
206 scoped_refptr<base::TaskRunner> task_runner_; | 212 scoped_refptr<base::TaskRunner> task_runner_; |
207 | 213 |
208 #if defined(OS_WIN) | 214 #if defined(OS_WIN) |
209 base::MessageLoopForIO::IOContext io_context_; | 215 base::MessageLoopForIO::IOContext io_context_; |
210 CompletionCallback callback_; | 216 CompletionCallback callback_; |
211 scoped_refptr<IOBuffer> in_flight_buf_; | 217 scoped_refptr<IOBuffer> in_flight_buf_; |
212 // WeakPtrFactory for posting tasks back to |this|. | 218 // This flag is set to true when we receive a Read request which is queued to |
213 base::WeakPtrFactory<Context> weak_ptr_factory_; | 219 // the thread pool. |
| 220 bool async_read_initiated_; |
| 221 // This flag is set to true when we receive a notification ReadAsyncResult() |
| 222 // on the calling thread which indicates that the asynchronous Read |
| 223 // operation is complete. |
| 224 bool async_read_completed_; |
| 225 // This flag is set to true when we receive an IO completion notification for |
| 226 // an asynchonously initiated Read operaton. OnIOComplete(). |
| 227 bool io_complete_for_read_received_; |
| 228 // Tracks the result of the IO completion operation. Set in OnIOComplete. |
| 229 int result_; |
214 #endif | 230 #endif |
215 | 231 |
216 DISALLOW_COPY_AND_ASSIGN(Context); | 232 DISALLOW_COPY_AND_ASSIGN(Context); |
217 }; | 233 }; |
218 | 234 |
219 } // namespace net | 235 } // namespace net |
220 | 236 |
221 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ | 237 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ |
OLD | NEW |