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/profiler/scoped_tracker.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_t result
) {
25 callback
.Run(static_cast<int>(result
));
30 FileStream::Context::IOResult::IOResult()
35 FileStream::Context::IOResult::IOResult(int64_t result
,
36 logging::SystemErrorCode os_error
)
37 : result(result
), os_error(os_error
) {
41 FileStream::Context::IOResult
FileStream::Context::IOResult::FromOSError(
42 logging::SystemErrorCode os_error
) {
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(int64_t offset
,
118 const Int64CompletionCallback
& callback
) {
119 DCHECK(!async_in_progress_
);
121 bool posted
= base::PostTaskAndReplyWithResult(
122 task_runner_
.get(), FROM_HERE
,
123 base::Bind(&Context::SeekFileImpl
, base::Unretained(this), offset
),
124 base::Bind(&Context::OnAsyncCompleted
, base::Unretained(this), callback
));
127 async_in_progress_
= true;
130 void FileStream::Context::Flush(const CompletionCallback
& callback
) {
131 DCHECK(!async_in_progress_
);
133 bool posted
= base::PostTaskAndReplyWithResult(
136 base::Bind(&Context::FlushFileImpl
, base::Unretained(this)),
137 base::Bind(&Context::OnAsyncCompleted
,
138 base::Unretained(this),
139 IntToInt64(callback
)));
142 async_in_progress_
= true;
145 bool FileStream::Context::IsOpen() const {
146 return file_
.IsValid();
149 FileStream::Context::OpenResult
FileStream::Context::OpenFileImpl(
150 const base::FilePath
& path
, int open_flags
) {
151 #if defined(OS_POSIX)
152 // Always use blocking IO.
153 open_flags
&= ~base::File::FLAG_ASYNC
;
156 #if defined(OS_ANDROID)
157 if (path
.IsContentUri()) {
158 // Check that only Read flags are set.
159 DCHECK_EQ(open_flags
& ~base::File::FLAG_ASYNC
,
160 base::File::FLAG_OPEN
| base::File::FLAG_READ
);
161 file
= base::OpenContentUriForRead(path
);
163 #endif // defined(OS_ANDROID)
164 // FileStream::Context actually closes the file asynchronously,
165 // independently from FileStream's destructor. It can cause problems for
166 // users wanting to delete the file right after FileStream deletion. Thus
167 // we are always adding SHARE_DELETE flag to accommodate such use case.
168 // TODO(rvargas): This sounds like a bug, as deleting the file would
169 // presumably happen on the wrong thread. There should be an async delete.
170 open_flags
|= base::File::FLAG_SHARE_DELETE
;
171 file
.Initialize(path
, open_flags
);
172 #if defined(OS_ANDROID)
174 #endif // defined(OS_ANDROID)
176 return OpenResult(base::File(),
177 IOResult::FromOSError(logging::GetLastSystemErrorCode()));
179 return OpenResult(file
.Pass(), IOResult(OK
, 0));
182 FileStream::Context::IOResult
FileStream::Context::CloseFileImpl() {
184 return IOResult(OK
, 0);
187 FileStream::Context::IOResult
FileStream::Context::FlushFileImpl() {
189 return IOResult(OK
, 0);
191 return IOResult::FromOSError(logging::GetLastSystemErrorCode());
194 void FileStream::Context::OnOpenCompleted(const CompletionCallback
& callback
,
195 OpenResult open_result
) {
196 file_
= open_result
.file
.Pass();
197 if (file_
.IsValid() && !orphaned_
)
200 OnAsyncCompleted(IntToInt64(callback
), open_result
.error_code
);
203 void FileStream::Context::CloseAndDelete() {
205 // Replace this CHECK with a DCHECK once we figure out the root cause of
206 // http://crbug.com/455066
207 CHECK(!async_in_progress_
);
209 if (file_
.IsValid()) {
210 bool posted
= task_runner_
.get()->PostTask(
212 base::Bind(base::IgnoreResult(&Context::CloseFileImpl
),
220 Int64CompletionCallback
FileStream::Context::IntToInt64(
221 const CompletionCallback
& callback
) {
222 return base::Bind(&CallInt64ToInt
, callback
);
225 void FileStream::Context::OnAsyncCompleted(
226 const Int64CompletionCallback
& callback
,
227 const IOResult
& result
) {
228 // TODO(pkasting): Remove ScopedTracker below once crbug.com/477117 is fixed.
229 tracked_objects::ScopedTracker
tracking_profile(
230 FROM_HERE_WITH_EXPLICIT_FUNCTION(
231 "477117 FileStream::Context::OnAsyncCompleted"));
232 // Reset this before Run() as Run() may issue a new async operation. Also it
233 // should be reset before Close() because it shouldn't run if any async
234 // operation is in progress.
235 async_in_progress_
= false;
239 callback
.Run(result
.result
);