blob: 5bcd23518a01a9ec85f94c689248fed93c569110 [file] [log] [blame]
[email protected]aab1b9e2012-11-06 00:29:511// 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>
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"
skyostil4891b25b2015-06-11 11:43:4511#include "base/location.h"
[email protected]aab1b9e2012-11-06 00:29:5112#include "base/logging.h"
Gabriel Charette1e5bed442018-04-24 23:25:4913#include "base/message_loop/message_loop_current.h"
Gabriel Charette19d2ae62018-04-10 14:10:5814#include "base/message_loop/message_pump_for_io.h"
skyostil4891b25b2015-06-11 11:43:4515#include "base/single_thread_task_runner.h"
[email protected]866534a92014-05-07 03:39:1516#include "base/task_runner.h"
gabf767595f2016-05-11 18:50:3517#include "base/threading/thread_task_runner_handle.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
[email protected]619a0398c2014-04-23 08:28:4540FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner)
sammc5648c852015-07-02 01:25:0041 : async_in_progress_(false),
[email protected]aab1b9e2012-11-06 00:29:5142 orphaned_(false),
ananta959eaea2015-02-03 19:50:5043 task_runner_(task_runner),
ananta806016e2015-02-06 20:30:0744 async_read_initiated_(false),
45 async_read_completed_(false),
46 io_complete_for_read_received_(false),
fdoray324a0722016-05-11 20:00:3547 result_(0) {}
[email protected]aab1b9e2012-11-06 00:29:5148
[email protected]a08305912014-03-21 00:41:1549FileStream::Context::Context(base::File file,
[email protected]53aafaed2013-06-17 19:16:3250 const scoped_refptr<base::TaskRunner>& task_runner)
dcheng70c49422016-03-02 23:20:3451 : file_(std::move(file)),
[email protected]aab1b9e2012-11-06 00:29:5152 async_in_progress_(false),
[email protected]aab1b9e2012-11-06 00:29:5153 orphaned_(false),
ananta959eaea2015-02-03 19:50:5054 task_runner_(task_runner),
ananta806016e2015-02-06 20:30:0755 async_read_initiated_(false),
56 async_read_completed_(false),
57 io_complete_for_read_received_(false),
58 result_(0) {
[email protected]50f91af2014-04-09 17:28:5659 if (file_.IsValid()) {
reillygaa435e72015-06-16 00:48:4860 DCHECK(file_.async());
[email protected]633ff3b12014-06-20 23:30:1861 OnFileOpened();
[email protected]50f91af2014-04-09 17:28:5662 }
[email protected]aab1b9e2012-11-06 00:29:5163}
64
65FileStream::Context::~Context() {
66}
67
[email protected]633ff3b12014-06-20 23:30:1868int FileStream::Context::Read(IOBuffer* buf,
69 int buf_len,
Matt Menkedadd6c72018-01-30 23:07:2570 CompletionOnceCallback callback) {
Eric Roman89e0df132018-03-29 21:52:4071 DCHECK(!async_in_progress_);
xunjielicf470d52016-10-18 22:10:1672
ananta806016e2015-02-06 20:30:0773 DCHECK(!async_read_initiated_);
74 DCHECK(!async_read_completed_);
75 DCHECK(!io_complete_for_read_received_);
[email protected]aab1b9e2012-11-06 00:29:5176
Matt Menkedadd6c72018-01-30 23:07:2577 IOCompletionIsPending(std::move(callback), buf);
ananta959eaea2015-02-03 19:50:5078
ananta806016e2015-02-06 20:30:0779 async_read_initiated_ = true;
ananta47f234d32015-02-14 02:10:5580 result_ = 0;
ananta959eaea2015-02-03 19:50:5081
ananta806016e2015-02-06 20:30:0782 task_runner_->PostTask(
83 FROM_HERE,
84 base::Bind(&FileStream::Context::ReadAsync, base::Unretained(this),
kylecharda69d882017-10-04 05:49:5285 file_.GetPlatformFile(), base::WrapRefCounted(buf), buf_len,
skyostil4891b25b2015-06-11 11:43:4586 &io_context_.overlapped, base::ThreadTaskRunnerHandle::Get()));
[email protected]bfb88ec2013-02-27 20:21:3587 return ERR_IO_PENDING;
[email protected]aab1b9e2012-11-06 00:29:5188}
89
[email protected]633ff3b12014-06-20 23:30:1890int FileStream::Context::Write(IOBuffer* buf,
91 int buf_len,
Matt Menkedadd6c72018-01-30 23:07:2592 CompletionOnceCallback callback) {
Eric Roman89e0df132018-03-29 21:52:4093 DCHECK(!async_in_progress_);
ananta806016e2015-02-06 20:30:0794
ananta47f234d32015-02-14 02:10:5595 result_ = 0;
96
[email protected]aab1b9e2012-11-06 00:29:5197 DWORD bytes_written = 0;
[email protected]a08305912014-03-21 00:41:1598 if (!WriteFile(file_.GetPlatformFile(), buf->data(), buf_len,
[email protected]aab1b9e2012-11-06 00:29:5199 &bytes_written, &io_context_.overlapped)) {
[email protected]bfb88ec2013-02-27 20:21:35100 IOResult error = IOResult::FromOSError(GetLastError());
Matt Menkedadd6c72018-01-30 23:07:25101 if (error.os_error == ERROR_IO_PENDING) {
102 IOCompletionIsPending(std::move(callback), buf);
103 } else {
[email protected]bfb88ec2013-02-27 20:21:35104 LOG(WARNING) << "WriteFile failed: " << error.os_error;
Matt Menkedadd6c72018-01-30 23:07:25105 }
Peter Kastingbe940e92014-11-20 23:14:08106 return static_cast<int>(error.result);
[email protected]aab1b9e2012-11-06 00:29:51107 }
[email protected]bfb88ec2013-02-27 20:21:35108
Matt Menkedadd6c72018-01-30 23:07:25109 IOCompletionIsPending(std::move(callback), buf);
[email protected]bfb88ec2013-02-27 20:21:35110 return ERR_IO_PENDING;
[email protected]aab1b9e2012-11-06 00:29:51111}
112
[email protected]633ff3b12014-06-20 23:30:18113FileStream::Context::IOResult FileStream::Context::SeekFileImpl(
wtc69f8ea82015-06-04 00:08:13114 int64_t offset) {
[email protected]633ff3b12014-06-20 23:30:18115 LARGE_INTEGER result;
reillygaa435e72015-06-16 00:48:48116 result.QuadPart = offset;
117 SetOffset(&io_context_.overlapped, result);
118 return IOResult(result.QuadPart, 0);
[email protected]aab1b9e2012-11-06 00:29:51119}
120
[email protected]633ff3b12014-06-20 23:30:18121void FileStream::Context::OnFileOpened() {
Robbie McElrathaddae862018-06-19 19:40:35122 HRESULT hr = base::MessageLoopCurrentForIO::Get()->RegisterIOHandler(
Gabriel Charette1e5bed442018-04-24 23:25:49123 file_.GetPlatformFile(), this);
Robbie McElrathaddae862018-06-19 19:40:35124 if (!SUCCEEDED(hr))
125 file_.Close();
[email protected]c1d9cf742013-09-12 06:37:19126}
127
Matt Menkedadd6c72018-01-30 23:07:25128void FileStream::Context::IOCompletionIsPending(CompletionOnceCallback callback,
129 IOBuffer* buf) {
[email protected]aab1b9e2012-11-06 00:29:51130 DCHECK(callback_.is_null());
Matt Menkedadd6c72018-01-30 23:07:25131 callback_ = std::move(callback);
[email protected]aab1b9e2012-11-06 00:29:51132 in_flight_buf_ = buf; // Hold until the async operation ends.
133 async_in_progress_ = true;
134}
135
[email protected]2da659e2013-05-23 20:51:34136void FileStream::Context::OnIOCompleted(
Gabriel Charette19d2ae62018-04-10 14:10:58137 base::MessagePumpForIO::IOContext* context,
[email protected]2da659e2013-05-23 20:51:34138 DWORD bytes_read,
139 DWORD error) {
[email protected]aab1b9e2012-11-06 00:29:51140 DCHECK_EQ(&io_context_, context);
141 DCHECK(!callback_.is_null());
142 DCHECK(async_in_progress_);
143
Eric Roman89e0df132018-03-29 21:52:40144 if (!async_read_initiated_)
ananta806016e2015-02-06 20:30:07145 async_in_progress_ = false;
ananta806016e2015-02-06 20:30:07146
[email protected]aab1b9e2012-11-06 00:29:51147 if (orphaned_) {
ananta47f234d32015-02-14 02:10:55148 io_complete_for_read_received_ = true;
149 // If we are called due to a pending read and the asynchronous read task
150 // has not completed we have to keep the context around until it completes.
151 if (async_read_initiated_ && !async_read_completed_)
152 return;
153 DeleteOrphanedContext();
[email protected]aab1b9e2012-11-06 00:29:51154 return;
155 }
156
[email protected]bfb88ec2013-02-27 20:21:35157 if (error == ERROR_HANDLE_EOF) {
ananta806016e2015-02-06 20:30:07158 result_ = 0;
[email protected]bfb88ec2013-02-27 20:21:35159 } else if (error) {
160 IOResult error_result = IOResult::FromOSError(error);
ananta806016e2015-02-06 20:30:07161 result_ = static_cast<int>(error_result.result);
[email protected]bfb88ec2013-02-27 20:21:35162 } else {
ananta47f234d32015-02-14 02:10:55163 if (result_)
164 DCHECK_EQ(result_, static_cast<int>(bytes_read));
ananta806016e2015-02-06 20:30:07165 result_ = bytes_read;
[email protected]aab1b9e2012-11-06 00:29:51166 IncrementOffset(&io_context_.overlapped, bytes_read);
[email protected]bfb88ec2013-02-27 20:21:35167 }
[email protected]aab1b9e2012-11-06 00:29:51168
ananta806016e2015-02-06 20:30:07169 if (async_read_initiated_)
170 io_complete_for_read_received_ = true;
171
172 InvokeUserCallback();
173}
174
175void FileStream::Context::InvokeUserCallback() {
176 // For an asynchonous Read operation don't invoke the user callback until
177 // we receive the IO completion notification and the asynchronous Read
178 // completion notification.
179 if (async_read_initiated_) {
180 if (!io_complete_for_read_received_ || !async_read_completed_)
181 return;
182 async_read_initiated_ = false;
183 io_complete_for_read_received_ = false;
184 async_read_completed_ = false;
ananta806016e2015-02-06 20:30:07185 async_in_progress_ = false;
186 }
[email protected]aab1b9e2012-11-06 00:29:51187 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_;
188 in_flight_buf_ = NULL;
Matt Menkedadd6c72018-01-30 23:07:25189 std::move(callback_).Run(result_);
[email protected]aab1b9e2012-11-06 00:29:51190}
191
ananta47f234d32015-02-14 02:10:55192void FileStream::Context::DeleteOrphanedContext() {
ananta47f234d32015-02-14 02:10:55193 async_in_progress_ = false;
194 callback_.Reset();
195 in_flight_buf_ = NULL;
196 CloseAndDelete();
197}
198
ananta959eaea2015-02-03 19:50:50199// static
200void FileStream::Context::ReadAsync(
ananta806016e2015-02-06 20:30:07201 FileStream::Context* context,
ananta959eaea2015-02-03 19:50:50202 HANDLE file,
ttuttle859dc7a2015-04-23 19:42:29203 scoped_refptr<IOBuffer> buf,
ananta959eaea2015-02-03 19:50:50204 int buf_len,
205 OVERLAPPED* overlapped,
skyostil4891b25b2015-06-11 11:43:45206 scoped_refptr<base::SingleThreadTaskRunner> origin_thread_task_runner) {
ananta959eaea2015-02-03 19:50:50207 DWORD bytes_read = 0;
ananta806016e2015-02-06 20:30:07208 BOOL ret = ::ReadFile(file, buf->data(), buf_len, &bytes_read, overlapped);
skyostil4891b25b2015-06-11 11:43:45209 origin_thread_task_runner->PostTask(
ananta47f234d32015-02-14 02:10:55210 FROM_HERE,
211 base::Bind(&FileStream::Context::ReadAsyncResult,
212 base::Unretained(context), ret, bytes_read, ::GetLastError()));
ananta959eaea2015-02-03 19:50:50213}
214
ananta47f234d32015-02-14 02:10:55215void FileStream::Context::ReadAsyncResult(BOOL read_file_ret,
216 DWORD bytes_read,
217 DWORD os_error) {
218 // If the context is orphaned and we already received the io completion
219 // notification then we should delete the context and get out.
220 if (orphaned_ && io_complete_for_read_received_) {
221 DeleteOrphanedContext();
222 return;
223 }
224
225 async_read_completed_ = true;
226 if (read_file_ret) {
ananta806016e2015-02-06 20:30:07227 result_ = bytes_read;
ananta47f234d32015-02-14 02:10:55228 InvokeUserCallback();
229 return;
230 }
ananta806016e2015-02-06 20:30:07231
ananta959eaea2015-02-03 19:50:50232 IOResult error = IOResult::FromOSError(os_error);
ananta47f234d32015-02-14 02:10:55233 if (error.os_error == ERROR_IO_PENDING) {
234 InvokeUserCallback();
235 } else {
ananta959eaea2015-02-03 19:50:50236 OnIOCompleted(&io_context_, 0, error.os_error);
ananta959eaea2015-02-03 19:50:50237 }
238}
239
[email protected]aab1b9e2012-11-06 00:29:51240} // namespace net