aw: Remove aw_switches
[chromium-blink-merge.git] / net / base / file_stream.h
blobda62937ced99610a92744b2b736a8e9f3fd490c7
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
8 // a time.
10 #ifndef NET_BASE_FILE_STREAM_H_
11 #define NET_BASE_FILE_STREAM_H_
13 #include "base/files/file.h"
14 #include "base/platform_file.h"
15 #include "base/task_runner.h"
16 #include "net/base/completion_callback.h"
17 #include "net/base/file_stream_whence.h"
18 #include "net/base/net_export.h"
19 #include "net/base/net_log.h"
21 namespace base {
22 class FilePath;
25 namespace net {
27 class IOBuffer;
29 class NET_EXPORT FileStream {
30 public:
31 // Creates a |FileStream| with a new |BoundNetLog| (based on |net_log|)
32 // attached. |net_log| may be NULL if no logging is needed.
33 // Uses |task_runner| for asynchronous operations.
34 FileStream(net::NetLog* net_log,
35 const scoped_refptr<base::TaskRunner>& task_runner);
37 // Same as above, but runs async tasks in base::WorkerPool.
38 explicit FileStream(net::NetLog* net_log);
40 // Construct a FileStream with an existing file handle and opening flags.
41 // |file| is valid file handle.
42 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was
43 // opened.
44 // |net_log| is the net log pointer to use to create a |BoundNetLog|. May be
45 // NULL if logging is not needed.
46 // Uses |task_runner| for asynchronous operations.
47 // Note: the new FileStream object takes ownership of the PlatformFile and
48 // will close it on destruction.
49 // This constructor is deprecated.
50 // TODO(rvargas): remove all references to PlatformFile.
51 FileStream(base::PlatformFile file,
52 int flags,
53 net::NetLog* net_log,
54 const scoped_refptr<base::TaskRunner>& task_runner);
56 // Same as above, but runs async tasks in base::WorkerPool.
57 // This constructor is deprecated.
58 FileStream(base::PlatformFile file, int flags, net::NetLog* net_log);
60 // Non-deprecated versions of the previous two constructors.
61 FileStream(base::File file,
62 net::NetLog* net_log,
63 const scoped_refptr<base::TaskRunner>& task_runner);
64 FileStream(base::File file, net::NetLog* net_log);
66 // The underlying file is closed automatically.
67 virtual ~FileStream();
69 // Call this method to open the FileStream asynchronously. The remaining
70 // methods cannot be used unless the file is opened successfully. Returns
71 // ERR_IO_PENDING if the operation is started. If the operation cannot be
72 // started then an error code is returned.
74 // Once the operation is done, |callback| will be run on the thread where
75 // Open() was called, with the result code. open_flags is a bitfield of
76 // base::PlatformFileFlags.
78 // If the file stream is not closed manually, the underlying file will be
79 // automatically closed when FileStream is destructed in an asynchronous
80 // manner (i.e. the file stream is closed in the background but you don't
81 // know when).
82 virtual int Open(const base::FilePath& path, int open_flags,
83 const CompletionCallback& callback);
85 // Call this method to open the FileStream synchronously.
86 // The remaining methods cannot be used unless this method returns OK. If
87 // the file cannot be opened then an error code is returned. open_flags is
88 // a bitfield of base::PlatformFileFlags
90 // If the file stream is not closed manually, the underlying file will be
91 // automatically closed when FileStream is destructed.
92 virtual int OpenSync(const base::FilePath& path, int open_flags);
94 // Returns ERR_IO_PENDING and closes the file asynchronously, calling
95 // |callback| when done.
96 // It is invalid to request any asynchronous operations while there is an
97 // in-flight asynchronous operation.
98 virtual int Close(const CompletionCallback& callback);
100 // Closes the file immediately and returns OK. If the file is open
101 // asynchronously, Close(const CompletionCallback&) should be used instead.
102 virtual int CloseSync();
104 // Returns true if Open succeeded and Close has not been called.
105 virtual bool IsOpen() const;
107 // Adjust the position from where data is read asynchronously.
108 // Upon success, ERR_IO_PENDING is returned and |callback| will be run
109 // on the thread where Seek() was called with the the stream position
110 // relative to the start of the file. Otherwise, an error code is returned.
111 // It is invalid to request any asynchronous operations while there is an
112 // in-flight asynchronous operation.
113 virtual int Seek(Whence whence, int64 offset,
114 const Int64CompletionCallback& callback);
116 // Adjust the position from where data is read synchronously.
117 // Upon success, the stream position relative to the start of the file is
118 // returned. Otherwise, an error code is returned. It is not valid to
119 // call SeekSync while a Read call has a pending completion.
120 virtual int64 SeekSync(Whence whence, int64 offset);
122 // Returns the number of bytes available to read from the current stream
123 // position until the end of the file. Otherwise, an error code is returned.
124 virtual int64 Available();
126 // Call this method to read data from the current stream position
127 // asynchronously. Up to buf_len bytes will be copied into buf. (In
128 // other words, partial reads are allowed.) Returns the number of bytes
129 // copied, 0 if at end-of-file, or an error code if the operation could
130 // not be performed.
132 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
133 // callback must be passed to this method. If the read could not
134 // complete synchronously, then ERR_IO_PENDING is returned, and the
135 // callback will be run on the thread where Read() was called, when the
136 // read has completed.
138 // It is valid to destroy or close the file stream while there is an
139 // asynchronous read in progress. That will cancel the read and allow
140 // the buffer to be freed.
142 // It is invalid to request any asynchronous operations while there is an
143 // in-flight asynchronous operation.
145 // This method must not be called if the stream was opened WRITE_ONLY.
146 virtual int Read(IOBuffer* buf, int buf_len,
147 const CompletionCallback& callback);
149 // Call this method to read data from the current stream position
150 // synchronously. Up to buf_len bytes will be copied into buf. (In
151 // other words, partial reads are allowed.) Returns the number of bytes
152 // copied, 0 if at end-of-file, or an error code if the operation could
153 // not be performed.
155 // The file must not be opened with PLATFORM_FILE_ASYNC.
156 // This method must not be called if the stream was opened WRITE_ONLY.
157 virtual int ReadSync(char* buf, int buf_len);
159 // Performs the same as ReadSync, but ensures that exactly buf_len bytes
160 // are copied into buf. A partial read may occur, but only as a result of
161 // end-of-file or fatal error. Returns the number of bytes copied into buf,
162 // 0 if at end-of-file and no bytes have been read into buf yet,
163 // or an error code if the operation could not be performed.
164 virtual int ReadUntilComplete(char *buf, int buf_len);
166 // Call this method to write data at the current stream position
167 // asynchronously. Up to buf_len bytes will be written from buf. (In
168 // other words, partial writes are allowed.) Returns the number of
169 // bytes written, or an error code if the operation could not be
170 // performed.
172 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
173 // callback must be passed to this method. If the write could not
174 // complete synchronously, then ERR_IO_PENDING is returned, and the
175 // callback will be run on the thread where Write() was called when
176 // the write has completed.
178 // It is valid to destroy or close the file stream while there is an
179 // asynchronous write in progress. That will cancel the write and allow
180 // the buffer to be freed.
182 // It is invalid to request any asynchronous operations while there is an
183 // in-flight asynchronous operation.
185 // This method must not be called if the stream was opened READ_ONLY.
187 // Zero byte writes are not allowed.
188 virtual int Write(IOBuffer* buf, int buf_len,
189 const CompletionCallback& callback);
191 // Call this method to write data at the current stream position
192 // synchronously. Up to buf_len bytes will be written from buf. (In
193 // other words, partial writes are allowed.) Returns the number of
194 // bytes written, or an error code if the operation could not be
195 // performed.
197 // The file must not be opened with PLATFORM_FILE_ASYNC.
198 // This method must not be called if the stream was opened READ_ONLY.
200 // Zero byte writes are not allowed.
201 virtual int WriteSync(const char* buf, int buf_len);
203 // Truncates the file to be |bytes| length. This is only valid for writable
204 // files. After truncation the file stream is positioned at |bytes|. The new
205 // position is returned, or a value < 0 on error.
206 // WARNING: one may not truncate a file beyond its current length on any
207 // platform with this call.
208 virtual int64 Truncate(int64 bytes);
210 // Forces out a filesystem sync on this file to make sure that the file was
211 // written out to disk and is not currently sitting in the buffer. This does
212 // not have to be called, it just forces one to happen at the time of
213 // calling.
215 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
216 // callback must be passed to this method. If the write could not
217 // complete synchronously, then ERR_IO_PENDING is returned, and the
218 // callback will be run on the thread where Flush() was called when
219 // the write has completed.
221 // It is valid to destroy or close the file stream while there is an
222 // asynchronous flush in progress. That will cancel the flush and allow
223 // the buffer to be freed.
225 // It is invalid to request any asynchronous operations while there is an
226 // in-flight asynchronous operation.
228 // This method should not be called if the stream was opened READ_ONLY.
229 virtual int Flush(const CompletionCallback& callback);
231 // Forces out a filesystem sync on this file to make sure that the file was
232 // written out to disk and is not currently sitting in the buffer. This does
233 // not have to be called, it just forces one to happen at the time of
234 // calling.
236 // Returns an error code if the operation could not be performed.
238 // This method should not be called if the stream was opened READ_ONLY.
239 virtual int FlushSync();
241 // Turns on UMA error statistics gathering.
242 void EnableErrorStatistics();
244 // Sets the source reference for net-internals logging.
245 // Creates source dependency events between |owner_bound_net_log| and
246 // |bound_net_log_|. Each gets an event showing the dependency on the other.
247 // If only one of those is valid, it gets an event showing that a change
248 // of ownership happened, but without details.
249 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log);
251 // Returns the underlying platform file for testing.
252 const base::File& GetFileForTesting() const;
254 private:
255 class Context;
257 net::BoundNetLog bound_net_log_;
259 // Context performing I/O operations. It was extracted into a separate class
260 // to perform asynchronous operations because FileStream can be destroyed
261 // before completion of an async operation. Also if a FileStream is destroyed
262 // without explicitly calling Close, the file should be closed asynchronously
263 // without delaying FileStream's destructor.
264 scoped_ptr<Context> context_;
266 DISALLOW_COPY_AND_ASSIGN(FileStream);
269 } // namespace net
271 #endif // NET_BASE_FILE_STREAM_H_