Chromecast Android buildfix: rework CommandLine initialization logic.
[chromium-blink-merge.git] / net / base / file_stream_context_posix.cc
blob44f59b1ef3b82c9b3032edcbef331901dbc4e518
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 <errno.h>
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/callback.h"
13 #include "base/files/file_path.h"
14 #include "base/location.h"
15 #include "base/logging.h"
16 #include "base/metrics/histogram.h"
17 #include "base/posix/eintr_wrapper.h"
18 #include "base/task_runner.h"
19 #include "base/task_runner_util.h"
20 #include "net/base/io_buffer.h"
21 #include "net/base/net_errors.h"
23 namespace net {
25 FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner)
26 : async_in_progress_(false),
27 orphaned_(false),
28 task_runner_(task_runner) {
31 FileStream::Context::Context(base::File file,
32 const scoped_refptr<base::TaskRunner>& task_runner)
33 : file_(file.Pass()),
34 async_in_progress_(false),
35 orphaned_(false),
36 task_runner_(task_runner) {
39 FileStream::Context::~Context() {
42 int FileStream::Context::Read(IOBuffer* in_buf,
43 int buf_len,
44 const CompletionCallback& callback) {
45 DCHECK(!async_in_progress_);
47 scoped_refptr<IOBuffer> buf = in_buf;
48 const bool posted = base::PostTaskAndReplyWithResult(
49 task_runner_.get(),
50 FROM_HERE,
51 base::Bind(&Context::ReadFileImpl, base::Unretained(this), buf, buf_len),
52 base::Bind(&Context::OnAsyncCompleted,
53 base::Unretained(this),
54 IntToInt64(callback)));
55 DCHECK(posted);
57 async_in_progress_ = true;
58 return ERR_IO_PENDING;
61 int FileStream::Context::Write(IOBuffer* in_buf,
62 int buf_len,
63 const CompletionCallback& callback) {
64 DCHECK(!async_in_progress_);
66 scoped_refptr<IOBuffer> buf = in_buf;
67 const bool posted = base::PostTaskAndReplyWithResult(
68 task_runner_.get(),
69 FROM_HERE,
70 base::Bind(&Context::WriteFileImpl, base::Unretained(this), buf, buf_len),
71 base::Bind(&Context::OnAsyncCompleted,
72 base::Unretained(this),
73 IntToInt64(callback)));
74 DCHECK(posted);
76 async_in_progress_ = true;
77 return ERR_IO_PENDING;
80 FileStream::Context::IOResult FileStream::Context::SeekFileImpl(
81 base::File::Whence whence,
82 int64 offset) {
83 int64 res = file_.Seek(whence, offset);
84 if (res == -1)
85 return IOResult::FromOSError(errno);
87 return IOResult(res, 0);
90 void FileStream::Context::OnFileOpened() {
93 FileStream::Context::IOResult FileStream::Context::ReadFileImpl(
94 scoped_refptr<IOBuffer> buf,
95 int buf_len) {
96 int res = file_.ReadAtCurrentPosNoBestEffort(buf->data(), buf_len);
97 if (res == -1)
98 return IOResult::FromOSError(errno);
100 return IOResult(res, 0);
103 FileStream::Context::IOResult FileStream::Context::WriteFileImpl(
104 scoped_refptr<IOBuffer> buf,
105 int buf_len) {
106 int res = file_.WriteAtCurrentPosNoBestEffort(buf->data(), buf_len);
107 if (res == -1)
108 return IOResult::FromOSError(errno);
110 return IOResult(res, 0);
113 } // namespace net