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/30 18:01:27
Wrong :)
Please update this with a large explanat
| |
| 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" | |
|
willchan no longer on Chromium
2012/10/30 18:01:27
<errno.h>
pivanof
2012/10/30 18:54:21
I wonder how did it work this way. :)
| |
| 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 Truncate(int64 bytes); | |
| 62 | |
| 63 //////////////////////////////////////////////////////////////////////////// | |
| 64 // Inline methods. | |
| 65 //////////////////////////////////////////////////////////////////////////// | |
| 66 | |
| 67 void set_record_uma(bool value) { record_uma_ = value; } | |
| 68 base::PlatformFile file() const { return file_; } | |
| 69 bool async_in_progress() const { return async_in_progress_; } | |
| 70 | |
| 71 //////////////////////////////////////////////////////////////////////////// | |
| 72 // Platform-independent methods implemented in file_stream_context.cc. | |
| 73 //////////////////////////////////////////////////////////////////////////// | |
| 74 | |
| 75 // Destroys the context. It can be deleted in the method or deletion can be | |
| 76 // deferred if some asynchronous operation is now in progress or if file is | |
| 77 // not closed yet. | |
| 78 void Orphan(); | |
| 79 | |
| 80 void OpenAsync(const FilePath& path, | |
| 81 int open_flags, | |
| 82 const CompletionCallback& callback); | |
| 83 int OpenSync(const FilePath& path, int open_flags); | |
| 84 | |
| 85 void CloseSync(); | |
| 86 | |
| 87 void SeekAsync(Whence whence, | |
| 88 int64 offset, | |
| 89 const Int64CompletionCallback& callback); | |
| 90 int64 SeekSync(Whence whence, int64 offset); | |
| 91 | |
| 92 void FlushAsync(const CompletionCallback& callback); | |
| 93 int FlushSync(); | |
| 94 | |
| 95 private: | |
| 96 //////////////////////////////////////////////////////////////////////////// | |
| 97 // Platform-independent methods implemented in file_stream_context.cc. | |
| 98 //////////////////////////////////////////////////////////////////////////// | |
| 99 | |
| 100 struct OpenResult { | |
| 101 base::PlatformFile file; | |
| 102 int error_code; | |
| 103 }; | |
| 104 | |
| 105 // Map system error into network error code and log it with |bound_net_log_|. | |
| 106 int RecordAndMapError(int error, FileErrorSource source) const; | |
| 107 | |
| 108 void BeginOpenEvent(const FilePath& path); | |
| 109 | |
| 110 OpenResult OpenFileImpl(const FilePath& path, int open_flags); | |
| 111 | |
| 112 int ProcessOpenError(int error_code); | |
| 113 void OnOpenCompleted(const CompletionCallback& callback, OpenResult result); | |
| 114 | |
| 115 void CloseAndDelete(); | |
| 116 void OnCloseCompleted(); | |
| 117 | |
| 118 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); | |
| 119 | |
| 120 // Checks for IO error that probably happened in async methods. | |
| 121 // If there was error reports it. | |
| 122 void CheckForIOError(int64* result, FileErrorSource source); | |
| 123 | |
| 124 // Called when asynchronous Seek() is completed. | |
| 125 // Reports error if needed and calls callback. | |
| 126 void ProcessAsyncResult(const Int64CompletionCallback& callback, | |
| 127 FileErrorSource source, | |
| 128 int64 result); | |
| 129 | |
| 130 // Called when asynchronous Open() or Seek() | |
| 131 // is completed. |result| contains the result or a network error code. | |
| 132 void OnAsyncCompleted(const Int64CompletionCallback& callback, int64 result); | |
| 133 | |
| 134 //////////////////////////////////////////////////////////////////////////// | |
| 135 // Helper stuff which is platform-dependent but is used in the platform- | |
| 136 // independent code implemented in file_stream_context.cc. These helpers were | |
| 137 // introduced solely to implement as much of the Context methods as | |
| 138 // possible independently from platform. | |
| 139 //////////////////////////////////////////////////////////////////////////// | |
| 140 | |
| 141 #if defined(OS_WIN) | |
|
willchan no longer on Chromium
2012/10/30 18:01:27
Can you move the ERROR_BAD_FILE enum up to the top
pivanof
2012/10/30 18:54:21
Yes, I can do that. Unfortunately it will add one
| |
| 142 enum { | |
| 143 ERROR_BAD_FILE = ERROR_INVALID_HANDLE | |
| 144 }; | |
| 145 | |
| 146 int GetLastErrno() { return GetLastError(); } | |
| 147 void OnAsyncFileOpened(); | |
| 148 #elif defined(OS_POSIX) | |
| 149 enum { | |
| 150 ERROR_BAD_FILE = EBADF | |
| 151 }; | |
| 152 | |
| 153 int GetLastErrno() { return errno; } | |
| 154 void OnAsyncFileOpened() {} | |
| 155 void CancelIo(base::PlatformFile) {} | |
| 156 #endif | |
| 157 | |
| 158 //////////////////////////////////////////////////////////////////////////// | |
| 159 // Platform-dependent methods implemented in | |
| 160 // file_stream_context_{win,posix}.cc. | |
| 161 //////////////////////////////////////////////////////////////////////////// | |
| 162 | |
| 163 // Adjusts the position from where the data is read. | |
| 164 int64 SeekFileImpl(Whence whence, int64 offset); | |
| 165 | |
| 166 // Flushes all data written to the stream. | |
| 167 int64 FlushFileImpl(); | |
| 168 | |
| 169 #if defined(OS_WIN) | |
| 170 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); | |
| 171 | |
| 172 // Implementation of MessageLoopForIO::IOHandler | |
| 173 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, | |
| 174 DWORD bytes_read, | |
| 175 DWORD error) OVERRIDE; | |
| 176 #elif defined(OS_POSIX) | |
| 177 // ReadFileImpl() is a simple wrapper around read() that handles EINTR | |
| 178 // signals and calls RecordAndMapError() to map errno to net error codes. | |
| 179 int64 ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | |
| 180 | |
| 181 // WriteFileImpl() is a simple wrapper around write() that handles EINTR | |
| 182 // signals and calls MapSystemError() to map errno to net error codes. | |
| 183 // It tries to write to completion. | |
| 184 int64 WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); | |
| 185 #endif | |
| 186 | |
| 187 base::PlatformFile file_; | |
| 188 bool record_uma_; | |
| 189 bool async_in_progress_; | |
| 190 bool orphaned_; | |
| 191 BoundNetLog bound_net_log_; | |
| 192 | |
| 193 #if defined(OS_WIN) | |
| 194 MessageLoopForIO::IOContext io_context_; | |
| 195 CompletionCallback callback_; | |
| 196 scoped_refptr<IOBuffer> in_flight_buf_; | |
| 197 FileErrorSource error_source_; | |
| 198 #endif | |
| 199 | |
| 200 DISALLOW_COPY_AND_ASSIGN(Context); | |
| 201 }; | |
| 202 | |
| 203 } // namespace net | |
| 204 | |
| 205 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ | |
| 206 | |
| OLD | NEW |