| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/leveldb/leveldb_mojo_proxy.h" | 5 #include "components/leveldb/leveldb_mojo_proxy.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "mojo/public/cpp/bindings/interface_request.h" | 12 #include "mojo/public/cpp/bindings/interface_request.h" |
| 13 #include "mojo/public/cpp/bindings/sync_call_restrictions.h" |
| 13 | 14 |
| 14 namespace leveldb { | 15 namespace leveldb { |
| 15 | 16 |
| 16 struct LevelDBMojoProxy::OpaqueLock { | 17 struct LevelDBMojoProxy::OpaqueLock { |
| 17 filesystem::mojom::FilePtr lock_file; | 18 filesystem::mojom::FilePtr lock_file; |
| 18 }; | 19 }; |
| 19 | 20 |
| 20 struct LevelDBMojoProxy::OpaqueDir { | 21 struct LevelDBMojoProxy::OpaqueDir { |
| 21 explicit OpaqueDir( | 22 explicit OpaqueDir( |
| 22 mojo::InterfacePtrInfo<filesystem::mojom::Directory> directory_info) { | 23 mojo::InterfacePtrInfo<filesystem::mojom::Directory> directory_info) { |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 OpaqueDir* dir) { | 178 OpaqueDir* dir) { |
| 178 // Only delete the directories on the thread that owns them. | 179 // Only delete the directories on the thread that owns them. |
| 179 delete dir; | 180 delete dir; |
| 180 outstanding_opaque_dirs_--; | 181 outstanding_opaque_dirs_--; |
| 181 } | 182 } |
| 182 | 183 |
| 183 void LevelDBMojoProxy::OpenFileHandleImpl(OpaqueDir* dir, | 184 void LevelDBMojoProxy::OpenFileHandleImpl(OpaqueDir* dir, |
| 184 std::string name, | 185 std::string name, |
| 185 uint32_t open_flags, | 186 uint32_t open_flags, |
| 186 base::File* output_file) { | 187 base::File* output_file) { |
| 188 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync; |
| 187 base::File file; | 189 base::File file; |
| 188 filesystem::mojom::FileError error = filesystem::mojom::FileError::FAILED; | 190 filesystem::mojom::FileError error = filesystem::mojom::FileError::FAILED; |
| 189 bool completed = | 191 bool completed = |
| 190 dir->directory->OpenFileHandle(name, open_flags, &error, &file); | 192 dir->directory->OpenFileHandle(name, open_flags, &error, &file); |
| 191 DCHECK(completed); | 193 DCHECK(completed); |
| 192 | 194 |
| 193 if (error != filesystem::mojom::FileError::OK) { | 195 if (error != filesystem::mojom::FileError::OK) { |
| 194 *output_file = base::File(static_cast<base::File::Error>(error)); | 196 *output_file = base::File(static_cast<base::File::Error>(error)); |
| 195 } else { | 197 } else { |
| 196 *output_file = std::move(file); | 198 *output_file = std::move(file); |
| 197 } | 199 } |
| 198 } | 200 } |
| 199 | 201 |
| 200 void LevelDBMojoProxy::SyncDirectoryImpl( | 202 void LevelDBMojoProxy::SyncDirectoryImpl( |
| 201 OpaqueDir* dir, | 203 OpaqueDir* dir, |
| 202 std::string name, | 204 std::string name, |
| 203 filesystem::mojom::FileError* out_error) { | 205 filesystem::mojom::FileError* out_error) { |
| 206 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync; |
| 204 filesystem::mojom::DirectoryPtr target; | 207 filesystem::mojom::DirectoryPtr target; |
| 205 bool completed = dir->directory->OpenDirectory( | 208 bool completed = dir->directory->OpenDirectory( |
| 206 name, MakeRequest(&target), | 209 name, MakeRequest(&target), |
| 207 filesystem::mojom::kFlagRead | filesystem::mojom::kFlagWrite, out_error); | 210 filesystem::mojom::kFlagRead | filesystem::mojom::kFlagWrite, out_error); |
| 208 DCHECK(completed); | 211 DCHECK(completed); |
| 209 | 212 |
| 210 if (*out_error != filesystem::mojom::FileError::OK) | 213 if (*out_error != filesystem::mojom::FileError::OK) |
| 211 return; | 214 return; |
| 212 | 215 |
| 213 completed = target->Flush(out_error); | 216 completed = target->Flush(out_error); |
| 214 DCHECK(completed); | 217 DCHECK(completed); |
| 215 } | 218 } |
| 216 | 219 |
| 217 void LevelDBMojoProxy::FileExistsImpl(OpaqueDir* dir, | 220 void LevelDBMojoProxy::FileExistsImpl(OpaqueDir* dir, |
| 218 std::string name, | 221 std::string name, |
| 219 bool* exists) { | 222 bool* exists) { |
| 223 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync; |
| 220 filesystem::mojom::FileError error = filesystem::mojom::FileError::FAILED; | 224 filesystem::mojom::FileError error = filesystem::mojom::FileError::FAILED; |
| 221 bool completed = dir->directory->Exists(name, &error, exists); | 225 bool completed = dir->directory->Exists(name, &error, exists); |
| 222 DCHECK(completed); | 226 DCHECK(completed); |
| 223 } | 227 } |
| 224 | 228 |
| 225 void LevelDBMojoProxy::GetChildrenImpl( | 229 void LevelDBMojoProxy::GetChildrenImpl( |
| 226 OpaqueDir* dir, | 230 OpaqueDir* dir, |
| 227 std::string name, | 231 std::string name, |
| 228 std::vector<std::string>* out_contents, | 232 std::vector<std::string>* out_contents, |
| 229 filesystem::mojom::FileError* out_error) { | 233 filesystem::mojom::FileError* out_error) { |
| 234 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync; |
| 230 filesystem::mojom::DirectoryPtr target; | 235 filesystem::mojom::DirectoryPtr target; |
| 231 filesystem::mojom::DirectoryRequest proxy(&target); | 236 filesystem::mojom::DirectoryRequest proxy(&target); |
| 232 bool completed = dir->directory->OpenDirectory( | 237 bool completed = dir->directory->OpenDirectory( |
| 233 name, std::move(proxy), | 238 name, std::move(proxy), |
| 234 filesystem::mojom::kFlagRead | filesystem::mojom::kFlagWrite, out_error); | 239 filesystem::mojom::kFlagRead | filesystem::mojom::kFlagWrite, out_error); |
| 235 DCHECK(completed); | 240 DCHECK(completed); |
| 236 | 241 |
| 237 if (*out_error != filesystem::mojom::FileError::OK) | 242 if (*out_error != filesystem::mojom::FileError::OK) |
| 238 return; | 243 return; |
| 239 | 244 |
| 240 base::Optional<std::vector<filesystem::mojom::DirectoryEntryPtr>> | 245 base::Optional<std::vector<filesystem::mojom::DirectoryEntryPtr>> |
| 241 directory_contents; | 246 directory_contents; |
| 242 completed = target->Read(out_error, &directory_contents); | 247 completed = target->Read(out_error, &directory_contents); |
| 243 DCHECK(completed); | 248 DCHECK(completed); |
| 244 | 249 |
| 245 if (directory_contents.has_value()) { | 250 if (directory_contents.has_value()) { |
| 246 for (size_t i = 0; i < directory_contents->size(); ++i) | 251 for (size_t i = 0; i < directory_contents->size(); ++i) |
| 247 out_contents->push_back(directory_contents.value()[i]->name); | 252 out_contents->push_back(directory_contents.value()[i]->name); |
| 248 } | 253 } |
| 249 } | 254 } |
| 250 | 255 |
| 251 void LevelDBMojoProxy::DeleteImpl(OpaqueDir* dir, | 256 void LevelDBMojoProxy::DeleteImpl(OpaqueDir* dir, |
| 252 std::string name, | 257 std::string name, |
| 253 uint32_t delete_flags, | 258 uint32_t delete_flags, |
| 254 filesystem::mojom::FileError* out_error) { | 259 filesystem::mojom::FileError* out_error) { |
| 260 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync; |
| 255 bool completed = dir->directory->Delete(name, delete_flags, out_error); | 261 bool completed = dir->directory->Delete(name, delete_flags, out_error); |
| 256 DCHECK(completed); | 262 DCHECK(completed); |
| 257 } | 263 } |
| 258 | 264 |
| 259 void LevelDBMojoProxy::CreateDirImpl(OpaqueDir* dir, | 265 void LevelDBMojoProxy::CreateDirImpl(OpaqueDir* dir, |
| 260 std::string name, | 266 std::string name, |
| 261 filesystem::mojom::FileError* out_error) { | 267 filesystem::mojom::FileError* out_error) { |
| 268 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync; |
| 262 bool completed = dir->directory->OpenDirectory( | 269 bool completed = dir->directory->OpenDirectory( |
| 263 name, nullptr, | 270 name, nullptr, |
| 264 filesystem::mojom::kFlagRead | filesystem::mojom::kFlagWrite | | 271 filesystem::mojom::kFlagRead | filesystem::mojom::kFlagWrite | |
| 265 filesystem::mojom::kFlagCreate, | 272 filesystem::mojom::kFlagCreate, |
| 266 out_error); | 273 out_error); |
| 267 DCHECK(completed); | 274 DCHECK(completed); |
| 268 } | 275 } |
| 269 | 276 |
| 270 void LevelDBMojoProxy::GetFileSizeImpl( | 277 void LevelDBMojoProxy::GetFileSizeImpl( |
| 271 OpaqueDir* dir, | 278 OpaqueDir* dir, |
| 272 const std::string& path, | 279 const std::string& path, |
| 273 uint64_t* file_size, | 280 uint64_t* file_size, |
| 274 filesystem::mojom::FileError* out_error) { | 281 filesystem::mojom::FileError* out_error) { |
| 282 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync; |
| 275 filesystem::mojom::FileInformationPtr info; | 283 filesystem::mojom::FileInformationPtr info; |
| 276 bool completed = dir->directory->StatFile(path, out_error, &info); | 284 bool completed = dir->directory->StatFile(path, out_error, &info); |
| 277 DCHECK(completed); | 285 DCHECK(completed); |
| 278 if (info) | 286 if (info) |
| 279 *file_size = info->size; | 287 *file_size = info->size; |
| 280 } | 288 } |
| 281 | 289 |
| 282 void LevelDBMojoProxy::RenameFileImpl(OpaqueDir* dir, | 290 void LevelDBMojoProxy::RenameFileImpl(OpaqueDir* dir, |
| 283 const std::string& old_path, | 291 const std::string& old_path, |
| 284 const std::string& new_path, | 292 const std::string& new_path, |
| 285 filesystem::mojom::FileError* out_error) { | 293 filesystem::mojom::FileError* out_error) { |
| 294 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync; |
| 286 bool completed = dir->directory->Rename(old_path, new_path, out_error); | 295 bool completed = dir->directory->Rename(old_path, new_path, out_error); |
| 287 DCHECK(completed); | 296 DCHECK(completed); |
| 288 } | 297 } |
| 289 | 298 |
| 290 void LevelDBMojoProxy::LockFileImpl(OpaqueDir* dir, | 299 void LevelDBMojoProxy::LockFileImpl(OpaqueDir* dir, |
| 291 const std::string& path, | 300 const std::string& path, |
| 292 filesystem::mojom::FileError* out_error, | 301 filesystem::mojom::FileError* out_error, |
| 293 OpaqueLock** out_lock) { | 302 OpaqueLock** out_lock) { |
| 303 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync; |
| 294 // Since a lock is associated with a file descriptor, we need to open and | 304 // Since a lock is associated with a file descriptor, we need to open and |
| 295 // have a persistent file on the other side of the connection. | 305 // have a persistent file on the other side of the connection. |
| 296 filesystem::mojom::FilePtr target; | 306 filesystem::mojom::FilePtr target; |
| 297 filesystem::mojom::FileRequest proxy(&target); | 307 filesystem::mojom::FileRequest proxy(&target); |
| 298 bool completed = dir->directory->OpenFile(path, std::move(proxy), | 308 bool completed = dir->directory->OpenFile(path, std::move(proxy), |
| 299 filesystem::mojom::kFlagOpenAlways | | 309 filesystem::mojom::kFlagOpenAlways | |
| 300 filesystem::mojom::kFlagRead | | 310 filesystem::mojom::kFlagRead | |
| 301 filesystem::mojom::kFlagWrite, | 311 filesystem::mojom::kFlagWrite, |
| 302 out_error); | 312 out_error); |
| 303 DCHECK(completed); | 313 DCHECK(completed); |
| 304 | 314 |
| 305 if (*out_error != filesystem::mojom::FileError::OK) | 315 if (*out_error != filesystem::mojom::FileError::OK) |
| 306 return; | 316 return; |
| 307 | 317 |
| 308 completed = target->Lock(out_error); | 318 completed = target->Lock(out_error); |
| 309 DCHECK(completed); | 319 DCHECK(completed); |
| 310 | 320 |
| 311 if (*out_error == filesystem::mojom::FileError::OK) { | 321 if (*out_error == filesystem::mojom::FileError::OK) { |
| 312 OpaqueLock* l = new OpaqueLock; | 322 OpaqueLock* l = new OpaqueLock; |
| 313 l->lock_file = std::move(target); | 323 l->lock_file = std::move(target); |
| 314 *out_lock = l; | 324 *out_lock = l; |
| 315 } | 325 } |
| 316 } | 326 } |
| 317 | 327 |
| 318 void LevelDBMojoProxy::UnlockFileImpl(std::unique_ptr<OpaqueLock> lock, | 328 void LevelDBMojoProxy::UnlockFileImpl(std::unique_ptr<OpaqueLock> lock, |
| 319 filesystem::mojom::FileError* out_error) { | 329 filesystem::mojom::FileError* out_error) { |
| 330 mojo::SyncCallRestrictions::ScopedAllowSyncCall allow_sync; |
| 320 lock->lock_file->Unlock(out_error); | 331 lock->lock_file->Unlock(out_error); |
| 321 } | 332 } |
| 322 | 333 |
| 323 } // namespace leveldb | 334 } // namespace leveldb |
| OLD | NEW |