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.h"
7 #include "net/base/file_stream_context.h"
8 #include "net/base/net_errors.h"
12 FileStream::FileStream(const scoped_refptr
<base::TaskRunner
>& task_runner
)
13 : context_(new Context(task_runner
)) {
16 FileStream::FileStream(base::File file
,
17 const scoped_refptr
<base::TaskRunner
>& task_runner
)
18 : context_(new Context(file
.Pass(), task_runner
)) {
21 FileStream::~FileStream() {
22 context_
.release()->Orphan();
25 int FileStream::Open(const base::FilePath
& path
, int open_flags
,
26 const CompletionCallback
& callback
) {
28 DLOG(FATAL
) << "File is already open!";
29 return ERR_UNEXPECTED
;
32 DCHECK(open_flags
& base::File::FLAG_ASYNC
);
33 context_
->Open(path
, open_flags
, callback
);
34 return ERR_IO_PENDING
;
37 int FileStream::Close(const CompletionCallback
& callback
) {
38 context_
->Close(callback
);
39 return ERR_IO_PENDING
;
42 bool FileStream::IsOpen() const {
43 return context_
->file().IsValid();
46 int FileStream::Seek(base::File::Whence whence
,
48 const Int64CompletionCallback
& callback
) {
50 return ERR_UNEXPECTED
;
52 context_
->Seek(whence
, offset
, callback
);
53 return ERR_IO_PENDING
;
56 int FileStream::Read(IOBuffer
* buf
,
58 const CompletionCallback
& callback
) {
60 return ERR_UNEXPECTED
;
62 // read(..., 0) will return 0, which indicates end-of-file.
63 DCHECK_GT(buf_len
, 0);
65 return context_
->Read(buf
, buf_len
, callback
);
68 int FileStream::Write(IOBuffer
* buf
,
70 const CompletionCallback
& callback
) {
72 return ERR_UNEXPECTED
;
74 DCHECK_GE(buf_len
, 0);
75 return context_
->Write(buf
, buf_len
, callback
);
78 int FileStream::Flush(const CompletionCallback
& callback
) {
80 return ERR_UNEXPECTED
;
82 context_
->Flush(callback
);
83 return ERR_IO_PENDING
;
86 const base::File
& FileStream::GetFileForTesting() const {
87 return context_
->file();