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 23 matching lines...) Expand all Loading... |
34 // attached. |net_log| may be NULL if no logging is needed. | 34 // attached. |net_log| may be NULL if no logging is needed. |
35 explicit FileStream(net::NetLog* net_log); | 35 explicit FileStream(net::NetLog* net_log); |
36 | 36 |
37 // Construct a FileStream with an existing file handle and opening flags. | 37 // Construct a FileStream with an existing file handle and opening flags. |
38 // |file| is valid file handle. | 38 // |file| is valid file handle. |
39 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was | 39 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was |
40 // opened. | 40 // opened. |
41 // |net_log| is the net log pointer to use to create a |BoundNetLog|. May be | 41 // |net_log| is the net log pointer to use to create a |BoundNetLog|. May be |
42 // NULL if logging is not needed. | 42 // NULL if logging is not needed. |
43 // The already opened file will not be automatically closed when FileStream | 43 // The already opened file will not be automatically closed when FileStream |
44 // is destructed. | 44 // is destroyed. |
| 45 // Note: if you use this constructor you have 3 options of further usage: |
| 46 // - Only sync operations are used on this stream; |
| 47 // - You wait for completion of all async operations; |
| 48 // - When you finished using stream you call any type of Close*() method |
| 49 // (including possibly CloseAndCancelAsync() or Close() with later |
| 50 // destruction without waiting for closing callback). |
| 51 // If your usage doesn't fall into one of 3 options above you are introducing |
| 52 // use-after-free bug when file descriptor could be used after you close it |
| 53 // (and it possibly gets reopened to another file). |
45 FileStream(base::PlatformFile file, int flags, net::NetLog* net_log); | 54 FileStream(base::PlatformFile file, int flags, net::NetLog* net_log); |
46 | 55 |
47 // If the file stream was opened with Open() or OpenSync(), the underlying | 56 // If the file stream was opened with Open() or OpenSync(), the underlying |
48 // file will be closed automatically by the destructor, if not closed | 57 // file will be closed automatically by the destructor, if not closed |
49 // manually. | 58 // manually. |
50 virtual ~FileStream(); | 59 virtual ~FileStream(); |
51 | 60 |
52 // Call this method to close the FileStream, which was previously opened in | 61 // Call this method to close the FileStream, which was previously opened in |
53 // the async mode (PLATFORM_FILE_ASYNC) asynchronously. | 62 // the async mode (PLATFORM_FILE_ASYNC) asynchronously. |
54 // | 63 // |
55 // Once the operation is done, |callback| will be run on the thread where | 64 // Once the operation is done, |callback| will be run on the thread where |
56 // Close() was called, with OK (i.e. an error is not propagated just like | 65 // Close() was called, with OK (i.e. an error is not propagated just like |
57 // CloseSync() does not). | 66 // CloseSync() does not). |
58 // | 67 // |
59 // It is not OK to call Close() multiple times. The behavior is not defined. | 68 // It is not OK to call Close() multiple times. The behavior is not defined. |
60 // Note that there must never be any pending async operations. | 69 // Note that there must never be any pending async operations. |
61 virtual void Close(const CompletionCallback& callback); | 70 virtual void Close(const CompletionCallback& callback); |
62 | 71 |
63 // Call this method to close the FileStream synchronously. | 72 // Call this method to close the FileStream synchronously. |
64 // It is OK to call CloseSync() multiple times. Redundant calls are | 73 // It is OK to call CloseSync() multiple times. Redundant calls are |
65 // ignored. Note that if there are any pending async operations, they'll | 74 // ignored. Note that if there are any pending async operations, then |
66 // be aborted. | 75 // behavior of this method is equivalent to CloseAndCancelAsync(). |
67 virtual void CloseSync(); | 76 virtual void CloseSync(); |
68 | 77 |
| 78 // Call this method to cancel pending async operations and close FileStream |
| 79 // asynchronously without any notification of completion. Method should be |
| 80 // used when you want to quickly destroy FileStream without actually calling |
| 81 // the destructor. |
| 82 virtual void CloseAndCancelAsync(); |
| 83 |
69 // Call this method to open the FileStream asynchronously. The remaining | 84 // Call this method to open the FileStream asynchronously. The remaining |
70 // methods cannot be used unless the file is opened successfully. Returns | 85 // methods cannot be used unless the file is opened successfully. Returns |
71 // ERR_IO_PENDING if the operation is started. If the operation cannot be | 86 // ERR_IO_PENDING if the operation is started. If the operation cannot be |
72 // started then an error code is returned. | 87 // started then an error code is returned. |
73 // | 88 // |
74 // Once the operation is done, |callback| will be run on the thread where | 89 // Once the operation is done, |callback| will be run on the thread where |
75 // Open() was called, with the result code. open_flags is a bitfield of | 90 // Open() was called, with the result code. open_flags is a bitfield of |
76 // base::PlatformFileFlags. | 91 // base::PlatformFileFlags. |
77 // | 92 // |
78 // If the file stream is not closed manually, the underlying file will be | 93 // If the file stream is not closed manually, the underlying file will be |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 #elif defined(OS_POSIX) | 237 #elif defined(OS_POSIX) |
223 FileStreamPosix impl_; | 238 FileStreamPosix impl_; |
224 #endif | 239 #endif |
225 | 240 |
226 DISALLOW_COPY_AND_ASSIGN(FileStream); | 241 DISALLOW_COPY_AND_ASSIGN(FileStream); |
227 }; | 242 }; |
228 | 243 |
229 } // namespace net | 244 } // namespace net |
230 | 245 |
231 #endif // NET_BASE_FILE_STREAM_H_ | 246 #endif // NET_BASE_FILE_STREAM_H_ |
OLD | NEW |