OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file defines FileStream, a basic interface for reading and writing files | 5 // This file defines FileStream, a basic interface for reading and writing files |
6 // synchronously or asynchronously with support for seeking to an offset. | 6 // synchronously or asynchronously with support for seeking to an offset. |
7 // Note that even when used asynchronously, only one operation is supported at | 7 // Note that even when used asynchronously, only one operation is supported at |
8 // a time. | 8 // a time. |
9 | 9 |
10 #ifndef NET_BASE_FILE_STREAM_H_ | 10 #ifndef NET_BASE_FILE_STREAM_H_ |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 // started then an error code is returned. | 57 // started then an error code is returned. |
58 // | 58 // |
59 // Once the operation is done, |callback| will be run on the thread where | 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 | 60 // Open() was called, with the result code. open_flags is a bitfield of |
61 // base::PlatformFileFlags. | 61 // base::PlatformFileFlags. |
62 // | 62 // |
63 // If the file stream is not closed manually, the underlying file will be | 63 // If the file stream is not closed manually, the underlying file will be |
64 // automatically closed when FileStream is destructed in an asynchronous | 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 | 65 // manner (i.e. the file stream is closed in the background but you don't |
66 // know when). | 66 // know when). |
67 virtual int Open(const base::FilePath& path, int open_flags, | 67 virtual int Open(const base::FilePath& path, |
| 68 int open_flags, |
68 const CompletionCallback& callback); | 69 const CompletionCallback& callback); |
69 | 70 |
70 // Returns ERR_IO_PENDING and closes the file asynchronously, calling | 71 // Returns ERR_IO_PENDING and closes the file asynchronously, calling |
71 // |callback| when done. | 72 // |callback| when done. |
72 // It is invalid to request any asynchronous operations while there is an | 73 // It is invalid to request any asynchronous operations while there is an |
73 // in-flight asynchronous operation. | 74 // in-flight asynchronous operation. |
74 virtual int Close(const CompletionCallback& callback); | 75 virtual int Close(const CompletionCallback& callback); |
75 | 76 |
76 // Returns true if Open succeeded and Close has not been called. | 77 // Returns true if Open succeeded and Close has not been called. |
77 virtual bool IsOpen() const; | 78 virtual bool IsOpen() const; |
78 | 79 |
79 // Adjust the position from where data is read asynchronously. | 80 // Adjust the position from where data is read asynchronously. |
80 // Upon success, ERR_IO_PENDING is returned and |callback| will be run | 81 // 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 // 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 // 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 // It is invalid to request any asynchronous operations while there is an |
84 // in-flight asynchronous operation. | 85 // in-flight asynchronous operation. |
85 virtual int Seek(Whence whence, int64 offset, | 86 virtual int Seek(Whence whence, |
| 87 int64 offset, |
86 const Int64CompletionCallback& callback); | 88 const Int64CompletionCallback& callback); |
87 | 89 |
88 // Call this method to read data from the current stream position | 90 // Call this method to read data from the current stream position |
89 // asynchronously. Up to buf_len bytes will be copied into buf. (In | 91 // asynchronously. Up to buf_len bytes will be copied into buf. (In |
90 // other words, partial reads are allowed.) Returns the number of bytes | 92 // 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 | 93 // copied, 0 if at end-of-file, or an error code if the operation could |
92 // not be performed. | 94 // not be performed. |
93 // | 95 // |
94 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null | 96 // 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 | 97 // callback must be passed to this method. If the read could not |
96 // complete synchronously, then ERR_IO_PENDING is returned, and the | 98 // complete synchronously, then ERR_IO_PENDING is returned, and the |
97 // callback will be run on the thread where Read() was called, when the | 99 // callback will be run on the thread where Read() was called, when the |
98 // read has completed. | 100 // read has completed. |
99 // | 101 // |
100 // It is valid to destroy or close the file stream while there is an | 102 // 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 | 103 // asynchronous read in progress. That will cancel the read and allow |
102 // the buffer to be freed. | 104 // the buffer to be freed. |
103 // | 105 // |
104 // It is invalid to request any asynchronous operations while there is an | 106 // It is invalid to request any asynchronous operations while there is an |
105 // in-flight asynchronous operation. | 107 // in-flight asynchronous operation. |
106 // | 108 // |
107 // This method must not be called if the stream was opened WRITE_ONLY. | 109 // This method must not be called if the stream was opened WRITE_ONLY. |
108 virtual int Read(IOBuffer* buf, int buf_len, | 110 virtual int Read(IOBuffer* buf, |
| 111 int buf_len, |
109 const CompletionCallback& callback); | 112 const CompletionCallback& callback); |
110 | 113 |
111 // Call this method to write data at the current stream position | 114 // Call this method to write data at the current stream position |
112 // asynchronously. Up to buf_len bytes will be written from buf. (In | 115 // asynchronously. Up to buf_len bytes will be written from buf. (In |
113 // other words, partial writes are allowed.) Returns the number of | 116 // other words, partial writes are allowed.) Returns the number of |
114 // bytes written, or an error code if the operation could not be | 117 // bytes written, or an error code if the operation could not be |
115 // performed. | 118 // performed. |
116 // | 119 // |
117 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null | 120 // 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 | 121 // callback must be passed to this method. If the write could not |
119 // complete synchronously, then ERR_IO_PENDING is returned, and the | 122 // complete synchronously, then ERR_IO_PENDING is returned, and the |
120 // callback will be run on the thread where Write() was called when | 123 // callback will be run on the thread where Write() was called when |
121 // the write has completed. | 124 // the write has completed. |
122 // | 125 // |
123 // It is valid to destroy or close the file stream while there is an | 126 // 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 | 127 // asynchronous write in progress. That will cancel the write and allow |
125 // the buffer to be freed. | 128 // the buffer to be freed. |
126 // | 129 // |
127 // It is invalid to request any asynchronous operations while there is an | 130 // It is invalid to request any asynchronous operations while there is an |
128 // in-flight asynchronous operation. | 131 // in-flight asynchronous operation. |
129 // | 132 // |
130 // This method must not be called if the stream was opened READ_ONLY. | 133 // This method must not be called if the stream was opened READ_ONLY. |
131 // | 134 // |
132 // Zero byte writes are not allowed. | 135 // Zero byte writes are not allowed. |
133 virtual int Write(IOBuffer* buf, int buf_len, | 136 virtual int Write(IOBuffer* buf, |
| 137 int buf_len, |
134 const CompletionCallback& callback); | 138 const CompletionCallback& callback); |
135 | 139 |
136 // Forces out a filesystem sync on this file to make sure that the file was | 140 // 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 | 141 // 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 | 142 // not have to be called, it just forces one to happen at the time of |
139 // calling. | 143 // calling. |
140 // | 144 // |
141 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null | 145 // 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 | 146 // callback must be passed to this method. If the write could not |
143 // complete synchronously, then ERR_IO_PENDING is returned, and the | 147 // complete synchronously, then ERR_IO_PENDING is returned, and the |
(...skipping 22 matching lines...) Expand all Loading... |
166 // without explicitly calling Close, the file should be closed asynchronously | 170 // without explicitly calling Close, the file should be closed asynchronously |
167 // without delaying FileStream's destructor. | 171 // without delaying FileStream's destructor. |
168 scoped_ptr<Context> context_; | 172 scoped_ptr<Context> context_; |
169 | 173 |
170 DISALLOW_COPY_AND_ASSIGN(FileStream); | 174 DISALLOW_COPY_AND_ASSIGN(FileStream); |
171 }; | 175 }; |
172 | 176 |
173 } // namespace net | 177 } // namespace net |
174 | 178 |
175 #endif // NET_BASE_FILE_STREAM_H_ | 179 #endif // NET_BASE_FILE_STREAM_H_ |
OLD | NEW |