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 // Stubs to make more of the Context code multi-platform. |
66 // complete. | 80 int GetLastErrno() { return errno; } |
67 void WaitForIOCompletion(); | 81 void RegisterInMessageLoop() {} |
| 82 |
| 83 // Map system error into network error code and log it with |bound_net_log_|. |
| 84 int RecordAndMapError(int error, FileErrorSource source) const; |
| 85 |
| 86 void BeginOpenEvent(const FilePath& path); |
| 87 |
| 88 // Opens a file. |
| 89 int OpenFileImpl(const FilePath& path, int open_flags); |
| 90 |
| 91 void CheckForOpenError(int* result); |
| 92 void OnOpenCompleted(const CompletionCallback& callback, int result); |
| 93 |
| 94 // Closes a file. |
| 95 void CloseFileImpl(); |
| 96 |
| 97 void OnCloseCompleted(const CompletionCallback& callback); |
| 98 |
| 99 // Adjusts the position from where the data is read. |
| 100 int64 SeekFileImpl(Whence whence, int64 offset); |
| 101 |
| 102 // ReadFile() is a simple wrapper around read() that handles EINTR signals |
| 103 // and calls RecordAndMapError() to map errno to net error codes. |
| 104 int ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
| 105 |
| 106 // WriteFile() is a simple wrapper around write() that handles EINTR signals |
| 107 // and calls MapSystemError() to map errno to net error codes. It tries |
| 108 // to write to completion. |
| 109 int WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); |
| 110 |
| 111 // Checks for IO error that probably happened in ReadFileImpl(), |
| 112 // WriteFileImpl() or SeekFileImpl(). If there was error reports it. |
| 113 template <typename R> |
| 114 void CheckForIOError(R* result, FileErrorSource source); |
| 115 |
| 116 // Called when asynchronous Read(), Write() or Seek() is completed. |
| 117 // Reports error if needed and calls callback. |
| 118 template <typename R> |
| 119 void OnIOCompleted(const base::Callback<void(R)>& callback, |
| 120 FileErrorSource source, |
| 121 R result); |
| 122 |
| 123 // Called when asynchronous Open(), Read(), Write() or Seek() |
| 124 // is completed. |result| contains the result or a network error code. |
| 125 template <typename R> |
| 126 void OnAsyncCompleted(const base::Callback<void(R)>& callback, R result); |
68 | 127 |
69 base::PlatformFile file_; | 128 base::PlatformFile file_; |
70 int open_flags_; | |
71 bool auto_closed_; | |
72 bool record_uma_; | 129 bool record_uma_; |
73 net::BoundNetLog bound_net_log_; | 130 bool async_in_progress_; |
74 base::WeakPtrFactory<FileStreamPosix> weak_ptr_factory_; | 131 bool orphaned_; |
75 scoped_ptr<base::WaitableEvent> on_io_complete_; | 132 BoundNetLog bound_net_log_; |
76 | 133 |
77 DISALLOW_COPY_AND_ASSIGN(FileStreamPosix); | 134 DISALLOW_COPY_AND_ASSIGN(Context); |
78 }; | 135 }; |
79 | 136 |
80 } // namespace net | 137 } // namespace net |
81 | 138 |
82 #endif // NET_BASE_FILE_STREAM_POSIX_H | 139 #endif // NET_BASE_FILE_STREAM_POSIX_H |
OLD | NEW |