| 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 #pragma once | 12 #pragma once |
| 13 | 13 |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/platform_file.h" | 15 #include "base/platform_file.h" |
| 16 #include "net/base/completion_callback.h" | 16 #include "net/base/completion_callback.h" |
| 17 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
| 18 #include "net/base/net_log.h" | 18 #include "net/base/net_log.h" |
| 19 | 19 |
| 20 class FilePath; | 20 class FilePath; |
| 21 | 21 |
| 22 namespace net { | 22 namespace net { |
| 23 | 23 |
| 24 class IOBuffer; |
| 25 |
| 24 // TODO(darin): Move this to a more generic location. | 26 // TODO(darin): Move this to a more generic location. |
| 25 // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. | 27 // This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. |
| 26 enum Whence { | 28 enum Whence { |
| 27 FROM_BEGIN = 0, | 29 FROM_BEGIN = 0, |
| 28 FROM_CURRENT = 1, | 30 FROM_CURRENT = 1, |
| 29 FROM_END = 2 | 31 FROM_END = 2 |
| 30 }; | 32 }; |
| 31 | 33 |
| 32 class NET_EXPORT FileStream { | 34 class NET_EXPORT FileStream { |
| 33 public: | 35 public: |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 // other words, partial reads are allowed.) Returns the number of bytes | 82 // other words, partial reads are allowed.) Returns the number of bytes |
| 81 // copied, 0 if at end-of-file, or an error code if the operation could | 83 // copied, 0 if at end-of-file, or an error code if the operation could |
| 82 // not be performed. | 84 // not be performed. |
| 83 // | 85 // |
| 84 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null | 86 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null |
| 85 // callback must be passed to this method. If the read could not | 87 // callback must be passed to this method. If the read could not |
| 86 // complete synchronously, then ERR_IO_PENDING is returned, and the | 88 // complete synchronously, then ERR_IO_PENDING is returned, and the |
| 87 // callback will be notified on the current thread (via the MessageLoop) | 89 // callback will be notified on the current thread (via the MessageLoop) |
| 88 // when the read has completed. | 90 // when the read has completed. |
| 89 // | 91 // |
| 90 // The memory pointed to by |buf| must remain valid until the callback is | |
| 91 // notified. TODO(satorux): Use IOBuffer instead of char*. | |
| 92 // | |
| 93 // It is valid to destroy or close the file stream while there is an | 92 // It is valid to destroy or close the file stream while there is an |
| 94 // asynchronous read in progress. That will cancel the read and allow | 93 // asynchronous read in progress. That will cancel the read and allow |
| 95 // the buffer to be freed. | 94 // the buffer to be freed. |
| 96 // | 95 // |
| 97 // It is invalid to request any asynchronous operations while there is an | 96 // It is invalid to request any asynchronous operations while there is an |
| 98 // in-flight asynchronous operation. | 97 // in-flight asynchronous operation. |
| 99 // | 98 // |
| 100 // This method must not be called if the stream was opened WRITE_ONLY. | 99 // This method must not be called if the stream was opened WRITE_ONLY. |
| 101 virtual int Read(char* buf, int buf_len, const CompletionCallback& callback); | 100 virtual int Read(IOBuffer* buf, int buf_len, |
| 101 const CompletionCallback& callback); |
| 102 | 102 |
| 103 // Call this method to read data from the current stream position | 103 // Call this method to read data from the current stream position |
| 104 // synchronously. Up to buf_len bytes will be copied into buf. (In | 104 // synchronously. Up to buf_len bytes will be copied into buf. (In |
| 105 // other words, partial reads are allowed.) Returns the number of bytes | 105 // other words, partial reads are allowed.) Returns the number of bytes |
| 106 // copied, 0 if at end-of-file, or an error code if the operation could | 106 // copied, 0 if at end-of-file, or an error code if the operation could |
| 107 // not be performed. | 107 // not be performed. |
| 108 // | 108 // |
| 109 // The file must not be opened with PLATFORM_FILE_ASYNC. | 109 // The file must not be opened with PLATFORM_FILE_ASYNC. |
| 110 // This method must not be called if the stream was opened WRITE_ONLY. | 110 // This method must not be called if the stream was opened WRITE_ONLY. |
| 111 virtual int ReadSync(char* buf, int buf_len); | 111 virtual int ReadSync(char* buf, int buf_len); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 122 // other words, partial writes are allowed.) Returns the number of | 122 // other words, partial writes are allowed.) Returns the number of |
| 123 // bytes written, or an error code if the operation could not be | 123 // bytes written, or an error code if the operation could not be |
| 124 // performed. | 124 // performed. |
| 125 // | 125 // |
| 126 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null | 126 // The file must be opened with PLATFORM_FILE_ASYNC, and a non-null |
| 127 // callback must be passed to this method. If the write could not | 127 // callback must be passed to this method. If the write could not |
| 128 // complete synchronously, then ERR_IO_PENDING is returned, and the | 128 // complete synchronously, then ERR_IO_PENDING is returned, and the |
| 129 // callback will be notified on the current thread (via the MessageLoop) | 129 // callback will be notified on the current thread (via the MessageLoop) |
| 130 // when the write has completed. | 130 // when the write has completed. |
| 131 // | 131 // |
| 132 // The memory pointed to by |buf| must remain valid until the callback | |
| 133 // is notified. TODO(satorux): Use IOBuffer instead of char*. | |
| 134 // | |
| 135 // It is valid to destroy or close the file stream while there is an | 132 // It is valid to destroy or close the file stream while there is an |
| 136 // asynchronous write in progress. That will cancel the write and allow | 133 // asynchronous write in progress. That will cancel the write and allow |
| 137 // the buffer to be freed. | 134 // the buffer to be freed. |
| 138 // | 135 // |
| 139 // It is invalid to request any asynchronous operations while there is an | 136 // It is invalid to request any asynchronous operations while there is an |
| 140 // in-flight asynchronous operation. | 137 // in-flight asynchronous operation. |
| 141 // | 138 // |
| 142 // This method must not be called if the stream was opened READ_ONLY. | 139 // This method must not be called if the stream was opened READ_ONLY. |
| 143 virtual int Write(const char* buf, int buf_len, | 140 virtual int Write(IOBuffer* buf, int buf_len, |
| 144 const CompletionCallback& callback); | 141 const CompletionCallback& callback); |
| 145 | 142 |
| 146 // Call this method to write data at the current stream position | 143 // Call this method to write data at the current stream position |
| 147 // synchronously. Up to buf_len bytes will be written from buf. (In | 144 // synchronously. Up to buf_len bytes will be written from buf. (In |
| 148 // other words, partial writes are allowed.) Returns the number of | 145 // other words, partial writes are allowed.) Returns the number of |
| 149 // bytes written, or an error code if the operation could not be | 146 // bytes written, or an error code if the operation could not be |
| 150 // performed. | 147 // performed. |
| 151 // | 148 // |
| 152 // The file must not be opened with PLATFORM_FILE_ASYNC. | 149 // The file must not be opened with PLATFORM_FILE_ASYNC. |
| 153 // This method must not be called if the stream was opened READ_ONLY. | 150 // This method must not be called if the stream was opened READ_ONLY. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 174 void EnableErrorStatistics(); | 171 void EnableErrorStatistics(); |
| 175 | 172 |
| 176 // Sets the source reference for net-internals logging. | 173 // Sets the source reference for net-internals logging. |
| 177 // Creates source dependency events between |owner_bound_net_log| and | 174 // Creates source dependency events between |owner_bound_net_log| and |
| 178 // |bound_net_log_|. Each gets an event showing the dependency on the other. | 175 // |bound_net_log_|. Each gets an event showing the dependency on the other. |
| 179 // If only one of those is valid, it gets an event showing that a change | 176 // If only one of those is valid, it gets an event showing that a change |
| 180 // of ownership happened, but without details. | 177 // of ownership happened, but without details. |
| 181 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); | 178 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); |
| 182 | 179 |
| 183 private: | 180 private: |
| 184 // Helper functions used to implement reads and writes. | |
| 185 int ReadInternal(char* buf, int buf_len, | |
| 186 const CompletionCallback& callback); | |
| 187 int WriteInternal(const char* buf, int buf_len, | |
| 188 const CompletionCallback& callback); | |
| 189 | |
| 190 class AsyncContext; | 181 class AsyncContext; |
| 191 friend class AsyncContext; | 182 friend class AsyncContext; |
| 192 friend class FileStreamTest; | 183 friend class FileStreamTest; |
| 193 | 184 |
| 194 // This member is used to support asynchronous reads. It is non-null when | 185 // This member is used to support asynchronous reads. It is non-null when |
| 195 // the FileStream was opened with PLATFORM_FILE_ASYNC. | 186 // the FileStream was opened with PLATFORM_FILE_ASYNC. |
| 196 scoped_ptr<AsyncContext> async_context_; | 187 scoped_ptr<AsyncContext> async_context_; |
| 197 | 188 |
| 198 base::PlatformFile file_; | 189 base::PlatformFile file_; |
| 199 int open_flags_; | 190 int open_flags_; |
| 200 bool auto_closed_; | 191 bool auto_closed_; |
| 201 bool record_uma_; | 192 bool record_uma_; |
| 202 net::BoundNetLog bound_net_log_; | 193 net::BoundNetLog bound_net_log_; |
| 203 | 194 |
| 204 DISALLOW_COPY_AND_ASSIGN(FileStream); | 195 DISALLOW_COPY_AND_ASSIGN(FileStream); |
| 205 }; | 196 }; |
| 206 | 197 |
| 207 } // namespace net | 198 } // namespace net |
| 208 | 199 |
| 209 #endif // NET_BASE_FILE_STREAM_H_ | 200 #endif // NET_BASE_FILE_STREAM_H_ |
| OLD | NEW |