• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "net/base/file_stream_context.h"
8 #include "net/base/net_errors.h"
9 
10 namespace net {
11 
FileStream(const scoped_refptr<base::TaskRunner> & task_runner)12 FileStream::FileStream(const scoped_refptr<base::TaskRunner>& task_runner)
13     : context_(new Context(task_runner)) {
14 }
15 
FileStream(base::File file,const scoped_refptr<base::TaskRunner> & task_runner)16 FileStream::FileStream(base::File file,
17                        const scoped_refptr<base::TaskRunner>& task_runner)
18     : context_(new Context(file.Pass(), task_runner)) {
19 }
20 
~FileStream()21 FileStream::~FileStream() {
22   context_.release()->Orphan();
23 }
24 
Open(const base::FilePath & path,int open_flags,const CompletionCallback & callback)25 int FileStream::Open(const base::FilePath& path, int open_flags,
26                      const CompletionCallback& callback) {
27   if (IsOpen()) {
28     DLOG(FATAL) << "File is already open!";
29     return ERR_UNEXPECTED;
30   }
31 
32   DCHECK(open_flags & base::File::FLAG_ASYNC);
33   context_->OpenAsync(path, open_flags, callback);
34   return ERR_IO_PENDING;
35 }
36 
Close(const CompletionCallback & callback)37 int FileStream::Close(const CompletionCallback& callback) {
38   context_->CloseAsync(callback);
39   return ERR_IO_PENDING;
40 }
41 
IsOpen() const42 bool FileStream::IsOpen() const {
43   return context_->file().IsValid();
44 }
45 
Seek(Whence whence,int64 offset,const Int64CompletionCallback & callback)46 int FileStream::Seek(Whence whence,
47                      int64 offset,
48                      const Int64CompletionCallback& callback) {
49   if (!IsOpen())
50     return ERR_UNEXPECTED;
51 
52   context_->SeekAsync(whence, offset, callback);
53   return ERR_IO_PENDING;
54 }
55 
Read(IOBuffer * buf,int buf_len,const CompletionCallback & callback)56 int FileStream::Read(IOBuffer* buf,
57                      int buf_len,
58                      const CompletionCallback& callback) {
59   if (!IsOpen())
60     return ERR_UNEXPECTED;
61 
62   // read(..., 0) will return 0, which indicates end-of-file.
63   DCHECK_GT(buf_len, 0);
64 
65   return context_->ReadAsync(buf, buf_len, callback);
66 }
67 
Write(IOBuffer * buf,int buf_len,const CompletionCallback & callback)68 int FileStream::Write(IOBuffer* buf,
69                       int buf_len,
70                       const CompletionCallback& callback) {
71   if (!IsOpen())
72     return ERR_UNEXPECTED;
73 
74   // write(..., 0) will return 0, which indicates end-of-file.
75   DCHECK_GT(buf_len, 0);
76 
77   return context_->WriteAsync(buf, buf_len, callback);
78 }
79 
Flush(const CompletionCallback & callback)80 int FileStream::Flush(const CompletionCallback& callback) {
81   if (!IsOpen())
82     return ERR_UNEXPECTED;
83 
84   context_->FlushAsync(callback);
85   return ERR_IO_PENDING;
86 }
87 
GetFileForTesting() const88 const base::File& FileStream::GetFileForTesting() const {
89   return context_->file();
90 }
91 
92 }  // namespace net
93