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