Chromium Code Reviews| 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 implements FileStream for POSIX. | 5 // This file implements FileStream for POSIX. |
| 6 | 6 |
| 7 #ifndef NET_BASE_FILE_STREAM_POSIX_H_ | 7 #ifndef NET_BASE_FILE_STREAM_POSIX_H_ |
| 8 #define NET_BASE_FILE_STREAM_POSIX_H_ | 8 #define NET_BASE_FILE_STREAM_POSIX_H_ |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
| 13 #include "net/base/completion_callback.h" | 11 #include "net/base/completion_callback.h" |
| 12 #include "net/base/file_stream.h" | |
| 13 #include "net/base/file_stream_metrics.h" | |
| 14 #include "net/base/file_stream_whence.h" | 14 #include "net/base/file_stream_whence.h" |
| 15 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
| 16 #include "net/base/net_log.h" | 16 #include "net/base/net_log.h" |
| 17 | 17 |
| 18 class FilePath; | 18 class FilePath; |
| 19 | 19 |
| 20 namespace base { | |
| 21 class WaitableEvent; | |
| 22 } | |
| 23 | |
| 24 namespace net { | 20 namespace net { |
| 25 | 21 |
| 26 class IOBuffer; | 22 class IOBuffer; |
| 27 | 23 |
| 28 class NET_EXPORT FileStreamPosix { | 24 class FileStream::AsyncContext { |
| 29 public: | 25 public: |
| 30 explicit FileStreamPosix(net::NetLog* net_log); | 26 explicit AsyncContext(const BoundNetLog& bound_net_log); |
| 31 FileStreamPosix(base::PlatformFile file, int flags, net::NetLog* net_log); | 27 AsyncContext(base::PlatformFile file, |
| 32 ~FileStreamPosix(); | 28 const BoundNetLog& bound_net_log, |
| 29 int open_flags); | |
| 33 | 30 |
|
willchan no longer on Chromium
2012/08/27 06:24:06
Define a destructor. The compiler generated destru
| |
| 34 // FileStream implementations. | 31 // Destroys the context. It can be deleted in the method or deletion can be |
| 35 void Close(const CompletionCallback& callback); | 32 // deferred to WorkerPool if some asynchronous operation is now in progress |
| 33 // or if auto-closing is needed. | |
| 34 void Destroy(); | |
| 35 | |
| 36 bool record_uma() const { return record_uma_; } | |
| 37 void set_record_uma(bool value) { record_uma_ = value; } | |
| 38 base::PlatformFile file() const { return file_; } | |
| 39 bool async_in_progress() const { return async_in_progress_; } | |
| 40 | |
| 41 // Sync and async versions of all operations | |
| 42 void OpenAsync(const FilePath& path, | |
| 43 int open_flags, | |
| 44 const CompletionCallback& callback); | |
| 45 int OpenSync(const FilePath& path, int open_flags); | |
| 46 | |
| 47 void CloseAsync(const CompletionCallback& callback); | |
| 36 void CloseSync(); | 48 void CloseSync(); |
| 37 int Open(const FilePath& path, int open_flags, | 49 |
| 38 const CompletionCallback& callback); | 50 void SeekAsync(Whence whence, |
| 39 int OpenSync(const FilePath& path, int open_flags); | 51 int64 offset, |
| 40 bool IsOpen() const; | 52 const Int64CompletionCallback& callback); |
| 41 int Seek(Whence whence, int64 offset, | |
| 42 const Int64CompletionCallback& callback); | |
| 43 int64 SeekSync(Whence whence, int64 offset); | 53 int64 SeekSync(Whence whence, int64 offset); |
| 44 int64 Available(); | 54 |
| 45 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 55 int64 GetFileSize(); |
|
willchan no longer on Chromium
2012/08/27 06:24:06
const?
| |
| 56 | |
| 57 int ReadAsync(IOBuffer* buf, | |
| 58 int buf_len, | |
| 59 const CompletionCallback& callback); | |
| 46 int ReadSync(char* buf, int buf_len); | 60 int ReadSync(char* buf, int buf_len); |
| 47 int ReadUntilComplete(char *buf, int buf_len); | 61 |
| 48 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 62 int WriteAsync(IOBuffer* buf, |
| 63 int buf_len, | |
| 64 const CompletionCallback& callback); | |
| 49 int WriteSync(const char* buf, int buf_len); | 65 int WriteSync(const char* buf, int buf_len); |
| 50 int64 Truncate(int64 bytes); | 66 |
| 51 int Flush(); | 67 int Flush(); |
| 52 void EnableErrorStatistics(); | |
| 53 void SetBoundNetLogSource( | |
| 54 const net::BoundNetLog& owner_bound_net_log); | |
| 55 base::PlatformFile GetPlatformFileForTesting(); | |
| 56 | 68 |
| 57 // Resets on_io_complete_ and WeakPtr's. | 69 int Truncate(int64 bytes); |
| 58 // Called when Read() or Write() is completed. | |
| 59 void ResetOnIOComplete(); | |
| 60 | 70 |
| 61 private: | 71 private: |
| 62 // Called when the file_ is closed asynchronously. | 72 // Map system error into network error code and log it with |bound_net_log_|. |
| 63 void OnClosed(const CompletionCallback& callback); | 73 // Method should be called with |net_log_lock_| locked. |
| 74 int RecordAndMapError(int error, FileErrorSource source); | |
| 64 | 75 |
| 65 // Waits until the in-flight async open/close/read/write operation is | 76 void BeginOpenEvent(const FilePath& path); |
| 66 // complete. | 77 |
| 67 void WaitForIOCompletion(); | 78 // Opens a file. |
| 79 int OpenFileImpl(const FilePath& path, int open_flags); | |
| 80 | |
| 81 void CheckForOpenError(int* result); | |
| 82 void OnOpenCompleted(const CompletionCallback& callback, int result); | |
| 83 | |
| 84 // Closes a file. | |
| 85 void CloseFileImpl(); | |
| 86 | |
| 87 void OnCloseCompleted(const CompletionCallback& callback); | |
| 88 | |
| 89 // Adjusts the position from where the data is read. | |
| 90 int64 SeekFileImpl(Whence whence, int64 offset); | |
| 91 | |
| 92 // ReadFile() is a simple wrapper around read() that handles EINTR signals | |
| 93 // and calls RecordAndMapError() to map errno to net error codes. | |
| 94 int ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | |
| 95 | |
| 96 // WriteFile() is a simple wrapper around write() that handles EINTR signals | |
| 97 // and calls MapSystemError() to map errno to net error codes. It tries | |
| 98 // to write to completion. | |
| 99 int WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | |
| 100 | |
| 101 // Checks for IO error that probably happened in ReadFileImpl(), | |
| 102 // WriteFileImpl() or SeekFileImpl(). If there was error reports it. | |
| 103 template <typename R> | |
| 104 void CheckForIOError(R* result, FileErrorSource source); | |
| 105 | |
| 106 // Called when asynchronous Read(), Write() or Seek() is completed. | |
| 107 // Reports error if needed and calls callback. | |
| 108 template <typename R> | |
| 109 void OnIOCompleted(const base::Callback<void(R)>& callback, | |
| 110 FileErrorSource source, | |
| 111 R result); | |
| 112 | |
| 113 // Called when asynchronous Close(), Open(), Read(), Write() or Seek() | |
| 114 // is completed. |result| contains the result or a network error code. | |
| 115 template <typename R> | |
| 116 void OnAsyncCompleted(const base::Callback<void(R)>& callback, R result); | |
| 117 | |
| 118 // Delete the context with asynchronous closing if necessary. | |
| 119 void DeleteAbandoned(); | |
| 68 | 120 |
| 69 base::PlatformFile file_; | 121 base::PlatformFile file_; |
| 70 int open_flags_; | |
| 71 bool auto_closed_; | |
| 72 bool record_uma_; | 122 bool record_uma_; |
| 73 net::BoundNetLog bound_net_log_; | 123 bool async_in_progress_; |
| 74 base::WeakPtrFactory<FileStreamPosix> weak_ptr_factory_; | 124 bool destroyed_; |
| 75 scoped_ptr<base::WaitableEvent> on_io_complete_; | 125 BoundNetLog bound_net_log_; |
| 76 | |
| 77 DISALLOW_COPY_AND_ASSIGN(FileStreamPosix); | |
| 78 }; | 126 }; |
|
willchan no longer on Chromium
2012/08/27 06:24:06
DISALLOW_COPY_AND_ASSIGN
| |
| 79 | 127 |
| 80 } // namespace net | 128 } // namespace net |
| 81 | 129 |
| 82 #endif // NET_BASE_FILE_STREAM_POSIX_H | 130 #endif // NET_BASE_FILE_STREAM_POSIX_H |
| OLD | NEW |