| 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/platform_file.h" | 14 #include "base/platform_file.h" |
| 14 #include "base/task_runner.h" | 15 #include "base/task_runner.h" |
| 15 #include "net/base/completion_callback.h" | 16 #include "net/base/completion_callback.h" |
| 16 #include "net/base/file_stream_whence.h" | 17 #include "net/base/file_stream_whence.h" |
| 17 #include "net/base/net_export.h" | 18 #include "net/base/net_export.h" |
| 18 #include "net/base/net_log.h" | 19 #include "net/base/net_log.h" |
| 19 | 20 |
| 20 namespace base { | 21 namespace base { |
| 21 class FilePath; | 22 class FilePath; |
| 22 } | 23 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 38 | 39 |
| 39 // Construct a FileStream with an existing file handle and opening flags. | 40 // Construct a FileStream with an existing file handle and opening flags. |
| 40 // |file| is valid file handle. | 41 // |file| is valid file handle. |
| 41 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was | 42 // |flags| is a bitfield of base::PlatformFileFlags when the file handle was |
| 42 // opened. | 43 // opened. |
| 43 // |net_log| is the net log pointer to use to create a |BoundNetLog|. May be | 44 // |net_log| is the net log pointer to use to create a |BoundNetLog|. May be |
| 44 // NULL if logging is not needed. | 45 // NULL if logging is not needed. |
| 45 // Uses |task_runner| for asynchronous operations. | 46 // Uses |task_runner| for asynchronous operations. |
| 46 // Note: the new FileStream object takes ownership of the PlatformFile and | 47 // Note: the new FileStream object takes ownership of the PlatformFile and |
| 47 // will close it on destruction. | 48 // will close it on destruction. |
| 49 // This constructor is deprecated. |
| 50 // TODO(rvargas): remove all references to PlatformFile. |
| 48 FileStream(base::PlatformFile file, | 51 FileStream(base::PlatformFile file, |
| 49 int flags, | 52 int flags, |
| 50 net::NetLog* net_log, | 53 net::NetLog* net_log, |
| 51 const scoped_refptr<base::TaskRunner>& task_runner); | 54 const scoped_refptr<base::TaskRunner>& task_runner); |
| 52 | 55 |
| 53 // Same as above, but runs async tasks in base::WorkerPool. | 56 // Same as above, but runs async tasks in base::WorkerPool. |
| 57 // This constructor is deprecated. |
| 54 FileStream(base::PlatformFile file, int flags, net::NetLog* net_log); | 58 FileStream(base::PlatformFile file, int flags, net::NetLog* net_log); |
| 55 | 59 |
| 60 // Non-deprecated versions of the previous two constructors. |
| 61 FileStream(base::File file, |
| 62 net::NetLog* net_log, |
| 63 const scoped_refptr<base::TaskRunner>& task_runner); |
| 64 FileStream(base::File file, net::NetLog* net_log); |
| 65 |
| 56 // The underlying file is closed automatically. | 66 // The underlying file is closed automatically. |
| 57 virtual ~FileStream(); | 67 virtual ~FileStream(); |
| 58 | 68 |
| 59 // Call this method to open the FileStream asynchronously. The remaining | 69 // Call this method to open the FileStream asynchronously. The remaining |
| 60 // methods cannot be used unless the file is opened successfully. Returns | 70 // methods cannot be used unless the file is opened successfully. Returns |
| 61 // ERR_IO_PENDING if the operation is started. If the operation cannot be | 71 // ERR_IO_PENDING if the operation is started. If the operation cannot be |
| 62 // started then an error code is returned. | 72 // started then an error code is returned. |
| 63 // | 73 // |
| 64 // Once the operation is done, |callback| will be run on the thread where | 74 // Once the operation is done, |callback| will be run on the thread where |
| 65 // Open() was called, with the result code. open_flags is a bitfield of | 75 // Open() was called, with the result code. open_flags is a bitfield of |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 void EnableErrorStatistics(); | 242 void EnableErrorStatistics(); |
| 233 | 243 |
| 234 // Sets the source reference for net-internals logging. | 244 // Sets the source reference for net-internals logging. |
| 235 // Creates source dependency events between |owner_bound_net_log| and | 245 // Creates source dependency events between |owner_bound_net_log| and |
| 236 // |bound_net_log_|. Each gets an event showing the dependency on the other. | 246 // |bound_net_log_|. Each gets an event showing the dependency on the other. |
| 237 // If only one of those is valid, it gets an event showing that a change | 247 // If only one of those is valid, it gets an event showing that a change |
| 238 // of ownership happened, but without details. | 248 // of ownership happened, but without details. |
| 239 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); | 249 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); |
| 240 | 250 |
| 241 // Returns the underlying platform file for testing. | 251 // Returns the underlying platform file for testing. |
| 242 base::PlatformFile GetPlatformFileForTesting(); | 252 const base::File& GetFileForTesting() const; |
| 243 | 253 |
| 244 private: | 254 private: |
| 245 class Context; | 255 class Context; |
| 246 | 256 |
| 247 bool is_async() const { return !!(open_flags_ & base::PLATFORM_FILE_ASYNC); } | |
| 248 | |
| 249 int open_flags_; | |
| 250 net::BoundNetLog bound_net_log_; | 257 net::BoundNetLog bound_net_log_; |
| 251 | 258 |
| 252 // Context performing I/O operations. It was extracted into separate class | 259 // Context performing I/O operations. It was extracted into a separate class |
| 253 // to perform asynchronous operations because FileStream can be destroyed | 260 // to perform asynchronous operations because FileStream can be destroyed |
| 254 // before completion of async operation. Also if async FileStream is destroyed | 261 // before completion of an async operation. Also if a FileStream is destroyed |
| 255 // without explicit closing file should be closed asynchronously without | 262 // without explicitly calling Close, the file should be closed asynchronously |
| 256 // delaying FileStream's destructor. To perform all that separate object is | 263 // without delaying FileStream's destructor. |
| 257 // necessary. | |
| 258 scoped_ptr<Context> context_; | 264 scoped_ptr<Context> context_; |
| 259 | 265 |
| 260 DISALLOW_COPY_AND_ASSIGN(FileStream); | 266 DISALLOW_COPY_AND_ASSIGN(FileStream); |
| 261 }; | 267 }; |
| 262 | 268 |
| 263 } // namespace net | 269 } // namespace net |
| 264 | 270 |
| 265 #endif // NET_BASE_FILE_STREAM_H_ | 271 #endif // NET_BASE_FILE_STREAM_H_ |
| OLD | NEW |