blob: 61118ad1f285413162b176b4f3752901f3a631ce [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"
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() {
122 base::MessageLoopForIO::current()->RegisterIOHandler(file_.GetPlatformFile(),
123 this);
[email protected]c1d9cf742013-09-12 06:37:19124}
125
Matt Menkedadd6c72018-01-30 23:07:25126void FileStream::Context::IOCompletionIsPending(CompletionOnceCallback callback,
127 IOBuffer* buf) {
[email protected]aab1b9e2012-11-06 00:29:51128 DCHECK(callback_.is_null());
Matt Menkedadd6c72018-01-30 23:07:25129 callback_ = std::move(callback);
[email protected]aab1b9e2012-11-06 00:29:51130 in_flight_buf_ = buf; // Hold until the async operation ends.
131 async_in_progress_ = true;
132}
133
[email protected]2da659e2013-05-23 20:51:34134void FileStream::Context::OnIOCompleted(
Gabriel Charette19d2ae62018-04-10 14:10:58135 base::MessagePumpForIO::IOContext* context,
[email protected]2da659e2013-05-23 20:51:34136 DWORD bytes_read,
137 DWORD error) {
[email protected]aab1b9e2012-11-06 00:29:51138 DCHECK_EQ(&io_context_, context);
139 DCHECK(!callback_.is_null());
140 DCHECK(async_in_progress_);
141
Eric Roman89e0df132018-03-29 21:52:40142 if (!async_read_initiated_)
ananta806016e2015-02-06 20:30:07143 async_in_progress_ = false;
ananta806016e2015-02-06 20:30:07144
[email protected]aab1b9e2012-11-06 00:29:51145 if (orphaned_) {
ananta47f234d32015-02-14 02:10:55146 io_complete_for_read_received_ = true;
147 // If we are called due to a pending read and the asynchronous read task
148 // has not completed we have to keep the context around until it completes.
149 if (async_read_initiated_ && !async_read_completed_)
150 return;
151 DeleteOrphanedContext();
[email protected]aab1b9e2012-11-06 00:29:51152 return;
153 }
154
[email protected]bfb88ec2013-02-27 20:21:35155 if (error == ERROR_HANDLE_EOF) {
ananta806016e2015-02-06 20:30:07156 result_ = 0;
[email protected]bfb88ec2013-02-27 20:21:35157 } else if (error) {
158 IOResult error_result = IOResult::FromOSError(error);
ananta806016e2015-02-06 20:30:07159 result_ = static_cast<int>(error_result.result);
[email protected]bfb88ec2013-02-27 20:21:35160 } else {
ananta47f234d32015-02-14 02:10:55161 if (result_)
162 DCHECK_EQ(result_, static_cast<int>(bytes_read));
ananta806016e2015-02-06 20:30:07163 result_ = bytes_read;
[email protected]aab1b9e2012-11-06 00:29:51164 IncrementOffset(&io_context_.overlapped, bytes_read);
[email protected]bfb88ec2013-02-27 20:21:35165 }
[email protected]aab1b9e2012-11-06 00:29:51166
ananta806016e2015-02-06 20:30:07167 if (async_read_initiated_)
168 io_complete_for_read_received_ = true;
169
170 InvokeUserCallback();
171}
172
173void FileStream::Context::InvokeUserCallback() {
174 // For an asynchonous Read operation don't invoke the user callback until
175 // we receive the IO completion notification and the asynchronous Read
176 // completion notification.
177 if (async_read_initiated_) {
178 if (!io_complete_for_read_received_ || !async_read_completed_)
179 return;
180 async_read_initiated_ = false;
181 io_complete_for_read_received_ = false;
182 async_read_completed_ = false;
ananta806016e2015-02-06 20:30:07183 async_in_progress_ = false;
184 }
[email protected]aab1b9e2012-11-06 00:29:51185 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_;
186 in_flight_buf_ = NULL;
Matt Menkedadd6c72018-01-30 23:07:25187 std::move(callback_).Run(result_);
[email protected]aab1b9e2012-11-06 00:29:51188}
189
ananta47f234d32015-02-14 02:10:55190void FileStream::Context::DeleteOrphanedContext() {
ananta47f234d32015-02-14 02:10:55191 async_in_progress_ = false;
192 callback_.Reset();
193 in_flight_buf_ = NULL;
194 CloseAndDelete();
195}
196
ananta959eaea2015-02-03 19:50:50197// static
198void FileStream::Context::ReadAsync(
ananta806016e2015-02-06 20:30:07199 FileStream::Context* context,
ananta959eaea2015-02-03 19:50:50200 HANDLE file,
ttuttle859dc7a2015-04-23 19:42:29201 scoped_refptr<IOBuffer> buf,
ananta959eaea2015-02-03 19:50:50202 int buf_len,
203 OVERLAPPED* overlapped,
skyostil4891b25b2015-06-11 11:43:45204 scoped_refptr<base::SingleThreadTaskRunner> origin_thread_task_runner) {
ananta959eaea2015-02-03 19:50:50205 DWORD bytes_read = 0;
ananta806016e2015-02-06 20:30:07206 BOOL ret = ::ReadFile(file, buf->data(), buf_len, &bytes_read, overlapped);
skyostil4891b25b2015-06-11 11:43:45207 origin_thread_task_runner->PostTask(
ananta47f234d32015-02-14 02:10:55208 FROM_HERE,
209 base::Bind(&FileStream::Context::ReadAsyncResult,
210 base::Unretained(context), ret, bytes_read, ::GetLastError()));
ananta959eaea2015-02-03 19:50:50211}
212
ananta47f234d32015-02-14 02:10:55213void FileStream::Context::ReadAsyncResult(BOOL read_file_ret,
214 DWORD bytes_read,
215 DWORD os_error) {
216 // If the context is orphaned and we already received the io completion
217 // notification then we should delete the context and get out.
218 if (orphaned_ && io_complete_for_read_received_) {
219 DeleteOrphanedContext();
220 return;
221 }
222
223 async_read_completed_ = true;
224 if (read_file_ret) {
ananta806016e2015-02-06 20:30:07225 result_ = bytes_read;
ananta47f234d32015-02-14 02:10:55226 InvokeUserCallback();
227 return;
228 }
ananta806016e2015-02-06 20:30:07229
ananta959eaea2015-02-03 19:50:50230 IOResult error = IOResult::FromOSError(os_error);
ananta47f234d32015-02-14 02:10:55231 if (error.os_error == ERROR_IO_PENDING) {
232 InvokeUserCallback();
233 } else {
ananta959eaea2015-02-03 19:50:50234 OnIOCompleted(&io_context_, 0, error.os_error);
ananta959eaea2015-02-03 19:50:50235 }
236}
237
[email protected]aab1b9e2012-11-06 00:29:51238} // namespace net