Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / base / file_stream_context_posix.cc
blob489216fbc24879faa5e048ad1730b2de310c1c9d
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/profiler/scoped_tracker.h"
19 #include "base/task_runner.h"
20 #include "base/task_runner_util.h"
21 #include "net/base/io_buffer.h"
22 #include "net/base/net_errors.h"
24 namespace net {
26 FileStream::Context::Context(const scoped_refptr<base::TaskRunner>& task_runner)
27 : async_in_progress_(false),
28 orphaned_(false),
29 task_runner_(task_runner) {
32 FileStream::Context::Context(base::File file,
33 const scoped_refptr<base::TaskRunner>& task_runner)
34 : file_(file.Pass()),
35 async_in_progress_(false),
36 orphaned_(false),
37 task_runner_(task_runner) {
40 FileStream::Context::~Context() {
43 int FileStream::Context::Read(IOBuffer* in_buf,
44 int buf_len,
45 const CompletionCallback& callback) {
46 DCHECK(!async_in_progress_);
48 scoped_refptr<IOBuffer> buf = in_buf;
49 const bool posted = base::PostTaskAndReplyWithResult(
50 task_runner_.get(),
51 FROM_HERE,
52 base::Bind(&Context::ReadFileImpl, base::Unretained(this), buf, buf_len),
53 base::Bind(&Context::OnAsyncCompleted,
54 base::Unretained(this),
55 IntToInt64(callback)));
56 DCHECK(posted);
58 async_in_progress_ = true;
59 return ERR_IO_PENDING;
62 int FileStream::Context::Write(IOBuffer* in_buf,
63 int buf_len,
64 const CompletionCallback& callback) {
65 DCHECK(!async_in_progress_);
67 scoped_refptr<IOBuffer> buf = in_buf;
68 const bool posted = base::PostTaskAndReplyWithResult(
69 task_runner_.get(),
70 FROM_HERE,
71 base::Bind(&Context::WriteFileImpl, base::Unretained(this), buf, buf_len),
72 base::Bind(&Context::OnAsyncCompleted,
73 base::Unretained(this),
74 IntToInt64(callback)));
75 DCHECK(posted);
77 async_in_progress_ = true;
78 return ERR_IO_PENDING;
81 FileStream::Context::IOResult FileStream::Context::SeekFileImpl(
82 base::File::Whence whence,
83 int64 offset) {
84 int64 res = file_.Seek(whence, offset);
85 if (res == -1)
86 return IOResult::FromOSError(errno);
88 return IOResult(res, 0);
91 void FileStream::Context::OnFileOpened() {
94 FileStream::Context::IOResult FileStream::Context::ReadFileImpl(
95 scoped_refptr<IOBuffer> buf,
96 int buf_len) {
97 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed.
98 tracked_objects::ScopedTracker tracking_profile(
99 FROM_HERE_WITH_EXPLICIT_FUNCTION(
100 "477117 FileStream::Context::ReadFileImpl"));
101 int res = file_.ReadAtCurrentPosNoBestEffort(buf->data(), buf_len);
102 if (res == -1)
103 return IOResult::FromOSError(errno);
105 return IOResult(res, 0);
108 FileStream::Context::IOResult FileStream::Context::WriteFileImpl(
109 scoped_refptr<IOBuffer> buf,
110 int buf_len) {
111 int res = file_.WriteAtCurrentPosNoBestEffort(buf->data(), buf_len);
112 if (res == -1)
113 return IOResult::FromOSError(errno);
115 return IOResult(res, 0);
118 } // namespace net