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/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(
81 base::File::Whence whence
,
83 int64 res
= file_
.Seek(whence
, offset
);
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
,
96 int res
= file_
.ReadAtCurrentPosNoBestEffort(buf
->data(), buf_len
);
98 return IOResult::FromOSError(errno
);
100 return IOResult(res
, 0);
103 FileStream::Context::IOResult
FileStream::Context::WriteFileImpl(
104 scoped_refptr
<IOBuffer
> buf
,
106 int res
= file_
.WriteAtCurrentPosNoBestEffort(buf
->data(), buf_len
);
108 return IOResult::FromOSError(errno
);
110 return IOResult(res
, 0);