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

Side by Side Diff: net/base/file_stream_context.h

Issue 323683002: net: FileStream cleanup (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More comments, fix init typo Created 6 years, 6 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 | Annotate | Revision Log
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 defines FileStream::Context class. 5 // This file defines FileStream::Context class.
6 // The general design of FileStream is as follows: file_stream.h defines 6 // The general design of FileStream is as follows: file_stream.h defines
7 // FileStream class which basically is just an "wrapper" not containing any 7 // FileStream class which basically is just an "wrapper" not containing any
8 // specific implementation details. It re-routes all its method calls to 8 // specific implementation details. It re-routes all its method calls to
9 // the instance of FileStream::Context (FileStream holds a scoped_ptr to 9 // the instance of FileStream::Context (FileStream holds a scoped_ptr to
10 // FileStream::Context instance). Context was extracted into a different class 10 // FileStream::Context instance). Context was extracted into a different class
(...skipping 15 matching lines...) Expand all
26 26
27 #ifndef NET_BASE_FILE_STREAM_CONTEXT_H_ 27 #ifndef NET_BASE_FILE_STREAM_CONTEXT_H_
28 #define NET_BASE_FILE_STREAM_CONTEXT_H_ 28 #define NET_BASE_FILE_STREAM_CONTEXT_H_
29 29
30 #include "base/files/file.h" 30 #include "base/files/file.h"
31 #include "base/message_loop/message_loop.h" 31 #include "base/message_loop/message_loop.h"
32 #include "base/move.h" 32 #include "base/move.h"
33 #include "base/task_runner.h" 33 #include "base/task_runner.h"
34 #include "net/base/completion_callback.h" 34 #include "net/base/completion_callback.h"
35 #include "net/base/file_stream.h" 35 #include "net/base/file_stream.h"
36 #include "net/base/file_stream_whence.h"
37 36
38 #if defined(OS_POSIX) 37 #if defined(OS_POSIX)
39 #include <errno.h> 38 #include <errno.h>
40 #endif 39 #endif
41 40
42 namespace base { 41 namespace base {
43 class FilePath; 42 class FilePath;
44 } 43 }
45 44
46 namespace net { 45 namespace net {
47 46
48 class IOBuffer; 47 class IOBuffer;
49 48
50 #if defined(OS_WIN) 49 #if defined(OS_WIN)
51 class FileStream::Context : public base::MessageLoopForIO::IOHandler { 50 class FileStream::Context : public base::MessageLoopForIO::IOHandler {
52 #elif defined(OS_POSIX) 51 #elif defined(OS_POSIX)
53 class FileStream::Context { 52 class FileStream::Context : public base::MessageLoopForIO::Watcher {
54 #endif 53 #endif
55 public: 54 public:
56 //////////////////////////////////////////////////////////////////////////// 55 ////////////////////////////////////////////////////////////////////////////
57 // Platform-dependent methods implemented in 56 // Platform-dependent methods implemented in
58 // file_stream_context_{win,posix}.cc. 57 // file_stream_context_{win,posix}.cc.
59 //////////////////////////////////////////////////////////////////////////// 58 ////////////////////////////////////////////////////////////////////////////
60 59
61 explicit Context(const scoped_refptr<base::TaskRunner>& task_runner); 60 explicit Context(const scoped_refptr<base::TaskRunner>& task_runner);
62 Context(base::File file, const scoped_refptr<base::TaskRunner>& task_runner); 61 Context(base::File file, const scoped_refptr<base::TaskRunner>& task_runner);
63 #if defined(OS_WIN)
64 virtual ~Context(); 62 virtual ~Context();
65 #elif defined(OS_POSIX)
66 ~Context();
67 #endif
68 63
69 int ReadAsync(IOBuffer* buf, 64 int Read(IOBuffer* buf,
70 int buf_len, 65 int buf_len,
71 const CompletionCallback& callback); 66 const CompletionCallback& callback);
72 67
73 int WriteAsync(IOBuffer* buf, 68 int ReadNonBlocking(IOBuffer* buf,
74 int buf_len, 69 int buf_len,
75 const CompletionCallback& callback); 70 const CompletionCallback& callback);
76 71
77 //////////////////////////////////////////////////////////////////////////// 72 int Write(IOBuffer* buf,
78 // Inline methods. 73 int buf_len,
79 //////////////////////////////////////////////////////////////////////////// 74 const CompletionCallback& callback);
75
76 int WriteNonBlocking(IOBuffer* buf,
77 int buf_len,
78 const CompletionCallback& callback);
80 79
81 const base::File& file() const { return file_; } 80 const base::File& file() const { return file_; }
82 bool async_in_progress() const { return async_in_progress_; } 81 bool async_in_progress() const { return async_in_progress_; }
83 82
84 //////////////////////////////////////////////////////////////////////////// 83 ////////////////////////////////////////////////////////////////////////////
85 // Platform-independent methods implemented in file_stream_context.cc. 84 // Platform-independent methods implemented in file_stream_context.cc.
86 //////////////////////////////////////////////////////////////////////////// 85 ////////////////////////////////////////////////////////////////////////////
87 86
88 // Destroys the context. It can be deleted in the method or deletion can be 87 // Destroys the context. It can be deleted in the method or deletion can be
89 // deferred if some asynchronous operation is now in progress or if file is 88 // deferred if some asynchronous operation is now in progress or if file is
90 // not closed yet. 89 // not closed yet.
91 void Orphan(); 90 void Orphan();
92 91
93 void OpenAsync(const base::FilePath& path, 92 void Open(const base::FilePath& path,
94 int open_flags, 93 int open_flags,
95 const CompletionCallback& callback); 94 const CompletionCallback& callback);
96 95
97 void CloseAsync(const CompletionCallback& callback); 96 void Close(const CompletionCallback& callback);
98 97
99 void SeekAsync(Whence whence, 98 void Seek(base::File::Whence whence,
100 int64 offset, 99 int64 offset,
101 const Int64CompletionCallback& callback); 100 const Int64CompletionCallback& callback);
102 101
103 void FlushAsync(const CompletionCallback& callback); 102 void Flush(const CompletionCallback& callback);
104 103
105 private: 104 private:
106 ////////////////////////////////////////////////////////////////////////////
107 // Platform-independent methods implemented in file_stream_context.cc.
108 ////////////////////////////////////////////////////////////////////////////
109
110 struct IOResult { 105 struct IOResult {
111 IOResult(); 106 IOResult();
112 IOResult(int64 result, int os_error); 107 IOResult(int64 result, int os_error);
113 static IOResult FromOSError(int64 os_error); 108 static IOResult FromOSError(int64 os_error);
114 109
115 int64 result; 110 int64 result;
116 int os_error; // Set only when result < 0. 111 int os_error; // Set only when result < 0.
117 }; 112 };
118 113
119 struct OpenResult { 114 struct OpenResult {
120 MOVE_ONLY_TYPE_FOR_CPP_03(OpenResult, RValue) 115 MOVE_ONLY_TYPE_FOR_CPP_03(OpenResult, RValue)
121 public: 116 public:
122 OpenResult(); 117 OpenResult();
123 OpenResult(base::File file, IOResult error_code); 118 OpenResult(base::File file, IOResult error_code);
124 // C++03 move emulation of this type. 119 // C++03 move emulation of this type.
125 OpenResult(RValue other); 120 OpenResult(RValue other);
126 OpenResult& operator=(RValue other); 121 OpenResult& operator=(RValue other);
127 122
128 base::File file; 123 base::File file;
129 IOResult error_code; 124 IOResult error_code;
130 }; 125 };
131 126
127 ////////////////////////////////////////////////////////////////////////////
128 // Platform-independent methods implemented in file_stream_context.cc.
129 ////////////////////////////////////////////////////////////////////////////
130
132 OpenResult OpenFileImpl(const base::FilePath& path, int open_flags); 131 OpenResult OpenFileImpl(const base::FilePath& path, int open_flags);
133 132
134 IOResult CloseFileImpl(); 133 IOResult CloseFileImpl();
135 134
135 IOResult FlushFileImpl();
136
136 void OnOpenCompleted(const CompletionCallback& callback, 137 void OnOpenCompleted(const CompletionCallback& callback,
137 OpenResult open_result); 138 OpenResult open_result);
138 139
139 void CloseAndDelete(); 140 void CloseAndDelete();
140 141
141 Int64CompletionCallback IntToInt64(const CompletionCallback& callback); 142 Int64CompletionCallback IntToInt64(const CompletionCallback& callback);
142 143
143 // Called when asynchronous Open() or Seek() 144 // Called when Open() or Seek() completes. |result| contains the result or a
144 // is completed. |result| contains the result or a network error code. 145 // network error code.
145 void OnAsyncCompleted(const Int64CompletionCallback& callback, 146 void OnAsyncCompleted(const Int64CompletionCallback& callback,
146 const IOResult& result); 147 const IOResult& result);
147 148
148 //////////////////////////////////////////////////////////////////////////// 149 ////////////////////////////////////////////////////////////////////////////
149 // Helper stuff which is platform-dependent but is used in the platform-
150 // independent code implemented in file_stream_context.cc. These helpers were
151 // introduced solely to implement as much of the Context methods as
152 // possible independently from platform.
153 ////////////////////////////////////////////////////////////////////////////
154
155 #if defined(OS_WIN)
156 int GetLastErrno() { return GetLastError(); }
157 void OnAsyncFileOpened();
158 #elif defined(OS_POSIX)
159 int GetLastErrno() { return errno; }
160 void OnAsyncFileOpened() {}
161 void CancelIo(base::PlatformFile) {}
162 #endif
163
164 ////////////////////////////////////////////////////////////////////////////
165 // Platform-dependent methods implemented in 150 // Platform-dependent methods implemented in
166 // file_stream_context_{win,posix}.cc. 151 // file_stream_context_{win,posix}.cc.
167 //////////////////////////////////////////////////////////////////////////// 152 ////////////////////////////////////////////////////////////////////////////
168 153
169 // Adjusts the position from where the data is read. 154 // Adjusts the position from where the data is read.
170 IOResult SeekFileImpl(Whence whence, int64 offset); 155 IOResult SeekFileImpl(base::File::Whence whence, int64 offset);
171 156
172 // Flushes all data written to the stream. 157 void OnFileOpened();
wtc 2014/06/16 23:01:44 Should we preserve this comment in the new code?
rvargas (doing something else) 2014/06/18 01:36:38 Looks kind of an empty comment to me, especially g
173 IOResult FlushFileImpl(); 158
159 // The stream is going away.
160 void CancelIO();
174 161
175 #if defined(OS_WIN) 162 #if defined(OS_WIN)
176 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); 163 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf);
177 164
178 // Implementation of MessageLoopForIO::IOHandler. 165 // Implementation of MessageLoopForIO::IOHandler.
179 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context, 166 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext* context,
180 DWORD bytes_read, 167 DWORD bytes_read,
181 DWORD error) OVERRIDE; 168 DWORD error) OVERRIDE;
182 #elif defined(OS_POSIX) 169 #elif defined(OS_POSIX)
183 // ReadFileImpl() is a simple wrapper around read() that handles EINTR 170 // ReadFileImpl() is a simple wrapper around read() that handles EINTR
184 // signals and calls RecordAndMapError() to map errno to net error codes. 171 // signals and calls RecordAndMapError() to map errno to net error codes.
185 IOResult ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); 172 IOResult ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len);
186 173
187 // WriteFileImpl() is a simple wrapper around write() that handles EINTR 174 // WriteFileImpl() is a simple wrapper around write() that handles EINTR
188 // signals and calls MapSystemError() to map errno to net error codes. 175 // signals and calls MapSystemError() to map errno to net error codes.
189 // It tries to write to completion. 176 // It tries to write to completion.
190 IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len); 177 IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len);
178
179 // MessageLoopForIO::Watcher interface
180 virtual void OnFileCanReadWithoutBlocking(int fd) OVERRIDE;
181 virtual void OnFileCanWriteWithoutBlocking(int fd) OVERRIDE;
191 #endif 182 #endif
192 183
193 base::File file_; 184 base::File file_;
194 bool async_in_progress_; 185 bool async_in_progress_;
195 bool orphaned_; 186 bool orphaned_;
196 scoped_refptr<base::TaskRunner> task_runner_; 187 scoped_refptr<base::TaskRunner> task_runner_;
188 CompletionCallback callback_;
189 scoped_refptr<IOBuffer> in_flight_buf_;
197 190
198 #if defined(OS_WIN) 191 #if defined(OS_WIN)
199 base::MessageLoopForIO::IOContext io_context_; 192 base::MessageLoopForIO::IOContext io_context_;
200 CompletionCallback callback_; 193 #elif defined(OS_POSIX)
201 scoped_refptr<IOBuffer> in_flight_buf_; 194 base::MessageLoopForIO::FileDescriptorWatcher file_watcher_;
195 int buf_len_;
wtc 2014/06/16 23:01:44 Document this member. I found that it is used in c
rvargas (doing something else) 2014/06/18 01:36:38 removed
202 #endif 196 #endif
203 197
204 DISALLOW_COPY_AND_ASSIGN(Context); 198 DISALLOW_COPY_AND_ASSIGN(Context);
205 }; 199 };
206 200
207 } // namespace net 201 } // namespace net
208 202
209 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_ 203 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698