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 Windows. | 5 // This file implements FileStream for Windows. |
| 6 | 6 |
| 7 #ifndef NET_BASE_FILE_STREAM_WIN_H_ | 7 #ifndef NET_BASE_FILE_STREAM_WIN_H_ |
| 8 #define NET_BASE_FILE_STREAM_WIN_H_ | 8 #define NET_BASE_FILE_STREAM_WIN_H_ |
| 9 | 9 |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/message_loop.h" |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
| 13 #include "base/synchronization/waitable_event.h" | |
| 14 #include "net/base/completion_callback.h" | 12 #include "net/base/completion_callback.h" |
| 13 #include "net/base/file_stream.h" | |
| 14 #include "net/base/file_stream_metrics.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 | 18 |
| 19 class FilePath; | 19 class FilePath; |
| 20 | 20 |
| 21 namespace base { | |
| 22 class WaitableEvent; | |
| 23 } | |
| 24 | |
| 25 namespace net { | 21 namespace net { |
| 26 | 22 |
| 27 class IOBuffer; | 23 class IOBuffer; |
| 28 | 24 |
| 29 class NET_EXPORT FileStreamWin { | 25 class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { |
| 30 public: | 26 public: |
| 31 explicit FileStreamWin(net::NetLog* net_log); | 27 explicit AsyncContext(const BoundNetLog& bound_net_log); |
| 32 FileStreamWin(base::PlatformFile file, int flags, net::NetLog* net_log); | 28 AsyncContext(base::PlatformFile file, |
| 33 ~FileStreamWin(); | 29 const BoundNetLog& bound_net_log, |
| 30 int open_flags); | |
|
willchan no longer on Chromium
2012/08/27 06:24:06
Define the destructor here too.
| |
| 34 | 31 |
| 35 // FileStream implementations. | 32 // Destroys the context. It can be deleted in the method or deletion can be |
| 36 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 Destroy(); | |
| 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); | |
| 37 void CloseSync(); | 49 void CloseSync(); |
| 38 int Open(const FilePath& path, int open_flags, | 50 |
| 39 const CompletionCallback& callback); | 51 void SeekAsync(Whence whence, |
| 40 int OpenSync(const FilePath& path, int open_flags); | 52 int64 offset, |
| 41 bool IsOpen() const; | 53 const Int64CompletionCallback& callback); |
| 42 int Seek(Whence whence, int64 offset, | |
| 43 const Int64CompletionCallback& callback); | |
| 44 int64 SeekSync(Whence whence, int64 offset); | 54 int64 SeekSync(Whence whence, int64 offset); |
| 45 int64 Available(); | 55 |
| 46 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 56 int64 GetFileSize(); |
|
willchan no longer on Chromium
2012/08/27 06:59:10
make this const too
| |
| 57 | |
| 58 int ReadAsync(IOBuffer* buf, | |
| 59 int buf_len, | |
| 60 const CompletionCallback& callback); | |
| 47 int ReadSync(char* buf, int buf_len); | 61 int ReadSync(char* buf, int buf_len); |
| 48 int ReadUntilComplete(char *buf, int buf_len); | 62 |
| 49 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 63 int WriteAsync(IOBuffer* buf, |
| 64 int buf_len, | |
| 65 const CompletionCallback& callback); | |
| 50 int WriteSync(const char* buf, int buf_len); | 66 int WriteSync(const char* buf, int buf_len); |
| 51 int64 Truncate(int64 bytes); | 67 |
| 52 int Flush(); | 68 int Flush(); |
| 53 void EnableErrorStatistics(); | 69 |
| 54 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); | 70 int Truncate(int64 bytes); |
| 55 base::PlatformFile GetPlatformFileForTesting(); | |
| 56 | 71 |
| 57 private: | 72 private: |
| 58 class AsyncContext; | 73 // Map system error into network error code and log it with |bound_net_log_|. |
| 74 // Method should be called with |net_log_lock_| locked. | |
| 75 int RecordAndMapError(int error, FileErrorSource source); | |
| 76 | |
| 77 void BeginOpenEvent(const FilePath& path); | |
| 78 | |
| 79 // Opens a file. | |
| 80 int OpenFileImpl(const FilePath& path, int open_flags); | |
| 81 | |
| 82 void CheckForOpenError(int* result); | |
| 83 | |
| 84 // Called when asynchronous Open() is completed. | |
| 85 void OnOpenCompleted(const CompletionCallback& callback, int result); | |
| 86 | |
| 87 // Called after any Open() is completed on thread where AsyncContext | |
| 88 // is created. | |
| 89 void RegisterInMessageLoop(); | |
| 90 | |
| 91 // Closes a file. | |
| 92 void CloseFileImpl(); | |
| 93 | |
| 94 void OnCloseCompleted(const CompletionCallback& callback); | |
| 59 | 95 |
| 60 // A helper method for Seek. | 96 // A helper method for Seek. |
| 61 void SeekFile(Whence whence, int64 offset, int64* result); | 97 int64 SeekFileImpl(Whence whence, int64 offset); |
| 62 | 98 |
| 63 // Called when the file_ is opened asynchronously. |result| contains the | 99 void CheckForSeekError(int64* result); |
| 64 // result as a network error code. | 100 void OnSeekCompleted(const CompletionCallback64& callback, int64 result); |
| 65 void OnOpened(const CompletionCallback& callback, int* result); | |
| 66 | 101 |
| 67 // Called when the file_ is closed asynchronously. | 102 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); |
| 68 void OnClosed(const CompletionCallback& callback); | |
| 69 | 103 |
| 70 // Called when the file_ is seeked asynchronously. | 104 // Implementation of MessageLoopForIO::IOHandler |
| 71 void OnSeeked(const Int64CompletionCallback& callback, int64* result); | 105 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
| 106 DWORD bytes_read, | |
| 107 DWORD error) OVERRIDE; | |
| 72 | 108 |
| 73 // Resets on_io_complete_ and WeakPtr's. | 109 // Called when asynchronous Open(), Close() or Seek() |
| 74 // Called in OnOpened, OnClosed and OnSeeked. | 110 // is completed. |result| contains the result or a network error code. |
| 75 void ResetOnIOComplete(); | 111 template <typename R> |
| 112 void OnAsyncCompleted(const base::Callback<void(R)>& callback, R result); | |
| 76 | 113 |
| 77 // Waits until the in-flight async open/close operation is complete. | 114 // Delete the context with asynchronous closing if necessary. |
| 78 void WaitForIOCompletion(); | 115 void DeleteAbandoned(); |
| 79 | 116 |
| 80 // This member is used to support asynchronous reads. It is non-null when | 117 MessageLoopForIO::IOContext io_context_; |
| 81 // the FileStreamWin was opened with PLATFORM_FILE_ASYNC. | 118 CompletionCallback callback_; |
| 82 scoped_ptr<AsyncContext> async_context_; | 119 scoped_refptr<IOBuffer> in_flight_buf_; |
| 83 | |
| 84 base::PlatformFile file_; | 120 base::PlatformFile file_; |
| 85 int open_flags_; | |
| 86 bool auto_closed_; | |
| 87 bool record_uma_; | 121 bool record_uma_; |
| 88 net::BoundNetLog bound_net_log_; | 122 bool async_in_progress_; |
| 89 base::WeakPtrFactory<FileStreamWin> weak_ptr_factory_; | 123 bool destroyed_; |
| 90 scoped_ptr<base::WaitableEvent> on_io_complete_; | 124 BoundNetLog bound_net_log_; |
| 91 | 125 FileErrorSource error_source_; |
| 92 DISALLOW_COPY_AND_ASSIGN(FileStreamWin); | |
| 93 }; | 126 }; |
| 94 | 127 |
| 95 } // namespace net | 128 } // namespace net |
| 96 | 129 |
| 97 #endif // NET_BASE_FILE_STREAM_WIN_H_ | 130 #endif // NET_BASE_FILE_STREAM_WIN_H_ |
| OLD | NEW |