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

Side by Side 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, 3 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file implements FileStream for Windows. 5 // This file implements FileStream for Windows.
6 6
7 #ifndef NET_BASE_FILE_STREAM_WIN_H_ 7 #ifndef NET_BASE_FILE_STREAM_WIN_H_
8 #define NET_BASE_FILE_STREAM_WIN_H_ 8 #define NET_BASE_FILE_STREAM_WIN_H_
9 9
10 #include "base/memory/scoped_ptr.h" 10 #include "base/message_loop.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/platform_file.h" 11 #include "base/platform_file.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "net/base/completion_callback.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" 15 #include "net/base/file_stream_whence.h"
16 #include "net/base/net_export.h" 16 #include "net/base/net_export.h"
17 #include "net/base/net_log.h" 17 #include "net/base/net_log.h"
18 18
19 class FilePath; 19 class FilePath;
20 20
21 namespace base {
22 class WaitableEvent;
23 }
24
25 namespace net { 21 namespace net {
26 22
27 class IOBuffer; 23 class IOBuffer;
28 24
29 class NET_EXPORT FileStreamWin { 25 class FileStream::Context : public MessageLoopForIO::IOHandler {
30 public: 26 public:
31 explicit FileStreamWin(net::NetLog* net_log); 27 explicit Context(const BoundNetLog& bound_net_log);
32 FileStreamWin(base::PlatformFile file, int flags, net::NetLog* net_log); 28 Context(base::PlatformFile file,
33 ~FileStreamWin(); 29 const BoundNetLog& bound_net_log,
30 int open_flags);
31 ~Context();
34 32
35 // FileStream implementations. 33 // Destroys the context. It can be deleted in the method or deletion can be
36 void Close(const CompletionCallback& callback); 34 // deferred to WorkerPool if some asynchronous operation is now in progress
35 // or if auto-closing is needed.
36 void Orphan();
37
38 bool record_uma() const { return record_uma_; }
39 void set_record_uma(bool value) { record_uma_ = value; }
40 base::PlatformFile file() const { return file_; }
41 bool async_in_progress() const { return async_in_progress_; }
42
43 // Sync and async versions of all operations
44 void OpenAsync(const FilePath& path,
45 int open_flags,
46 const CompletionCallback& callback);
47 int OpenSync(const FilePath& path, int open_flags);
48
49 void CloseAsync(const CompletionCallback& callback);
37 void CloseSync(); 50 void CloseSync();
38 int Open(const FilePath& path, int open_flags, 51
39 const CompletionCallback& callback); 52 void SeekAsync(Whence whence,
40 int OpenSync(const FilePath& path, int open_flags); 53 int64 offset,
41 bool IsOpen() const; 54 const Int64CompletionCallback& callback);
42 int Seek(Whence whence, int64 offset,
43 const Int64CompletionCallback& callback);
44 int64 SeekSync(Whence whence, int64 offset); 55 int64 SeekSync(Whence whence, int64 offset);
45 int64 Available(); 56
46 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); 57 int64 GetFileSize() const;
58
59 int ReadAsync(IOBuffer* buf,
60 int buf_len,
61 const CompletionCallback& callback);
47 int ReadSync(char* buf, int buf_len); 62 int ReadSync(char* buf, int buf_len);
48 int ReadUntilComplete(char *buf, int buf_len); 63
49 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); 64 int WriteAsync(IOBuffer* buf,
65 int buf_len,
66 const CompletionCallback& callback);
50 int WriteSync(const char* buf, int buf_len); 67 int WriteSync(const char* buf, int buf_len);
51 int64 Truncate(int64 bytes); 68
52 int Flush(); 69 int Flush();
53 void EnableErrorStatistics(); 70
54 void SetBoundNetLogSource(const net::BoundNetLog& owner_bound_net_log); 71 int Truncate(int64 bytes);
55 base::PlatformFile GetPlatformFileForTesting();
56 72
57 private: 73 private:
58 class AsyncContext; 74 enum {
75 kErrorBadFile = ERROR_INVALID_HANDLE
76 };
77
78 int GetLastErrno() { return GetLastError(); }
79
80 // Map system error into network error code and log it with |bound_net_log_|.
81 int RecordAndMapError(int error, FileErrorSource source) const;
82
83 void BeginOpenEvent(const FilePath& path);
84
85 // Opens a file.
86 int OpenFileImpl(const FilePath& path, int open_flags);
87
88 void CheckForOpenError(int* result);
89
90 // Called when asynchronous Open() is completed.
91 void OnOpenCompleted(const CompletionCallback& callback, int result);
92
93 // Called after any Open() is completed on thread where AsyncContext
94 // is created.
95 void RegisterInMessageLoop();
96
97 // Closes a file.
98 void CloseFileImpl();
99
100 void OnCloseCompleted(const CompletionCallback& callback);
59 101
60 // A helper method for Seek. 102 // A helper method for Seek.
61 void SeekFile(Whence whence, int64 offset, int64* result); 103 int64 SeekFileImpl(Whence whence, int64 offset);
62 104
63 // Called when the file_ is opened asynchronously. |result| contains the 105 void CheckForSeekError(int64* result);
64 // result as a network error code. 106 void OnSeekCompleted(const Int64CompletionCallback& callback, int64 result);
65 void OnOpened(const CompletionCallback& callback, int* result);
66 107
67 // Called when the file_ is closed asynchronously. 108 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf);
68 void OnClosed(const CompletionCallback& callback);
69 109
70 // Called when the file_ is seeked asynchronously. 110 // Implementation of MessageLoopForIO::IOHandler
71 void OnSeeked(const Int64CompletionCallback& callback, int64* result); 111 virtual void OnIOCompleted(MessageLoopForIO::IOContext* context,
112 DWORD bytes_read,
113 DWORD error) OVERRIDE;
72 114
73 // Resets on_io_complete_ and WeakPtr's. 115 // Called when asynchronous Open() or Seek() is completed. |result| contains
74 // Called in OnOpened, OnClosed and OnSeeked. 116 // the result or a network error code.
75 void ResetOnIOComplete(); 117 template <typename R>
118 void OnAsyncCompleted(const base::Callback<void(R)>& callback, R result);
76 119
77 // Waits until the in-flight async open/close operation is complete. 120 MessageLoopForIO::IOContext io_context_;
78 void WaitForIOCompletion(); 121 CompletionCallback callback_;
122 scoped_refptr<IOBuffer> in_flight_buf_;
123 base::PlatformFile file_;
124 bool record_uma_;
125 bool async_in_progress_;
126 bool orphaned_;
127 BoundNetLog bound_net_log_;
128 FileErrorSource error_source_;
79 129
80 // This member is used to support asynchronous reads. It is non-null when 130 DISALLOW_COPY_AND_ASSIGN(Context);
81 // the FileStreamWin was opened with PLATFORM_FILE_ASYNC.
82 scoped_ptr<AsyncContext> async_context_;
83
84 base::PlatformFile file_;
85 int open_flags_;
86 bool auto_closed_;
87 bool record_uma_;
88 net::BoundNetLog bound_net_log_;
89 base::WeakPtrFactory<FileStreamWin> weak_ptr_factory_;
90 scoped_ptr<base::WaitableEvent> on_io_complete_;
91
92 DISALLOW_COPY_AND_ASSIGN(FileStreamWin);
93 }; 131 };
94 132
95 } // namespace net 133 } // namespace net
96 134
97 #endif // NET_BASE_FILE_STREAM_WIN_H_ 135 #endif // NET_BASE_FILE_STREAM_WIN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698