Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // This file implements FileStream for Windows. | |
|
willchan no longer on Chromium
2012/10/28 05:58:14
No longer true
| |
| 6 | |
| 7 #ifndef NET_BASE_FILE_STREAM_CONTEXT_H_ | |
| 8 #define NET_BASE_FILE_STREAM_CONTEXT_H_ | |
| 9 | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/platform_file.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" | |
| 16 #include "net/base/net_log.h" | |
| 17 | |
| 18 #if defined(OS_POSIX) | |
| 19 #include "errno.h" | |
| 20 #endif | |
| 21 | |
| 22 class FilePath; | |
| 23 | |
| 24 namespace net { | |
| 25 | |
| 26 class IOBuffer; | |
| 27 | |
| 28 #if defined(OS_WIN) | |
| 29 class FileStream::Context : public MessageLoopForIO::IOHandler { | |
| 30 #elif defined(OS_POSIX) | |
| 31 class FileStream::Context { | |
| 32 #endif | |
| 33 public: | |
| 34 //////////////////////////////////////////////////////////////////////////// | |
| 35 // Platform-dependent methods implemented in | |
| 36 // file_stream_context_{win,posix}.cc. | |
| 37 //////////////////////////////////////////////////////////////////////////// | |
| 38 | |
| 39 explicit Context(const BoundNetLog& bound_net_log); | |
| 40 Context(base::PlatformFile file, | |
| 41 const BoundNetLog& bound_net_log, | |
| 42 int open_flags); | |
| 43 #if defined(OS_WIN) | |
| 44 virtual ~Context(); | |
| 45 #elif defined(OS_POSIX) | |
| 46 ~Context(); | |
| 47 #endif | |
| 48 | |
| 49 int64 GetFileSize() const; | |
| 50 | |
| 51 int ReadAsync(IOBuffer* buf, | |
| 52 int buf_len, | |
| 53 const CompletionCallback& callback); | |
| 54 int ReadSync(char* buf, int buf_len); | |
| 55 | |
| 56 int WriteAsync(IOBuffer* buf, | |
| 57 int buf_len, | |
| 58 const CompletionCallback& callback); | |
| 59 int WriteSync(const char* buf, int buf_len); | |
| 60 | |
| 61 int Flush(); | |
| 62 | |
| 63 int Truncate(int64 bytes); | |
| 64 | |
| 65 //////////////////////////////////////////////////////////////////////////// | |
| 66 // Inline methods. | |
| 67 //////////////////////////////////////////////////////////////////////////// | |
| 68 | |
| 69 void set_record_uma(bool value) { record_uma_ = value; } | |
| 70 base::PlatformFile file() const { return file_; } | |
| 71 bool async_in_progress() const { return async_in_progress_; } | |
| 72 | |
| 73 //////////////////////////////////////////////////////////////////////////// | |
| 74 // Platform-independent methods implemented in file_stream_context.cc. | |
| 75 //////////////////////////////////////////////////////////////////////////// | |
| 76 | |
| 77 // Destroys the context. It can be deleted in the method or deletion can be | |
| 78 // deferred if some asynchronous operation is now in progress or if file is | |
| 79 // not closed yet. | |
| 80 void Orphan(); | |
| 81 | |
| 82 void OpenAsync(const FilePath& path, | |
| 83 int open_flags, | |
| 84 const CompletionCallback& callback); | |
| 85 int OpenSync(const FilePath& path, int open_flags); | |
| 86 | |
| 87 void CloseSync(); | |
| 88 | |
| 89 void SeekAsync(Whence whence, | |
| 90 int64 offset, | |
| 91 const Int64CompletionCallback& callback); | |
| 92 int64 SeekSync(Whence whence, int64 offset); | |
| 93 | |
| 94 private: | |
| 95 //////////////////////////////////////////////////////////////////////////// | |
| 96 // Platform-independent methods implemented in file_stream_context.cc. | |
| 97 //////////////////////////////////////////////////////////////////////////// | |
| 98 | |
| 99 struct OpenResult { | |
| 100 base::PlatformFile file; | |
| 101 int error_code; | |
| 102 }; | |
| 103 | |
| 104 // Map system error into network error code and log it with |bound_net_log_|. | |
| 105 int RecordAndMapError(int error, FileErrorSource source) const; | |
| 106 | |
| 107 void BeginOpenEvent(const FilePath& path); | |
| 108 | |
| 109 OpenResult OpenFileImpl(const FilePath& path, int open_flags); | |
| 110 | |
| 111 int ProcessOpenError(int error_code); | |
| 112 void OnOpenCompleted(const CompletionCallback& callback, OpenResult result); | |
| 113 | |
| 114 void CloseAndDelete(); | |
| 115 void OnCloseCompleted(); | |
| 116 | |
| 117 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); | |
| 118 | |
| 119 // Checks for IO error that probably happened in SeekFileImpl(). | |
| 120 // If there was error reports it. | |
| 121 void CheckForIOError(int64* result, FileErrorSource source); | |
| 122 | |
| 123 // Called when asynchronous Seek() is completed. | |
| 124 // Reports error if needed and calls callback. | |
| 125 void ProcessAsyncResult(const Int64CompletionCallback& callback, | |
| 126 FileErrorSource source, | |
| 127 int64 result); | |
| 128 | |
| 129 // Called when asynchronous Open() or Seek() | |
| 130 // is completed. |result| contains the result or a network error code. | |
| 131 void OnAsyncCompleted(const Int64CompletionCallback& callback, int64 result); | |
| 132 | |
| 133 //////////////////////////////////////////////////////////////////////////// | |
| 134 // Helper stuff which is platform-dependent but is used in the platform- | |
| 135 // independent code implemented in file_stream_context.cc. These helpers were | |
| 136 // introduced solely to implement as much of the Context methods as | |
| 137 // possible independently from platform. | |
| 138 //////////////////////////////////////////////////////////////////////////// | |
| 139 | |
| 140 #if defined(OS_WIN) | |
| 141 enum { | |
| 142 ERROR_BAD_FILE = ERROR_INVALID_HANDLE | |
| 143 }; | |
| 144 | |
| 145 int GetLastErrno() { return GetLastError(); } | |
| 146 void OnAsyncFileOpened(); | |
| 147 #elif defined(OS_POSIX) | |
| 148 enum { | |
| 149 ERROR_BAD_FILE = EBADF | |
| 150 }; | |
| 151 | |
| 152 int GetLastErrno() { return errno; } | |
| 153 void OnAsyncFileOpened() {} | |
| 154 void CancelIo(base::PlatformFile) {} | |
| 155 #endif | |
| 156 | |
| 157 //////////////////////////////////////////////////////////////////////////// | |
| 158 // Platform-dependent methods implemented in | |
| 159 // file_stream_context_{win,posix}.cc. | |
| 160 //////////////////////////////////////////////////////////////////////////// | |
| 161 | |
| 162 // Adjusts the position from where the data is read. | |
| 163 int64 SeekFileImpl(Whence whence, int64 offset); | |
| 164 | |
| 165 #if defined(OS_WIN) | |
| 166 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); | |
| 167 | |
| 168 // Implementation of MessageLoopForIO::IOHandler | |
| 169 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | |
| 170 DWORD bytes_read, | |
| 171 DWORD error) OVERRIDE; | |
| 172 #elif defined(OS_POSIX) | |
| 173 // ReadFileImpl() is a simple wrapper around read() that handles EINTR | |
| 174 // signals and calls RecordAndMapError() to map errno to net error codes. | |
| 175 int64 ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | |
| 176 | |
| 177 // WriteFileImpl() is a simple wrapper around write() that handles EINTR | |
| 178 // signals and calls MapSystemError() to map errno to net error codes. | |
| 179 // It tries to write to completion. | |
| 180 int64 WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | |
| 181 #endif | |
| 182 | |
| 183 base::PlatformFile file_; | |
| 184 bool record_uma_; | |
| 185 bool async_in_progress_; | |
| 186 bool orphaned_; | |
| 187 BoundNetLog bound_net_log_; | |
| 188 | |
| 189 #if defined(OS_WIN) | |
| 190 MessageLoopForIO::IOContext io_context_; | |
| 191 CompletionCallback callback_; | |
| 192 scoped_refptr<IOBuffer> in_flight_buf_; | |
| 193 FileErrorSource error_source_; | |
| 194 #endif | |
| 195 | |
| 196 DISALLOW_COPY_AND_ASSIGN(Context); | |
| 197 }; | |
| 198 | |
| 199 } // namespace net | |
| 200 | |
| 201 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ | |
| 202 | |
| OLD | NEW |