[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / base / file_stream_context.h
blobc1846c460d354a58b98d45b3d8694d5edf7f8651
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
3 // found in the LICENSE file.
5 // This file defines FileStream::Context class.
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
8 // specific implementation details. It re-routes all its method calls to
9 // the instance of FileStream::Context (FileStream holds a scoped_ptr to
10 // FileStream::Context instance). Context was extracted into a different class
11 // to be able to do and finish all async operations even when FileStream
12 // instance is deleted. So FileStream's destructor can schedule file
13 // closing to be done by Context in WorkerPool (or the TaskRunner passed to
14 // constructor) and then just return (releasing Context pointer from
15 // scoped_ptr) without waiting for actual closing to complete.
16 // Implementation of FileStream::Context is divided in two parts: some methods
17 // and members are platform-independent and some depend on the platform. This
18 // header file contains the complete definition of Context class including all
19 // platform-dependent parts (because of that it has a lot of #if-#else
20 // branching). Implementations of all platform-independent methods are
21 // located in file_stream_context.cc, and all platform-dependent methods are
22 // in file_stream_context_{win,posix}.cc. This separation provides better
23 // readability of Context's code. And we tried to make as much Context code
24 // platform-independent as possible. So file_stream_context_{win,posix}.cc are
25 // much smaller than file_stream_context.cc now.
27 #ifndef NET_BASE_FILE_STREAM_CONTEXT_H_
28 #define NET_BASE_FILE_STREAM_CONTEXT_H_
30 #include "base/files/file.h"
31 #include "base/memory/weak_ptr.h"
32 #include "base/message_loop/message_loop.h"
33 #include "base/move.h"
34 #include "base/single_thread_task_runner.h"
35 #include "base/task_runner.h"
36 #include "net/base/completion_callback.h"
37 #include "net/base/file_stream.h"
39 #if defined(OS_POSIX)
40 #include <errno.h>
41 #endif
43 namespace base {
44 class FilePath;
47 namespace net {
49 class IOBuffer;
51 #if defined(OS_WIN)
52 class FileStream::Context : public base::MessageLoopForIO::IOHandler {
53 #elif defined(OS_POSIX)
54 class FileStream::Context {
55 #endif
56 public:
57 ////////////////////////////////////////////////////////////////////////////
58 // Platform-dependent methods implemented in
59 // file_stream_context_{win,posix}.cc.
60 ////////////////////////////////////////////////////////////////////////////
62 explicit Context(const scoped_refptr<base::TaskRunner>& task_runner);
63 Context(base::File file, const scoped_refptr<base::TaskRunner>& task_runner);
64 #if defined(OS_WIN)
65 ~Context() override;
66 #elif defined(OS_POSIX)
67 ~Context();
68 #endif
70 int Read(IOBuffer* buf,
71 int buf_len,
72 const CompletionCallback& callback);
74 int Write(IOBuffer* buf,
75 int buf_len,
76 const CompletionCallback& callback);
78 bool async_in_progress() const { return async_in_progress_; }
80 ////////////////////////////////////////////////////////////////////////////
81 // Platform-independent methods implemented in file_stream_context.cc.
82 ////////////////////////////////////////////////////////////////////////////
84 // Destroys the context. It can be deleted in the method or deletion can be
85 // deferred if some asynchronous operation is now in progress or if file is
86 // not closed yet.
87 void Orphan();
89 void Open(const base::FilePath& path,
90 int open_flags,
91 const CompletionCallback& callback);
93 void Close(const CompletionCallback& callback);
95 // Seeks |offset| bytes from the start of the file.
96 void Seek(int64_t offset, const Int64CompletionCallback& callback);
98 void Flush(const CompletionCallback& callback);
100 bool IsOpen() const;
102 private:
103 struct IOResult {
104 IOResult();
105 IOResult(int64_t result, logging::SystemErrorCode os_error);
106 static IOResult FromOSError(logging::SystemErrorCode os_error);
108 int64_t result;
109 logging::SystemErrorCode os_error; // Set only when result < 0.
112 struct OpenResult {
113 MOVE_ONLY_TYPE_FOR_CPP_03(OpenResult, RValue)
114 public:
115 OpenResult();
116 OpenResult(base::File file, IOResult error_code);
117 // C++03 move emulation of this type.
118 OpenResult(RValue other);
119 OpenResult& operator=(RValue other);
121 base::File file;
122 IOResult error_code;
125 ////////////////////////////////////////////////////////////////////////////
126 // Platform-independent methods implemented in file_stream_context.cc.
127 ////////////////////////////////////////////////////////////////////////////
129 OpenResult OpenFileImpl(const base::FilePath& path, int open_flags);
131 IOResult CloseFileImpl();
133 IOResult FlushFileImpl();
135 void OnOpenCompleted(const CompletionCallback& callback,
136 OpenResult open_result);
138 void CloseAndDelete();
140 Int64CompletionCallback IntToInt64(const CompletionCallback& callback);
142 // Called when Open() or Seek() completes. |result| contains the result or a
143 // network error code.
144 void OnAsyncCompleted(const Int64CompletionCallback& callback,
145 const IOResult& result);
147 ////////////////////////////////////////////////////////////////////////////
148 // Platform-dependent methods implemented in
149 // file_stream_context_{win,posix}.cc.
150 ////////////////////////////////////////////////////////////////////////////
152 // Adjusts the position from where the data is read.
153 IOResult SeekFileImpl(int64_t offset);
155 void OnFileOpened();
157 #if defined(OS_WIN)
158 void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf);
160 // Implementation of MessageLoopForIO::IOHandler.
161 void OnIOCompleted(base::MessageLoopForIO::IOContext* context,
162 DWORD bytes_read,
163 DWORD error) override;
165 // Invokes the user callback.
166 void InvokeUserCallback();
168 // Deletes an orphaned context.
169 void DeleteOrphanedContext();
171 // The ReadFile call on Windows can execute synchonously at times.
172 // http://support.microsoft.com/kb/156932. This ends up blocking the calling
173 // thread which is undesirable. To avoid this we execute the ReadFile call
174 // on a worker thread.
175 // The |context| parameter is a pointer to the current Context instance. It
176 // is safe to pass this as is to the pool as the Context instance should
177 // remain valid until the pending Read operation completes.
178 // The |file| parameter is the handle to the file being read.
179 // The |buf| parameter is the buffer where we want the ReadFile to read the
180 // data into.
181 // The |buf_len| parameter contains the number of bytes to be read.
182 // The |overlapped| parameter is a pointer to the OVERLAPPED structure being
183 // used.
184 // The |origin_thread_task_runner| is a task runner instance used to post
185 // tasks back to the originating thread.
186 static void ReadAsync(
187 FileStream::Context* context,
188 HANDLE file,
189 scoped_refptr<IOBuffer> buf,
190 int buf_len,
191 OVERLAPPED* overlapped,
192 scoped_refptr<base::SingleThreadTaskRunner> origin_thread_task_runner);
194 // This callback executes on the main calling thread. It informs the caller
195 // about the result of the ReadFile call.
196 // The |read_file_ret| parameter contains the return value of the ReadFile
197 // call.
198 // The |bytes_read| contains the number of bytes read from the file, if
199 // ReadFile succeeds.
200 // The |os_error| parameter contains the value of the last error returned by
201 // the ReadFile API.
202 void ReadAsyncResult(BOOL read_file_ret, DWORD bytes_read, DWORD os_error);
204 #elif defined(OS_POSIX)
205 // ReadFileImpl() is a simple wrapper around read() that handles EINTR
206 // signals and calls RecordAndMapError() to map errno to net error codes.
207 IOResult ReadFileImpl(scoped_refptr<IOBuffer> buf, int buf_len);
209 // WriteFileImpl() is a simple wrapper around write() that handles EINTR
210 // signals and calls MapSystemError() to map errno to net error codes.
211 // It tries to write to completion.
212 IOResult WriteFileImpl(scoped_refptr<IOBuffer> buf, int buf_len);
213 #endif
215 base::File file_;
216 bool async_in_progress_;
217 bool orphaned_;
218 scoped_refptr<base::TaskRunner> task_runner_;
220 #if defined(OS_WIN)
221 base::MessageLoopForIO::IOContext io_context_;
222 CompletionCallback callback_;
223 scoped_refptr<IOBuffer> in_flight_buf_;
224 // This flag is set to true when we receive a Read request which is queued to
225 // the thread pool.
226 bool async_read_initiated_;
227 // This flag is set to true when we receive a notification ReadAsyncResult()
228 // on the calling thread which indicates that the asynchronous Read
229 // operation is complete.
230 bool async_read_completed_;
231 // This flag is set to true when we receive an IO completion notification for
232 // an asynchonously initiated Read operaton. OnIOComplete().
233 bool io_complete_for_read_received_;
234 // Tracks the result of the IO completion operation. Set in OnIOComplete.
235 int result_;
236 #endif
238 DISALLOW_COPY_AND_ASSIGN(Context);
241 } // namespace net
243 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_