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); |
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(); |
| 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 with some network logging. |
| 80 // The result code is written to |result|. |
| 81 void OpenFileImpl(const FilePath& path, int open_flags, int* result); |
| 82 |
| 83 void CheckForOpenError(int* result); |
| 84 |
| 85 // Called when asynchronous Open() is completed. |
| 86 void OnOpenCompleted(const CompletionCallback& callback, int* result); |
| 87 |
| 88 // Called after any Open() is completed on thread where AsyncContext |
| 89 // is created. |
| 90 void RegisterInMessageLoop(); |
| 91 |
| 92 // Closes a file with some network logging. |
| 93 void CloseFileImpl(); |
| 94 |
| 95 void OnCloseCompleted(const CompletionCallback& callback, int* result); |
59 | 96 |
60 // A helper method for Seek. | 97 // A helper method for Seek. |
61 void SeekFile(Whence whence, int64 offset, int64* result); | 98 void SeekFileImpl(Whence whence, int64 offset, int64* result); |
62 | 99 |
63 // Called when the file_ is opened asynchronously. |result| contains the | 100 void CheckForSeekError(int64* result); |
64 // result as a network error code. | 101 void OnSeekCompleted(const CompletionCallback64& callback, int64* result); |
65 void OnOpened(const CompletionCallback& callback, int* result); | |
66 | 102 |
67 // Called when the file_ is closed asynchronously. | 103 void IOCompletionIsPending(const CompletionCallback& callback, |
68 void OnClosed(const CompletionCallback& callback); | 104 IOBuffer* buf); |
69 | 105 |
70 // Called when the file_ is seeked asynchronously. | 106 // Implementation of MessageLoopForIO::IOHandler |
71 void OnSeeked(const Int64CompletionCallback& callback, int64* result); | 107 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
| 108 DWORD bytes_read, |
| 109 DWORD error) OVERRIDE; |
72 | 110 |
73 // Resets on_io_complete_ and WeakPtr's. | 111 // Called when asynchronous Open(), Close() or Seek() |
74 // Called in OnOpened, OnClosed and OnSeeked. | 112 // is completed. |result| contains the result or a network error code. |
75 void ResetOnIOComplete(); | 113 template <typename R> |
| 114 void OnAsyncCompleted(const base::Callback<void(R)>& callback, R* result); |
76 | 115 |
77 // Waits until the in-flight async open/close operation is complete. | 116 // Delete the context with asynchronous closing if necessary. |
78 void WaitForIOCompletion(); | 117 void DeleteAbandoned(); |
79 | 118 |
80 // This member is used to support asynchronous reads. It is non-null when | 119 MessageLoopForIO::IOContext io_context_; |
81 // the FileStreamWin was opened with PLATFORM_FILE_ASYNC. | 120 CompletionCallback callback_; |
82 scoped_ptr<AsyncContext> async_context_; | 121 scoped_refptr<IOBuffer> in_flight_buf_; |
83 | |
84 base::PlatformFile file_; | 122 base::PlatformFile file_; |
85 int open_flags_; | |
86 bool auto_closed_; | |
87 bool record_uma_; | 123 bool record_uma_; |
88 net::BoundNetLog bound_net_log_; | 124 bool async_in_progress_; |
89 base::WeakPtrFactory<FileStreamWin> weak_ptr_factory_; | 125 bool destroyed_; |
90 scoped_ptr<base::WaitableEvent> on_io_complete_; | 126 BoundNetLog bound_net_log_; |
91 | 127 FileErrorSource error_source_; |
92 DISALLOW_COPY_AND_ASSIGN(FileStreamWin); | |
93 }; | 128 }; |
94 | 129 |
95 } // namespace net | 130 } // namespace net |
96 | 131 |
97 #endif // NET_BASE_FILE_STREAM_WIN_H_ | 132 #endif // NET_BASE_FILE_STREAM_WIN_H_ |
OLD | NEW |