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::Context { |
| 29 public: | 25 public: |
| 30 explicit FileStreamPosix(net::NetLog* net_log); | 26 explicit Context(const BoundNetLog& bound_net_log); |
| 31 FileStreamPosix(base::PlatformFile file, int flags, net::NetLog* net_log); | 27 Context(base::PlatformFile file, |
| 32 ~FileStreamPosix(); | 28 const BoundNetLog& bound_net_log, |
|
willchan no longer on Chromium
2012/09/05 00:15:48
Indentation is off
| |
| 29 int open_flags); | |
| 30 ~Context(); | |
| 33 | 31 |
| 34 // FileStream implementations. | 32 // Destroys the context. It can be deleted in the method or deletion can be |
| 35 void Close(const CompletionCallback& callback); | 33 // deferred to WorkerPool if some asynchronous operation is now in progress |
| 34 // or if auto-closing is needed. | |
| 35 void Orphan(); | |
| 36 | |
| 37 bool record_uma() const { return record_uma_; } | |
| 38 void set_record_uma(bool value) { record_uma_ = value; } | |
| 39 base::PlatformFile file() const { return file_; } | |
| 40 bool async_in_progress() const { return async_in_progress_; } | |
| 41 | |
| 42 // Sync and async versions of all operations | |
| 43 void OpenAsync(const FilePath& path, | |
| 44 int open_flags, | |
| 45 const CompletionCallback& callback); | |
| 46 int OpenSync(const FilePath& path, int open_flags); | |
| 47 | |
| 48 void CloseAsync(const CompletionCallback& callback); | |
| 36 void CloseSync(); | 49 void CloseSync(); |
| 37 int Open(const FilePath& path, int open_flags, | 50 |
| 38 const CompletionCallback& callback); | 51 void SeekAsync(Whence whence, |
| 39 int OpenSync(const FilePath& path, int open_flags); | 52 int64 offset, |
| 40 bool IsOpen() const; | 53 const Int64CompletionCallback& callback); |
| 41 int Seek(Whence whence, int64 offset, | |
| 42 const Int64CompletionCallback& callback); | |
| 43 int64 SeekSync(Whence whence, int64 offset); | 54 int64 SeekSync(Whence whence, int64 offset); |
| 44 int64 Available(); | 55 |
| 45 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 56 int64 GetFileSize() const; |
| 57 | |
| 58 int ReadAsync(IOBuffer* buf, | |
| 59 int buf_len, | |
| 60 const CompletionCallback& callback); | |
| 46 int ReadSync(char* buf, int buf_len); | 61 int ReadSync(char* buf, int buf_len); |
| 47 int ReadUntilComplete(char *buf, int buf_len); | 62 |
| 48 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 63 int WriteAsync(IOBuffer* buf, |
| 64 int buf_len, | |
| 65 const CompletionCallback& callback); | |
| 49 int WriteSync(const char* buf, int buf_len); | 66 int WriteSync(const char* buf, int buf_len); |
| 50 int64 Truncate(int64 bytes); | 67 |
| 51 int Flush(); | 68 int Flush(); |
| 52 void EnableErrorStatistics(); | |
| 53 void SetBoundNetLogSource( | |
| 54 const net::BoundNetLog& owner_bound_net_log); | |
| 55 base::PlatformFile GetPlatformFileForTesting(); | |
| 56 | 69 |
| 57 // Resets on_io_complete_ and WeakPtr's. | 70 int Truncate(int64 bytes); |
| 58 // Called when Read() or Write() is completed. | |
| 59 void ResetOnIOComplete(); | |
| 60 | 71 |
| 61 private: | 72 private: |
| 62 // Called when the file_ is closed asynchronously. | 73 // Map system error into network error code and log it with |bound_net_log_|. |
| 63 void OnClosed(const CompletionCallback& callback); | 74 int RecordAndMapError(int error, FileErrorSource source) const; |
| 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 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); | |
| 68 | 117 |
| 69 base::PlatformFile file_; | 118 base::PlatformFile file_; |
| 70 int open_flags_; | |
| 71 bool auto_closed_; | |
| 72 bool record_uma_; | 119 bool record_uma_; |
| 73 net::BoundNetLog bound_net_log_; | 120 bool async_in_progress_; |
| 74 base::WeakPtrFactory<FileStreamPosix> weak_ptr_factory_; | 121 bool orphaned_; |
| 75 scoped_ptr<base::WaitableEvent> on_io_complete_; | 122 BoundNetLog bound_net_log_; |
| 76 | 123 |
| 77 DISALLOW_COPY_AND_ASSIGN(FileStreamPosix); | 124 DISALLOW_COPY_AND_ASSIGN(Context); |
| 78 }; | 125 }; |
| 79 | 126 |
| 80 } // namespace net | 127 } // namespace net |
| 81 | 128 |
| 82 #endif // NET_BASE_FILE_STREAM_POSIX_H | 129 #endif // NET_BASE_FILE_STREAM_POSIX_H |
| OLD | NEW |