[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/base/file_stream_context.h" |
| 6 | |
| 7 | #include <windows.h> |
dcheng | 70c4942 | 2016-03-02 23:20:34 | [diff] [blame] | 8 | #include <utility> |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 9 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 11 | #include "base/location.h" |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 12 | #include "base/logging.h" |
gab | f64a25e | 2017-05-12 19:42:56 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame^] | 14 | #include "base/message_loop/message_pump_for_io.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 15 | #include "base/single_thread_task_runner.h" |
[email protected] | 866534a9 | 2014-05-07 03:39:15 | [diff] [blame] | 16 | #include "base/task_runner.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 17 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 18 | #include "net/base/io_buffer.h" |
| 19 | #include "net/base/net_errors.h" |
| 20 | |
| 21 | namespace net { |
| 22 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 23 | namespace { |
| 24 | |
| 25 | void SetOffset(OVERLAPPED* overlapped, const LARGE_INTEGER& offset) { |
| 26 | overlapped->Offset = offset.LowPart; |
| 27 | overlapped->OffsetHigh = offset.HighPart; |
| 28 | } |
| 29 | |
| 30 | void IncrementOffset(OVERLAPPED* overlapped, DWORD count) { |
| 31 | LARGE_INTEGER offset; |
| 32 | offset.LowPart = overlapped->Offset; |
| 33 | offset.HighPart = overlapped->OffsetHigh; |
| 34 | offset.QuadPart += static_cast<LONGLONG>(count); |
| 35 | SetOffset(overlapped, offset); |
| 36 | } |
| 37 | |
| 38 | } // namespace |
| 39 | |
[email protected] | 619a0398c | 2014-04-23 08:28:45 | [diff] [blame] | 40 | FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner) |
sammc | 5648c85 | 2015-07-02 01:25:00 | [diff] [blame] | 41 | : async_in_progress_(false), |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 42 | orphaned_(false), |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 43 | task_runner_(task_runner), |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 44 | async_read_initiated_(false), |
| 45 | async_read_completed_(false), |
| 46 | io_complete_for_read_received_(false), |
fdoray | 324a072 | 2016-05-11 20:00:35 | [diff] [blame] | 47 | result_(0) {} |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 48 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 49 | FileStream::Context::Context(base::File file, |
[email protected] | 53aafaed | 2013-06-17 19:16:32 | [diff] [blame] | 50 | const scoped_refptr<base::TaskRunner>& task_runner) |
dcheng | 70c4942 | 2016-03-02 23:20:34 | [diff] [blame] | 51 | : file_(std::move(file)), |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 52 | async_in_progress_(false), |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 53 | orphaned_(false), |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 54 | task_runner_(task_runner), |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 55 | async_read_initiated_(false), |
| 56 | async_read_completed_(false), |
| 57 | io_complete_for_read_received_(false), |
| 58 | result_(0) { |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 59 | if (file_.IsValid()) { |
reillyg | aa435e7 | 2015-06-16 00:48:48 | [diff] [blame] | 60 | DCHECK(file_.async()); |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 61 | OnFileOpened(); |
[email protected] | 50f91af | 2014-04-09 17:28:56 | [diff] [blame] | 62 | } |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | FileStream::Context::~Context() { |
| 66 | } |
| 67 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 68 | int FileStream::Context::Read(IOBuffer* buf, |
| 69 | int buf_len, |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 70 | CompletionOnceCallback callback) { |
Eric Roman | 89e0df13 | 2018-03-29 21:52:40 | [diff] [blame] | 71 | DCHECK(!async_in_progress_); |
xunjieli | cf470d5 | 2016-10-18 22:10:16 | [diff] [blame] | 72 | |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 73 | DCHECK(!async_read_initiated_); |
| 74 | DCHECK(!async_read_completed_); |
| 75 | DCHECK(!io_complete_for_read_received_); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 76 | |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 77 | IOCompletionIsPending(std::move(callback), buf); |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 78 | |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 79 | async_read_initiated_ = true; |
ananta | 47f234d3 | 2015-02-14 02:10:55 | [diff] [blame] | 80 | result_ = 0; |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 81 | |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 82 | task_runner_->PostTask( |
| 83 | FROM_HERE, |
| 84 | base::Bind(&FileStream::Context::ReadAsync, base::Unretained(this), |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 85 | file_.GetPlatformFile(), base::WrapRefCounted(buf), buf_len, |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 86 | &io_context_.overlapped, base::ThreadTaskRunnerHandle::Get())); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 87 | return ERR_IO_PENDING; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 90 | int FileStream::Context::Write(IOBuffer* buf, |
| 91 | int buf_len, |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 92 | CompletionOnceCallback callback) { |
Eric Roman | 89e0df13 | 2018-03-29 21:52:40 | [diff] [blame] | 93 | DCHECK(!async_in_progress_); |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 94 | |
ananta | 47f234d3 | 2015-02-14 02:10:55 | [diff] [blame] | 95 | result_ = 0; |
| 96 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 97 | DWORD bytes_written = 0; |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 98 | if (!WriteFile(file_.GetPlatformFile(), buf->data(), buf_len, |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 99 | &bytes_written, &io_context_.overlapped)) { |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 100 | IOResult error = IOResult::FromOSError(GetLastError()); |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 101 | if (error.os_error == ERROR_IO_PENDING) { |
| 102 | IOCompletionIsPending(std::move(callback), buf); |
| 103 | } else { |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 104 | LOG(WARNING) << "WriteFile failed: " << error.os_error; |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 105 | } |
Peter Kasting | be940e9 | 2014-11-20 23:14:08 | [diff] [blame] | 106 | return static_cast<int>(error.result); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 107 | } |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 108 | |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 109 | IOCompletionIsPending(std::move(callback), buf); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 110 | return ERR_IO_PENDING; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 113 | FileStream::Context::IOResult FileStream::Context::SeekFileImpl( |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 114 | int64_t offset) { |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 115 | LARGE_INTEGER result; |
reillyg | aa435e7 | 2015-06-16 00:48:48 | [diff] [blame] | 116 | result.QuadPart = offset; |
| 117 | SetOffset(&io_context_.overlapped, result); |
| 118 | return IOResult(result.QuadPart, 0); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 119 | } |
| 120 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 121 | void FileStream::Context::OnFileOpened() { |
| 122 | base::MessageLoopForIO::current()->RegisterIOHandler(file_.GetPlatformFile(), |
| 123 | this); |
[email protected] | c1d9cf74 | 2013-09-12 06:37:19 | [diff] [blame] | 124 | } |
| 125 | |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 126 | void FileStream::Context::IOCompletionIsPending(CompletionOnceCallback callback, |
| 127 | IOBuffer* buf) { |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 128 | DCHECK(callback_.is_null()); |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 129 | callback_ = std::move(callback); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 130 | in_flight_buf_ = buf; // Hold until the async operation ends. |
| 131 | async_in_progress_ = true; |
| 132 | } |
| 133 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 134 | void FileStream::Context::OnIOCompleted( |
Gabriel Charette | 19d2ae6 | 2018-04-10 14:10:58 | [diff] [blame^] | 135 | base::MessagePumpForIO::IOContext* context, |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 136 | DWORD bytes_read, |
| 137 | DWORD error) { |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 138 | DCHECK_EQ(&io_context_, context); |
| 139 | DCHECK(!callback_.is_null()); |
| 140 | DCHECK(async_in_progress_); |
| 141 | |
Eric Roman | 89e0df13 | 2018-03-29 21:52:40 | [diff] [blame] | 142 | if (!async_read_initiated_) |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 143 | async_in_progress_ = false; |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 144 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 145 | if (orphaned_) { |
ananta | 47f234d3 | 2015-02-14 02:10:55 | [diff] [blame] | 146 | io_complete_for_read_received_ = true; |
| 147 | // If we are called due to a pending read and the asynchronous read task |
| 148 | // has not completed we have to keep the context around until it completes. |
| 149 | if (async_read_initiated_ && !async_read_completed_) |
| 150 | return; |
| 151 | DeleteOrphanedContext(); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 155 | if (error == ERROR_HANDLE_EOF) { |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 156 | result_ = 0; |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 157 | } else if (error) { |
| 158 | IOResult error_result = IOResult::FromOSError(error); |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 159 | result_ = static_cast<int>(error_result.result); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 160 | } else { |
ananta | 47f234d3 | 2015-02-14 02:10:55 | [diff] [blame] | 161 | if (result_) |
| 162 | DCHECK_EQ(result_, static_cast<int>(bytes_read)); |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 163 | result_ = bytes_read; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 164 | IncrementOffset(&io_context_.overlapped, bytes_read); |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 165 | } |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 166 | |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 167 | if (async_read_initiated_) |
| 168 | io_complete_for_read_received_ = true; |
| 169 | |
| 170 | InvokeUserCallback(); |
| 171 | } |
| 172 | |
| 173 | void FileStream::Context::InvokeUserCallback() { |
| 174 | // For an asynchonous Read operation don't invoke the user callback until |
| 175 | // we receive the IO completion notification and the asynchronous Read |
| 176 | // completion notification. |
| 177 | if (async_read_initiated_) { |
| 178 | if (!io_complete_for_read_received_ || !async_read_completed_) |
| 179 | return; |
| 180 | async_read_initiated_ = false; |
| 181 | io_complete_for_read_received_ = false; |
| 182 | async_read_completed_ = false; |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 183 | async_in_progress_ = false; |
| 184 | } |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 185 | scoped_refptr<IOBuffer> temp_buf = in_flight_buf_; |
| 186 | in_flight_buf_ = NULL; |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 187 | std::move(callback_).Run(result_); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 188 | } |
| 189 | |
ananta | 47f234d3 | 2015-02-14 02:10:55 | [diff] [blame] | 190 | void FileStream::Context::DeleteOrphanedContext() { |
ananta | 47f234d3 | 2015-02-14 02:10:55 | [diff] [blame] | 191 | async_in_progress_ = false; |
| 192 | callback_.Reset(); |
| 193 | in_flight_buf_ = NULL; |
| 194 | CloseAndDelete(); |
| 195 | } |
| 196 | |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 197 | // static |
| 198 | void FileStream::Context::ReadAsync( |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 199 | FileStream::Context* context, |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 200 | HANDLE file, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 201 | scoped_refptr<IOBuffer> buf, |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 202 | int buf_len, |
| 203 | OVERLAPPED* overlapped, |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 204 | scoped_refptr<base::SingleThreadTaskRunner> origin_thread_task_runner) { |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 205 | DWORD bytes_read = 0; |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 206 | BOOL ret = ::ReadFile(file, buf->data(), buf_len, &bytes_read, overlapped); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 207 | origin_thread_task_runner->PostTask( |
ananta | 47f234d3 | 2015-02-14 02:10:55 | [diff] [blame] | 208 | FROM_HERE, |
| 209 | base::Bind(&FileStream::Context::ReadAsyncResult, |
| 210 | base::Unretained(context), ret, bytes_read, ::GetLastError())); |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 211 | } |
| 212 | |
ananta | 47f234d3 | 2015-02-14 02:10:55 | [diff] [blame] | 213 | void FileStream::Context::ReadAsyncResult(BOOL read_file_ret, |
| 214 | DWORD bytes_read, |
| 215 | DWORD os_error) { |
| 216 | // If the context is orphaned and we already received the io completion |
| 217 | // notification then we should delete the context and get out. |
| 218 | if (orphaned_ && io_complete_for_read_received_) { |
| 219 | DeleteOrphanedContext(); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | async_read_completed_ = true; |
| 224 | if (read_file_ret) { |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 225 | result_ = bytes_read; |
ananta | 47f234d3 | 2015-02-14 02:10:55 | [diff] [blame] | 226 | InvokeUserCallback(); |
| 227 | return; |
| 228 | } |
ananta | 806016e | 2015-02-06 20:30:07 | [diff] [blame] | 229 | |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 230 | IOResult error = IOResult::FromOSError(os_error); |
ananta | 47f234d3 | 2015-02-14 02:10:55 | [diff] [blame] | 231 | if (error.os_error == ERROR_IO_PENDING) { |
| 232 | InvokeUserCallback(); |
| 233 | } else { |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 234 | OnIOCompleted(&io_context_, 0, error.os_error); |
ananta | 959eaea | 2015-02-03 19:50:50 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 238 | } // namespace net |