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, |
willchan no longer on Chromium
2012/09/11 23:05:02
Indentation
| |
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 enum { |
75 kErrorBadFile = ERROR_INVALID_HANDLE | |
willchan no longer on Chromium
2012/09/11 23:05:02
There's a fun thread on chromium-dev about enum st
| |
76 }; | |
77 | |
78 struct OpenResult { | |
79 base::PlatformFile file; | |
80 int error_code; | |
81 }; | |
82 | |
83 int GetLastErrno() { return GetLastError(); } | |
84 | |
85 // Map system error into network error code and log it with |bound_net_log_|. | |
86 int RecordAndMapError(int error, FileErrorSource source) const; | |
87 | |
88 void BeginOpenEvent(const FilePath& path); | |
89 | |
90 // Opens a file. | |
91 OpenResult OpenFileImpl(const FilePath& path, int open_flags); | |
92 | |
93 int ProcessOpenError(int error_code); | |
94 | |
95 // Called when asynchronous Open() is completed. | |
96 void OnOpenCompleted(const CompletionCallback& callback, OpenResult result); | |
97 | |
98 // Called after opening a file in async mode. | |
99 void OnAsyncFileOpened(); | |
100 | |
101 void OnCloseCompleted(const CompletionCallback& callback); | |
59 | 102 |
60 // A helper method for Seek. | 103 // A helper method for Seek. |
61 void SeekFile(Whence whence, int64 offset, int64* result); | 104 int64 SeekFileImpl(Whence whence, int64 offset); |
62 | 105 |
63 // Called when the file_ is opened asynchronously. |result| contains the | 106 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); |
64 // result as a network error code. | |
65 void OnOpened(const CompletionCallback& callback, int* result); | |
66 | 107 |
67 // Called when the file_ is closed asynchronously. | 108 // Implementation of MessageLoopForIO::IOHandler |
68 void OnClosed(const CompletionCallback& callback); | 109 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
110 DWORD bytes_read, | |
111 DWORD error) OVERRIDE; | |
69 | 112 |
70 // Called when the file_ is seeked asynchronously. | 113 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); |
71 void OnSeeked(const Int64CompletionCallback& callback, int64* result); | |
72 | 114 |
73 // Resets on_io_complete_ and WeakPtr's. | 115 // Checks for IO error that probably happened in SeekFileImpl(). |
74 // Called in OnOpened, OnClosed and OnSeeked. | 116 // If there was error reports it. |
75 void ResetOnIOComplete(); | 117 void CheckForIOError(int64* result, FileErrorSource source); |
76 | 118 |
77 // Waits until the in-flight async open/close operation is complete. | 119 // Called when asynchronous Seek() is completed. |
78 void WaitForIOCompletion(); | 120 // Reports error if needed and calls callback. |
121 void ProcessAsyncResult(const Int64CompletionCallback& callback, | |
122 FileErrorSource source, | |
123 int64 result); | |
79 | 124 |
80 // This member is used to support asynchronous reads. It is non-null when | 125 // Called when asynchronous Open() or Seek() |
81 // the FileStreamWin was opened with PLATFORM_FILE_ASYNC. | 126 // is completed. |result| contains the result or a network error code. |
82 scoped_ptr<AsyncContext> async_context_; | 127 void OnAsyncCompleted(const Int64CompletionCallback& callback, int64 result); |
83 | 128 |
129 MessageLoopForIO::IOContext io_context_; | |
130 CompletionCallback callback_; | |
131 scoped_refptr<IOBuffer> in_flight_buf_; | |
84 base::PlatformFile file_; | 132 base::PlatformFile file_; |
85 int open_flags_; | |
86 bool auto_closed_; | |
87 bool record_uma_; | 133 bool record_uma_; |
88 net::BoundNetLog bound_net_log_; | 134 bool async_in_progress_; |
89 base::WeakPtrFactory<FileStreamWin> weak_ptr_factory_; | 135 bool orphaned_; |
90 scoped_ptr<base::WaitableEvent> on_io_complete_; | 136 BoundNetLog bound_net_log_; |
137 FileErrorSource error_source_; | |
91 | 138 |
92 DISALLOW_COPY_AND_ASSIGN(FileStreamWin); | 139 DISALLOW_COPY_AND_ASSIGN(Context); |
93 }; | 140 }; |
94 | 141 |
95 } // namespace net | 142 } // namespace net |
96 | 143 |
97 #endif // NET_BASE_FILE_STREAM_WIN_H_ | 144 #endif // NET_BASE_FILE_STREAM_WIN_H_ |
OLD | NEW |