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::Context : public MessageLoopForIO::IOHandler { |
30 public: | 26 public: |
31 explicit FileStreamWin(net::NetLog* net_log); | 27 explicit Context(const BoundNetLog& bound_net_log); |
32 FileStreamWin(base::PlatformFile file, int flags, net::NetLog* net_log); | 28 Context(base::PlatformFile file, |
33 ~FileStreamWin(); | 29 const BoundNetLog& bound_net_log, |
| 30 int open_flags); |
| 31 ~Context(); |
34 | 32 |
35 // FileStream implementations. | 33 // Destroys the context. It can be deleted in the method or deletion can be |
36 void Close(const CompletionCallback& callback); | 34 // deferred to WorkerPool if some asynchronous operation is now in progress |
| 35 // or if auto-closing is needed. |
| 36 void Orphan(); |
| 37 |
| 38 bool record_uma() const { return record_uma_; } |
| 39 void set_record_uma(bool value) { record_uma_ = value; } |
| 40 base::PlatformFile file() const { return file_; } |
| 41 bool async_in_progress() const { return async_in_progress_; } |
| 42 |
| 43 // Sync and async versions of all operations |
| 44 void OpenAsync(const FilePath& path, |
| 45 int open_flags, |
| 46 const CompletionCallback& callback); |
| 47 int OpenSync(const FilePath& path, int open_flags); |
| 48 |
| 49 void CloseAsync(const CompletionCallback& callback); |
37 void CloseSync(); | 50 void CloseSync(); |
38 int Open(const FilePath& path, int open_flags, | 51 |
39 const CompletionCallback& callback); | 52 void SeekAsync(Whence whence, |
40 int OpenSync(const FilePath& path, int open_flags); | 53 int64 offset, |
41 bool IsOpen() const; | 54 const Int64CompletionCallback& callback); |
42 int Seek(Whence whence, int64 offset, | |
43 const Int64CompletionCallback& callback); | |
44 int64 SeekSync(Whence whence, int64 offset); | 55 int64 SeekSync(Whence whence, int64 offset); |
45 int64 Available(); | 56 |
46 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 57 int64 GetFileSize() const; |
| 58 |
| 59 int ReadAsync(IOBuffer* buf, |
| 60 int buf_len, |
| 61 const CompletionCallback& callback); |
47 int ReadSync(char* buf, int buf_len); | 62 int ReadSync(char* buf, int buf_len); |
48 int ReadUntilComplete(char *buf, int buf_len); | 63 |
49 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 64 int WriteAsync(IOBuffer* buf, |
| 65 int buf_len, |
| 66 const CompletionCallback& callback); |
50 int WriteSync(const char* buf, int buf_len); | 67 int WriteSync(const char* buf, int buf_len); |
51 int64 Truncate(int64 bytes); | 68 |
52 int Flush(); | 69 int Flush(); |
53 void EnableErrorStatistics(); | 70 |
54 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); | 71 int Truncate(int64 bytes); |
55 base::PlatformFile GetPlatformFileForTesting(); | |
56 | 72 |
57 private: | 73 private: |
58 class AsyncContext; | 74 // Map system error into network error code and log it with |bound_net_log_|. |
| 75 int RecordAndMapError(int error, FileErrorSource source) const; |
| 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 Int64CompletionCallback& 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() or Seek() is completed. |result| contains |
74 // Called in OnOpened, OnClosed and OnSeeked. | 110 // 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 MessageLoopForIO::IOContext io_context_; |
78 void WaitForIOCompletion(); | 115 CompletionCallback callback_; |
| 116 scoped_refptr<IOBuffer> in_flight_buf_; |
| 117 base::PlatformFile file_; |
| 118 bool record_uma_; |
| 119 bool async_in_progress_; |
| 120 bool orphaned_; |
| 121 BoundNetLog bound_net_log_; |
| 122 FileErrorSource error_source_; |
79 | 123 |
80 // This member is used to support asynchronous reads. It is non-null when | 124 DISALLOW_COPY_AND_ASSIGN(Context); |
81 // the FileStreamWin was opened with PLATFORM_FILE_ASYNC. | |
82 scoped_ptr<AsyncContext> async_context_; | |
83 | |
84 base::PlatformFile file_; | |
85 int open_flags_; | |
86 bool auto_closed_; | |
87 bool record_uma_; | |
88 net::BoundNetLog bound_net_log_; | |
89 base::WeakPtrFactory<FileStreamWin> weak_ptr_factory_; | |
90 scoped_ptr<base::WaitableEvent> on_io_complete_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(FileStreamWin); | |
93 }; | 125 }; |
94 | 126 |
95 } // namespace net | 127 } // namespace net |
96 | 128 |
97 #endif // NET_BASE_FILE_STREAM_WIN_H_ | 129 #endif // NET_BASE_FILE_STREAM_WIN_H_ |
OLD | NEW |