| 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" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 // --------------------------------------------------------------------- | 71 // --------------------------------------------------------------------- |
| 72 | 72 |
| 73 void FileStream::Context::Orphan() { | 73 void FileStream::Context::Orphan() { |
| 74 DCHECK(!orphaned_); | 74 DCHECK(!orphaned_); |
| 75 | 75 |
| 76 orphaned_ = true; | 76 orphaned_ = true; |
| 77 | 77 |
| 78 if (!async_in_progress_) { | 78 if (!async_in_progress_) { |
| 79 CloseAndDelete(); | 79 CloseAndDelete(); |
| 80 } else if (file_.IsValid()) { | 80 } else if (file_.IsValid()) { |
| 81 CancelIo(file_.GetPlatformFile()); | 81 CancelIO(); |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 void FileStream::Context::OpenAsync(const base::FilePath& path, | 85 void FileStream::Context::Open(const base::FilePath& path, |
| 86 int open_flags, | 86 int open_flags, |
| 87 const CompletionCallback& callback) { | 87 const CompletionCallback& callback) { |
| 88 DCHECK(!async_in_progress_); | 88 DCHECK(!async_in_progress_); |
| 89 | 89 |
| 90 bool posted = base::PostTaskAndReplyWithResult( | 90 bool posted = base::PostTaskAndReplyWithResult( |
| 91 task_runner_.get(), | 91 task_runner_.get(), |
| 92 FROM_HERE, | 92 FROM_HERE, |
| 93 base::Bind( | 93 base::Bind( |
| 94 &Context::OpenFileImpl, base::Unretained(this), path, open_flags), | 94 &Context::OpenFileImpl, base::Unretained(this), path, open_flags), |
| 95 base::Bind(&Context::OnOpenCompleted, base::Unretained(this), callback)); | 95 base::Bind(&Context::OnOpenCompleted, base::Unretained(this), callback)); |
| 96 DCHECK(posted); | 96 DCHECK(posted); |
| 97 | 97 |
| 98 async_in_progress_ = true; | 98 async_in_progress_ = true; |
| 99 } | 99 } |
| 100 | 100 |
| 101 void FileStream::Context::CloseAsync(const CompletionCallback& callback) { | 101 void FileStream::Context::Close(const CompletionCallback& callback) { |
| 102 DCHECK(!async_in_progress_); | 102 DCHECK(!async_in_progress_); |
| 103 bool posted = base::PostTaskAndReplyWithResult( | 103 bool posted = base::PostTaskAndReplyWithResult( |
| 104 task_runner_.get(), | 104 task_runner_.get(), |
| 105 FROM_HERE, | 105 FROM_HERE, |
| 106 base::Bind(&Context::CloseFileImpl, base::Unretained(this)), | 106 base::Bind(&Context::CloseFileImpl, base::Unretained(this)), |
| 107 base::Bind(&Context::OnAsyncCompleted, | 107 base::Bind(&Context::OnAsyncCompleted, |
| 108 base::Unretained(this), | 108 base::Unretained(this), |
| 109 IntToInt64(callback))); | 109 IntToInt64(callback))); |
| 110 DCHECK(posted); | 110 DCHECK(posted); |
| 111 | 111 |
| 112 async_in_progress_ = true; | 112 async_in_progress_ = true; |
| 113 } | 113 } |
| 114 | 114 |
| 115 void FileStream::Context::SeekAsync(Whence whence, | 115 void FileStream::Context::Seek(base::File::Whence whence, |
| 116 int64 offset, | 116 int64 offset, |
| 117 const Int64CompletionCallback& callback) { | 117 const Int64CompletionCallback& callback) { |
| 118 DCHECK(!async_in_progress_); | 118 DCHECK(!async_in_progress_); |
| 119 | 119 |
| 120 bool posted = base::PostTaskAndReplyWithResult( | 120 bool posted = base::PostTaskAndReplyWithResult( |
| 121 task_runner_.get(), | 121 task_runner_.get(), |
| 122 FROM_HERE, | 122 FROM_HERE, |
| 123 base::Bind( | 123 base::Bind( |
| 124 &Context::SeekFileImpl, base::Unretained(this), whence, offset), | 124 &Context::SeekFileImpl, base::Unretained(this), whence, offset), |
| 125 base::Bind(&Context::OnAsyncCompleted, | 125 base::Bind(&Context::OnAsyncCompleted, |
| 126 base::Unretained(this), | 126 base::Unretained(this), |
| 127 callback)); | 127 callback)); |
| 128 DCHECK(posted); | 128 DCHECK(posted); |
| 129 | 129 |
| 130 async_in_progress_ = true; | 130 async_in_progress_ = true; |
| 131 } | 131 } |
| 132 | 132 |
| 133 void FileStream::Context::FlushAsync(const CompletionCallback& callback) { | 133 void FileStream::Context::Flush(const CompletionCallback& callback) { |
| 134 DCHECK(!async_in_progress_); | 134 DCHECK(!async_in_progress_); |
| 135 | 135 |
| 136 bool posted = base::PostTaskAndReplyWithResult( | 136 bool posted = base::PostTaskAndReplyWithResult( |
| 137 task_runner_.get(), | 137 task_runner_.get(), |
| 138 FROM_HERE, | 138 FROM_HERE, |
| 139 base::Bind(&Context::FlushFileImpl, base::Unretained(this)), | 139 base::Bind(&Context::FlushFileImpl, base::Unretained(this)), |
| 140 base::Bind(&Context::OnAsyncCompleted, | 140 base::Bind(&Context::OnAsyncCompleted, |
| 141 base::Unretained(this), | 141 base::Unretained(this), |
| 142 IntToInt64(callback))); | 142 IntToInt64(callback))); |
| 143 DCHECK(posted); | 143 DCHECK(posted); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 165 // users wanting to delete the file right after FileStream deletion. Thus | 165 // users wanting to delete the file right after FileStream deletion. Thus |
| 166 // we are always adding SHARE_DELETE flag to accommodate such use case. | 166 // we are always adding SHARE_DELETE flag to accommodate such use case. |
| 167 // TODO(rvargas): This sounds like a bug, as deleting the file would | 167 // TODO(rvargas): This sounds like a bug, as deleting the file would |
| 168 // presumably happen on the wrong thread. There should be an async delete. | 168 // presumably happen on the wrong thread. There should be an async delete. |
| 169 open_flags |= base::File::FLAG_SHARE_DELETE; | 169 open_flags |= base::File::FLAG_SHARE_DELETE; |
| 170 file.Initialize(path, open_flags); | 170 file.Initialize(path, open_flags); |
| 171 #if defined(OS_ANDROID) | 171 #if defined(OS_ANDROID) |
| 172 } | 172 } |
| 173 #endif // defined(OS_ANDROID) | 173 #endif // defined(OS_ANDROID) |
| 174 if (!file.IsValid()) | 174 if (!file.IsValid()) |
| 175 return OpenResult(base::File(), IOResult::FromOSError(GetLastErrno())); | 175 return OpenResult(base::File(), |
| 176 IOResult::FromOSError(logging::GetLastSystemErrorCode())); |
| 176 | 177 |
| 177 return OpenResult(file.Pass(), IOResult(OK, 0)); | 178 return OpenResult(file.Pass(), IOResult(OK, 0)); |
| 178 } | 179 } |
| 179 | 180 |
| 180 FileStream::Context::IOResult FileStream::Context::CloseFileImpl() { | 181 FileStream::Context::IOResult FileStream::Context::CloseFileImpl() { |
| 181 file_.Close(); | 182 file_.Close(); |
| 182 return IOResult(OK, 0); | 183 return IOResult(OK, 0); |
| 183 } | 184 } |
| 184 | 185 |
| 186 FileStream::Context::IOResult FileStream::Context::FlushFileImpl() { |
| 187 if (file_.Flush()) |
| 188 return IOResult(OK, 0); |
| 189 |
| 190 return IOResult::FromOSError(logging::GetLastSystemErrorCode()); |
| 191 } |
| 192 |
| 185 void FileStream::Context::OnOpenCompleted(const CompletionCallback& callback, | 193 void FileStream::Context::OnOpenCompleted(const CompletionCallback& callback, |
| 186 OpenResult open_result) { | 194 OpenResult open_result) { |
| 187 file_ = open_result.file.Pass(); | 195 file_ = open_result.file.Pass(); |
| 188 if (file_.IsValid() && !orphaned_) | 196 if (file_.IsValid() && !orphaned_) |
| 189 OnAsyncFileOpened(); | 197 OnFileOpened(); |
| 190 | 198 |
| 191 OnAsyncCompleted(IntToInt64(callback), open_result.error_code); | 199 OnAsyncCompleted(IntToInt64(callback), open_result.error_code); |
| 192 } | 200 } |
| 193 | 201 |
| 194 void FileStream::Context::CloseAndDelete() { | 202 void FileStream::Context::CloseAndDelete() { |
| 195 DCHECK(!async_in_progress_); | 203 DCHECK(!async_in_progress_); |
| 196 | 204 |
| 197 if (file_.IsValid()) { | 205 if (file_.IsValid()) { |
| 198 bool posted = task_runner_.get()->PostTask( | 206 bool posted = task_runner_.get()->PostTask( |
| 199 FROM_HERE, | 207 FROM_HERE, |
| 200 base::Bind(base::IgnoreResult(&Context::CloseFileImpl), | 208 base::Bind(base::IgnoreResult(&Context::CloseFileImpl), |
| 201 base::Owned(this))); | 209 base::Owned(this))); |
| 202 DCHECK(posted); | 210 DCHECK(posted); |
| 203 } else { | 211 } else { |
| 204 delete this; | 212 delete this; |
| 205 } | 213 } |
| 206 } | 214 } |
| 207 | 215 |
| 208 Int64CompletionCallback FileStream::Context::IntToInt64( | 216 Int64CompletionCallback FileStream::Context::IntToInt64( |
| 209 const CompletionCallback& callback) { | 217 const CompletionCallback& callback) { |
| 210 return base::Bind(&CallInt64ToInt, callback); | 218 return base::Bind(&CallInt64ToInt, callback); |
| 211 } | 219 } |
| 212 | 220 |
| 213 void FileStream::Context::OnAsyncCompleted( | 221 void FileStream::Context::OnAsyncCompleted( |
| 214 const Int64CompletionCallback& callback, | 222 const Int64CompletionCallback& callback, |
| 215 const IOResult& result) { | 223 const IOResult& result) { |
| 216 // Reset this before Run() as Run() may issue a new async operation. Also it | 224 // Reset this before Run() as Run() may issue a new async operation. Also it |
| 217 // should be reset before CloseAsync() because it shouldn't run if any async | 225 // should be reset before Close() because it shouldn't run if any async |
| 218 // operation is in progress. | 226 // operation is in progress. |
| 219 async_in_progress_ = false; | 227 async_in_progress_ = false; |
| 220 if (orphaned_) | 228 if (orphaned_) |
| 221 CloseAndDelete(); | 229 CloseAndDelete(); |
| 222 else | 230 else |
| 223 callback.Run(result.result); | 231 callback.Run(result.result); |
| 224 } | 232 } |
| 225 | 233 |
| 226 } // namespace net | 234 } // namespace net |
| OLD | NEW |