| 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/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
| 10 #include "base/task_runner.h" | 10 #include "base/task_runner.h" |
| 11 #include "base/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
| 12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 | 15 |
| 16 #if defined(OS_ANDROID) | 16 #if defined(OS_ANDROID) |
| 17 #include "base/android/content_uri_utils.h" | 17 #include "base/android/content_uri_utils.h" |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 void CallInt64ToInt(const CompletionCallback& callback, int64 result) { | 24 void CallInt64ToInt(const CompletionCallback& callback, int64 result) { |
| 25 callback.Run(static_cast<int>(result)); | 25 callback.Run(static_cast<int>(result)); |
| 26 } | 26 } |
| 27 | 27 |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 FileStream::Context::IOResult::IOResult() | 30 FileStream::Context::IOResult::IOResult() : result(OK), os_error(0) { |
| 31 : result(OK), | |
| 32 os_error(0) { | |
| 33 } | 31 } |
| 34 | 32 |
| 35 FileStream::Context::IOResult::IOResult(int64 result, int os_error) | 33 FileStream::Context::IOResult::IOResult(int64 result, int os_error) |
| 36 : result(result), | 34 : result(result), os_error(os_error) { |
| 37 os_error(os_error) { | |
| 38 } | 35 } |
| 39 | 36 |
| 40 // static | 37 // static |
| 41 FileStream::Context::IOResult FileStream::Context::IOResult::FromOSError( | 38 FileStream::Context::IOResult FileStream::Context::IOResult::FromOSError( |
| 42 int64 os_error) { | 39 int64 os_error) { |
| 43 return IOResult(MapSystemError(os_error), os_error); | 40 return IOResult(MapSystemError(os_error), os_error); |
| 44 } | 41 } |
| 45 | 42 |
| 46 // --------------------------------------------------------------------- | 43 // --------------------------------------------------------------------- |
| 47 | 44 |
| 48 FileStream::Context::OpenResult::OpenResult() { | 45 FileStream::Context::OpenResult::OpenResult() { |
| 49 } | 46 } |
| 50 | 47 |
| 51 FileStream::Context::OpenResult::OpenResult(base::File file, | 48 FileStream::Context::OpenResult::OpenResult(base::File file, |
| 52 IOResult error_code) | 49 IOResult error_code) |
| 53 : file(file.Pass()), | 50 : file(file.Pass()), error_code(error_code) { |
| 54 error_code(error_code) { | |
| 55 } | 51 } |
| 56 | 52 |
| 57 FileStream::Context::OpenResult::OpenResult(RValue other) | 53 FileStream::Context::OpenResult::OpenResult(RValue other) |
| 58 : file(other.object->file.Pass()), | 54 : file(other.object->file.Pass()), error_code(other.object->error_code) { |
| 59 error_code(other.object->error_code) { | |
| 60 } | 55 } |
| 61 | 56 |
| 62 FileStream::Context::OpenResult& FileStream::Context::OpenResult::operator=( | 57 FileStream::Context::OpenResult& FileStream::Context::OpenResult::operator=( |
| 63 RValue other) { | 58 RValue other) { |
| 64 if (this != other.object) { | 59 if (this != other.object) { |
| 65 file = other.object->file.Pass(); | 60 file = other.object->file.Pass(); |
| 66 error_code = other.object->error_code; | 61 error_code = other.object->error_code; |
| 67 } | 62 } |
| 68 return *this; | 63 return *this; |
| 69 } | 64 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 void FileStream::Context::Seek(base::File::Whence whence, | 112 void FileStream::Context::Seek(base::File::Whence whence, |
| 118 int64 offset, | 113 int64 offset, |
| 119 const Int64CompletionCallback& callback) { | 114 const Int64CompletionCallback& callback) { |
| 120 DCHECK(!async_in_progress_); | 115 DCHECK(!async_in_progress_); |
| 121 | 116 |
| 122 bool posted = base::PostTaskAndReplyWithResult( | 117 bool posted = base::PostTaskAndReplyWithResult( |
| 123 task_runner_.get(), | 118 task_runner_.get(), |
| 124 FROM_HERE, | 119 FROM_HERE, |
| 125 base::Bind( | 120 base::Bind( |
| 126 &Context::SeekFileImpl, base::Unretained(this), whence, offset), | 121 &Context::SeekFileImpl, base::Unretained(this), whence, offset), |
| 127 base::Bind(&Context::OnAsyncCompleted, | 122 base::Bind(&Context::OnAsyncCompleted, base::Unretained(this), callback)); |
| 128 base::Unretained(this), | |
| 129 callback)); | |
| 130 DCHECK(posted); | 123 DCHECK(posted); |
| 131 | 124 |
| 132 async_in_progress_ = true; | 125 async_in_progress_ = true; |
| 133 } | 126 } |
| 134 | 127 |
| 135 void FileStream::Context::Flush(const CompletionCallback& callback) { | 128 void FileStream::Context::Flush(const CompletionCallback& callback) { |
| 136 DCHECK(!async_in_progress_); | 129 DCHECK(!async_in_progress_); |
| 137 | 130 |
| 138 bool posted = base::PostTaskAndReplyWithResult( | 131 bool posted = base::PostTaskAndReplyWithResult( |
| 139 task_runner_.get(), | 132 task_runner_.get(), |
| 140 FROM_HERE, | 133 FROM_HERE, |
| 141 base::Bind(&Context::FlushFileImpl, base::Unretained(this)), | 134 base::Bind(&Context::FlushFileImpl, base::Unretained(this)), |
| 142 base::Bind(&Context::OnAsyncCompleted, | 135 base::Bind(&Context::OnAsyncCompleted, |
| 143 base::Unretained(this), | 136 base::Unretained(this), |
| 144 IntToInt64(callback))); | 137 IntToInt64(callback))); |
| 145 DCHECK(posted); | 138 DCHECK(posted); |
| 146 | 139 |
| 147 async_in_progress_ = true; | 140 async_in_progress_ = true; |
| 148 } | 141 } |
| 149 | 142 |
| 150 FileStream::Context::OpenResult FileStream::Context::OpenFileImpl( | 143 FileStream::Context::OpenResult FileStream::Context::OpenFileImpl( |
| 151 const base::FilePath& path, int open_flags) { | 144 const base::FilePath& path, |
| 145 int open_flags) { |
| 152 #if defined(OS_POSIX) | 146 #if defined(OS_POSIX) |
| 153 // Always use blocking IO. | 147 // Always use blocking IO. |
| 154 open_flags &= ~base::File::FLAG_ASYNC; | 148 open_flags &= ~base::File::FLAG_ASYNC; |
| 155 #endif | 149 #endif |
| 156 base::File file; | 150 base::File file; |
| 157 #if defined(OS_ANDROID) | 151 #if defined(OS_ANDROID) |
| 158 if (path.IsContentUri()) { | 152 if (path.IsContentUri()) { |
| 159 // Check that only Read flags are set. | 153 // Check that only Read flags are set. |
| 160 DCHECK_EQ(open_flags & ~base::File::FLAG_ASYNC, | 154 DCHECK_EQ(open_flags & ~base::File::FLAG_ASYNC, |
| 161 base::File::FLAG_OPEN | base::File::FLAG_READ); | 155 base::File::FLAG_OPEN | base::File::FLAG_READ); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 // should be reset before Close() because it shouldn't run if any async | 221 // should be reset before Close() because it shouldn't run if any async |
| 228 // operation is in progress. | 222 // operation is in progress. |
| 229 async_in_progress_ = false; | 223 async_in_progress_ = false; |
| 230 if (orphaned_) | 224 if (orphaned_) |
| 231 CloseAndDelete(); | 225 CloseAndDelete(); |
| 232 else | 226 else |
| 233 callback.Run(result.result); | 227 callback.Run(result.result); |
| 234 } | 228 } |
| 235 | 229 |
| 236 } // namespace net | 230 } // namespace net |
| OLD | NEW |