OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/file_stream_context.h" | 5 #include "net/base/file_stream_context.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/message_loop/message_loop_proxy.h" | 8 #include "base/message_loop/message_loop_proxy.h" |
9 #include "base/task_runner_util.h" | 9 #include "base/task_runner_util.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 #include "net/base/file_stream_net_log_parameters.h" | 11 #include "net/base/file_stream_net_log_parameters.h" |
12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
13 | 13 |
14 #if defined(OS_ANDROID) | |
15 #include "base/android/content_uri_utils.h" | |
16 #endif | |
17 | |
14 namespace { | 18 namespace { |
15 | 19 |
16 void CallInt64ToInt(const net::CompletionCallback& callback, int64 result) { | 20 void CallInt64ToInt(const net::CompletionCallback& callback, int64 result) { |
17 callback.Run(static_cast<int>(result)); | 21 callback.Run(static_cast<int>(result)); |
18 } | 22 } |
19 | 23 |
20 } | 24 } |
21 | 25 |
22 namespace net { | 26 namespace net { |
23 | 27 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 if (!async_in_progress_) { | 61 if (!async_in_progress_) { |
58 CloseAndDelete(); | 62 CloseAndDelete(); |
59 } else if (file_ != base::kInvalidPlatformFileValue) { | 63 } else if (file_ != base::kInvalidPlatformFileValue) { |
60 CancelIo(file_); | 64 CancelIo(file_); |
61 } | 65 } |
62 } | 66 } |
63 | 67 |
64 void FileStream::Context::OpenAsync(const base::FilePath& path, | 68 void FileStream::Context::OpenAsync(const base::FilePath& path, |
65 int open_flags, | 69 int open_flags, |
66 const CompletionCallback& callback) { | 70 const CompletionCallback& callback) { |
71 #if defined(OS_ANDROID) | |
72 if (path.IsContentUrl()) { | |
73 OpenContentUrlAsync(path, open_flags, callback); | |
74 return; | |
75 } | |
76 #endif | |
mmenke
2013/11/05 02:33:19
Can't you just move all content URL code down int
qinmin
2013/11/05 03:38:41
Done.
| |
67 DCHECK(!async_in_progress_); | 77 DCHECK(!async_in_progress_); |
68 | 78 |
69 BeginOpenEvent(path); | 79 BeginOpenEvent(path); |
70 | 80 |
71 const bool posted = base::PostTaskAndReplyWithResult( | 81 const bool posted = base::PostTaskAndReplyWithResult( |
72 task_runner_.get(), | 82 task_runner_.get(), |
73 FROM_HERE, | 83 FROM_HERE, |
74 base::Bind( | 84 base::Bind( |
75 &Context::OpenFileImpl, base::Unretained(this), path, open_flags), | 85 &Context::OpenFileImpl, base::Unretained(this), path, open_flags), |
76 base::Bind(&Context::OnOpenCompleted, base::Unretained(this), callback)); | 86 base::Bind(&Context::OnOpenCompleted, base::Unretained(this), callback)); |
77 DCHECK(posted); | 87 DCHECK(posted); |
78 | 88 |
79 async_in_progress_ = true; | 89 async_in_progress_ = true; |
80 } | 90 } |
81 | 91 |
82 int FileStream::Context::OpenSync(const base::FilePath& path, int open_flags) { | 92 int FileStream::Context::OpenSync(const base::FilePath& path, int open_flags) { |
93 #if defined(OS_ANDROID) | |
94 if (path.IsContentUrl()) | |
95 return OpenContentUrlSync(path, open_flags); | |
96 #endif | |
83 DCHECK(!async_in_progress_); | 97 DCHECK(!async_in_progress_); |
84 | 98 |
85 BeginOpenEvent(path); | 99 BeginOpenEvent(path); |
86 OpenResult result = OpenFileImpl(path, open_flags); | 100 OpenResult result = OpenFileImpl(path, open_flags); |
87 file_ = result.file; | 101 file_ = result.file; |
88 if (file_ == base::kInvalidPlatformFileValue) { | 102 if (file_ == base::kInvalidPlatformFileValue) { |
89 ProcessOpenError(result.error_code); | 103 ProcessOpenError(result.error_code); |
90 } else { | 104 } else { |
91 // TODO(satorux): Remove this once all async clients are migrated to use | 105 // TODO(satorux): Remove this once all async clients are migrated to use |
92 // Open(). crbug.com/114783 | 106 // Open(). crbug.com/114783 |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
259 // Reset this before Run() as Run() may issue a new async operation. Also it | 273 // Reset this before Run() as Run() may issue a new async operation. Also it |
260 // should be reset before CloseAsync() because it shouldn't run if any async | 274 // should be reset before CloseAsync() because it shouldn't run if any async |
261 // operation is in progress. | 275 // operation is in progress. |
262 async_in_progress_ = false; | 276 async_in_progress_ = false; |
263 if (orphaned_) | 277 if (orphaned_) |
264 CloseAndDelete(); | 278 CloseAndDelete(); |
265 else | 279 else |
266 callback.Run(result); | 280 callback.Run(result); |
267 } | 281 } |
268 | 282 |
283 #if defined(OS_ANDROID) | |
284 void FileStream::Context::OpenContentUrlAsync( | |
285 const base::FilePath& path, | |
286 int open_flags, | |
287 const CompletionCallback& callback) { | |
288 DCHECK(!async_in_progress_); | |
289 const bool posted = base::PostTaskAndReplyWithResult( | |
290 task_runner_.get(), | |
291 FROM_HERE, | |
292 base::Bind(&Context::OpenContentUrlImpl, base::Unretained(this), | |
293 path, open_flags), | |
294 base::Bind(&Context::OnOpenCompleted, base::Unretained(this), callback)); | |
295 DCHECK(posted); | |
296 | |
297 async_in_progress_ = true; | |
298 } | |
299 | |
300 int FileStream::Context::OpenContentUrlSync( | |
301 const base::FilePath& path, int open_flags) { | |
302 DCHECK(!async_in_progress_); | |
303 | |
304 OpenResult result = OpenContentUrlImpl(path, open_flags); | |
305 file_ = result.file; | |
306 if (file_ == base::kInvalidPlatformFileValue) { | |
307 ProcessOpenError(result.error_code); | |
308 } else { | |
309 if (open_flags & base::PLATFORM_FILE_ASYNC) | |
310 OnAsyncFileOpened(); | |
311 } | |
312 return result.error_code.result; | |
313 } | |
314 | |
315 FileStream::Context::OpenResult FileStream::Context::OpenContentUrlImpl( | |
316 const base::FilePath& path, int open_flags) { | |
317 // Check that only Read flags are set. | |
318 DCHECK_EQ(open_flags & ~base::PLATFORM_FILE_ASYNC, | |
319 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); | |
320 base::PlatformFile file = base::OpenContentUrlForRead(path); | |
321 if (file == base::kInvalidPlatformFileValue) | |
322 return OpenResult(file, IOResult::FromOSError(GetLastErrno())); | |
323 | |
324 return OpenResult(file, IOResult(OK, 0)); | |
325 } | |
326 #endif | |
327 | |
269 } // namespace net | 328 } // namespace net |
270 | 329 |
OLD | NEW |