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

Unified Diff: net/base/file_stream_posix.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_posix.h
===================================================================
--- net/base/file_stream_posix.h (revision 151422)
+++ net/base/file_stream_posix.h (working copy)
@@ -7,74 +7,122 @@
#ifndef NET_BASE_FILE_STREAM_POSIX_H_
#define NET_BASE_FILE_STREAM_POSIX_H_
-#include "base/memory/scoped_ptr.h"
-#include "base/memory/weak_ptr.h"
#include "base/platform_file.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 FileStreamPosix {
+class FileStream::AsyncContext {
public:
- explicit FileStreamPosix(net::NetLog* net_log);
- FileStreamPosix(base::PlatformFile file, int flags, net::NetLog* net_log);
- ~FileStreamPosix();
+ 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 a destructor. The compiler generated destru
- // 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:24:06 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();
- // Resets on_io_complete_ and WeakPtr's.
- // Called when Read() or Write() is completed.
- void ResetOnIOComplete();
+ int Truncate(int64 bytes);
private:
- // Called when the file_ is closed asynchronously.
- void OnClosed(const CompletionCallback& callback);
+ // 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);
- // Waits until the in-flight async open/close/read/write operation is
- // complete.
- void WaitForIOCompletion();
+ void BeginOpenEvent(const FilePath& path);
+ // Opens a file.
+ int OpenFileImpl(const FilePath& path, int open_flags);
+
+ void CheckForOpenError(int* result);
+ void OnOpenCompleted(const CompletionCallback& callback, int result);
+
+ // Closes a file.
+ void CloseFileImpl();
+
+ void OnCloseCompleted(const CompletionCallback& callback);
+
+ // Adjusts the position from where the data is read.
+ int64 SeekFileImpl(Whence whence, int64 offset);
+
+ // ReadFile() is a simple wrapper around read() that handles EINTR signals
+ // and calls RecordAndMapError() to map errno to net error codes.
+ int ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len);
+
+ // WriteFile() is a simple wrapper around write() that handles EINTR signals
+ // and calls MapSystemError() to map errno to net error codes. It tries
+ // to write to completion.
+ int WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len);
+
+ // Checks for IO error that probably happened in ReadFileImpl(),
+ // WriteFileImpl() or SeekFileImpl(). If there was error reports it.
+ template <typename R>
+ void CheckForIOError(R* result, FileErrorSource source);
+
+ // Called when asynchronous Read(), Write() or Seek() is completed.
+ // Reports error if needed and calls callback.
+ template <typename R>
+ void OnIOCompleted(const base::Callback<void(R)>& callback,
+ FileErrorSource source,
+ R result);
+
+ // Called when asynchronous Close(), Open(), Read(), Write() 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);
+
+ // Delete the context with asynchronous closing if necessary.
+ void DeleteAbandoned();
+
base::PlatformFile file_;
- int open_flags_;
- bool auto_closed_;
bool record_uma_;
- net::BoundNetLog bound_net_log_;
- base::WeakPtrFactory<FileStreamPosix> weak_ptr_factory_;
- scoped_ptr<base::WaitableEvent> on_io_complete_;
-
- DISALLOW_COPY_AND_ASSIGN(FileStreamPosix);
+ bool async_in_progress_;
+ bool destroyed_;
+ BoundNetLog bound_net_log_;
};
willchan no longer on Chromium 2012/08/27 06:24:06 DISALLOW_COPY_AND_ASSIGN
} // namespace net

Powered by Google App Engine
This is Rietveld 408576698