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/profiler/scoped_tracker.h"
11 #include "base/task_runner.h"
12 #include "base/task_runner_util.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "base/values.h"
15 #include "net/base/net_errors.h"
17 #if defined(OS_ANDROID)
18 #include "base/android/content_uri_utils.h"
25 void CallInt64ToInt(const CompletionCallback
& callback
, int64 result
) {
26 callback
.Run(static_cast<int>(result
));
31 FileStream::Context::IOResult::IOResult()
36 FileStream::Context::IOResult::IOResult(int64 result
,
37 logging::SystemErrorCode os_error
)
43 FileStream::Context::IOResult
FileStream::Context::IOResult::FromOSError(
44 logging::SystemErrorCode os_error
) {
45 return IOResult(MapSystemError(os_error
), os_error
);
48 // ---------------------------------------------------------------------
50 FileStream::Context::OpenResult::OpenResult() {
53 FileStream::Context::OpenResult::OpenResult(base::File file
,
56 error_code(error_code
) {
59 FileStream::Context::OpenResult::OpenResult(RValue other
)
60 : file(other
.object
->file
.Pass()),
61 error_code(other
.object
->error_code
) {
64 FileStream::Context::OpenResult
& FileStream::Context::OpenResult::operator=(
66 if (this != other
.object
) {
67 file
= other
.object
->file
.Pass();
68 error_code
= other
.object
->error_code
;
73 // ---------------------------------------------------------------------
75 void FileStream::Context::Orphan() {
80 if (!async_in_progress_
) {
82 } else if (file_
.IsValid()) {
84 CancelIo(file_
.GetPlatformFile());
89 void FileStream::Context::Open(const base::FilePath
& path
,
91 const CompletionCallback
& callback
) {
92 DCHECK(!async_in_progress_
);
94 bool posted
= base::PostTaskAndReplyWithResult(
98 &Context::OpenFileImpl
, base::Unretained(this), path
, open_flags
),
99 base::Bind(&Context::OnOpenCompleted
, base::Unretained(this), callback
));
102 async_in_progress_
= true;
105 void FileStream::Context::Close(const CompletionCallback
& callback
) {
106 DCHECK(!async_in_progress_
);
107 bool posted
= base::PostTaskAndReplyWithResult(
110 base::Bind(&Context::CloseFileImpl
, base::Unretained(this)),
111 base::Bind(&Context::OnAsyncCompleted
,
112 base::Unretained(this),
113 IntToInt64(callback
)));
116 async_in_progress_
= true;
119 void FileStream::Context::Seek(base::File::Whence whence
,
121 const Int64CompletionCallback
& callback
) {
122 DCHECK(!async_in_progress_
);
124 bool posted
= base::PostTaskAndReplyWithResult(
128 &Context::SeekFileImpl
, base::Unretained(this), whence
, offset
),
129 base::Bind(&Context::OnAsyncCompleted
,
130 base::Unretained(this),
134 async_in_progress_
= true;
137 void FileStream::Context::Flush(const CompletionCallback
& callback
) {
138 DCHECK(!async_in_progress_
);
140 bool posted
= base::PostTaskAndReplyWithResult(
143 base::Bind(&Context::FlushFileImpl
, base::Unretained(this)),
144 base::Bind(&Context::OnAsyncCompleted
,
145 base::Unretained(this),
146 IntToInt64(callback
)));
149 async_in_progress_
= true;
152 FileStream::Context::OpenResult
FileStream::Context::OpenFileImpl(
153 const base::FilePath
& path
, int open_flags
) {
154 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
155 tracked_objects::ScopedTracker
tracking_profile(
156 FROM_HERE_WITH_EXPLICIT_FUNCTION(
157 "423948 FileStream::Context::OpenFileImpl"));
159 #if defined(OS_POSIX)
160 // Always use blocking IO.
161 open_flags
&= ~base::File::FLAG_ASYNC
;
164 #if defined(OS_ANDROID)
165 if (path
.IsContentUri()) {
166 // Check that only Read flags are set.
167 DCHECK_EQ(open_flags
& ~base::File::FLAG_ASYNC
,
168 base::File::FLAG_OPEN
| base::File::FLAG_READ
);
169 file
= base::OpenContentUriForRead(path
);
171 #endif // defined(OS_ANDROID)
172 // FileStream::Context actually closes the file asynchronously,
173 // independently from FileStream's destructor. It can cause problems for
174 // users wanting to delete the file right after FileStream deletion. Thus
175 // we are always adding SHARE_DELETE flag to accommodate such use case.
176 // TODO(rvargas): This sounds like a bug, as deleting the file would
177 // presumably happen on the wrong thread. There should be an async delete.
178 open_flags
|= base::File::FLAG_SHARE_DELETE
;
179 file
.Initialize(path
, open_flags
);
180 #if defined(OS_ANDROID)
182 #endif // defined(OS_ANDROID)
184 return OpenResult(base::File(),
185 IOResult::FromOSError(logging::GetLastSystemErrorCode()));
187 return OpenResult(file
.Pass(), IOResult(OK
, 0));
190 FileStream::Context::IOResult
FileStream::Context::CloseFileImpl() {
192 return IOResult(OK
, 0);
195 FileStream::Context::IOResult
FileStream::Context::FlushFileImpl() {
197 return IOResult(OK
, 0);
199 return IOResult::FromOSError(logging::GetLastSystemErrorCode());
202 void FileStream::Context::OnOpenCompleted(const CompletionCallback
& callback
,
203 OpenResult open_result
) {
204 file_
= open_result
.file
.Pass();
205 if (file_
.IsValid() && !orphaned_
) {
206 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
207 tracked_objects::ScopedTracker
tracking_profile(
208 FROM_HERE_WITH_EXPLICIT_FUNCTION(
209 "423948 FileStream::Context::OnOpenCompleted"));
214 OnAsyncCompleted(IntToInt64(callback
), open_result
.error_code
);
217 void FileStream::Context::CloseAndDelete() {
219 // Replace this CHECK with a DCHECK once we figure out the root cause of
220 // http://crbug.com/455066
221 CHECK(!async_in_progress_
);
223 if (file_
.IsValid()) {
224 bool posted
= task_runner_
.get()->PostTask(
226 base::Bind(base::IgnoreResult(&Context::CloseFileImpl
),
234 Int64CompletionCallback
FileStream::Context::IntToInt64(
235 const CompletionCallback
& callback
) {
236 return base::Bind(&CallInt64ToInt
, callback
);
239 void FileStream::Context::OnAsyncCompleted(
240 const Int64CompletionCallback
& callback
,
241 const IOResult
& result
) {
242 // Reset this before Run() as Run() may issue a new async operation. Also it
243 // should be reset before Close() because it shouldn't run if any async
244 // operation is in progress.
245 async_in_progress_
= false;
249 callback
.Run(result
.result
);