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"
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"
26 FileStream::Context::Context(const scoped_refptr
<base::TaskRunner
>& task_runner
)
27 : async_in_progress_(false),
29 task_runner_(task_runner
) {
32 FileStream::Context::Context(base::File file
,
33 const scoped_refptr
<base::TaskRunner
>& task_runner
)
35 async_in_progress_(false),
37 task_runner_(task_runner
) {
40 FileStream::Context::~Context() {
43 int FileStream::Context::Read(IOBuffer
* in_buf
,
45 const CompletionCallback
& callback
) {
46 DCHECK(!async_in_progress_
);
48 scoped_refptr
<IOBuffer
> buf
= in_buf
;
49 const bool posted
= base::PostTaskAndReplyWithResult(
52 base::Bind(&Context::ReadFileImpl
, base::Unretained(this), buf
, buf_len
),
53 base::Bind(&Context::OnAsyncCompleted
,
54 base::Unretained(this),
55 IntToInt64(callback
)));
58 async_in_progress_
= true;
59 return ERR_IO_PENDING
;
62 int FileStream::Context::Write(IOBuffer
* in_buf
,
64 const CompletionCallback
& callback
) {
65 DCHECK(!async_in_progress_
);
67 scoped_refptr
<IOBuffer
> buf
= in_buf
;
68 const bool posted
= base::PostTaskAndReplyWithResult(
71 base::Bind(&Context::WriteFileImpl
, base::Unretained(this), buf
, buf_len
),
72 base::Bind(&Context::OnAsyncCompleted
,
73 base::Unretained(this),
74 IntToInt64(callback
)));
77 async_in_progress_
= true;
78 return ERR_IO_PENDING
;
81 FileStream::Context::IOResult
FileStream::Context::SeekFileImpl(
82 base::File::Whence whence
,
84 int64 res
= file_
.Seek(whence
, offset
);
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
,
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
);
103 return IOResult::FromOSError(errno
);
105 return IOResult(res
, 0);
108 FileStream::Context::IOResult
FileStream::Context::WriteFileImpl(
109 scoped_refptr
<IOBuffer
> buf
,
111 int res
= file_
.WriteAtCurrentPosNoBestEffort(buf
->data(), buf_len
);
113 return IOResult::FromOSError(errno
);
115 return IOResult(res
, 0);