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