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