blob: cf4947fda5fe6121b5dfaa376b13d27ec469753f [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"
gabf64a25e2017-05-12 19:42:5613#include "base/message_loop/message_loop.h"
skyostil4891b25b2015-06-11 11:43:4514#include "base/single_thread_task_runner.h"
[email protected]866534a92014-05-07 03:39:1515#include "base/task_runner.h"
gabf767595f2016-05-11 18:50:3516#include "base/threading/thread_task_runner_handle.h"
[email protected]aab1b9e2012-11-06 00:29:5117#include "net/base/io_buffer.h"
18#include "net/base/net_errors.h"
19
20namespace net {
21
[email protected]aab1b9e2012-11-06 00:29:5122namespace {
23
24void SetOffset(OVERLAPPED* overlapped, const LARGE_INTEGER& offset) {
25 overlapped->Offset = offset.LowPart;
26 overlapped->OffsetHigh = offset.HighPart;
27}
28
29void 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]619a0398c2014-04-23 08:28:4539FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner)
sammc5648c852015-07-02 01:25:0040 : async_in_progress_(false),
xunjielicf470d52016-10-18 22:10:1641 last_operation_(NONE),
[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),
xunjielicf470d52016-10-18 22:10:1653 last_operation_(NONE),
[email protected]aab1b9e2012-11-06 00:29:5154 orphaned_(false),
ananta959eaea2015-02-03 19:50:5055 task_runner_(task_runner),
ananta806016e2015-02-06 20:30:0756 async_read_initiated_(false),
57 async_read_completed_(false),
58 io_complete_for_read_received_(false),
59 result_(0) {
[email protected]50f91af2014-04-09 17:28:5660 if (file_.IsValid()) {
reillygaa435e72015-06-16 00:48:4861 DCHECK(file_.async());
[email protected]633ff3b12014-06-20 23:30:1862 OnFileOpened();
[email protected]50f91af2014-04-09 17:28:5663 }
[email protected]aab1b9e2012-11-06 00:29:5164}
65
66FileStream::Context::~Context() {
67}
68
[email protected]633ff3b12014-06-20 23:30:1869int FileStream::Context::Read(IOBuffer* buf,
70 int buf_len,
Matt Menkedadd6c72018-01-30 23:07:2571 CompletionOnceCallback callback) {
xunjielicf470d52016-10-18 22:10:1672 CheckNoAsyncInProgress();
73
ananta806016e2015-02-06 20:30:0774 DCHECK(!async_read_initiated_);
75 DCHECK(!async_read_completed_);
76 DCHECK(!io_complete_for_read_received_);
[email protected]aab1b9e2012-11-06 00:29:5177
xunjielicf470d52016-10-18 22:10:1678 last_operation_ = READ;
Matt Menkedadd6c72018-01-30 23:07:2579 IOCompletionIsPending(std::move(callback), buf);
ananta959eaea2015-02-03 19:50:5080
ananta806016e2015-02-06 20:30:0781 async_read_initiated_ = true;
ananta47f234d32015-02-14 02:10:5582 result_ = 0;
ananta959eaea2015-02-03 19:50:5083
ananta806016e2015-02-06 20:30:0784 task_runner_->PostTask(
85 FROM_HERE,
86 base::Bind(&FileStream::Context::ReadAsync, base::Unretained(this),
kylecharda69d882017-10-04 05:49:5287 file_.GetPlatformFile(), base::WrapRefCounted(buf), buf_len,
skyostil4891b25b2015-06-11 11:43:4588 &io_context_.overlapped, base::ThreadTaskRunnerHandle::Get()));
[email protected]bfb88ec2013-02-27 20:21:3589 return ERR_IO_PENDING;
[email protected]aab1b9e2012-11-06 00:29:5190}
91
[email protected]633ff3b12014-06-20 23:30:1892int FileStream::Context::Write(IOBuffer* buf,
93 int buf_len,
Matt Menkedadd6c72018-01-30 23:07:2594 CompletionOnceCallback callback) {
xunjielicf470d52016-10-18 22:10:1695 CheckNoAsyncInProgress();
ananta806016e2015-02-06 20:30:0796
xunjielicf470d52016-10-18 22:10:1697 last_operation_ = WRITE;
ananta47f234d32015-02-14 02:10:5598 result_ = 0;
99
[email protected]aab1b9e2012-11-06 00:29:51100 DWORD bytes_written = 0;
[email protected]a08305912014-03-21 00:41:15101 if (!WriteFile(file_.GetPlatformFile(), buf->data(), buf_len,
[email protected]aab1b9e2012-11-06 00:29:51102 &bytes_written, &io_context_.overlapped)) {
[email protected]bfb88ec2013-02-27 20:21:35103 IOResult error = IOResult::FromOSError(GetLastError());
Matt Menkedadd6c72018-01-30 23:07:25104 if (error.os_error == ERROR_IO_PENDING) {
105 IOCompletionIsPending(std::move(callback), buf);
106 } else {
[email protected]bfb88ec2013-02-27 20:21:35107 LOG(WARNING) << "WriteFile failed: " << error.os_error;
Matt Menkedadd6c72018-01-30 23:07:25108 }
Peter Kastingbe940e92014-11-20 23:14:08109 return static_cast<int>(error.result);
[email protected]aab1b9e2012-11-06 00:29:51110 }
[email protected]bfb88ec2013-02-27 20:21:35111
Matt Menkedadd6c72018-01-30 23:07:25112 IOCompletionIsPending(std::move(callback), buf);
[email protected]bfb88ec2013-02-27 20:21:35113 return ERR_IO_PENDING;
[email protected]aab1b9e2012-11-06 00:29:51114}
115
[email protected]633ff3b12014-06-20 23:30:18116FileStream::Context::IOResult FileStream::Context::SeekFileImpl(
wtc69f8ea82015-06-04 00:08:13117 int64_t offset) {
[email protected]633ff3b12014-06-20 23:30:18118 LARGE_INTEGER result;
reillygaa435e72015-06-16 00:48:48119 result.QuadPart = offset;
120 SetOffset(&io_context_.overlapped, result);
121 return IOResult(result.QuadPart, 0);
[email protected]aab1b9e2012-11-06 00:29:51122}
123
[email protected]633ff3b12014-06-20 23:30:18124void FileStream::Context::OnFileOpened() {
125 base::MessageLoopForIO::current()->RegisterIOHandler(file_.GetPlatformFile(),
126 this);
[email protected]c1d9cf742013-09-12 06:37:19127}
128
Matt Menkedadd6c72018-01-30 23:07:25129void FileStream::Context::IOCompletionIsPending(CompletionOnceCallback callback,
130 IOBuffer* buf) {
[email protected]aab1b9e2012-11-06 00:29:51131 DCHECK(callback_.is_null());
Matt Menkedadd6c72018-01-30 23:07:25132 callback_ = std::move(callback);
[email protected]aab1b9e2012-11-06 00:29:51133 in_flight_buf_ = buf; // Hold until the async operation ends.
134 async_in_progress_ = true;
135}
136
[email protected]2da659e2013-05-23 20:51:34137void FileStream::Context::OnIOCompleted(
138 base::MessageLoopForIO::IOContext* context,
139 DWORD bytes_read,
140 DWORD error) {
[email protected]aab1b9e2012-11-06 00:29:51141 DCHECK_EQ(&io_context_, context);
142 DCHECK(!callback_.is_null());
143 DCHECK(async_in_progress_);
144
xunjielicf470d52016-10-18 22:10:16145 if (!async_read_initiated_) {
146 last_operation_ = NONE;
ananta806016e2015-02-06 20:30:07147 async_in_progress_ = false;
xunjielicf470d52016-10-18 22:10:16148 }
ananta806016e2015-02-06 20:30:07149
[email protected]aab1b9e2012-11-06 00:29:51150 if (orphaned_) {
ananta47f234d32015-02-14 02:10:55151 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]aab1b9e2012-11-06 00:29:51157 return;
158 }
159
[email protected]bfb88ec2013-02-27 20:21:35160 if (error == ERROR_HANDLE_EOF) {
ananta806016e2015-02-06 20:30:07161 result_ = 0;
[email protected]bfb88ec2013-02-27 20:21:35162 } else if (error) {
163 IOResult error_result = IOResult::FromOSError(error);
ananta806016e2015-02-06 20:30:07164 result_ = static_cast<int>(error_result.result);
[email protected]bfb88ec2013-02-27 20:21:35165 } else {
ananta47f234d32015-02-14 02:10:55166 if (result_)
167 DCHECK_EQ(result_, static_cast<int>(bytes_read));
ananta806016e2015-02-06 20:30:07168 result_ = bytes_read;
[email protected]aab1b9e2012-11-06 00:29:51169 IncrementOffset(&io_context_.overlapped, bytes_read);
[email protected]bfb88ec2013-02-27 20:21:35170 }
[email protected]aab1b9e2012-11-06 00:29:51171
ananta806016e2015-02-06 20:30:07172 if (async_read_initiated_)
173 io_complete_for_read_received_ = true;
174
175 InvokeUserCallback();
176}
177
178void 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;
xunjielicf470d52016-10-18 22:10:16188 last_operation_ = NONE;
ananta806016e2015-02-06 20:30:07189 async_in_progress_ = false;
190 }
[email protected]aab1b9e2012-11-06 00:29:51191 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_;
192 in_flight_buf_ = NULL;
Matt Menkedadd6c72018-01-30 23:07:25193 std::move(callback_).Run(result_);
[email protected]aab1b9e2012-11-06 00:29:51194}
195
ananta47f234d32015-02-14 02:10:55196void FileStream::Context::DeleteOrphanedContext() {
xunjielicf470d52016-10-18 22:10:16197 last_operation_ = NONE;
ananta47f234d32015-02-14 02:10:55198 async_in_progress_ = false;
199 callback_.Reset();
200 in_flight_buf_ = NULL;
201 CloseAndDelete();
202}
203
ananta959eaea2015-02-03 19:50:50204// static
205void FileStream::Context::ReadAsync(
ananta806016e2015-02-06 20:30:07206 FileStream::Context* context,
ananta959eaea2015-02-03 19:50:50207 HANDLE file,
ttuttle859dc7a2015-04-23 19:42:29208 scoped_refptr<IOBuffer> buf,
ananta959eaea2015-02-03 19:50:50209 int buf_len,
210 OVERLAPPED* overlapped,
skyostil4891b25b2015-06-11 11:43:45211 scoped_refptr<base::SingleThreadTaskRunner> origin_thread_task_runner) {
ananta959eaea2015-02-03 19:50:50212 DWORD bytes_read = 0;
ananta806016e2015-02-06 20:30:07213 BOOL ret = ::ReadFile(file, buf->data(), buf_len, &bytes_read, overlapped);
skyostil4891b25b2015-06-11 11:43:45214 origin_thread_task_runner->PostTask(
ananta47f234d32015-02-14 02:10:55215 FROM_HERE,
216 base::Bind(&FileStream::Context::ReadAsyncResult,
217 base::Unretained(context), ret, bytes_read, ::GetLastError()));
ananta959eaea2015-02-03 19:50:50218}
219
ananta47f234d32015-02-14 02:10:55220void 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) {
ananta806016e2015-02-06 20:30:07232 result_ = bytes_read;
ananta47f234d32015-02-14 02:10:55233 InvokeUserCallback();
234 return;
235 }
ananta806016e2015-02-06 20:30:07236
ananta959eaea2015-02-03 19:50:50237 IOResult error = IOResult::FromOSError(os_error);
ananta47f234d32015-02-14 02:10:55238 if (error.os_error == ERROR_IO_PENDING) {
239 InvokeUserCallback();
240 } else {
ananta959eaea2015-02-03 19:50:50241 OnIOCompleted(&io_context_, 0, error.os_error);
ananta959eaea2015-02-03 19:50:50242 }
243}
244
[email protected]aab1b9e2012-11-06 00:29:51245} // namespace net