Update V8 to version 4.7.42.
[chromium-blink-merge.git] / net / base / file_stream_context_posix.cc
blob9991c69c986e8823894e6be62ef88b73cd5c2570
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/posix/eintr_wrapper.h"
17 #include "base/profiler/scoped_tracker.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 int64_t offset) {
82 int64_t res = file_.Seek(base::File::FROM_BEGIN, offset);
83 if (res == -1)
84 return IOResult::FromOSError(errno);
86 return IOResult(res, 0);
89 void FileStream::Context::OnFileOpened() {
92 FileStream::Context::IOResult FileStream::Context::ReadFileImpl(
93 scoped_refptr<IOBuffer> buf,
94 int buf_len) {
95 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed.
96 tracked_objects::ScopedTracker tracking_profile(
97 FROM_HERE_WITH_EXPLICIT_FUNCTION(
98 "477117 FileStream::Context::ReadFileImpl"));
99 int res = file_.ReadAtCurrentPosNoBestEffort(buf->data(), buf_len);
100 if (res == -1)
101 return IOResult::FromOSError(errno);
103 return IOResult(res, 0);
106 FileStream::Context::IOResult FileStream::Context::WriteFileImpl(
107 scoped_refptr<IOBuffer> buf,
108 int buf_len) {
109 int res = file_.WriteAtCurrentPosNoBestEffort(buf->data(), buf_len);
110 if (res == -1)
111 return IOResult::FromOSError(errno);
113 return IOResult(res, 0);
116 } // namespace net