Add signalSyncPoint to the WebGraphicsContext3D command buffer impls.
[chromium-blink-merge.git] / net / base / file_stream.h
blob979aa8422215cd67d7618107dc01adcd702af1a9
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/platform_file.h"
14 #include "net/base/completion_callback.h"
15 #include "net/base/file_stream_whence.h"
16 #include "net/base/net_export.h"
17 #include "net/base/net_log.h"
19 namespace base {
20 class FilePath;
23 namespace net {
25 class IOBuffer;
27 class NET_EXPORT FileStream {
28 public:
29 // Creates a |FileStream| with a new |BoundNetLog| (based on |net_log|)
30 // attached. |net_log| may be NULL if no logging is needed.
31 explicit FileStream(net::NetLog* net_log);
33 // Construct a FileStream with an existing file handle and opening flags.
34 // |file| is valid file handle.
35 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was
36 // opened.
37 // |net_log| is the net log pointer to use to create a |BoundNetLog|. May be
38 // NULL if logging is not needed.
39 // Note: the new FileStream object takes ownership of the PlatformFile and
40 // will close it on destruction.
41 FileStream(base::PlatformFile file, int flags, net::NetLog* net_log);
43 // The underlying file is closed automatically.
44 virtual ~FileStream();
46 // Call this method to open the FileStream asynchronously. The remaining
47 // methods cannot be used unless the file is opened successfully. Returns
48 // ERR_IO_PENDING if the operation is started. If the operation cannot be
49 // started then an error code is returned.
51 // Once the operation is done, |callback| will be run on the thread where
52 // Open() was called, with the result code. open_flags is a bitfield of
53 // base::PlatformFileFlags.
55 // If the file stream is not closed manually, the underlying file will be
56 // automatically closed when FileStream is destructed in an asynchronous
57 // manner (i.e. the file stream is closed in the background but you don't
58 // know when).
59 virtual int Open(const base::FilePath& path, int open_flags,
60 const CompletionCallback& callback);
62 // Call this method to open the FileStream synchronously.
63 // The remaining methods cannot be used unless this method returns OK. If
64 // the file cannot be opened then an error code is returned. open_flags is
65 // a bitfield of base::PlatformFileFlags
67 // If the file stream is not closed manually, the underlying file will be
68 // automatically closed when FileStream is destructed.
69 virtual int OpenSync(const base::FilePath& path, int open_flags);
71 // Returns true if Open succeeded and Close has not been called.
72 virtual bool IsOpen() const;
74 // Adjust the position from where data is read asynchronously.
75 // Upon success, ERR_IO_PENDING is returned and |callback| will be run
76 // on the thread where Seek() was called with the the stream position
77 // relative to the start of the file. Otherwise, an error code is returned.
78 // It is invalid to request any asynchronous operations while there is an
79 // in-flight asynchronous operation.
80 virtual int Seek(Whence whence, int64 offset,
81 const Int64CompletionCallback& callback);
83 // Adjust the position from where data is read synchronously.
84 // Upon success, the stream position relative to the start of the file is
85 // returned. Otherwise, an error code is returned. It is not valid to
86 // call SeekSync while a Read call has a pending completion.
87 virtual int64 SeekSync(Whence whence, int64 offset);
89 // Returns the number of bytes available to read from the current stream
90 // position until the end of the file. Otherwise, an error code is returned.
91 virtual int64 Available();
93 // Call this method to read data from the current stream position
94 // asynchronously. Up to buf_len bytes will be copied into buf. (In
95 // other words, partial reads are allowed.) Returns the number of bytes
96 // copied, 0 if at end-of-file, or an error code if the operation could
97 // not be performed.
99 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
100 // callback must be passed to this method. If the read could not
101 // complete synchronously, then ERR_IO_PENDING is returned, and the
102 // callback will be run on the thread where Read() was called, when the
103 // read has completed.
105 // It is valid to destroy or close the file stream while there is an
106 // asynchronous read in progress. That will cancel the read and allow
107 // the buffer to be freed.
109 // It is invalid to request any asynchronous operations while there is an
110 // in-flight asynchronous operation.
112 // This method must not be called if the stream was opened WRITE_ONLY.
113 virtual int Read(IOBuffer* buf, int buf_len,
114 const CompletionCallback& callback);
116 // Call this method to read data from the current stream position
117 // synchronously. Up to buf_len bytes will be copied into buf. (In
118 // other words, partial reads are allowed.) Returns the number of bytes
119 // copied, 0 if at end-of-file, or an error code if the operation could
120 // not be performed.
122 // The file must not be opened with PLATFORM_FILE_ASYNC.
123 // This method must not be called if the stream was opened WRITE_ONLY.
124 virtual int ReadSync(char* buf, int buf_len);
126 // Performs the same as ReadSync, but ensures that exactly buf_len bytes
127 // are copied into buf. A partial read may occur, but only as a result of
128 // end-of-file or fatal error. Returns the number of bytes copied into buf,
129 // 0 if at end-of-file and no bytes have been read into buf yet,
130 // or an error code if the operation could not be performed.
131 virtual int ReadUntilComplete(char *buf, int buf_len);
133 // Call this method to write data at the current stream position
134 // asynchronously. Up to buf_len bytes will be written from buf. (In
135 // other words, partial writes are allowed.) Returns the number of
136 // bytes written, or an error code if the operation could not be
137 // performed.
139 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
140 // callback must be passed to this method. If the write could not
141 // complete synchronously, then ERR_IO_PENDING is returned, and the
142 // callback will be run on the thread where Write() was called when
143 // the write has completed.
145 // It is valid to destroy or close the file stream while there is an
146 // asynchronous write in progress. That will cancel the write and allow
147 // the buffer to be freed.
149 // It is invalid to request any asynchronous operations while there is an
150 // in-flight asynchronous operation.
152 // This method must not be called if the stream was opened READ_ONLY.
154 // Zero byte writes are not allowed.
155 virtual int Write(IOBuffer* buf, int buf_len,
156 const CompletionCallback& callback);
158 // Call this method to write data at the current stream position
159 // synchronously. Up to buf_len bytes will be written from buf. (In
160 // other words, partial writes are allowed.) Returns the number of
161 // bytes written, or an error code if the operation could not be
162 // performed.
164 // The file must not be opened with PLATFORM_FILE_ASYNC.
165 // This method must not be called if the stream was opened READ_ONLY.
167 // Zero byte writes are not allowed.
168 virtual int WriteSync(const char* buf, int buf_len);
170 // Truncates the file to be |bytes| length. This is only valid for writable
171 // files. After truncation the file stream is positioned at |bytes|. The new
172 // position is returned, or a value < 0 on error.
173 // WARNING: one may not truncate a file beyond its current length on any
174 // platform with this call.
175 virtual int64 Truncate(int64 bytes);
177 // Forces out a filesystem sync on this file to make sure that the file was
178 // written out to disk and is not currently sitting in the buffer. This does
179 // not have to be called, it just forces one to happen at the time of
180 // calling.
182 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
183 // callback must be passed to this method. If the write could not
184 // complete synchronously, then ERR_IO_PENDING is returned, and the
185 // callback will be run on the thread where Flush() was called when
186 // the write has completed.
188 // It is valid to destroy or close the file stream while there is an
189 // asynchronous flush in progress. That will cancel the flush and allow
190 // the buffer to be freed.
192 // It is invalid to request any asynchronous operations while there is an
193 // in-flight asynchronous operation.
195 // This method should not be called if the stream was opened READ_ONLY.
196 virtual int Flush(const CompletionCallback& callback);
198 // Forces out a filesystem sync on this file to make sure that the file was
199 // written out to disk and is not currently sitting in the buffer. This does
200 // not have to be called, it just forces one to happen at the time of
201 // calling.
203 // Returns an error code if the operation could not be performed.
205 // This method should not be called if the stream was opened READ_ONLY.
206 virtual int FlushSync();
208 // Turns on UMA error statistics gathering.
209 void EnableErrorStatistics();
211 // Sets the source reference for net-internals logging.
212 // Creates source dependency events between |owner_bound_net_log| and
213 // |bound_net_log_|. Each gets an event showing the dependency on the other.
214 // If only one of those is valid, it gets an event showing that a change
215 // of ownership happened, but without details.
216 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log);
218 // Returns the underlying platform file for testing.
219 base::PlatformFile GetPlatformFileForTesting();
221 private:
222 class Context;
224 bool is_async() const { return !!(open_flags_ & base::PLATFORM_FILE_ASYNC); }
226 int open_flags_;
227 net::BoundNetLog bound_net_log_;
229 // Context performing I/O operations. It was extracted into separate class
230 // to perform asynchronous operations because FileStream can be destroyed
231 // before completion of async operation. Also if async FileStream is destroyed
232 // without explicit closing file should be closed asynchronously without
233 // delaying FileStream's destructor. To perform all that separate object is
234 // necessary.
235 scoped_ptr<Context> context_;
237 DISALLOW_COPY_AND_ASSIGN(FileStream);
240 } // namespace net
242 #endif // NET_BASE_FILE_STREAM_H_