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

Side by Side 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 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 POSIX. 5 // This file implements FileStream for POSIX.
6 6
7 #ifndef NET_BASE_FILE_STREAM_POSIX_H_ 7 #ifndef NET_BASE_FILE_STREAM_POSIX_H_
8 #define NET_BASE_FILE_STREAM_POSIX_H_ 8 #define NET_BASE_FILE_STREAM_POSIX_H_
9 9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/platform_file.h" 10 #include "base/platform_file.h"
13 #include "net/base/completion_callback.h" 11 #include "net/base/completion_callback.h"
12 #include "net/base/file_stream.h"
13 #include "net/base/file_stream_metrics.h"
14 #include "net/base/file_stream_whence.h" 14 #include "net/base/file_stream_whence.h"
15 #include "net/base/net_export.h" 15 #include "net/base/net_export.h"
16 #include "net/base/net_log.h" 16 #include "net/base/net_log.h"
17 17
18 class FilePath; 18 class FilePath;
19 19
20 namespace base {
21 class WaitableEvent;
22 }
23
24 namespace net { 20 namespace net {
25 21
26 class IOBuffer; 22 class IOBuffer;
27 23
28 class NET_EXPORT FileStreamPosix { 24 class FileStream::AsyncContext {
29 public: 25 public:
30 explicit FileStreamPosix(net::NetLog* net_log); 26 explicit AsyncContext(const BoundNetLog& bound_net_log);
31 FileStreamPosix(base::PlatformFile file, int flags, net::NetLog* net_log); 27 AsyncContext(base::PlatformFile file,
32 ~FileStreamPosix(); 28 const BoundNetLog& bound_net_log,
29 int open_flags);
33 30
34 // FileStream implementations. 31 // Destroys the context. It can be deleted in the method or deletion can be
35 void Close(const CompletionCallback& callback); 32 // deferred to WorkerPool if some asynchronous operation is now in progress
33 // or if auto-closing is needed.
34 void Destroy();
35
36 bool record_uma() const { return record_uma_; }
37 void set_record_uma(bool value) { record_uma_ = value; }
38 base::PlatformFile file() const { return file_; }
39 bool async_in_progress() const { return async_in_progress_; }
40
41 // Sync and async versions of all operations
42 void OpenAsync(const FilePath& path,
43 int open_flags,
44 const CompletionCallback& callback);
45 int OpenSync(const FilePath& path, int open_flags);
46
47 void CloseAsync(const CompletionCallback& callback);
36 void CloseSync(); 48 void CloseSync();
37 int Open(const FilePath& path, int open_flags, 49
38 const CompletionCallback& callback); 50 void SeekAsync(Whence whence,
39 int OpenSync(const FilePath& path, int open_flags); 51 int64 offset,
40 bool IsOpen() const; 52 const Int64CompletionCallback& callback);
41 int Seek(Whence whence, int64 offset,
42 const Int64CompletionCallback& callback);
43 int64 SeekSync(Whence whence, int64 offset); 53 int64 SeekSync(Whence whence, int64 offset);
44 int64 Available(); 54
45 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback); 55 int64 GetFileSize();
56
57 int ReadAsync(IOBuffer* buf,
58 int buf_len,
59 const CompletionCallback& callback);
46 int ReadSync(char* buf, int buf_len); 60 int ReadSync(char* buf, int buf_len);
47 int ReadUntilComplete(char *buf, int buf_len); 61
48 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback); 62 int WriteAsync(IOBuffer* buf,
63 int buf_len,
64 const CompletionCallback& callback);
49 int WriteSync(const char* buf, int buf_len); 65 int WriteSync(const char* buf, int buf_len);
50 int64 Truncate(int64 bytes); 66
51 int Flush(); 67 int Flush();
52 void EnableErrorStatistics();
53 void SetBoundNetLogSource(
54 const net::BoundNetLog& owner_bound_net_log);
55 base::PlatformFile GetPlatformFileForTesting();
56 68
57 // Resets on_io_complete_ and WeakPtr's. 69 int Truncate(int64 bytes);
58 // Called when Read() or Write() is completed.
59 void ResetOnIOComplete();
60 70
61 private: 71 private:
62 // Called when the file_ is closed asynchronously. 72 // Map system error into network error code and log it with |bound_net_log_|.
63 void OnClosed(const CompletionCallback& callback); 73 // Method should be called with |net_log_lock_| locked.
74 int RecordAndMapError(int error, FileErrorSource source);
64 75
65 // Waits until the in-flight async open/close/read/write operation is 76 void BeginOpenEvent(const FilePath& path);
66 // complete. 77
67 void WaitForIOCompletion(); 78 // Opens a file with some network logging.
79 // The result code is written to |result|.
80 void OpenFileImpl(const FilePath& path, int open_flags, int* result);
81
82 void CheckForOpenError(int* result);
83 void OnOpenCompleted(const CompletionCallback& callback, int* result);
84
85 // Closes a file with some network logging.
86 void CloseFileImpl();
87
88 void OnCloseCompleted(const CompletionCallback& callback, int* result);
89
90 // Adjusts the position from where the data is read.
91 void SeekFileImpl(Whence whence, int64 offset, int64* result);
92
93 // ReadFile() is a simple wrapper around read() that handles EINTR signals
94 // and calls RecordAndMapError() to map errno to net error codes.
95 void ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len, int* result);
96
97 // WriteFile() is a simple wrapper around write() that handles EINTR signals
98 // and calls MapSystemError() to map errno to net error codes. It tries
99 // to write to completion.
100 void WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len, int* result);
101
102 // Checks for IO error that probably happened in ReadFileImpl(),
103 // WriteFileImpl() or SeekFileImpl(). If there was error reports it.
104 template <typename R>
105 void CheckForIOError(R* result, FileErrorSource source);
106
107 // Called when asynchronous Read(), Write() or Seek() is completed.
108 // Reports error if needed and calls callback.
109 template <typename R>
110 void OnIOCompleted(const base::Callback<void(R)>& callback,
111 R* result,
112 FileErrorSource source);
113
114 // Called when asynchronous Close(), Open(), Read(), Write() or Seek()
115 // is completed. |result| contains the result or a network error code.
116 template <typename R>
117 void OnAsyncCompleted(const base::Callback<void(R)>& callback, R* result);
118
119 // Delete the context with asynchronous closing if necessary.
120 void DeleteAbandoned();
68 121
69 base::PlatformFile file_; 122 base::PlatformFile file_;
70 int open_flags_;
71 bool auto_closed_;
72 bool record_uma_; 123 bool record_uma_;
73 net::BoundNetLog bound_net_log_; 124 bool async_in_progress_;
74 base::WeakPtrFactory<FileStreamPosix> weak_ptr_factory_; 125 bool destroyed_;
75 scoped_ptr<base::WaitableEvent> on_io_complete_; 126 BoundNetLog bound_net_log_;
76
77 DISALLOW_COPY_AND_ASSIGN(FileStreamPosix);
78 }; 127 };
79 128
80 } // namespace net 129 } // namespace net
81 130
82 #endif // NET_BASE_FILE_STREAM_POSIX_H 131 #endif // NET_BASE_FILE_STREAM_POSIX_H
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698