Work on Windows GN component build.
[chromium-blink-merge.git] / net / base / file_stream.cc
blobabce8ecbdb114a365613b697b4449dfbd9645da0
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"
11 namespace net {
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) {
28 if (IsOpen()) {
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_->file().IsValid();
47 int FileStream::Seek(base::File::Whence whence,
48 int64 offset,
49 const Int64CompletionCallback& callback) {
50 if (!IsOpen())
51 return ERR_UNEXPECTED;
53 context_->Seek(whence, offset, callback);
54 return ERR_IO_PENDING;
57 int FileStream::Read(IOBuffer* buf,
58 int buf_len,
59 const CompletionCallback& callback) {
60 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
61 tracked_objects::ScopedTracker tracking_profile(
62 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 FileStream::Read"));
64 if (!IsOpen())
65 return ERR_UNEXPECTED;
67 // read(..., 0) will return 0, which indicates end-of-file.
68 DCHECK_GT(buf_len, 0);
70 return context_->Read(buf, buf_len, callback);
73 int FileStream::Write(IOBuffer* buf,
74 int buf_len,
75 const CompletionCallback& callback) {
76 if (!IsOpen())
77 return ERR_UNEXPECTED;
79 DCHECK_GE(buf_len, 0);
80 return context_->Write(buf, buf_len, callback);
83 int FileStream::Flush(const CompletionCallback& callback) {
84 if (!IsOpen())
85 return ERR_UNEXPECTED;
87 context_->Flush(callback);
88 return ERR_IO_PENDING;
91 const base::File& FileStream::GetFileForTesting() const {
92 return context_->file();
95 } // namespace net