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 #pragma once | 9 #pragma once |
10 | 10 |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
14 #include "base/synchronization/waitable_event.h" | |
15 #include "net/base/completion_callback.h" | 12 #include "net/base/completion_callback.h" |
16 #include "net/base/file_stream_whence.h" | 13 #include "net/base/file_stream_whence.h" |
17 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
18 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
19 | 16 |
20 class FilePath; | 17 class FilePath; |
21 | 18 |
22 namespace base { | 19 namespace base { |
23 class WaitableEvent; | 20 class WaitableEvent; |
24 } | 21 } |
25 | 22 |
26 namespace net { | 23 namespace net { |
27 | 24 |
28 class IOBuffer; | 25 class IOBuffer; |
29 | 26 |
30 class NET_EXPORT FileStreamWin { | 27 class NET_EXPORT FileStreamWin { |
31 public: | 28 public: |
32 explicit FileStreamWin(net::NetLog* net_log); | 29 explicit FileStreamWin(net::NetLog* net_log); |
33 FileStreamWin(base::PlatformFile file, int flags, net::NetLog* net_log); | 30 FileStreamWin(base::PlatformFile file, int flags, net::NetLog* net_log); |
34 ~FileStreamWin(); | 31 ~FileStreamWin(); |
35 | 32 |
36 // FileStream implementations. | 33 // FileStream implementations. |
37 void Close(const CompletionCallback& callback); | 34 void Close(const CompletionCallback& callback); |
38 void CloseSync(); | 35 void CloseSync(); |
| 36 void CloseAndCancelAsync(); |
39 int Open(const FilePath& path, int open_flags, | 37 int Open(const FilePath& path, int open_flags, |
40 const CompletionCallback& callback); | 38 const CompletionCallback& callback); |
41 int OpenSync(const FilePath& path, int open_flags); | 39 int OpenSync(const FilePath& path, int open_flags); |
42 bool IsOpen() const; | 40 bool IsOpen() const; |
43 int Seek(Whence whence, int64 offset, | 41 int Seek(Whence whence, int64 offset, |
44 const Int64CompletionCallback& callback); | 42 const Int64CompletionCallback& callback); |
45 int64 SeekSync(Whence whence, int64 offset); | 43 int64 SeekSync(Whence whence, int64 offset); |
46 int64 Available(); | 44 int64 Available(); |
47 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 45 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
48 int ReadSync(char* buf, int buf_len); | 46 int ReadSync(char* buf, int buf_len); |
49 int ReadUntilComplete(char *buf, int buf_len); | 47 int ReadUntilComplete(char *buf, int buf_len); |
50 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 48 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
51 int WriteSync(const char* buf, int buf_len); | 49 int WriteSync(const char* buf, int buf_len); |
52 int64 Truncate(int64 bytes); | 50 int64 Truncate(int64 bytes); |
53 int Flush(); | 51 int Flush(); |
54 void EnableErrorStatistics(); | 52 void EnableErrorStatistics(); |
55 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); | 53 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); |
56 base::PlatformFile GetPlatformFileForTesting(); | 54 base::PlatformFile GetPlatformFileForTesting(); |
57 | 55 |
58 private: | 56 private: |
59 class AsyncContext; | 57 class AsyncContext; |
60 | 58 |
61 // A helper method for Seek. | 59 bool is_async() { return !!(open_flags_ & base::PLATFORM_FILE_ASYNC); } |
62 void SeekFile(Whence whence, int64 offset, int64* result); | |
63 | 60 |
64 // Called when the file_ is opened asynchronously. |result| contains the | 61 // Context performing I/O operations. Despite its name the context is used |
65 // result as a network error code. | 62 // for synchronous operations too, but it was extracted into separate class |
66 void OnOpened(const CompletionCallback& callback, int* result); | 63 // to perform asynchronous operations correctly. |
| 64 AsyncContext* context_; |
67 | 65 |
68 // Called when the file_ is closed asynchronously. | |
69 void OnClosed(const CompletionCallback& callback); | |
70 | |
71 // Called when the file_ is seeked asynchronously. | |
72 void OnSeeked(const Int64CompletionCallback& callback, int64* result); | |
73 | |
74 // Resets on_io_complete_ and WeakPtr's. | |
75 // Called in OnOpened, OnClosed and OnSeeked. | |
76 void ResetOnIOComplete(); | |
77 | |
78 // Waits until the in-flight async open/close operation is complete. | |
79 void WaitForIOCompletion(); | |
80 | |
81 // This member is used to support asynchronous reads. It is non-null when | |
82 // the FileStreamWin was opened with PLATFORM_FILE_ASYNC. | |
83 scoped_ptr<AsyncContext> async_context_; | |
84 | |
85 base::PlatformFile file_; | |
86 int open_flags_; | 66 int open_flags_; |
87 bool auto_closed_; | |
88 bool record_uma_; | |
89 net::BoundNetLog bound_net_log_; | 67 net::BoundNetLog bound_net_log_; |
90 base::WeakPtrFactory<FileStreamWin> weak_ptr_factory_; | |
91 scoped_ptr<base::WaitableEvent> on_io_complete_; | |
92 | 68 |
93 DISALLOW_COPY_AND_ASSIGN(FileStreamWin); | 69 DISALLOW_COPY_AND_ASSIGN(FileStreamWin); |
94 }; | 70 }; |
95 | 71 |
96 } // namespace net | 72 } // namespace net |
97 | 73 |
98 #endif // NET_BASE_FILE_STREAM_WIN_H_ | 74 #endif // NET_BASE_FILE_STREAM_WIN_H_ |
OLD | NEW |