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/message_loop/message_loop.h"
32 #include "base/move.h"
33 #include "base/task_runner.h"
34 #include "net/base/completion_callback.h"
35 #include "net/base/file_stream.h"
50 class FileStream::Context
: public base::MessageLoopForIO::IOHandler
{
51 #elif defined(OS_POSIX)
52 class FileStream::Context
{
55 ////////////////////////////////////////////////////////////////////////////
56 // Platform-dependent methods implemented in
57 // file_stream_context_{win,posix}.cc.
58 ////////////////////////////////////////////////////////////////////////////
60 explicit Context(const scoped_refptr
<base::TaskRunner
>& task_runner
);
61 Context(base::File file
, const scoped_refptr
<base::TaskRunner
>& task_runner
);
64 #elif defined(OS_POSIX)
68 int Read(IOBuffer
* buf
,
70 const CompletionCallback
& callback
);
72 int Write(IOBuffer
* buf
,
74 const CompletionCallback
& callback
);
76 const base::File
& file() const { return file_
; }
77 bool async_in_progress() const { return async_in_progress_
; }
79 ////////////////////////////////////////////////////////////////////////////
80 // Platform-independent methods implemented in file_stream_context.cc.
81 ////////////////////////////////////////////////////////////////////////////
83 // Destroys the context. It can be deleted in the method or deletion can be
84 // deferred if some asynchronous operation is now in progress or if file is
88 void Open(const base::FilePath
& path
,
90 const CompletionCallback
& callback
);
92 void Close(const CompletionCallback
& callback
);
94 void Seek(base::File::Whence whence
,
96 const Int64CompletionCallback
& callback
);
98 void Flush(const CompletionCallback
& callback
);
103 IOResult(int64 result
, int os_error
);
104 static IOResult
FromOSError(int64 os_error
);
107 int os_error
; // Set only when result < 0.
111 MOVE_ONLY_TYPE_FOR_CPP_03(OpenResult
, RValue
)
114 OpenResult(base::File file
, IOResult error_code
);
115 // C++03 move emulation of this type.
116 OpenResult(RValue other
);
117 OpenResult
& operator=(RValue other
);
123 ////////////////////////////////////////////////////////////////////////////
124 // Platform-independent methods implemented in file_stream_context.cc.
125 ////////////////////////////////////////////////////////////////////////////
127 OpenResult
OpenFileImpl(const base::FilePath
& path
, int open_flags
);
129 IOResult
CloseFileImpl();
131 IOResult
FlushFileImpl();
133 void OnOpenCompleted(const CompletionCallback
& callback
,
134 OpenResult open_result
);
136 void CloseAndDelete();
138 Int64CompletionCallback
IntToInt64(const CompletionCallback
& callback
);
140 // Called when Open() or Seek() completes. |result| contains the result or a
141 // network error code.
142 void OnAsyncCompleted(const Int64CompletionCallback
& callback
,
143 const IOResult
& result
);
145 ////////////////////////////////////////////////////////////////////////////
146 // Platform-dependent methods implemented in
147 // file_stream_context_{win,posix}.cc.
148 ////////////////////////////////////////////////////////////////////////////
150 // Adjusts the position from where the data is read.
151 IOResult
SeekFileImpl(base::File::Whence whence
, int64 offset
);
156 void IOCompletionIsPending(const CompletionCallback
& callback
, IOBuffer
* buf
);
158 // Implementation of MessageLoopForIO::IOHandler.
159 virtual void OnIOCompleted(base::MessageLoopForIO::IOContext
* context
,
161 DWORD error
) OVERRIDE
;
162 #elif defined(OS_POSIX)
163 // ReadFileImpl() is a simple wrapper around read() that handles EINTR
164 // signals and calls RecordAndMapError() to map errno to net error codes.
165 IOResult
ReadFileImpl(scoped_refptr
<IOBuffer
> buf
, int buf_len
);
167 // WriteFileImpl() is a simple wrapper around write() that handles EINTR
168 // signals and calls MapSystemError() to map errno to net error codes.
169 // It tries to write to completion.
170 IOResult
WriteFileImpl(scoped_refptr
<IOBuffer
> buf
, int buf_len
);
174 bool async_in_progress_
;
176 scoped_refptr
<base::TaskRunner
> task_runner_
;
179 base::MessageLoopForIO::IOContext io_context_
;
180 CompletionCallback callback_
;
181 scoped_refptr
<IOBuffer
> in_flight_buf_
;
184 DISALLOW_COPY_AND_ASSIGN(Context
);
189 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_