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 #include "net/base/file_stream_context.h"
7 #include "base/files/file_path.h"
8 #include "base/location.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/task_runner.h"
11 #include "base/task_runner_util.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "base/values.h"
14 #include "net/base/net_errors.h"
16 #if defined(OS_ANDROID)
17 #include "base/android/content_uri_utils.h"
24 void CallInt64ToInt(const CompletionCallback
& callback
, int64 result
) {
25 callback
.Run(static_cast<int>(result
));
30 FileStream::Context::IOResult::IOResult()
35 FileStream::Context::IOResult::IOResult(int64 result
, int os_error
)
41 FileStream::Context::IOResult
FileStream::Context::IOResult::FromOSError(
43 return IOResult(MapSystemError(os_error
), os_error
);
46 // ---------------------------------------------------------------------
48 FileStream::Context::OpenResult::OpenResult() {
51 FileStream::Context::OpenResult::OpenResult(base::File file
,
54 error_code(error_code
) {
57 FileStream::Context::OpenResult::OpenResult(RValue other
)
58 : file(other
.object
->file
.Pass()),
59 error_code(other
.object
->error_code
) {
62 FileStream::Context::OpenResult
& FileStream::Context::OpenResult::operator=(
64 if (this != other
.object
) {
65 file
= other
.object
->file
.Pass();
66 error_code
= other
.object
->error_code
;
71 // ---------------------------------------------------------------------
73 void FileStream::Context::Orphan() {
78 if (!async_in_progress_
) {
80 } else if (file_
.IsValid()) {
82 CancelIo(file_
.GetPlatformFile());
87 void FileStream::Context::Open(const base::FilePath
& path
,
89 const CompletionCallback
& callback
) {
90 DCHECK(!async_in_progress_
);
92 bool posted
= base::PostTaskAndReplyWithResult(
96 &Context::OpenFileImpl
, base::Unretained(this), path
, open_flags
),
97 base::Bind(&Context::OnOpenCompleted
, base::Unretained(this), callback
));
100 async_in_progress_
= true;
103 void FileStream::Context::Close(const CompletionCallback
& callback
) {
104 DCHECK(!async_in_progress_
);
105 bool posted
= base::PostTaskAndReplyWithResult(
108 base::Bind(&Context::CloseFileImpl
, base::Unretained(this)),
109 base::Bind(&Context::OnAsyncCompleted
,
110 base::Unretained(this),
111 IntToInt64(callback
)));
114 async_in_progress_
= true;
117 void FileStream::Context::Seek(base::File::Whence whence
,
119 const Int64CompletionCallback
& callback
) {
120 DCHECK(!async_in_progress_
);
122 bool posted
= base::PostTaskAndReplyWithResult(
126 &Context::SeekFileImpl
, base::Unretained(this), whence
, offset
),
127 base::Bind(&Context::OnAsyncCompleted
,
128 base::Unretained(this),
132 async_in_progress_
= true;
135 void FileStream::Context::Flush(const CompletionCallback
& callback
) {
136 DCHECK(!async_in_progress_
);
138 bool posted
= base::PostTaskAndReplyWithResult(
141 base::Bind(&Context::FlushFileImpl
, base::Unretained(this)),
142 base::Bind(&Context::OnAsyncCompleted
,
143 base::Unretained(this),
144 IntToInt64(callback
)));
147 async_in_progress_
= true;
150 FileStream::Context::OpenResult
FileStream::Context::OpenFileImpl(
151 const base::FilePath
& path
, int open_flags
) {
152 #if defined(OS_POSIX)
153 // Always use blocking IO.
154 open_flags
&= ~base::File::FLAG_ASYNC
;
157 #if defined(OS_ANDROID)
158 if (path
.IsContentUri()) {
159 // Check that only Read flags are set.
160 DCHECK_EQ(open_flags
& ~base::File::FLAG_ASYNC
,
161 base::File::FLAG_OPEN
| base::File::FLAG_READ
);
162 file
= base::OpenContentUriForRead(path
);
164 #endif // defined(OS_ANDROID)
165 // FileStream::Context actually closes the file asynchronously,
166 // independently from FileStream's destructor. It can cause problems for
167 // users wanting to delete the file right after FileStream deletion. Thus
168 // we are always adding SHARE_DELETE flag to accommodate such use case.
169 // TODO(rvargas): This sounds like a bug, as deleting the file would
170 // presumably happen on the wrong thread. There should be an async delete.
171 open_flags
|= base::File::FLAG_SHARE_DELETE
;
172 file
.Initialize(path
, open_flags
);
173 #if defined(OS_ANDROID)
175 #endif // defined(OS_ANDROID)
177 return OpenResult(base::File(),
178 IOResult::FromOSError(logging::GetLastSystemErrorCode()));
180 return OpenResult(file
.Pass(), IOResult(OK
, 0));
183 FileStream::Context::IOResult
FileStream::Context::CloseFileImpl() {
185 return IOResult(OK
, 0);
188 FileStream::Context::IOResult
FileStream::Context::FlushFileImpl() {
190 return IOResult(OK
, 0);
192 return IOResult::FromOSError(logging::GetLastSystemErrorCode());
195 void FileStream::Context::OnOpenCompleted(const CompletionCallback
& callback
,
196 OpenResult open_result
) {
197 file_
= open_result
.file
.Pass();
198 if (file_
.IsValid() && !orphaned_
)
201 OnAsyncCompleted(IntToInt64(callback
), open_result
.error_code
);
204 void FileStream::Context::CloseAndDelete() {
205 DCHECK(!async_in_progress_
);
207 if (file_
.IsValid()) {
208 bool posted
= task_runner_
.get()->PostTask(
210 base::Bind(base::IgnoreResult(&Context::CloseFileImpl
),
218 Int64CompletionCallback
FileStream::Context::IntToInt64(
219 const CompletionCallback
& callback
) {
220 return base::Bind(&CallInt64ToInt
, callback
);
223 void FileStream::Context::OnAsyncCompleted(
224 const Int64CompletionCallback
& callback
,
225 const IOResult
& result
) {
226 // Reset this before Run() as Run() may issue a new async operation. Also it
227 // should be reset before Close() because it shouldn't run if any async
228 // operation is in progress.
229 async_in_progress_
= false;
233 callback
.Run(result
.result
);