Chromium Code Reviews| Index: net/base/file_stream_win.h |
| =================================================================== |
| --- net/base/file_stream_win.h (revision 151422) |
| +++ net/base/file_stream_win.h (working copy) |
| @@ -7,89 +7,136 @@ |
| #ifndef NET_BASE_FILE_STREAM_WIN_H_ |
| #define NET_BASE_FILE_STREAM_WIN_H_ |
| -#include "base/memory/scoped_ptr.h" |
| -#include "base/memory/weak_ptr.h" |
| +#include "base/message_loop.h" |
| #include "base/platform_file.h" |
| -#include "base/synchronization/waitable_event.h" |
| #include "net/base/completion_callback.h" |
| +#include "net/base/file_stream.h" |
| +#include "net/base/file_stream_metrics.h" |
| #include "net/base/file_stream_whence.h" |
| #include "net/base/net_export.h" |
| #include "net/base/net_log.h" |
| class FilePath; |
| -namespace base { |
| -class WaitableEvent; |
| -} |
| - |
| namespace net { |
| class IOBuffer; |
| -class NET_EXPORT FileStreamWin { |
| +class FileStream::Context : public MessageLoopForIO::IOHandler { |
| public: |
| - explicit FileStreamWin(net::NetLog* net_log); |
| - FileStreamWin(base::PlatformFile file, int flags, net::NetLog* net_log); |
| - ~FileStreamWin(); |
| + explicit Context(const BoundNetLog& bound_net_log); |
| + Context(base::PlatformFile file, |
| + const BoundNetLog& bound_net_log, |
|
willchan no longer on Chromium
2012/09/11 23:05:02
Indentation
|
| + int open_flags); |
| + ~Context(); |
| - // FileStream implementations. |
| - void Close(const CompletionCallback& callback); |
| + // Destroys the context. It can be deleted in the method or deletion can be |
| + // deferred to WorkerPool if some asynchronous operation is now in progress |
| + // or if auto-closing is needed. |
| + void Orphan(); |
| + |
| + bool record_uma() const { return record_uma_; } |
| + void set_record_uma(bool value) { record_uma_ = value; } |
| + base::PlatformFile file() const { return file_; } |
| + bool async_in_progress() const { return async_in_progress_; } |
| + |
| + // Sync and async versions of all operations |
| + void OpenAsync(const FilePath& path, |
| + int open_flags, |
| + const CompletionCallback& callback); |
| + int OpenSync(const FilePath& path, int open_flags); |
| + |
| + void CloseAsync(const CompletionCallback& callback); |
| void CloseSync(); |
| - int Open(const FilePath& path, int open_flags, |
| - const CompletionCallback& callback); |
| - int OpenSync(const FilePath& path, int open_flags); |
| - bool IsOpen() const; |
| - int Seek(Whence whence, int64 offset, |
| - const Int64CompletionCallback& callback); |
| + |
| + void SeekAsync(Whence whence, |
| + int64 offset, |
| + const Int64CompletionCallback& callback); |
| int64 SeekSync(Whence whence, int64 offset); |
| - int64 Available(); |
| - int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| + |
| + int64 GetFileSize() const; |
| + |
| + int ReadAsync(IOBuffer* buf, |
| + int buf_len, |
| + const CompletionCallback& callback); |
| int ReadSync(char* buf, int buf_len); |
| - int ReadUntilComplete(char *buf, int buf_len); |
| - int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); |
| + |
| + int WriteAsync(IOBuffer* buf, |
| + int buf_len, |
| + const CompletionCallback& callback); |
| int WriteSync(const char* buf, int buf_len); |
| - int64 Truncate(int64 bytes); |
| + |
| int Flush(); |
| - void EnableErrorStatistics(); |
| - void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); |
| - base::PlatformFile GetPlatformFileForTesting(); |
| + int Truncate(int64 bytes); |
| + |
| private: |
| - class AsyncContext; |
| + enum { |
| + 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
|
| + }; |
| + struct OpenResult { |
| + base::PlatformFile file; |
| + int error_code; |
| + }; |
| + |
| + int GetLastErrno() { return GetLastError(); } |
| + |
| + // Map system error into network error code and log it with |bound_net_log_|. |
| + int RecordAndMapError(int error, FileErrorSource source) const; |
| + |
| + void BeginOpenEvent(const FilePath& path); |
| + |
| + // Opens a file. |
| + OpenResult OpenFileImpl(const FilePath& path, int open_flags); |
| + |
| + int ProcessOpenError(int error_code); |
| + |
| + // Called when asynchronous Open() is completed. |
| + void OnOpenCompleted(const CompletionCallback& callback, OpenResult result); |
| + |
| + // Called after opening a file in async mode. |
| + void OnAsyncFileOpened(); |
| + |
| + void OnCloseCompleted(const CompletionCallback& callback); |
| + |
| // A helper method for Seek. |
| - void SeekFile(Whence whence, int64 offset, int64* result); |
| + int64 SeekFileImpl(Whence whence, int64 offset); |
| - // Called when the file_ is opened asynchronously. |result| contains the |
| - // result as a network error code. |
| - void OnOpened(const CompletionCallback& callback, int* result); |
| + void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); |
| - // Called when the file_ is closed asynchronously. |
| - void OnClosed(const CompletionCallback& callback); |
| + // Implementation of MessageLoopForIO::IOHandler |
| + virtual void OnIOCompleted(MessageLoopForIO::IOContext* context, |
| + DWORD bytes_read, |
| + DWORD error) OVERRIDE; |
| - // Called when the file_ is seeked asynchronously. |
| - void OnSeeked(const Int64CompletionCallback& callback, int64* result); |
| + Int64CompletionCallback IntToInt64(const CompletionCallback& callback); |
| - // Resets on_io_complete_ and WeakPtr's. |
| - // Called in OnOpened, OnClosed and OnSeeked. |
| - void ResetOnIOComplete(); |
| + // Checks for IO error that probably happened in SeekFileImpl(). |
| + // If there was error reports it. |
| + void CheckForIOError(int64* result, FileErrorSource source); |
| - // Waits until the in-flight async open/close operation is complete. |
| - void WaitForIOCompletion(); |
| + // Called when asynchronous Seek() is completed. |
| + // Reports error if needed and calls callback. |
| + void ProcessAsyncResult(const Int64CompletionCallback& callback, |
| + FileErrorSource source, |
| + int64 result); |
| - // This member is used to support asynchronous reads. It is non-null when |
| - // the FileStreamWin was opened with PLATFORM_FILE_ASYNC. |
| - scoped_ptr<AsyncContext> async_context_; |
| + // Called when asynchronous Open() or Seek() |
| + // is completed. |result| contains the result or a network error code. |
| + void OnAsyncCompleted(const Int64CompletionCallback& callback, int64 result); |
| + MessageLoopForIO::IOContext io_context_; |
| + CompletionCallback callback_; |
| + scoped_refptr<IOBuffer> in_flight_buf_; |
| base::PlatformFile file_; |
| - int open_flags_; |
| - bool auto_closed_; |
| bool record_uma_; |
| - net::BoundNetLog bound_net_log_; |
| - base::WeakPtrFactory<FileStreamWin> weak_ptr_factory_; |
| - scoped_ptr<base::WaitableEvent> on_io_complete_; |
| + bool async_in_progress_; |
| + bool orphaned_; |
| + BoundNetLog bound_net_log_; |
| + FileErrorSource error_source_; |
| - DISALLOW_COPY_AND_ASSIGN(FileStreamWin); |
| + DISALLOW_COPY_AND_ASSIGN(Context); |
| }; |
| } // namespace net |