Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1687)

Unified Diff: net/base/file_stream_win.h

Issue 10701050: net: Implement canceling of all async operations in FileStream. (Closed) Base URL: https://src.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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,122 @@
#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::AsyncContext : public MessageLoopForIO::IOHandler {
public:
- explicit FileStreamWin(net::NetLog* net_log);
- FileStreamWin(base::PlatformFile file, int flags, net::NetLog* net_log);
- ~FileStreamWin();
+ explicit AsyncContext(const BoundNetLog& bound_net_log);
+ AsyncContext(base::PlatformFile file,
+ const BoundNetLog& bound_net_log,
+ int open_flags);
willchan no longer on Chromium 2012/08/27 06:24:06 Define the destructor here too.
- // 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 Destroy();
+
+ 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();
willchan no longer on Chromium 2012/08/27 06:59:10 make this const too
+
+ 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;
+ // Map system error into network error code and log it with |bound_net_log_|.
+ // Method should be called with |net_log_lock_| locked.
+ int RecordAndMapError(int error, FileErrorSource source);
+ void BeginOpenEvent(const FilePath& path);
+
+ // Opens a file.
+ int OpenFileImpl(const FilePath& path, int open_flags);
+
+ void CheckForOpenError(int* result);
+
+ // Called when asynchronous Open() is completed.
+ void OnOpenCompleted(const CompletionCallback& callback, int result);
+
+ // Called after any Open() is completed on thread where AsyncContext
+ // is created.
+ void RegisterInMessageLoop();
+
+ // Closes a file.
+ void CloseFileImpl();
+
+ 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 CheckForSeekError(int64* result);
+ void OnSeekCompleted(const CompletionCallback64& callback, int64 result);
- // Called when the file_ is closed asynchronously.
- void OnClosed(const CompletionCallback& callback);
+ void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf);
- // Called when the file_ is seeked asynchronously.
- void OnSeeked(const Int64CompletionCallback& callback, int64* result);
+ // Implementation of MessageLoopForIO::IOHandler
+ virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
+ DWORD bytes_read,
+ DWORD error) OVERRIDE;
- // Resets on_io_complete_ and WeakPtr's.
- // Called in OnOpened, OnClosed and OnSeeked.
- void ResetOnIOComplete();
+ // Called when asynchronous Open(), Close() or Seek()
+ // is completed. |result| contains the result or a network error code.
+ template <typename R>
+ void OnAsyncCompleted(const base::Callback<void(R)>& callback, R result);
- // Waits until the in-flight async open/close operation is complete.
- void WaitForIOCompletion();
+ // Delete the context with asynchronous closing if necessary.
+ void DeleteAbandoned();
- // 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_;
-
+ 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_;
-
- DISALLOW_COPY_AND_ASSIGN(FileStreamWin);
+ bool async_in_progress_;
+ bool destroyed_;
+ BoundNetLog bound_net_log_;
+ FileErrorSource error_source_;
};
} // namespace net

Powered by Google App Engine
This is Rietveld 408576698