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_ |
11 #define NET_BASE_FILE_STREAM_H_ | 11 #define NET_BASE_FILE_STREAM_H_ |
12 | 12 |
13 #include "base/files/file.h" | 13 #include "base/files/file.h" |
14 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
15 #include "net/base/file_stream_whence.h" | |
16 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
17 | 16 |
18 namespace base { | 17 namespace base { |
19 class FilePath; | 18 class FilePath; |
20 class TaskRunner; | 19 class TaskRunner; |
21 } | 20 } |
22 | 21 |
23 namespace net { | 22 namespace net { |
24 | 23 |
25 class IOBuffer; | 24 class IOBuffer; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 | 61 |
63 // Returns true if Open succeeded and Close has not been called. | 62 // Returns true if Open succeeded and Close has not been called. |
64 virtual bool IsOpen() const; | 63 virtual bool IsOpen() const; |
65 | 64 |
66 // Adjust the position from where data is read asynchronously. | 65 // Adjust the position from where data is read asynchronously. |
67 // Upon success, ERR_IO_PENDING is returned and |callback| will be run | 66 // Upon success, ERR_IO_PENDING is returned and |callback| will be run |
68 // on the thread where Seek() was called with the the stream position | 67 // on the thread where Seek() was called with the the stream position |
69 // relative to the start of the file. Otherwise, an error code is returned. | 68 // relative to the start of the file. Otherwise, an error code is returned. |
70 // It is invalid to request any asynchronous operations while there is an | 69 // It is invalid to request any asynchronous operations while there is an |
71 // in-flight asynchronous operation. | 70 // in-flight asynchronous operation. |
72 virtual int Seek(Whence whence, int64 offset, | 71 virtual int Seek(base::File::Whence whence, int64 offset, |
73 const Int64CompletionCallback& callback); | 72 const Int64CompletionCallback& callback); |
74 | 73 |
75 // Call this method to read data from the current stream position | 74 // Call this method to read data from the current stream position |
76 // asynchronously. Up to buf_len bytes will be copied into buf. (In | 75 // asynchronously. Up to buf_len bytes will be copied into buf. (In |
77 // other words, partial reads are allowed.) Returns the number of bytes | 76 // other words, partial reads are allowed.) Returns the number of bytes |
78 // copied, 0 if at end-of-file, or an error code if the operation could | 77 // copied, 0 if at end-of-file, or an error code if the operation could |
79 // not be performed. | 78 // not be performed. |
80 // | 79 // |
81 // The file must be opened with FLAG_ASYNC, and a non-null | 80 // The file must be opened with FLAG_ASYNC, and a non-null |
82 // callback must be passed to this method. If the read could not | 81 // callback must be passed to this method. If the read could not |
83 // complete synchronously, then ERR_IO_PENDING is returned, and the | 82 // complete synchronously, then ERR_IO_PENDING is returned, and the |
84 // callback will be run on the thread where Read() was called, when the | 83 // callback will be run on the thread where Read() was called, when the |
85 // read has completed. | 84 // read has completed. |
86 // | 85 // |
87 // It is valid to destroy or close the file stream while there is an | 86 // It is valid to destroy or close the file stream while there is an |
88 // asynchronous read in progress. That will cancel the read and allow | 87 // asynchronous read in progress. That will cancel the read and allow |
89 // the buffer to be freed. | 88 // the buffer to be freed. |
90 // | 89 // |
91 // It is invalid to request any asynchronous operations while there is an | 90 // It is invalid to request any asynchronous operations while there is an |
92 // in-flight asynchronous operation. | 91 // in-flight asynchronous operation. |
93 // | 92 // |
94 // This method must not be called if the stream was opened WRITE_ONLY. | 93 // This method must not be called if the stream was opened WRITE_ONLY. |
95 virtual int Read(IOBuffer* buf, int buf_len, | 94 virtual int Read(IOBuffer* buf, int buf_len, |
96 const CompletionCallback& callback); | 95 const CompletionCallback& callback); |
97 | 96 |
97 // Same as Read, but delays starting the operation until it should complete | |
98 // without blocking, reducing the load on the task_runner. | |
99 virtual int ReadNoBlocking(IOBuffer* buf, int buf_len, | |
Sergey Ulanov
2014/06/12 08:10:21
Do we really need to differentiate between blockin
rvargas (doing something else)
2014/06/13 02:49:10
Unfortunately, I think that decision should be tak
Sergey Ulanov
2014/06/13 19:31:50
Read() can be made non-blocking and such that it w
rvargas (doing something else)
2014/06/13 19:58:24
I'm not really trying to change the way FileStream
Sergey Ulanov
2014/06/14 00:58:22
FileStream owns the file descriptors and it doesn'
rvargas (doing something else)
2014/06/14 01:42:55
FileStream can receive a file on construction, and
| |
100 const CompletionCallback& callback); | |
101 | |
98 // Call this method to write data at the current stream position | 102 // Call this method to write data at the current stream position |
99 // asynchronously. Up to buf_len bytes will be written from buf. (In | 103 // asynchronously. Up to buf_len bytes will be written from buf. (In |
100 // other words, partial writes are allowed.) Returns the number of | 104 // other words, partial writes are allowed.) Returns the number of |
101 // bytes written, or an error code if the operation could not be | 105 // bytes written, or an error code if the operation could not be |
102 // performed. | 106 // performed. |
103 // | 107 // |
104 // The file must be opened with FLAG_ASYNC, and a non-null | 108 // The file must be opened with FLAG_ASYNC, and a non-null |
105 // callback must be passed to this method. If the write could not | 109 // callback must be passed to this method. If the write could not |
106 // complete synchronously, then ERR_IO_PENDING is returned, and the | 110 // complete synchronously, then ERR_IO_PENDING is returned, and the |
107 // callback will be run on the thread where Write() was called when | 111 // callback will be run on the thread where Write() was called when |
108 // the write has completed. | 112 // the write has completed. |
109 // | 113 // |
110 // It is valid to destroy or close the file stream while there is an | 114 // It is valid to destroy or close the file stream while there is an |
111 // asynchronous write in progress. That will cancel the write and allow | 115 // asynchronous write in progress. That will cancel the write and allow |
112 // the buffer to be freed. | 116 // the buffer to be freed. |
113 // | 117 // |
114 // It is invalid to request any asynchronous operations while there is an | 118 // It is invalid to request any asynchronous operations while there is an |
115 // in-flight asynchronous operation. | 119 // in-flight asynchronous operation. |
116 // | 120 // |
117 // This method must not be called if the stream was opened READ_ONLY. | 121 // This method must not be called if the stream was opened READ_ONLY. |
118 // | 122 // |
119 // Zero byte writes are not allowed. | 123 // Zero byte writes are not allowed. |
120 virtual int Write(IOBuffer* buf, int buf_len, | 124 virtual int Write(IOBuffer* buf, int buf_len, |
121 const CompletionCallback& callback); | 125 const CompletionCallback& callback); |
122 | 126 |
127 // Same as Write, but delays starting the operation until it should complete | |
128 // without blocking, reducing the load on the task_runner. | |
129 virtual int WriteNoBlocking(IOBuffer* buf, int buf_len, | |
130 const CompletionCallback& callback); | |
131 | |
123 // Forces out a filesystem sync on this file to make sure that the file was | 132 // Forces out a filesystem sync on this file to make sure that the file was |
124 // written out to disk and is not currently sitting in the buffer. This does | 133 // written out to disk and is not currently sitting in the buffer. This does |
125 // not have to be called, it just forces one to happen at the time of | 134 // not have to be called, it just forces one to happen at the time of |
126 // calling. | 135 // calling. |
127 // | 136 // |
128 // The file must be opened with FLAG_ASYNC, and a non-null | 137 // The file must be opened with FLAG_ASYNC, and a non-null |
129 // callback must be passed to this method. If the write could not | 138 // callback must be passed to this method. If the write could not |
130 // complete synchronously, then ERR_IO_PENDING is returned, and the | 139 // complete synchronously, then ERR_IO_PENDING is returned, and the |
131 // callback will be run on the thread where Flush() was called when | 140 // callback will be run on the thread where Flush() was called when |
132 // the write has completed. | 141 // the write has completed. |
(...skipping 20 matching lines...) Expand all Loading... | |
153 // without explicitly calling Close, the file should be closed asynchronously | 162 // without explicitly calling Close, the file should be closed asynchronously |
154 // without delaying FileStream's destructor. | 163 // without delaying FileStream's destructor. |
155 scoped_ptr<Context> context_; | 164 scoped_ptr<Context> context_; |
156 | 165 |
157 DISALLOW_COPY_AND_ASSIGN(FileStream); | 166 DISALLOW_COPY_AND_ASSIGN(FileStream); |
158 }; | 167 }; |
159 | 168 |
160 } // namespace net | 169 } // namespace net |
161 | 170 |
162 #endif // NET_BASE_FILE_STREAM_H_ | 171 #endif // NET_BASE_FILE_STREAM_H_ |
OLD | NEW |