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