blob: 9c8a2a483f464be948f80031c08da30307344fe3 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
[email protected]aab1b9e2012-11-06 00:29:512// 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>
dcheng70c49422016-03-02 23:20:348#include <utility>
[email protected]aab1b9e2012-11-06 00:29:519
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
Avi Drissman41c4a412023-01-11 22:45:3711#include "base/functional/bind.h"
skyostil4891b25b2015-06-11 11:43:4512#include "base/location.h"
[email protected]aab1b9e2012-11-06 00:29:5113#include "base/logging.h"
Gabriel Charette19d2ae62018-04-10 14:10:5814#include "base/message_loop/message_pump_for_io.h"
Carlos Caballerob25fe8472020-07-17 10:27:1715#include "base/task/current_thread.h"
Patrick Monette643cdf62021-10-15 19:13:4216#include "base/task/single_thread_task_runner.h"
17#include "base/task/task_runner.h"
[email protected]aab1b9e2012-11-06 00:29:5118#include "net/base/io_buffer.h"
19#include "net/base/net_errors.h"
20
21namespace net {
22
[email protected]aab1b9e2012-11-06 00:29:5123namespace {
24
25void SetOffset(OVERLAPPED* overlapped, const LARGE_INTEGER& offset) {
26 overlapped->Offset = offset.LowPart;
27 overlapped->OffsetHigh = offset.HighPart;
28}
29
30void 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 Charette96d5c642020-04-09 20:31:1040FileStream::Context::Context(scoped_refptr<base::TaskRunner> task_runner)
41 : Context(base::File(), std::move(task_runner)) {}
[email protected]aab1b9e2012-11-06 00:29:5142
[email protected]a08305912014-03-21 00:41:1543FileStream::Context::Context(base::File file,
Gabriel Charette96d5c642020-04-09 20:31:1044 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]50f91af2014-04-09 17:28:5648 if (file_.IsValid()) {
reillygaa435e72015-06-16 00:48:4849 DCHECK(file_.async());
[email protected]633ff3b12014-06-20 23:30:1850 OnFileOpened();
[email protected]50f91af2014-04-09 17:28:5651 }
[email protected]aab1b9e2012-11-06 00:29:5152}
53
Tsuyoshi Horo07c3f0e2022-06-16 07:30:4754FileStream::Context::~Context() = default;
[email protected]aab1b9e2012-11-06 00:29:5155
[email protected]633ff3b12014-06-20 23:30:1856int FileStream::Context::Read(IOBuffer* buf,
57 int buf_len,
Matt Menkedadd6c72018-01-30 23:07:2558 CompletionOnceCallback callback) {
Eric Roman89e0df132018-03-29 21:52:4059 DCHECK(!async_in_progress_);
xunjielicf470d52016-10-18 22:10:1660
ananta806016e2015-02-06 20:30:0761 DCHECK(!async_read_initiated_);
62 DCHECK(!async_read_completed_);
63 DCHECK(!io_complete_for_read_received_);
[email protected]aab1b9e2012-11-06 00:29:5164
Matt Menkedadd6c72018-01-30 23:07:2565 IOCompletionIsPending(std::move(callback), buf);
ananta959eaea2015-02-03 19:50:5066
ananta806016e2015-02-06 20:30:0767 async_read_initiated_ = true;
ananta47f234d32015-02-14 02:10:5568 result_ = 0;
ananta959eaea2015-02-03 19:50:5069
ananta806016e2015-02-06 20:30:0770 task_runner_->PostTask(
71 FROM_HERE,
kylecharf4fe5172019-02-15 18:53:4972 base::BindOnce(&FileStream::Context::ReadAsync, base::Unretained(this),
73 file_.GetPlatformFile(), base::WrapRefCounted(buf),
74 buf_len, &io_context_.overlapped,
Sean Maher5b9af51f2022-11-21 15:32:4775 base::SingleThreadTaskRunner::GetCurrentDefault()));
[email protected]bfb88ec2013-02-27 20:21:3576 return ERR_IO_PENDING;
[email protected]aab1b9e2012-11-06 00:29:5177}
78
[email protected]633ff3b12014-06-20 23:30:1879int FileStream::Context::Write(IOBuffer* buf,
80 int buf_len,
Matt Menkedadd6c72018-01-30 23:07:2581 CompletionOnceCallback callback) {
Eric Roman89e0df132018-03-29 21:52:4082 DCHECK(!async_in_progress_);
ananta806016e2015-02-06 20:30:0783
ananta47f234d32015-02-14 02:10:5584 result_ = 0;
85
[email protected]aab1b9e2012-11-06 00:29:5186 DWORD bytes_written = 0;
[email protected]a08305912014-03-21 00:41:1587 if (!WriteFile(file_.GetPlatformFile(), buf->data(), buf_len,
[email protected]aab1b9e2012-11-06 00:29:5188 &bytes_written, &io_context_.overlapped)) {
[email protected]bfb88ec2013-02-27 20:21:3589 IOResult error = IOResult::FromOSError(GetLastError());
Matt Menkedadd6c72018-01-30 23:07:2590 if (error.os_error == ERROR_IO_PENDING) {
91 IOCompletionIsPending(std::move(callback), buf);
92 } else {
[email protected]bfb88ec2013-02-27 20:21:3593 LOG(WARNING) << "WriteFile failed: " << error.os_error;
Matt Menkedadd6c72018-01-30 23:07:2594 }
Peter Kastingbe940e92014-11-20 23:14:0895 return static_cast<int>(error.result);
[email protected]aab1b9e2012-11-06 00:29:5196 }
[email protected]bfb88ec2013-02-27 20:21:3597
Matt Menkedadd6c72018-01-30 23:07:2598 IOCompletionIsPending(std::move(callback), buf);
[email protected]bfb88ec2013-02-27 20:21:3599 return ERR_IO_PENDING;
[email protected]aab1b9e2012-11-06 00:29:51100}
101
[email protected]633ff3b12014-06-20 23:30:18102FileStream::Context::IOResult FileStream::Context::SeekFileImpl(
wtc69f8ea82015-06-04 00:08:13103 int64_t offset) {
[email protected]633ff3b12014-06-20 23:30:18104 LARGE_INTEGER result;
reillygaa435e72015-06-16 00:48:48105 result.QuadPart = offset;
106 SetOffset(&io_context_.overlapped, result);
107 return IOResult(result.QuadPart, 0);
[email protected]aab1b9e2012-11-06 00:29:51108}
109
[email protected]633ff3b12014-06-20 23:30:18110void FileStream::Context::OnFileOpened() {
Carlos Caballerob25fe8472020-07-17 10:27:17111 HRESULT hr = base::CurrentIOThread::Get()->RegisterIOHandler(
Gabriel Charette1e5bed442018-04-24 23:25:49112 file_.GetPlatformFile(), this);
Robbie McElrathaddae862018-06-19 19:40:35113 if (!SUCCEEDED(hr))
114 file_.Close();
[email protected]c1d9cf742013-09-12 06:37:19115}
116
Matt Menkedadd6c72018-01-30 23:07:25117void FileStream::Context::IOCompletionIsPending(CompletionOnceCallback callback,
118 IOBuffer* buf) {
[email protected]aab1b9e2012-11-06 00:29:51119 DCHECK(callback_.is_null());
Matt Menkedadd6c72018-01-30 23:07:25120 callback_ = std::move(callback);
[email protected]aab1b9e2012-11-06 00:29:51121 in_flight_buf_ = buf; // Hold until the async operation ends.
122 async_in_progress_ = true;
123}
124
[email protected]2da659e2013-05-23 20:51:34125void FileStream::Context::OnIOCompleted(
Gabriel Charette19d2ae62018-04-10 14:10:58126 base::MessagePumpForIO::IOContext* context,
[email protected]2da659e2013-05-23 20:51:34127 DWORD bytes_read,
128 DWORD error) {
[email protected]aab1b9e2012-11-06 00:29:51129 DCHECK_EQ(&io_context_, context);
130 DCHECK(!callback_.is_null());
131 DCHECK(async_in_progress_);
132
Eric Roman89e0df132018-03-29 21:52:40133 if (!async_read_initiated_)
ananta806016e2015-02-06 20:30:07134 async_in_progress_ = false;
135
[email protected]aab1b9e2012-11-06 00:29:51136 if (orphaned_) {
ananta47f234d32015-02-14 02:10:55137 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]aab1b9e2012-11-06 00:29:51143 return;
144 }
145
[email protected]bfb88ec2013-02-27 20:21:35146 if (error == ERROR_HANDLE_EOF) {
ananta806016e2015-02-06 20:30:07147 result_ = 0;
[email protected]bfb88ec2013-02-27 20:21:35148 } else if (error) {
149 IOResult error_result = IOResult::FromOSError(error);
ananta806016e2015-02-06 20:30:07150 result_ = static_cast<int>(error_result.result);
[email protected]bfb88ec2013-02-27 20:21:35151 } else {
ananta47f234d32015-02-14 02:10:55152 if (result_)
153 DCHECK_EQ(result_, static_cast<int>(bytes_read));
ananta806016e2015-02-06 20:30:07154 result_ = bytes_read;
[email protected]aab1b9e2012-11-06 00:29:51155 IncrementOffset(&io_context_.overlapped, bytes_read);
[email protected]bfb88ec2013-02-27 20:21:35156 }
[email protected]aab1b9e2012-11-06 00:29:51157
ananta806016e2015-02-06 20:30:07158 if (async_read_initiated_)
159 io_complete_for_read_received_ = true;
160
161 InvokeUserCallback();
162}
163
164void 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]aab1b9e2012-11-06 00:29:51176 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_;
Raul Tambre94493c652019-03-11 17:18:35177 in_flight_buf_ = nullptr;
Matt Menkedadd6c72018-01-30 23:07:25178 std::move(callback_).Run(result_);
[email protected]aab1b9e2012-11-06 00:29:51179}
180
ananta47f234d32015-02-14 02:10:55181void FileStream::Context::DeleteOrphanedContext() {
182 async_in_progress_ = false;
183 callback_.Reset();
Raul Tambre94493c652019-03-11 17:18:35184 in_flight_buf_ = nullptr;
ananta47f234d32015-02-14 02:10:55185 CloseAndDelete();
186}
187
ananta959eaea2015-02-03 19:50:50188// static
189void FileStream::Context::ReadAsync(
ananta806016e2015-02-06 20:30:07190 FileStream::Context* context,
ananta959eaea2015-02-03 19:50:50191 HANDLE file,
ttuttle859dc7a2015-04-23 19:42:29192 scoped_refptr<IOBuffer> buf,
ananta959eaea2015-02-03 19:50:50193 int buf_len,
194 OVERLAPPED* overlapped,
skyostil4891b25b2015-06-11 11:43:45195 scoped_refptr<base::SingleThreadTaskRunner> origin_thread_task_runner) {
ananta959eaea2015-02-03 19:50:50196 DWORD bytes_read = 0;
ananta806016e2015-02-06 20:30:07197 BOOL ret = ::ReadFile(file, buf->data(), buf_len, &bytes_read, overlapped);
skyostil4891b25b2015-06-11 11:43:45198 origin_thread_task_runner->PostTask(
kylecharf4fe5172019-02-15 18:53:49199 FROM_HERE, base::BindOnce(&FileStream::Context::ReadAsyncResult,
200 base::Unretained(context), ret, bytes_read,
201 ::GetLastError()));
ananta959eaea2015-02-03 19:50:50202}
203
ananta47f234d32015-02-14 02:10:55204void 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) {
ananta806016e2015-02-06 20:30:07216 result_ = bytes_read;
ananta47f234d32015-02-14 02:10:55217 InvokeUserCallback();
218 return;
219 }
ananta806016e2015-02-06 20:30:07220
ananta959eaea2015-02-03 19:50:50221 IOResult error = IOResult::FromOSError(os_error);
ananta47f234d32015-02-14 02:10:55222 if (error.os_error == ERROR_IO_PENDING) {
223 InvokeUserCallback();
224 } else {
ananta959eaea2015-02-03 19:50:50225 OnIOCompleted(&io_context_, 0, error.os_error);
ananta959eaea2015-02-03 19:50:50226 }
227}
228
[email protected]aab1b9e2012-11-06 00:29:51229} // namespace net