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 // If the file stream was opened with Open() or OpenSync(), the underlying |
47 // file will be closed automatically by the destructor, if not closed | 42 // file will be closed automatically by the destructor, if not closed |
48 // manually. | 43 // manually. |
49 virtual ~FileStream(); | 44 virtual ~FileStream(); |
50 | 45 |
51 // Call this method to close the FileStream, which was previously opened in | 46 // Call this method to close the FileStream, which was previously opened in |
52 // the async mode (PLATFORM_FILE_ASYNC) asynchronously. | 47 // the async mode (PLATFORM_FILE_ASYNC) asynchronously. |
53 // | 48 // |
54 // 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 |
55 // Close() was called, with OK (i.e. an error is not propagated just like | 50 // Close() was called, with OK (i.e. an error is not propagated just like |
56 // CloseSync() does not). | 51 // CloseSync() does not). |
57 // | 52 // |
58 // It is not OK to call Close() multiple times. The behavior is not defined. | 53 // 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. | 54 // Note that there must never be any pending async operations. |
60 virtual void Close(const CompletionCallback& callback); | 55 virtual void Close(const CompletionCallback& callback); |
61 | 56 |
62 // Call this method to close the FileStream synchronously. | 57 // Call this method to close the FileStream synchronously. |
63 // It is OK to call CloseSync() multiple times. Redundant calls are | 58 // It is OK to call CloseSync() multiple times. Redundant calls are |
64 // ignored. Note that if there are any pending async operations, they'll | 59 // ignored. Note that if there are any pending async operations, they'll |
65 // be aborted. | 60 // be aborted and actual closing of the stream will happen asynchronously |
61 // (to avoid races with async operation). | |
66 virtual void CloseSync(); | 62 virtual void CloseSync(); |
67 | 63 |
68 // Call this method to open the FileStream asynchronously. The remaining | 64 // Call this method to open the FileStream asynchronously. The remaining |
69 // methods cannot be used unless the file is opened successfully. Returns | 65 // 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 | 66 // ERR_IO_PENDING if the operation is started. If the operation cannot be |
71 // started then an error code is returned. | 67 // started then an error code is returned. |
72 // | 68 // |
73 // Once the operation is done, |callback| will be run on the thread where | 69 // 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 | 70 // Open() was called, with the result code. open_flags is a bitfield of |
75 // base::PlatformFileFlags. | 71 // base::PlatformFileFlags. |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
209 // Creates source dependency events between |owner_bound_net_log| and | 205 // Creates source dependency events between |owner_bound_net_log| and |
210 // |bound_net_log_|. Each gets an event showing the dependency on the other. | 206 // |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 | 207 // If only one of those is valid, it gets an event showing that a change |
212 // of ownership happened, but without details. | 208 // of ownership happened, but without details. |
213 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); | 209 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); |
214 | 210 |
215 // Returns the underlying platform file for testing. | 211 // Returns the underlying platform file for testing. |
216 base::PlatformFile GetPlatformFileForTesting(); | 212 base::PlatformFile GetPlatformFileForTesting(); |
217 | 213 |
218 private: | 214 private: |
219 #if defined(OS_WIN) | 215 class Context; |
220 FileStreamWin impl_; | 216 |
221 #elif defined(OS_POSIX) | 217 bool is_async() { return !!(open_flags_ & base::PLATFORM_FILE_ASYNC); } |
willchan no longer on Chromium
2012/09/11 23:05:02
make this const?
| |
222 FileStreamPosix impl_; | 218 |
223 #endif | 219 int open_flags_; |
220 net::BoundNetLog bound_net_log_; | |
221 | |
222 // Context performing I/O operations. It was extracted into separate class | |
223 // to perform asynchronous operations because FileStream can be destroyed | |
224 // before completion of async operation. Also if async FileStream is destroyed | |
225 // without explicit closing file should be closed asynchronously without | |
226 // delaying FileStream's destructor. To perform all that separate object is | |
227 // necessary. | |
228 scoped_ptr<Context> context_; | |
224 | 229 |
225 DISALLOW_COPY_AND_ASSIGN(FileStream); | 230 DISALLOW_COPY_AND_ASSIGN(FileStream); |
226 }; | 231 }; |
227 | 232 |
228 } // namespace net | 233 } // namespace net |
229 | 234 |
230 #endif // NET_BASE_FILE_STREAM_H_ | 235 #endif // NET_BASE_FILE_STREAM_H_ |
OLD | NEW |