Move StartsWith[ASCII] to base namespace.
[chromium-blink-merge.git] / net / base / file_stream_context_win.cc
blobd78bf91b1c768473878732b51b6d973e7add8cd4
1 // 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.
5 #include "net/base/file_stream_context.h"
7 #include <windows.h>
9 #include "base/files/file_path.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/task_runner.h"
15 #include "base/thread_task_runner_handle.h"
16 #include "base/threading/worker_pool.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/net_errors.h"
20 namespace net {
22 namespace {
24 void SetOffset(OVERLAPPED* overlapped, const LARGE_INTEGER& offset) {
25 overlapped->Offset = offset.LowPart;
26 overlapped->OffsetHigh = offset.HighPart;
29 void 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);
37 } // namespace
39 FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner)
40 : io_context_(),
41 async_in_progress_(false),
42 orphaned_(false),
43 task_runner_(task_runner),
44 async_read_initiated_(false),
45 async_read_completed_(false),
46 io_complete_for_read_received_(false),
47 result_(0) {
48 io_context_.handler = this;
49 memset(&io_context_.overlapped, 0, sizeof(io_context_.overlapped));
52 FileStream::Context::Context(base::File file,
53 const scoped_refptr<base::TaskRunner>& task_runner)
54 : io_context_(),
55 file_(file.Pass()),
56 async_in_progress_(false),
57 orphaned_(false),
58 task_runner_(task_runner),
59 async_read_initiated_(false),
60 async_read_completed_(false),
61 io_complete_for_read_received_(false),
62 result_(0) {
63 io_context_.handler = this;
64 memset(&io_context_.overlapped, 0, sizeof(io_context_.overlapped));
65 if (file_.IsValid()) {
66 // TODO(hashimoto): Check that file_ is async.
67 OnFileOpened();
71 FileStream::Context::~Context() {
74 int FileStream::Context::Read(IOBuffer* buf,
75 int buf_len,
76 const CompletionCallback& callback) {
77 CHECK(!async_in_progress_);
78 DCHECK(!async_read_initiated_);
79 DCHECK(!async_read_completed_);
80 DCHECK(!io_complete_for_read_received_);
82 IOCompletionIsPending(callback, buf);
84 async_read_initiated_ = true;
85 result_ = 0;
87 task_runner_->PostTask(
88 FROM_HERE,
89 base::Bind(&FileStream::Context::ReadAsync, base::Unretained(this),
90 file_.GetPlatformFile(), make_scoped_refptr(buf), buf_len,
91 &io_context_.overlapped, base::ThreadTaskRunnerHandle::Get()));
92 return ERR_IO_PENDING;
95 int FileStream::Context::Write(IOBuffer* buf,
96 int buf_len,
97 const CompletionCallback& callback) {
98 CHECK(!async_in_progress_);
100 result_ = 0;
102 DWORD bytes_written = 0;
103 if (!WriteFile(file_.GetPlatformFile(), buf->data(), buf_len,
104 &bytes_written, &io_context_.overlapped)) {
105 IOResult error = IOResult::FromOSError(GetLastError());
106 if (error.os_error == ERROR_IO_PENDING)
107 IOCompletionIsPending(callback, buf);
108 else
109 LOG(WARNING) << "WriteFile failed: " << error.os_error;
110 return static_cast<int>(error.result);
113 IOCompletionIsPending(callback, buf);
114 return ERR_IO_PENDING;
117 FileStream::Context::IOResult FileStream::Context::SeekFileImpl(
118 base::File::Whence whence,
119 int64_t offset) {
120 LARGE_INTEGER result;
121 result.QuadPart = file_.Seek(whence, offset);
122 if (result.QuadPart >= 0) {
123 SetOffset(&io_context_.overlapped, result);
124 return IOResult(result.QuadPart, 0);
127 return IOResult::FromOSError(GetLastError());
130 void FileStream::Context::OnFileOpened() {
131 base::MessageLoopForIO::current()->RegisterIOHandler(file_.GetPlatformFile(),
132 this);
135 void FileStream::Context::IOCompletionIsPending(
136 const CompletionCallback& callback,
137 IOBuffer* buf) {
138 DCHECK(callback_.is_null());
139 callback_ = callback;
140 in_flight_buf_ = buf; // Hold until the async operation ends.
141 async_in_progress_ = true;
144 void FileStream::Context::OnIOCompleted(
145 base::MessageLoopForIO::IOContext* context,
146 DWORD bytes_read,
147 DWORD error) {
148 DCHECK_EQ(&io_context_, context);
149 DCHECK(!callback_.is_null());
150 DCHECK(async_in_progress_);
152 if (!async_read_initiated_)
153 async_in_progress_ = false;
155 if (orphaned_) {
156 io_complete_for_read_received_ = true;
157 // If we are called due to a pending read and the asynchronous read task
158 // has not completed we have to keep the context around until it completes.
159 if (async_read_initiated_ && !async_read_completed_)
160 return;
161 DeleteOrphanedContext();
162 return;
165 if (error == ERROR_HANDLE_EOF) {
166 result_ = 0;
167 } else if (error) {
168 IOResult error_result = IOResult::FromOSError(error);
169 result_ = static_cast<int>(error_result.result);
170 } else {
171 if (result_)
172 DCHECK_EQ(result_, static_cast<int>(bytes_read));
173 result_ = bytes_read;
174 IncrementOffset(&io_context_.overlapped, bytes_read);
177 if (async_read_initiated_)
178 io_complete_for_read_received_ = true;
180 InvokeUserCallback();
183 void FileStream::Context::InvokeUserCallback() {
184 // For an asynchonous Read operation don't invoke the user callback until
185 // we receive the IO completion notification and the asynchronous Read
186 // completion notification.
187 if (async_read_initiated_) {
188 if (!io_complete_for_read_received_ || !async_read_completed_)
189 return;
190 async_read_initiated_ = false;
191 io_complete_for_read_received_ = false;
192 async_read_completed_ = false;
193 async_in_progress_ = false;
195 CompletionCallback temp_callback = callback_;
196 callback_.Reset();
197 scoped_refptr<IOBuffer> temp_buf = in_flight_buf_;
198 in_flight_buf_ = NULL;
199 temp_callback.Run(result_);
202 void FileStream::Context::DeleteOrphanedContext() {
203 async_in_progress_ = false;
204 callback_.Reset();
205 in_flight_buf_ = NULL;
206 CloseAndDelete();
209 // static
210 void FileStream::Context::ReadAsync(
211 FileStream::Context* context,
212 HANDLE file,
213 scoped_refptr<IOBuffer> buf,
214 int buf_len,
215 OVERLAPPED* overlapped,
216 scoped_refptr<base::SingleThreadTaskRunner> origin_thread_task_runner) {
217 DWORD bytes_read = 0;
218 BOOL ret = ::ReadFile(file, buf->data(), buf_len, &bytes_read, overlapped);
219 origin_thread_task_runner->PostTask(
220 FROM_HERE,
221 base::Bind(&FileStream::Context::ReadAsyncResult,
222 base::Unretained(context), ret, bytes_read, ::GetLastError()));
225 void FileStream::Context::ReadAsyncResult(BOOL read_file_ret,
226 DWORD bytes_read,
227 DWORD os_error) {
228 // If the context is orphaned and we already received the io completion
229 // notification then we should delete the context and get out.
230 if (orphaned_ && io_complete_for_read_received_) {
231 DeleteOrphanedContext();
232 return;
235 async_read_completed_ = true;
236 if (read_file_ret) {
237 result_ = bytes_read;
238 InvokeUserCallback();
239 return;
242 IOResult error = IOResult::FromOSError(os_error);
243 if (error.os_error == ERROR_IO_PENDING) {
244 InvokeUserCallback();
245 } else {
246 OnIOCompleted(&io_context_, 0, error.os_error);
250 } // namespace net