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 "base/profiler/scoped_tracker.h"
8 #include "net/base/file_stream_context.h"
9 #include "net/base/net_errors.h"
13 FileStream::FileStream(const scoped_refptr
<base::TaskRunner
>& task_runner
)
14 : context_(new Context(task_runner
)) {
17 FileStream::FileStream(base::File file
,
18 const scoped_refptr
<base::TaskRunner
>& task_runner
)
19 : context_(new Context(file
.Pass(), task_runner
)) {
22 FileStream::~FileStream() {
23 context_
.release()->Orphan();
26 int FileStream::Open(const base::FilePath
& path
, int open_flags
,
27 const CompletionCallback
& callback
) {
29 DLOG(FATAL
) << "File is already open!";
30 return ERR_UNEXPECTED
;
33 DCHECK(open_flags
& base::File::FLAG_ASYNC
);
34 context_
->Open(path
, open_flags
, callback
);
35 return ERR_IO_PENDING
;
38 int FileStream::Close(const CompletionCallback
& callback
) {
39 context_
->Close(callback
);
40 return ERR_IO_PENDING
;
43 bool FileStream::IsOpen() const {
44 return context_
->IsOpen();
47 int FileStream::Seek(int64_t offset
, const Int64CompletionCallback
& callback
) {
49 return ERR_UNEXPECTED
;
51 context_
->Seek(offset
, callback
);
52 return ERR_IO_PENDING
;
55 int FileStream::Read(IOBuffer
* buf
,
57 const CompletionCallback
& callback
) {
58 // TODO(rvargas): Remove ScopedTracker below once crbug.com/475751 is fixed.
59 tracked_objects::ScopedTracker
tracking_profile(
60 FROM_HERE_WITH_EXPLICIT_FUNCTION("475751 FileStream::Read"));
63 return ERR_UNEXPECTED
;
65 // read(..., 0) will return 0, which indicates end-of-file.
66 DCHECK_GT(buf_len
, 0);
68 return context_
->Read(buf
, buf_len
, callback
);
71 int FileStream::Write(IOBuffer
* buf
,
73 const CompletionCallback
& callback
) {
75 return ERR_UNEXPECTED
;
77 DCHECK_GE(buf_len
, 0);
78 return context_
->Write(buf
, buf_len
, callback
);
81 int FileStream::Flush(const CompletionCallback
& callback
) {
83 return ERR_UNEXPECTED
;
85 context_
->Flush(callback
);
86 return ERR_IO_PENDING
;