OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/base/file_stream.h" | |
6 | |
7 #include "base/profiler/scoped_tracker.h" | |
8 #include "net/base/file_stream_context.h" | |
9 #include "net/base/net_errors.h" | |
10 | |
11 namespace net { | |
12 | |
13 FileStream::FileStream(const scoped_refptr<base::TaskRunner>& task_runner) | |
14 : context_(new Context(task_runner)) { | |
15 } | |
16 | |
17 FileStream::FileStream(base::File file, | |
18 const scoped_refptr<base::TaskRunner>& task_runner) | |
19 : context_(new Context(file.Pass(), task_runner)) { | |
20 } | |
21 | |
22 FileStream::~FileStream() { | |
23 context_.release()->Orphan(); | |
24 } | |
25 | |
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; | |
31 } | |
32 | |
33 DCHECK(open_flags & base::File::FLAG_ASYNC); | |
34 context_->Open(path, open_flags, callback); | |
35 return ERR_IO_PENDING; | |
36 } | |
37 | |
38 int FileStream::Close(const CompletionCallback& callback) { | |
39 context_->Close(callback); | |
40 return ERR_IO_PENDING; | |
41 } | |
42 | |
43 bool FileStream::IsOpen() const { | |
44 return context_->file().IsValid(); | |
45 } | |
46 | |
47 int FileStream::Seek(base::File::Whence whence, | |
48 int64 offset, | |
49 const Int64CompletionCallback& callback) { | |
50 if (!IsOpen()) | |
51 return ERR_UNEXPECTED; | |
52 | |
53 context_->Seek(whence, offset, callback); | |
54 return ERR_IO_PENDING; | |
55 } | |
56 | |
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")); | |
63 | |
64 if (!IsOpen()) | |
65 return ERR_UNEXPECTED; | |
66 | |
67 // read(..., 0) will return 0, which indicates end-of-file. | |
68 DCHECK_GT(buf_len, 0); | |
69 | |
70 return context_->Read(buf, buf_len, callback); | |
71 } | |
72 | |
73 int FileStream::Write(IOBuffer* buf, | |
74 int buf_len, | |
75 const CompletionCallback& callback) { | |
76 if (!IsOpen()) | |
77 return ERR_UNEXPECTED; | |
78 | |
79 DCHECK_GE(buf_len, 0); | |
80 return context_->Write(buf, buf_len, callback); | |
81 } | |
82 | |
83 int FileStream::Flush(const CompletionCallback& callback) { | |
84 if (!IsOpen()) | |
85 return ERR_UNEXPECTED; | |
86 | |
87 context_->Flush(callback); | |
88 return ERR_IO_PENDING; | |
89 } | |
90 | |
91 const base::File& FileStream::GetFileForTesting() const { | |
92 return context_->file(); | |
93 } | |
94 | |
95 } // namespace net | |
OLD | NEW |