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 and then just return (releasing
14 // Context pointer from scoped_ptr) without waiting for actual closing to
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/message_loop.h"
31 #include "base/platform_file.h"
32 #include "net/base/completion_callback.h"
33 #include "net/base/file_stream.h"
34 #include "net/base/file_stream_metrics.h"
35 #include "net/base/file_stream_whence.h"
36 #include "net/base/net_log.h"
49 class FileStream::Context
: public MessageLoopForIO::IOHandler
{
50 #elif defined(OS_POSIX)
51 class FileStream::Context
{
54 ////////////////////////////////////////////////////////////////////////////
55 // Platform-dependent methods implemented in
56 // file_stream_context_{win,posix}.cc.
57 ////////////////////////////////////////////////////////////////////////////
59 explicit Context(const BoundNetLog
& bound_net_log
);
60 Context(base::PlatformFile file
,
61 const BoundNetLog
& bound_net_log
,
65 #elif defined(OS_POSIX)
69 int64
GetFileSize() const;
71 int ReadAsync(IOBuffer
* buf
,
73 const CompletionCallback
& callback
);
74 int ReadSync(char* buf
, int buf_len
);
76 int WriteAsync(IOBuffer
* buf
,
78 const CompletionCallback
& callback
);
79 int WriteSync(const char* buf
, int buf_len
);
81 int Truncate(int64 bytes
);
83 ////////////////////////////////////////////////////////////////////////////
85 ////////////////////////////////////////////////////////////////////////////
87 void set_record_uma(bool value
) { record_uma_
= value
; }
88 base::PlatformFile
file() const { return file_
; }
89 bool async_in_progress() const { return async_in_progress_
; }
91 ////////////////////////////////////////////////////////////////////////////
92 // Platform-independent methods implemented in file_stream_context.cc.
93 ////////////////////////////////////////////////////////////////////////////
95 // Destroys the context. It can be deleted in the method or deletion can be
96 // deferred if some asynchronous operation is now in progress or if file is
100 void OpenAsync(const FilePath
& path
,
102 const CompletionCallback
& callback
);
103 int OpenSync(const FilePath
& path
, int open_flags
);
107 void SeekAsync(Whence whence
,
109 const Int64CompletionCallback
& callback
);
110 int64
SeekSync(Whence whence
, int64 offset
);
112 void FlushAsync(const CompletionCallback
& callback
);
116 ////////////////////////////////////////////////////////////////////////////
117 // Error code that is platform-dependent but is used in the platform-
118 // independent code implemented in file_stream_context.cc.
119 ////////////////////////////////////////////////////////////////////////////
122 ERROR_BAD_FILE
= ERROR_INVALID_HANDLE
123 #elif defined(OS_POSIX)
124 ERROR_BAD_FILE
= EBADF
128 ////////////////////////////////////////////////////////////////////////////
129 // Platform-independent methods implemented in file_stream_context.cc.
130 ////////////////////////////////////////////////////////////////////////////
133 base::PlatformFile file
;
137 // Map system error into network error code and log it with |bound_net_log_|.
138 int RecordAndMapError(int error
, FileErrorSource source
) const;
140 void BeginOpenEvent(const FilePath
& path
);
142 OpenResult
OpenFileImpl(const FilePath
& path
, int open_flags
);
144 int ProcessOpenError(int error_code
);
145 void OnOpenCompleted(const CompletionCallback
& callback
, OpenResult result
);
147 void CloseAndDelete();
148 void OnCloseCompleted();
150 Int64CompletionCallback
IntToInt64(const CompletionCallback
& callback
);
152 // Checks for IO error that probably happened in async methods.
153 // If there was error reports it.
154 void CheckForIOError(int64
* result
, FileErrorSource source
);
156 // Called when asynchronous Seek() is completed.
157 // Reports error if needed and calls callback.
158 void ProcessAsyncResult(const Int64CompletionCallback
& callback
,
159 FileErrorSource source
,
162 // Called when asynchronous Open() or Seek()
163 // is completed. |result| contains the result or a network error code.
164 void OnAsyncCompleted(const Int64CompletionCallback
& callback
, int64 result
);
166 ////////////////////////////////////////////////////////////////////////////
167 // Helper stuff which is platform-dependent but is used in the platform-
168 // independent code implemented in file_stream_context.cc. These helpers were
169 // introduced solely to implement as much of the Context methods as
170 // possible independently from platform.
171 ////////////////////////////////////////////////////////////////////////////
174 int GetLastErrno() { return GetLastError(); }
175 void OnAsyncFileOpened();
176 #elif defined(OS_POSIX)
177 int GetLastErrno() { return errno
; }
178 void OnAsyncFileOpened() {}
179 void CancelIo(base::PlatformFile
) {}
182 ////////////////////////////////////////////////////////////////////////////
183 // Platform-dependent methods implemented in
184 // file_stream_context_{win,posix}.cc.
185 ////////////////////////////////////////////////////////////////////////////
187 // Adjusts the position from where the data is read.
188 int64
SeekFileImpl(Whence whence
, int64 offset
);
190 // Flushes all data written to the stream.
191 int64
FlushFileImpl();
194 void IOCompletionIsPending(const CompletionCallback
& callback
, IOBuffer
* buf
);
196 // Implementation of MessageLoopForIO::IOHandler
197 virtual void OnIOCompleted(MessageLoopForIO::IOContext
* context
,
199 DWORD error
) OVERRIDE
;
200 #elif defined(OS_POSIX)
201 // ReadFileImpl() is a simple wrapper around read() that handles EINTR
202 // signals and calls RecordAndMapError() to map errno to net error codes.
203 int64
ReadFileImpl(scoped_refptr
<IOBuffer
> buf
, int buf_len
);
205 // WriteFileImpl() is a simple wrapper around write() that handles EINTR
206 // signals and calls MapSystemError() to map errno to net error codes.
207 // It tries to write to completion.
208 int64
WriteFileImpl(scoped_refptr
<IOBuffer
> buf
, int buf_len
);
211 base::PlatformFile file_
;
213 bool async_in_progress_
;
215 BoundNetLog bound_net_log_
;
218 MessageLoopForIO::IOContext io_context_
;
219 CompletionCallback callback_
;
220 scoped_refptr
<IOBuffer
> in_flight_buf_
;
221 FileErrorSource error_source_
;
224 DISALLOW_COPY_AND_ASSIGN(Context
);
229 #endif // NET_BASE_FILE_STREAM_CONTEXT_H_