Chromium Code Reviews| 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 #include "net/base/file_stream_context.h" | 5 #include "net/base/file_stream_context.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 IncrementOffset(&io_context_.overlapped, bytes_read); | 167 IncrementOffset(&io_context_.overlapped, bytes_read); |
| 168 } | 168 } |
| 169 | 169 |
| 170 if (async_read_initiated_) | 170 if (async_read_initiated_) |
| 171 io_complete_for_read_received_ = true; | 171 io_complete_for_read_received_ = true; |
| 172 | 172 |
| 173 InvokeUserCallback(); | 173 InvokeUserCallback(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 void FileStream::Context::InvokeUserCallback() { | 176 void FileStream::Context::InvokeUserCallback() { |
| 177 if (callback_.is_null()) | |
| 178 return; | |
| 177 // For an asynchonous Read operation don't invoke the user callback until | 179 // For an asynchonous Read operation don't invoke the user callback until |
| 178 // we receive the IO completion notification and the asynchronous Read | 180 // we receive the IO completion notification and the asynchronous Read |
| 179 // completion notification. | 181 // completion notification. |
| 180 if (async_read_initiated_) { | 182 if (async_read_initiated_) { |
| 181 if (!io_complete_for_read_received_ || !async_read_completed_) | 183 if (!io_complete_for_read_received_ || !async_read_completed_) |
| 182 return; | 184 return; |
| 183 async_read_initiated_ = false; | 185 async_read_initiated_ = false; |
| 184 io_complete_for_read_received_ = false; | 186 io_complete_for_read_received_ = false; |
| 185 async_read_completed_ = false; | 187 async_read_completed_ = false; |
| 186 async_in_progress_ = false; | 188 async_in_progress_ = false; |
| 187 } | 189 } |
| 188 CompletionCallback temp_callback = callback_; | 190 CompletionCallback temp_callback = callback_; |
| 189 callback_.Reset(); | 191 callback_.Reset(); |
| 190 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_; | 192 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_; |
| 191 in_flight_buf_ = NULL; | 193 in_flight_buf_ = NULL; |
| 192 temp_callback.Run(result_); | 194 temp_callback.Run(result_); |
| 193 } | 195 } |
| 194 | 196 |
| 195 // static | 197 // static |
| 196 void FileStream::Context::ReadAsync( | 198 void FileStream::Context::ReadAsync( |
| 197 FileStream::Context* context, | 199 FileStream::Context* context, |
| 198 HANDLE file, | 200 HANDLE file, |
| 199 scoped_refptr<net::IOBuffer> buf, | 201 scoped_refptr<net::IOBuffer> buf, |
| 200 int buf_len, | 202 int buf_len, |
| 201 OVERLAPPED* overlapped, | 203 OVERLAPPED* overlapped, |
| 202 scoped_refptr<base::MessageLoopProxy> origin_thread_loop) { | 204 scoped_refptr<base::MessageLoopProxy> origin_thread_loop) { |
| 203 DWORD bytes_read = 0; | 205 DWORD bytes_read = 0; |
| 204 BOOL ret = ::ReadFile(file, buf->data(), buf_len, &bytes_read, overlapped); | 206 BOOL ret = ::ReadFile(file, buf->data(), buf_len, &bytes_read, overlapped); |
| 205 origin_thread_loop->PostTask( | 207 origin_thread_loop->PostTask( |
| 206 FROM_HERE, base::Bind(&FileStream::Context::ReadAsyncResult, | 208 FROM_HERE, base::Bind(&FileStream::Context::ReadAsyncResult, |
|
rvargas (doing something else)
2015/02/12 22:24:54
ret = TRUE + a completion callback is the expected
ananta
2015/02/12 22:47:59
Done.
| |
| 207 base::Unretained(context), ret ? bytes_read : 0, | 209 base::Unretained(context), ret ? bytes_read : 0, |
| 208 ret ? 0 : ::GetLastError())); | 210 ret ? 0 : ::GetLastError())); |
| 209 } | 211 } |
| 210 | 212 |
| 211 void FileStream::Context::ReadAsyncResult(DWORD bytes_read, DWORD os_error) { | 213 void FileStream::Context::ReadAsyncResult(DWORD bytes_read, DWORD os_error) { |
| 212 if (!os_error) | 214 if (!os_error) |
| 213 result_ = bytes_read; | 215 result_ = bytes_read; |
| 214 | 216 |
| 215 IOResult error = IOResult::FromOSError(os_error); | 217 IOResult error = IOResult::FromOSError(os_error); |
| 216 if (error.os_error == ERROR_HANDLE_EOF) { | 218 if (error.os_error == ERROR_HANDLE_EOF) { |
| 217 // Report EOF by returning 0 bytes read. | 219 // Report EOF by returning 0 bytes read. |
| 218 OnIOCompleted(&io_context_, 0, error.os_error); | 220 OnIOCompleted(&io_context_, 0, error.os_error); |
| 219 } else if (error.os_error != ERROR_IO_PENDING) { | 221 } else if (error.os_error != ERROR_IO_PENDING) { |
| 220 // We don't need to inform the caller about ERROR_PENDING_IO as that was | 222 // We don't need to inform the caller about ERROR_PENDING_IO as that was |
| 221 // already done when the ReadFile call was queued to the worker pool. | 223 // already done when the ReadFile call was queued to the worker pool. |
| 222 if (error.os_error) { | 224 if (error.os_error) { |
| 223 LOG(WARNING) << "ReadFile failed: " << error.os_error; | 225 LOG(WARNING) << "ReadFile failed: " << error.os_error; |
| 224 OnIOCompleted(&io_context_, 0, error.os_error); | 226 OnIOCompleted(&io_context_, 0, error.os_error); |
| 225 } | 227 } |
| 226 } | 228 } |
| 227 async_read_completed_ = true; | 229 async_read_completed_ = true; |
| 228 InvokeUserCallback(); | 230 InvokeUserCallback(); |
| 229 } | 231 } |
| 230 | 232 |
| 231 } // namespace net | 233 } // namespace net |
| OLD | NEW |