blob: 4e417dc02f50c4b224bc70039d9f6695a02b0ee9 [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"
skyostil4891b25b2015-06-11 11:43:4513#include "base/single_thread_task_runner.h"
[email protected]866534a92014-05-07 03:39:1514#include "base/task_runner.h"
gabf767595f2016-05-11 18:50:3515#include "base/threading/thread_task_runner_handle.h"
ananta959eaea2015-02-03 19:50:5016#include "base/threading/worker_pool.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,
71 const CompletionCallback& 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;
[email protected]bfb88ec2013-02-27 20:21:3579 IOCompletionIsPending(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),
87 file_.GetPlatformFile(), make_scoped_refptr(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,
94 const CompletionCallback& 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());
Peter Kastingbe940e92014-11-20 23:14:08104 if (error.os_error == ERROR_IO_PENDING)
[email protected]aab1b9e2012-11-06 00:29:51105 IOCompletionIsPending(callback, buf);
Peter Kastingbe940e92014-11-20 23:14:08106 else
[email protected]bfb88ec2013-02-27 20:21:35107 LOG(WARNING) << "WriteFile failed: " << error.os_error;
Peter Kastingbe940e92014-11-20 23:14:08108 return static_cast<int>(error.result);
[email protected]aab1b9e2012-11-06 00:29:51109 }
[email protected]bfb88ec2013-02-27 20:21:35110
111 IOCompletionIsPending(callback, buf);
112 return ERR_IO_PENDING;
[email protected]aab1b9e2012-11-06 00:29:51113}
114
[email protected]633ff3b12014-06-20 23:30:18115FileStream::Context::IOResult FileStream::Context::SeekFileImpl(
wtc69f8ea82015-06-04 00:08:13116 int64_t offset) {
[email protected]633ff3b12014-06-20 23:30:18117 LARGE_INTEGER result;
reillygaa435e72015-06-16 00:48:48118 result.QuadPart = offset;
119 SetOffset(&io_context_.overlapped, result);
120 return IOResult(result.QuadPart, 0);
[email protected]aab1b9e2012-11-06 00:29:51121}
122
[email protected]633ff3b12014-06-20 23:30:18123void FileStream::Context::OnFileOpened() {
124 base::MessageLoopForIO::current()->RegisterIOHandler(file_.GetPlatformFile(),
125 this);
[email protected]c1d9cf742013-09-12 06:37:19126}
127
[email protected]aab1b9e2012-11-06 00:29:51128void FileStream::Context::IOCompletionIsPending(
129 const CompletionCallback& callback,
130 IOBuffer* buf) {
131 DCHECK(callback_.is_null());
132 callback_ = callback;
133 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 CompletionCallback temp_callback = callback_;
192 callback_.Reset();
193 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_;
194 in_flight_buf_ = NULL;
ananta806016e2015-02-06 20:30:07195 temp_callback.Run(result_);
[email protected]aab1b9e2012-11-06 00:29:51196}
197
ananta47f234d32015-02-14 02:10:55198void FileStream::Context::DeleteOrphanedContext() {
xunjielicf470d52016-10-18 22:10:16199 last_operation_ = NONE;
ananta47f234d32015-02-14 02:10:55200 async_in_progress_ = false;
201 callback_.Reset();
202 in_flight_buf_ = NULL;
203 CloseAndDelete();
204}
205
ananta959eaea2015-02-03 19:50:50206// static
207void FileStream::Context::ReadAsync(
ananta806016e2015-02-06 20:30:07208 FileStream::Context* context,
ananta959eaea2015-02-03 19:50:50209 HANDLE file,
ttuttle859dc7a2015-04-23 19:42:29210 scoped_refptr<IOBuffer> buf,
ananta959eaea2015-02-03 19:50:50211 int buf_len,
212 OVERLAPPED* overlapped,
skyostil4891b25b2015-06-11 11:43:45213 scoped_refptr<base::SingleThreadTaskRunner> origin_thread_task_runner) {
ananta959eaea2015-02-03 19:50:50214 DWORD bytes_read = 0;
ananta806016e2015-02-06 20:30:07215 BOOL ret = ::ReadFile(file, buf->data(), buf_len, &bytes_read, overlapped);
skyostil4891b25b2015-06-11 11:43:45216 origin_thread_task_runner->PostTask(
ananta47f234d32015-02-14 02:10:55217 FROM_HERE,
218 base::Bind(&FileStream::Context::ReadAsyncResult,
219 base::Unretained(context), ret, bytes_read, ::GetLastError()));
ananta959eaea2015-02-03 19:50:50220}
221
ananta47f234d32015-02-14 02:10:55222void FileStream::Context::ReadAsyncResult(BOOL read_file_ret,
223 DWORD bytes_read,
224 DWORD os_error) {
225 // If the context is orphaned and we already received the io completion
226 // notification then we should delete the context and get out.
227 if (orphaned_ && io_complete_for_read_received_) {
228 DeleteOrphanedContext();
229 return;
230 }
231
232 async_read_completed_ = true;
233 if (read_file_ret) {
ananta806016e2015-02-06 20:30:07234 result_ = bytes_read;
ananta47f234d32015-02-14 02:10:55235 InvokeUserCallback();
236 return;
237 }
ananta806016e2015-02-06 20:30:07238
ananta959eaea2015-02-03 19:50:50239 IOResult error = IOResult::FromOSError(os_error);
ananta47f234d32015-02-14 02:10:55240 if (error.os_error == ERROR_IO_PENDING) {
241 InvokeUserCallback();
242 } else {
ananta959eaea2015-02-03 19:50:50243 OnIOCompleted(&io_context_, 0, error.os_error);
ananta959eaea2015-02-03 19:50:50244 }
245}
246
[email protected]aab1b9e2012-11-06 00:29:51247} // namespace net