include <limits> for std::numeric_limits
[chromium-blink-merge.git] / net / base / file_stream.h
blob89db65e15b5ae49e50de78874e64c3babf7df640
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 "net/base/completion_callback.h"
15 #include "net/base/net_export.h"
17 namespace base {
18 class FilePath;
19 class TaskRunner;
22 namespace net {
24 class IOBuffer;
26 class NET_EXPORT FileStream {
27 public:
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
47 // base::File::Flags.
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
52 // know when).
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 where data is read asynchronously.
66 // Upon success, ERR_IO_PENDING is returned and |callback| will be run
67 // on the thread where Seek() was called with the the stream position
68 // relative to the start of the file. Otherwise, an error code is returned.
69 // It is invalid to request any asynchronous operations while there is an
70 // in-flight asynchronous operation.
71 virtual int Seek(base::File::Whence whence, int64 offset,
72 const Int64CompletionCallback& callback);
74 // Call this method to read data from the current stream position
75 // asynchronously. Up to buf_len bytes will be copied into buf. (In
76 // other words, partial reads are allowed.) Returns the number of bytes
77 // copied, 0 if at end-of-file, or an error code if the operation could
78 // not be performed.
80 // The file must be opened with FLAG_ASYNC, and a non-null
81 // callback must be passed to this method. If the read could not
82 // complete synchronously, then ERR_IO_PENDING is returned, and the
83 // callback will be run on the thread where Read() was called, when the
84 // read has completed.
86 // It is valid to destroy or close the file stream while there is an
87 // asynchronous read in progress. That will cancel the read and allow
88 // the buffer to be freed.
90 // It is invalid to request any asynchronous operations while there is an
91 // in-flight asynchronous operation.
93 // This method must not be called if the stream was opened WRITE_ONLY.
94 virtual int Read(IOBuffer* buf, int buf_len,
95 const CompletionCallback& callback);
97 // Call this method to write data at the current stream position
98 // asynchronously. Up to buf_len bytes will be written from buf. (In
99 // other words, partial writes are allowed.) Returns the number of
100 // bytes written, or an error code if the operation could not be
101 // performed.
103 // The file must be opened with FLAG_ASYNC, and a non-null
104 // callback must be passed to this method. If the write could not
105 // complete synchronously, then ERR_IO_PENDING is returned, and the
106 // callback will be run on the thread where Write() was called when
107 // the write has completed.
109 // It is valid to destroy or close the file stream while there is an
110 // asynchronous write in progress. That will cancel the write and allow
111 // the buffer to be freed.
113 // It is invalid to request any asynchronous operations while there is an
114 // in-flight asynchronous operation.
116 // This method must not be called if the stream was opened READ_ONLY.
118 // Zero byte writes are not allowed.
119 virtual int Write(IOBuffer* buf, int buf_len,
120 const CompletionCallback& callback);
122 // Forces out a filesystem sync on this file to make sure that the file was
123 // written out to disk and is not currently sitting in the buffer. This does
124 // not have to be called, it just forces one to happen at the time of
125 // calling.
127 // The file must be opened with FLAG_ASYNC, and a non-null
128 // callback must be passed to this method. If the write could not
129 // complete synchronously, then ERR_IO_PENDING is returned, and the
130 // callback will be run on the thread where Flush() was called when
131 // the write has completed.
133 // It is valid to destroy or close the file stream while there is an
134 // asynchronous flush in progress. That will cancel the flush and allow
135 // the buffer to be freed.
137 // It is invalid to request any asynchronous operations while there is an
138 // in-flight asynchronous operation.
140 // This method should not be called if the stream was opened READ_ONLY.
141 virtual int Flush(const CompletionCallback& callback);
143 // Returns the underlying file for testing.
144 const base::File& GetFileForTesting() const;
146 private:
147 class Context;
149 // Context performing I/O operations. It was extracted into a separate class
150 // to perform asynchronous operations because FileStream can be destroyed
151 // before completion of an async operation. Also if a FileStream is destroyed
152 // without explicitly calling Close, the file should be closed asynchronously
153 // without delaying FileStream's destructor.
154 scoped_ptr<Context> context_;
156 DISALLOW_COPY_AND_ASSIGN(FileStream);
159 } // namespace net
161 #endif // NET_BASE_FILE_STREAM_H_