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 // This file defines FileStream, a basic interface for reading and writing files
6 // synchronously or asynchronously with support for seeking to an offset.
7 // Note that even when used asynchronously, only one operation is supported at
10 #ifndef NET_BASE_FILE_STREAM_H_
11 #define NET_BASE_FILE_STREAM_H_
13 #include "base/files/file.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/net_export.h"
26 class NET_EXPORT FileStream
{
28 // Creates a FileStream.
29 // Uses |task_runner| for asynchronous operations.
30 explicit FileStream(const scoped_refptr
<base::TaskRunner
>& task_runner
);
32 // Construct a FileStream with an existing valid |file|.
33 // Uses |task_runner| for asynchronous operations.
34 FileStream(base::File file
,
35 const scoped_refptr
<base::TaskRunner
>& task_runner
);
37 // The underlying file is closed automatically.
38 virtual ~FileStream();
40 // Call this method to open the FileStream asynchronously. The remaining
41 // methods cannot be used unless the file is opened successfully. Returns
42 // ERR_IO_PENDING if the operation is started. If the operation cannot be
43 // started then an error code is returned.
45 // Once the operation is done, |callback| will be run on the thread where
46 // Open() was called, with the result code. open_flags is a bitfield of
49 // If the file stream is not closed manually, the underlying file will be
50 // automatically closed when FileStream is destructed in an asynchronous
51 // manner (i.e. the file stream is closed in the background but you don't
53 virtual int Open(const base::FilePath
& path
, int open_flags
,
54 const CompletionCallback
& callback
);
56 // Returns ERR_IO_PENDING and closes the file asynchronously, calling
57 // |callback| when done.
58 // It is invalid to request any asynchronous operations while there is an
59 // in-flight asynchronous operation.
60 virtual int Close(const CompletionCallback
& callback
);
62 // Returns true if Open succeeded and Close has not been called.
63 virtual bool IsOpen() const;
65 // Adjust the position from the start of the file where data is read
66 // asynchronously. Upon success, ERR_IO_PENDING is returned and |callback|
67 // will be run on the thread where Seek() was called with the the stream
68 // position relative to the start of the file. Otherwise, an error code is
69 // returned. It is invalid to request any asynchronous operations while there
70 // is an in-flight asynchronous operation.
71 virtual int Seek(int64_t offset
, const Int64CompletionCallback
& callback
);
73 // Call this method to read data from the current stream position
74 // asynchronously. Up to buf_len bytes will be copied into buf. (In
75 // other words, partial reads are allowed.) Returns the number of bytes
76 // copied, 0 if at end-of-file, or an error code if the operation could
79 // The file must be opened with FLAG_ASYNC, and a non-null
80 // callback must be passed to this method. If the read could not
81 // complete synchronously, then ERR_IO_PENDING is returned, and the
82 // callback will be run on the thread where Read() was called, when the
83 // read has completed.
85 // It is valid to destroy or close the file stream while there is an
86 // asynchronous read in progress. That will cancel the read and allow
87 // the buffer to be freed.
89 // It is invalid to request any asynchronous operations while there is an
90 // in-flight asynchronous operation.
92 // This method must not be called if the stream was opened WRITE_ONLY.
93 virtual int Read(IOBuffer
* buf
, int buf_len
,
94 const CompletionCallback
& callback
);
96 // Call this method to write data at the current stream position
97 // asynchronously. Up to buf_len bytes will be written from buf. (In
98 // other words, partial writes are allowed.) Returns the number of
99 // bytes written, or an error code if the operation could not be
102 // The file must be opened with FLAG_ASYNC, and a non-null
103 // callback must be passed to this method. If the write could not
104 // complete synchronously, then ERR_IO_PENDING is returned, and the
105 // callback will be run on the thread where Write() was called when
106 // the write has completed.
108 // It is valid to destroy or close the file stream while there is an
109 // asynchronous write in progress. That will cancel the write and allow
110 // the buffer to be freed.
112 // It is invalid to request any asynchronous operations while there is an
113 // in-flight asynchronous operation.
115 // This method must not be called if the stream was opened READ_ONLY.
117 // Zero byte writes are not allowed.
118 virtual int Write(IOBuffer
* buf
, int buf_len
,
119 const CompletionCallback
& callback
);
121 // Forces out a filesystem sync on this file to make sure that the file was
122 // written out to disk and is not currently sitting in the buffer. This does
123 // not have to be called, it just forces one to happen at the time of
126 // The file must be opened with FLAG_ASYNC, and a non-null
127 // callback must be passed to this method. If the write could not
128 // complete synchronously, then ERR_IO_PENDING is returned, and the
129 // callback will be run on the thread where Flush() was called when
130 // the write has completed.
132 // It is valid to destroy or close the file stream while there is an
133 // asynchronous flush in progress. That will cancel the flush and allow
134 // the buffer to be freed.
136 // It is invalid to request any asynchronous operations while there is an
137 // in-flight asynchronous operation.
139 // This method should not be called if the stream was opened READ_ONLY.
140 virtual int Flush(const CompletionCallback
& callback
);
145 // Context performing I/O operations. It was extracted into a separate class
146 // to perform asynchronous operations because FileStream can be destroyed
147 // before completion of an async operation. Also if a FileStream is destroyed
148 // without explicitly calling Close, the file should be closed asynchronously
149 // without delaying FileStream's destructor.
150 scoped_ptr
<Context
> context_
;
152 DISALLOW_COPY_AND_ASSIGN(FileStream
);
157 #endif // NET_BASE_FILE_STREAM_H_