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 POSIX. | 5 // This file implements FileStream for POSIX. |
6 | 6 |
7 #ifndef NET_BASE_FILE_STREAM_POSIX_H_ | 7 #ifndef NET_BASE_FILE_STREAM_POSIX_H_ |
8 #define NET_BASE_FILE_STREAM_POSIX_H_ | 8 #define NET_BASE_FILE_STREAM_POSIX_H_ |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include <errno.h> |
11 #include "base/memory/weak_ptr.h" | 11 |
12 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
13 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
14 #include "net/base/file_stream.h" | |
15 #include "net/base/file_stream_metrics.h" | |
14 #include "net/base/file_stream_whence.h" | 16 #include "net/base/file_stream_whence.h" |
15 #include "net/base/net_export.h" | 17 #include "net/base/net_export.h" |
16 #include "net/base/net_log.h" | 18 #include "net/base/net_log.h" |
17 | 19 |
18 class FilePath; | 20 class FilePath; |
19 | 21 |
20 namespace base { | |
21 class WaitableEvent; | |
22 } | |
23 | |
24 namespace net { | 22 namespace net { |
25 | 23 |
26 class IOBuffer; | 24 class IOBuffer; |
27 | 25 |
28 class NET_EXPORT FileStreamPosix { | 26 class FileStream::Context { |
29 public: | 27 public: |
30 explicit FileStreamPosix(net::NetLog* net_log); | 28 explicit Context(const BoundNetLog& bound_net_log); |
31 FileStreamPosix(base::PlatformFile file, int flags, net::NetLog* net_log); | 29 Context(base::PlatformFile file, |
32 ~FileStreamPosix(); | 30 const BoundNetLog& bound_net_log, |
31 int open_flags); | |
32 ~Context(); | |
33 | 33 |
34 // FileStream implementations. | 34 // Destroys the context. It can be deleted in the method or deletion can be |
35 void Close(const CompletionCallback& callback); | 35 // deferred to WorkerPool if some asynchronous operation is now in progress |
36 // or if auto-closing is needed. | |
37 void Orphan(); | |
38 | |
39 bool record_uma() const { return record_uma_; } | |
40 void set_record_uma(bool value) { record_uma_ = value; } | |
41 base::PlatformFile file() const { return file_; } | |
42 bool async_in_progress() const { return async_in_progress_; } | |
43 | |
44 // Sync and async versions of all operations | |
45 void OpenAsync(const FilePath& path, | |
46 int open_flags, | |
47 const CompletionCallback& callback); | |
48 int OpenSync(const FilePath& path, int open_flags); | |
49 | |
50 void CloseAsync(const CompletionCallback& callback); | |
36 void CloseSync(); | 51 void CloseSync(); |
37 int Open(const FilePath& path, int open_flags, | 52 |
38 const CompletionCallback& callback); | 53 void SeekAsync(Whence whence, |
39 int OpenSync(const FilePath& path, int open_flags); | 54 int64 offset, |
40 bool IsOpen() const; | 55 const Int64CompletionCallback& callback); |
41 int Seek(Whence whence, int64 offset, | |
42 const Int64CompletionCallback& callback); | |
43 int64 SeekSync(Whence whence, int64 offset); | 56 int64 SeekSync(Whence whence, int64 offset); |
44 int64 Available(); | 57 |
45 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 58 int64 GetFileSize() const; |
59 | |
60 int ReadAsync(IOBuffer* buf, | |
61 int buf_len, | |
62 const CompletionCallback& callback); | |
46 int ReadSync(char* buf, int buf_len); | 63 int ReadSync(char* buf, int buf_len); |
47 int ReadUntilComplete(char *buf, int buf_len); | 64 |
48 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); | 65 int WriteAsync(IOBuffer* buf, |
66 int buf_len, | |
67 const CompletionCallback& callback); | |
49 int WriteSync(const char* buf, int buf_len); | 68 int WriteSync(const char* buf, int buf_len); |
50 int64 Truncate(int64 bytes); | 69 |
51 int Flush(); | 70 int Flush(); |
52 void EnableErrorStatistics(); | |
53 void SetBoundNetLogSource( | |
54 const net::BoundNetLog& owner_bound_net_log); | |
55 base::PlatformFile GetPlatformFileForTesting(); | |
56 | 71 |
57 // Resets on_io_complete_ and WeakPtr's. | 72 int Truncate(int64 bytes); |
58 // Called when Read() or Write() is completed. | |
59 void ResetOnIOComplete(); | |
60 | 73 |
61 private: | 74 private: |
62 // Called when the file_ is closed asynchronously. | 75 enum { |
63 void OnClosed(const CompletionCallback& callback); | 76 kErrorBadFile = EBADF |
77 }; | |
64 | 78 |
65 // Waits until the in-flight async open/close/read/write operation is | 79 struct OpenResult { |
66 // complete. | 80 base::PlatformFile file; |
67 void WaitForIOCompletion(); | 81 int error_code; |
82 }; | |
83 | |
84 // Stubs to make more of the Context code multi-platform. | |
willchan no longer on Chromium
2012/09/11 23:05:02
Can you use banner comments to indicate which sect
| |
85 int GetLastErrno() { return errno; } | |
86 void OnAsyncFileOpened() {} | |
87 | |
88 // Map system error into network error code and log it with |bound_net_log_|. | |
89 int RecordAndMapError(int error, FileErrorSource source) const; | |
90 | |
91 void BeginOpenEvent(const FilePath& path); | |
92 | |
93 // Opens a file. | |
94 OpenResult OpenFileImpl(const FilePath& path, int open_flags); | |
95 | |
96 int ProcessOpenError(int error_code); | |
97 void OnOpenCompleted(const CompletionCallback& callback, OpenResult result); | |
98 | |
99 void OnCloseCompleted(const CompletionCallback& callback); | |
100 | |
101 // Adjusts the position from where the data is read. | |
102 int64 SeekFileImpl(Whence whence, int64 offset); | |
103 | |
104 // ReadFile() is a simple wrapper around read() that handles EINTR signals | |
105 // and calls RecordAndMapError() to map errno to net error codes. | |
106 int64 ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | |
107 | |
108 // WriteFile() is a simple wrapper around write() that handles EINTR signals | |
109 // and calls MapSystemError() to map errno to net error codes. It tries | |
110 // to write to completion. | |
111 int64 WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | |
112 | |
113 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); | |
114 | |
115 // Checks for IO error that probably happened in ReadFileImpl(), | |
116 // WriteFileImpl() or SeekFileImpl(). If there was error reports it. | |
117 void CheckForIOError(int64* result, FileErrorSource source); | |
118 | |
119 // Called when asynchronous Read(), Write() or Seek() is completed. | |
120 // Reports error if needed and calls callback. | |
121 void ProcessAsyncResult(const Int64CompletionCallback& callback, | |
122 FileErrorSource source, | |
123 int64 result); | |
124 | |
125 // Called when asynchronous Open(), Read(), Write() or Seek() | |
126 // is completed. |result| contains the result or a network error code. | |
127 void OnAsyncCompleted(const Int64CompletionCallback& callback, int64 result); | |
68 | 128 |
69 base::PlatformFile file_; | 129 base::PlatformFile file_; |
70 int open_flags_; | |
71 bool auto_closed_; | |
72 bool record_uma_; | 130 bool record_uma_; |
73 net::BoundNetLog bound_net_log_; | 131 bool async_in_progress_; |
74 base::WeakPtrFactory<FileStreamPosix> weak_ptr_factory_; | 132 bool orphaned_; |
75 scoped_ptr<base::WaitableEvent> on_io_complete_; | 133 BoundNetLog bound_net_log_; |
76 | 134 |
77 DISALLOW_COPY_AND_ASSIGN(FileStreamPosix); | 135 DISALLOW_COPY_AND_ASSIGN(Context); |
78 }; | 136 }; |
79 | 137 |
80 } // namespace net | 138 } // namespace net |
81 | 139 |
82 #endif // NET_BASE_FILE_STREAM_POSIX_H | 140 #endif // NET_BASE_FILE_STREAM_POSIX_H |
OLD | NEW |