Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / net / base / file_stream.h
blob850c78fe2288625bca16d8af5643808517dfc8fc
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"
20 namespace base {
21 class FilePath;
24 namespace net {
26 class IOBuffer;
28 class NET_EXPORT FileStream {
29 public:
30 // Creates a FileStream.
31 // Uses |task_runner| for asynchronous operations.
32 explicit FileStream(const scoped_refptr<base::TaskRunner>& task_runner);
34 // Construct a FileStream with an existing file handle and opening flags.
35 // |file| is valid file handle.
36 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was
37 // opened.
38 // Uses |task_runner| for asynchronous operations.
39 // Note: the new FileStream object takes ownership of the PlatformFile and
40 // will close it on destruction.
41 // This constructor is deprecated.
42 // TODO(rvargas): remove all references to PlatformFile.
43 FileStream(base::PlatformFile file,
44 int flags,
45 const scoped_refptr<base::TaskRunner>& task_runner);
47 // Non-deprecated version of the previous constructor.
48 FileStream(base::File file,
49 const scoped_refptr<base::TaskRunner>& task_runner);
51 // The underlying file is closed automatically.
52 virtual ~FileStream();
54 // Call this method to open the FileStream asynchronously. The remaining
55 // methods cannot be used unless the file is opened successfully. Returns
56 // ERR_IO_PENDING if the operation is started. If the operation cannot be
57 // started then an error code is returned.
59 // Once the operation is done, |callback| will be run on the thread where
60 // Open() was called, with the result code. open_flags is a bitfield of
61 // base::PlatformFileFlags.
63 // If the file stream is not closed manually, the underlying file will be
64 // automatically closed when FileStream is destructed in an asynchronous
65 // manner (i.e. the file stream is closed in the background but you don't
66 // know when).
67 virtual int Open(const base::FilePath& path, int open_flags,
68 const CompletionCallback& callback);
70 // Returns ERR_IO_PENDING and closes the file asynchronously, calling
71 // |callback| when done.
72 // It is invalid to request any asynchronous operations while there is an
73 // in-flight asynchronous operation.
74 virtual int Close(const CompletionCallback& callback);
76 // Returns true if Open succeeded and Close has not been called.
77 virtual bool IsOpen() const;
79 // Adjust the position from where data is read asynchronously.
80 // Upon success, ERR_IO_PENDING is returned and |callback| will be run
81 // on the thread where Seek() was called with the the stream position
82 // relative to the start of the file. Otherwise, an error code is returned.
83 // It is invalid to request any asynchronous operations while there is an
84 // in-flight asynchronous operation.
85 virtual int Seek(Whence whence, int64 offset,
86 const Int64CompletionCallback& callback);
88 // Call this method to read data from the current stream position
89 // asynchronously. Up to buf_len bytes will be copied into buf. (In
90 // other words, partial reads are allowed.) Returns the number of bytes
91 // copied, 0 if at end-of-file, or an error code if the operation could
92 // not be performed.
94 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
95 // callback must be passed to this method. If the read could not
96 // complete synchronously, then ERR_IO_PENDING is returned, and the
97 // callback will be run on the thread where Read() was called, when the
98 // read has completed.
100 // It is valid to destroy or close the file stream while there is an
101 // asynchronous read in progress. That will cancel the read and allow
102 // the buffer to be freed.
104 // It is invalid to request any asynchronous operations while there is an
105 // in-flight asynchronous operation.
107 // This method must not be called if the stream was opened WRITE_ONLY.
108 virtual int Read(IOBuffer* buf, int buf_len,
109 const CompletionCallback& callback);
111 // Call this method to write data at the current stream position
112 // asynchronously. Up to buf_len bytes will be written from buf. (In
113 // other words, partial writes are allowed.) Returns the number of
114 // bytes written, or an error code if the operation could not be
115 // performed.
117 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
118 // callback must be passed to this method. If the write could not
119 // complete synchronously, then ERR_IO_PENDING is returned, and the
120 // callback will be run on the thread where Write() was called when
121 // the write has completed.
123 // It is valid to destroy or close the file stream while there is an
124 // asynchronous write in progress. That will cancel the write and allow
125 // the buffer to be freed.
127 // It is invalid to request any asynchronous operations while there is an
128 // in-flight asynchronous operation.
130 // This method must not be called if the stream was opened READ_ONLY.
132 // Zero byte writes are not allowed.
133 virtual int Write(IOBuffer* buf, int buf_len,
134 const CompletionCallback& callback);
136 // Forces out a filesystem sync on this file to make sure that the file was
137 // written out to disk and is not currently sitting in the buffer. This does
138 // not have to be called, it just forces one to happen at the time of
139 // calling.
141 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null
142 // callback must be passed to this method. If the write could not
143 // complete synchronously, then ERR_IO_PENDING is returned, and the
144 // callback will be run on the thread where Flush() was called when
145 // the write has completed.
147 // It is valid to destroy or close the file stream while there is an
148 // asynchronous flush in progress. That will cancel the flush and allow
149 // the buffer to be freed.
151 // It is invalid to request any asynchronous operations while there is an
152 // in-flight asynchronous operation.
154 // This method should not be called if the stream was opened READ_ONLY.
155 virtual int Flush(const CompletionCallback& callback);
157 // Returns the underlying platform file for testing.
158 const base::File& GetFileForTesting() const;
160 private:
161 class Context;
163 // Context performing I/O operations. It was extracted into a separate class
164 // to perform asynchronous operations because FileStream can be destroyed
165 // before completion of an async operation. Also if a FileStream is destroyed
166 // without explicitly calling Close, the file should be closed asynchronously
167 // without delaying FileStream's destructor.
168 scoped_ptr<Context> context_;
170 DISALLOW_COPY_AND_ASSIGN(FileStream);
173 } // namespace net
175 #endif // NET_BASE_FILE_STREAM_H_