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/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"
25 FileStream::Context::Context(const scoped_refptr
<base::TaskRunner
>& task_runner
)
26 : async_in_progress_(false),
28 task_runner_(task_runner
) {
31 FileStream::Context::Context(base::File file
,
32 const scoped_refptr
<base::TaskRunner
>& task_runner
)
34 async_in_progress_(false),
36 task_runner_(task_runner
) {
39 FileStream::Context::~Context() {
42 int FileStream::Context::Read(IOBuffer
* in_buf
,
44 const CompletionCallback
& callback
) {
45 DCHECK(!async_in_progress_
);
47 scoped_refptr
<IOBuffer
> buf
= in_buf
;
48 const bool posted
= base::PostTaskAndReplyWithResult(
51 base::Bind(&Context::ReadFileImpl
, base::Unretained(this), buf
, buf_len
),
52 base::Bind(&Context::OnAsyncCompleted
,
53 base::Unretained(this),
54 IntToInt64(callback
)));
57 async_in_progress_
= true;
58 return ERR_IO_PENDING
;
61 int FileStream::Context::Write(IOBuffer
* in_buf
,
63 const CompletionCallback
& callback
) {
64 DCHECK(!async_in_progress_
);
66 scoped_refptr
<IOBuffer
> buf
= in_buf
;
67 const bool posted
= base::PostTaskAndReplyWithResult(
70 base::Bind(&Context::WriteFileImpl
, base::Unretained(this), buf
, buf_len
),
71 base::Bind(&Context::OnAsyncCompleted
,
72 base::Unretained(this),
73 IntToInt64(callback
)));
76 async_in_progress_
= true;
77 return ERR_IO_PENDING
;
80 FileStream::Context::IOResult
FileStream::Context::SeekFileImpl(
82 int64_t res
= file_
.Seek(base::File::FROM_BEGIN
, offset
);
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
,
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
);
101 return IOResult::FromOSError(errno
);
103 return IOResult(res
, 0);
106 FileStream::Context::IOResult
FileStream::Context::WriteFileImpl(
107 scoped_refptr
<IOBuffer
> buf
,
109 int res
= file_
.WriteAtCurrentPosNoBestEffort(buf
->data(), buf_len
);
111 return IOResult::FromOSError(errno
);
113 return IOResult(res
, 0);