xref: /aosp_15_r20/external/cronet/net/base/file_stream.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker // This file defines FileStream, a basic interface for reading and writing files
6*6777b538SAndroid Build Coastguard Worker // synchronously or asynchronously with support for seeking to an offset.
7*6777b538SAndroid Build Coastguard Worker // Note that even when used asynchronously, only one operation is supported at
8*6777b538SAndroid Build Coastguard Worker // a time.
9*6777b538SAndroid Build Coastguard Worker 
10*6777b538SAndroid Build Coastguard Worker #ifndef NET_BASE_FILE_STREAM_H_
11*6777b538SAndroid Build Coastguard Worker #define NET_BASE_FILE_STREAM_H_
12*6777b538SAndroid Build Coastguard Worker 
13*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
14*6777b538SAndroid Build Coastguard Worker 
15*6777b538SAndroid Build Coastguard Worker #include <memory>
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker #include "base/files/file.h"
18*6777b538SAndroid Build Coastguard Worker #include "net/base/completion_once_callback.h"
19*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
20*6777b538SAndroid Build Coastguard Worker 
21*6777b538SAndroid Build Coastguard Worker namespace base {
22*6777b538SAndroid Build Coastguard Worker class FilePath;
23*6777b538SAndroid Build Coastguard Worker class TaskRunner;
24*6777b538SAndroid Build Coastguard Worker }
25*6777b538SAndroid Build Coastguard Worker 
26*6777b538SAndroid Build Coastguard Worker namespace net {
27*6777b538SAndroid Build Coastguard Worker 
28*6777b538SAndroid Build Coastguard Worker class IOBuffer;
29*6777b538SAndroid Build Coastguard Worker 
30*6777b538SAndroid Build Coastguard Worker class NET_EXPORT FileStream {
31*6777b538SAndroid Build Coastguard Worker  public:
32*6777b538SAndroid Build Coastguard Worker   // Uses |task_runner| for asynchronous operations.
33*6777b538SAndroid Build Coastguard Worker   explicit FileStream(const scoped_refptr<base::TaskRunner>& task_runner);
34*6777b538SAndroid Build Coastguard Worker 
35*6777b538SAndroid Build Coastguard Worker   // Construct a FileStream with an already opened file. |file| must be opened
36*6777b538SAndroid Build Coastguard Worker   // for async reading on Windows, and sync reading everywehere else.
37*6777b538SAndroid Build Coastguard Worker   //
38*6777b538SAndroid Build Coastguard Worker   // Uses |task_runner| for asynchronous operations.
39*6777b538SAndroid Build Coastguard Worker   FileStream(base::File file,
40*6777b538SAndroid Build Coastguard Worker              const scoped_refptr<base::TaskRunner>& task_runner);
41*6777b538SAndroid Build Coastguard Worker 
42*6777b538SAndroid Build Coastguard Worker   FileStream(const FileStream&) = delete;
43*6777b538SAndroid Build Coastguard Worker   FileStream& operator=(const FileStream&) = delete;
44*6777b538SAndroid Build Coastguard Worker   // The underlying file is closed automatically.
45*6777b538SAndroid Build Coastguard Worker   virtual ~FileStream();
46*6777b538SAndroid Build Coastguard Worker 
47*6777b538SAndroid Build Coastguard Worker   // Call this method to open the FileStream asynchronously.  The remaining
48*6777b538SAndroid Build Coastguard Worker   // methods cannot be used unless the file is opened successfully. Returns
49*6777b538SAndroid Build Coastguard Worker   // ERR_IO_PENDING if the operation is started. If the operation cannot be
50*6777b538SAndroid Build Coastguard Worker   // started then an error code is returned.
51*6777b538SAndroid Build Coastguard Worker   //
52*6777b538SAndroid Build Coastguard Worker   // Once the operation is done, |callback| will be run on the thread where
53*6777b538SAndroid Build Coastguard Worker   // Open() was called, with the result code. open_flags is a bitfield of
54*6777b538SAndroid Build Coastguard Worker   // base::File::Flags.
55*6777b538SAndroid Build Coastguard Worker   //
56*6777b538SAndroid Build Coastguard Worker   // If the file stream is not closed manually, the underlying file will be
57*6777b538SAndroid Build Coastguard Worker   // automatically closed when FileStream is destructed in an asynchronous
58*6777b538SAndroid Build Coastguard Worker   // manner (i.e. the file stream is closed in the background but you don't
59*6777b538SAndroid Build Coastguard Worker   // know when).
60*6777b538SAndroid Build Coastguard Worker   virtual int Open(const base::FilePath& path,
61*6777b538SAndroid Build Coastguard Worker                    int open_flags,
62*6777b538SAndroid Build Coastguard Worker                    CompletionOnceCallback callback);
63*6777b538SAndroid Build Coastguard Worker 
64*6777b538SAndroid Build Coastguard Worker   // Returns ERR_IO_PENDING and closes the file asynchronously, calling
65*6777b538SAndroid Build Coastguard Worker   // |callback| when done.
66*6777b538SAndroid Build Coastguard Worker   // It is invalid to request any asynchronous operations while there is an
67*6777b538SAndroid Build Coastguard Worker   // in-flight asynchronous operation.
68*6777b538SAndroid Build Coastguard Worker   virtual int Close(CompletionOnceCallback callback);
69*6777b538SAndroid Build Coastguard Worker 
70*6777b538SAndroid Build Coastguard Worker   // Returns true if Open succeeded and Close has not been called.
71*6777b538SAndroid Build Coastguard Worker   virtual bool IsOpen() const;
72*6777b538SAndroid Build Coastguard Worker 
73*6777b538SAndroid Build Coastguard Worker   // Adjust the position from the start of the file where data is read
74*6777b538SAndroid Build Coastguard Worker   // asynchronously. Upon success, ERR_IO_PENDING is returned and |callback|
75*6777b538SAndroid Build Coastguard Worker   // will be run on the thread where Seek() was called with the the stream
76*6777b538SAndroid Build Coastguard Worker   // position relative to the start of the file.  Otherwise, an error code is
77*6777b538SAndroid Build Coastguard Worker   // returned. It is invalid to request any asynchronous operations while there
78*6777b538SAndroid Build Coastguard Worker   // is an in-flight asynchronous operation.
79*6777b538SAndroid Build Coastguard Worker   virtual int Seek(int64_t offset, Int64CompletionOnceCallback callback);
80*6777b538SAndroid Build Coastguard Worker 
81*6777b538SAndroid Build Coastguard Worker   // Call this method to read data from the current stream position
82*6777b538SAndroid Build Coastguard Worker   // asynchronously. Up to buf_len bytes will be copied into buf.  (In
83*6777b538SAndroid Build Coastguard Worker   // other words, partial reads are allowed.)  Returns the number of bytes
84*6777b538SAndroid Build Coastguard Worker   // copied, 0 if at end-of-file, or an error code if the operation could
85*6777b538SAndroid Build Coastguard Worker   // not be performed.
86*6777b538SAndroid Build Coastguard Worker   //
87*6777b538SAndroid Build Coastguard Worker   // The file must be opened with FLAG_ASYNC, and a non-null
88*6777b538SAndroid Build Coastguard Worker   // callback must be passed to this method. If the read could not
89*6777b538SAndroid Build Coastguard Worker   // complete synchronously, then ERR_IO_PENDING is returned, and the
90*6777b538SAndroid Build Coastguard Worker   // callback will be run on the thread where Read() was called, when the
91*6777b538SAndroid Build Coastguard Worker   // read has completed.
92*6777b538SAndroid Build Coastguard Worker   //
93*6777b538SAndroid Build Coastguard Worker   // It is valid to destroy or close the file stream while there is an
94*6777b538SAndroid Build Coastguard Worker   // asynchronous read in progress.  That will cancel the read and allow
95*6777b538SAndroid Build Coastguard Worker   // the buffer to be freed.
96*6777b538SAndroid Build Coastguard Worker   //
97*6777b538SAndroid Build Coastguard Worker   // It is invalid to request any asynchronous operations while there is an
98*6777b538SAndroid Build Coastguard Worker   // in-flight asynchronous operation.
99*6777b538SAndroid Build Coastguard Worker   //
100*6777b538SAndroid Build Coastguard Worker   // This method must not be called if the stream was opened WRITE_ONLY.
101*6777b538SAndroid Build Coastguard Worker   virtual int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
102*6777b538SAndroid Build Coastguard Worker 
103*6777b538SAndroid Build Coastguard Worker   // Call this method to write data at the current stream position
104*6777b538SAndroid Build Coastguard Worker   // asynchronously.  Up to buf_len bytes will be written from buf. (In
105*6777b538SAndroid Build Coastguard Worker   // other words, partial writes are allowed.)  Returns the number of
106*6777b538SAndroid Build Coastguard Worker   // bytes written, or an error code if the operation could not be
107*6777b538SAndroid Build Coastguard Worker   // performed.
108*6777b538SAndroid Build Coastguard Worker   //
109*6777b538SAndroid Build Coastguard Worker   // The file must be opened with FLAG_ASYNC, and a non-null
110*6777b538SAndroid Build Coastguard Worker   // callback must be passed to this method. If the write could not
111*6777b538SAndroid Build Coastguard Worker   // complete synchronously, then ERR_IO_PENDING is returned, and the
112*6777b538SAndroid Build Coastguard Worker   // callback will be run on the thread where Write() was called when
113*6777b538SAndroid Build Coastguard Worker   // the write has completed.
114*6777b538SAndroid Build Coastguard Worker   //
115*6777b538SAndroid Build Coastguard Worker   // It is valid to destroy or close the file stream while there is an
116*6777b538SAndroid Build Coastguard Worker   // asynchronous write in progress.  That will cancel the write and allow
117*6777b538SAndroid Build Coastguard Worker   // the buffer to be freed.
118*6777b538SAndroid Build Coastguard Worker   //
119*6777b538SAndroid Build Coastguard Worker   // It is invalid to request any asynchronous operations while there is an
120*6777b538SAndroid Build Coastguard Worker   // in-flight asynchronous operation.
121*6777b538SAndroid Build Coastguard Worker   //
122*6777b538SAndroid Build Coastguard Worker   // This method must not be called if the stream was opened READ_ONLY.
123*6777b538SAndroid Build Coastguard Worker   //
124*6777b538SAndroid Build Coastguard Worker   // Zero byte writes are not allowed.
125*6777b538SAndroid Build Coastguard Worker   virtual int Write(IOBuffer* buf,
126*6777b538SAndroid Build Coastguard Worker                     int buf_len,
127*6777b538SAndroid Build Coastguard Worker                     CompletionOnceCallback callback);
128*6777b538SAndroid Build Coastguard Worker 
129*6777b538SAndroid Build Coastguard Worker   // Gets status information about File. May fail synchronously, but never
130*6777b538SAndroid Build Coastguard Worker   // succeeds synchronously.
131*6777b538SAndroid Build Coastguard Worker   //
132*6777b538SAndroid Build Coastguard Worker   // It is invalid to request any asynchronous operations while there is an
133*6777b538SAndroid Build Coastguard Worker   // in-flight asynchronous operation.
134*6777b538SAndroid Build Coastguard Worker   //
135*6777b538SAndroid Build Coastguard Worker   // |file_info| must remain valid until |callback| is invoked.
136*6777b538SAndroid Build Coastguard Worker   virtual int GetFileInfo(base::File::Info* file_info,
137*6777b538SAndroid Build Coastguard Worker                           CompletionOnceCallback callback);
138*6777b538SAndroid Build Coastguard Worker 
139*6777b538SAndroid Build Coastguard Worker   // Forces out a filesystem sync on this file to make sure that the file was
140*6777b538SAndroid Build Coastguard Worker   // written out to disk and is not currently sitting in the buffer. This does
141*6777b538SAndroid Build Coastguard Worker   // not have to be called, it just forces one to happen at the time of
142*6777b538SAndroid Build Coastguard Worker   // calling.
143*6777b538SAndroid Build Coastguard Worker   //
144*6777b538SAndroid Build Coastguard Worker   // The file must be opened with FLAG_ASYNC, and a non-null
145*6777b538SAndroid Build Coastguard Worker   // callback must be passed to this method. If the write could not
146*6777b538SAndroid Build Coastguard Worker   // complete synchronously, then ERR_IO_PENDING is returned, and the
147*6777b538SAndroid Build Coastguard Worker   // callback will be run on the thread where Flush() was called when
148*6777b538SAndroid Build Coastguard Worker   // the write has completed.
149*6777b538SAndroid Build Coastguard Worker   //
150*6777b538SAndroid Build Coastguard Worker   // It is valid to destroy or close the file stream while there is an
151*6777b538SAndroid Build Coastguard Worker   // asynchronous flush in progress.  That will cancel the flush and allow
152*6777b538SAndroid Build Coastguard Worker   // the buffer to be freed.
153*6777b538SAndroid Build Coastguard Worker   //
154*6777b538SAndroid Build Coastguard Worker   // It is invalid to request any asynchronous operations while there is an
155*6777b538SAndroid Build Coastguard Worker   // in-flight asynchronous operation.
156*6777b538SAndroid Build Coastguard Worker   //
157*6777b538SAndroid Build Coastguard Worker   // This method should not be called if the stream was opened READ_ONLY.
158*6777b538SAndroid Build Coastguard Worker   virtual int Flush(CompletionOnceCallback callback);
159*6777b538SAndroid Build Coastguard Worker 
160*6777b538SAndroid Build Coastguard Worker  private:
161*6777b538SAndroid Build Coastguard Worker   class Context;
162*6777b538SAndroid Build Coastguard Worker 
163*6777b538SAndroid Build Coastguard Worker   // Context performing I/O operations. It was extracted into a separate class
164*6777b538SAndroid Build Coastguard Worker   // to perform asynchronous operations because FileStream can be destroyed
165*6777b538SAndroid Build Coastguard Worker   // before completion of an async operation. Also if a FileStream is destroyed
166*6777b538SAndroid Build Coastguard Worker   // without explicitly calling Close, the file should be closed asynchronously
167*6777b538SAndroid Build Coastguard Worker   // without delaying FileStream's destructor.
168*6777b538SAndroid Build Coastguard Worker   std::unique_ptr<Context> context_;
169*6777b538SAndroid Build Coastguard Worker };
170*6777b538SAndroid Build Coastguard Worker 
171*6777b538SAndroid Build Coastguard Worker }  // namespace net
172*6777b538SAndroid Build Coastguard Worker 
173*6777b538SAndroid Build Coastguard Worker #endif  // NET_BASE_FILE_STREAM_H_
174