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/platform_file.h" | 13 #include "base/platform_file.h" |
14 #include "net/base/completion_callback.h" | 14 #include "net/base/completion_callback.h" |
15 #include "net/base/file_stream_whence.h" | 15 #include "net/base/file_stream_whence.h" |
16 #include "net/base/net_export.h" | 16 #include "net/base/net_export.h" |
17 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
18 #if defined(OS_WIN) | |
19 #include "net/base/file_stream_win.h" | |
20 #elif defined(OS_POSIX) | |
21 #include "net/base/file_stream_posix.h" | |
22 #endif | |
23 | 18 |
24 class FilePath; | 19 class FilePath; |
25 | 20 |
26 namespace net { | 21 namespace net { |
27 | 22 |
28 class IOBuffer; | 23 class IOBuffer; |
29 | 24 |
30 class NET_EXPORT FileStream { | 25 class NET_EXPORT FileStream { |
31 public: | 26 public: |
32 // Creates a |FileStream| with a new |BoundNetLog| (based on |net_log|) | 27 // Creates a |FileStream| with a new |BoundNetLog| (based on |net_log|) |
33 // attached. |net_log| may be NULL if no logging is needed. | 28 // attached. |net_log| may be NULL if no logging is needed. |
34 explicit FileStream(net::NetLog* net_log); | 29 explicit FileStream(net::NetLog* net_log); |
35 | 30 |
36 // Construct a FileStream with an existing file handle and opening flags. | 31 // Construct a FileStream with an existing file handle and opening flags. |
37 // |file| is valid file handle. | 32 // |file| is valid file handle. |
38 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was | 33 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was |
39 // opened. | 34 // opened. |
40 // |net_log| is the net log pointer to use to create a |BoundNetLog|. May be | 35 // |net_log| is the net log pointer to use to create a |BoundNetLog|. May be |
41 // NULL if logging is not needed. | 36 // NULL if logging is not needed. |
42 // The already opened file will not be automatically closed when FileStream | 37 // Note: the new FileStream object takes ownership of the PlatformFile and |
43 // is destructed. | 38 // will close it on destruction. |
44 FileStream(base::PlatformFile file, int flags, net::NetLog* net_log); | 39 FileStream(base::PlatformFile file, int flags, net::NetLog* net_log); |
45 | 40 |
46 // If the file stream was opened with Open() or OpenSync(), the underlying | 41 // The underlying file is closed automatically. |
47 // file will be closed automatically by the destructor, if not closed | |
48 // manually. | |
49 virtual ~FileStream(); | 42 virtual ~FileStream(); |
50 | 43 |
51 // Call this method to close the FileStream, which was previously opened in | |
52 // the async mode (PLATFORM_FILE_ASYNC) asynchronously. | |
53 // | |
54 // Once the operation is done, |callback| will be run on the thread where | |
55 // Close() was called, with OK (i.e. an error is not propagated just like | |
56 // CloseSync() does not). | |
57 // | |
58 // It is not OK to call Close() multiple times. The behavior is not defined. | |
59 // Note that there must never be any pending async operations. | |
60 virtual void Close(const CompletionCallback& callback); | |
61 | |
62 // Call this method to close the FileStream synchronously. | |
63 // It is OK to call CloseSync() multiple times. Redundant calls are | |
64 // ignored. Note that if there are any pending async operations, they'll | |
65 // be aborted. | |
66 virtual void CloseSync(); | |
67 | |
68 // Call this method to open the FileStream asynchronously. The remaining | 44 // Call this method to open the FileStream asynchronously. The remaining |
69 // methods cannot be used unless the file is opened successfully. Returns | 45 // methods cannot be used unless the file is opened successfully. Returns |
70 // ERR_IO_PENDING if the operation is started. If the operation cannot be | 46 // ERR_IO_PENDING if the operation is started. If the operation cannot be |
71 // started then an error code is returned. | 47 // started then an error code is returned. |
72 // | 48 // |
73 // Once the operation is done, |callback| will be run on the thread where | 49 // Once the operation is done, |callback| will be run on the thread where |
74 // Open() was called, with the result code. open_flags is a bitfield of | 50 // Open() was called, with the result code. open_flags is a bitfield of |
75 // base::PlatformFileFlags. | 51 // base::PlatformFileFlags. |
76 // | 52 // |
77 // If the file stream is not closed manually, the underlying file will be | 53 // If the file stream is not closed manually, the underlying file will be |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 // Creates source dependency events between |owner_bound_net_log| and | 185 // Creates source dependency events between |owner_bound_net_log| and |
210 // |bound_net_log_|. Each gets an event showing the dependency on the other. | 186 // |bound_net_log_|. Each gets an event showing the dependency on the other. |
211 // If only one of those is valid, it gets an event showing that a change | 187 // If only one of those is valid, it gets an event showing that a change |
212 // of ownership happened, but without details. | 188 // of ownership happened, but without details. |
213 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); | 189 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); |
214 | 190 |
215 // Returns the underlying platform file for testing. | 191 // Returns the underlying platform file for testing. |
216 base::PlatformFile GetPlatformFileForTesting(); | 192 base::PlatformFile GetPlatformFileForTesting(); |
217 | 193 |
218 private: | 194 private: |
219 #if defined(OS_WIN) | 195 class Context; |
220 FileStreamWin impl_; | 196 |
221 #elif defined(OS_POSIX) | 197 bool is_async() const { return !!(open_flags_ & base::PLATFORM_FILE_ASYNC); } |
222 FileStreamPosix impl_; | 198 |
223 #endif | 199 int open_flags_; |
| 200 net::BoundNetLog bound_net_log_; |
| 201 |
| 202 // Context performing I/O operations. It was extracted into separate class |
| 203 // to perform asynchronous operations because FileStream can be destroyed |
| 204 // before completion of async operation. Also if async FileStream is destroyed |
| 205 // without explicit closing file should be closed asynchronously without |
| 206 // delaying FileStream's destructor. To perform all that separate object is |
| 207 // necessary. |
| 208 scoped_ptr<Context> context_; |
224 | 209 |
225 DISALLOW_COPY_AND_ASSIGN(FileStream); | 210 DISALLOW_COPY_AND_ASSIGN(FileStream); |
226 }; | 211 }; |
227 | 212 |
228 } // namespace net | 213 } // namespace net |
229 | 214 |
230 #endif // NET_BASE_FILE_STREAM_H_ | 215 #endif // NET_BASE_FILE_STREAM_H_ |
OLD | NEW |