storage_service_impl.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SERVICES_STORAGE_STORAGE_SERVICE_IMPL_H_
  5. #define COMPONENTS_SERVICES_STORAGE_STORAGE_SERVICE_IMPL_H_
  6. #include <memory>
  7. #include <set>
  8. #include "base/containers/unique_ptr_adapters.h"
  9. #include "base/files/file_path.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/task/sequenced_task_runner.h"
  12. #include "build/build_config.h"
  13. #include "components/services/storage/partition_impl.h"
  14. #include "components/services/storage/public/mojom/filesystem/directory.mojom.h"
  15. #include "components/services/storage/public/mojom/storage_service.mojom.h"
  16. #include "mojo/public/cpp/bindings/pending_receiver.h"
  17. #include "mojo/public/cpp/bindings/receiver.h"
  18. #include "mojo/public/cpp/bindings/remote.h"
  19. namespace storage {
  20. class PartitionImpl;
  21. // Implementation of the main StorageService Mojo interface. This is the root
  22. // owner of all Storage service instance state, managing the set of active
  23. // persistent and in-memory partitions.
  24. class StorageServiceImpl : public mojom::StorageService {
  25. public:
  26. // NOTE: |io_task_runner| is only used in sandboxed environments and can be
  27. // null otherwise. If non-null, it should specify a task runner that will
  28. // never block and is thus capable of reliably facilitating IPC to the
  29. // browser.
  30. StorageServiceImpl(mojo::PendingReceiver<mojom::StorageService> receiver,
  31. scoped_refptr<base::SequencedTaskRunner> io_task_runner);
  32. StorageServiceImpl(const StorageServiceImpl&) = delete;
  33. StorageServiceImpl& operator=(const StorageServiceImpl&) = delete;
  34. ~StorageServiceImpl() override;
  35. const auto& partitions() const { return partitions_; }
  36. // mojom::StorageService implementation:
  37. void EnableAggressiveDomStorageFlushing() override;
  38. #if !BUILDFLAG(IS_ANDROID)
  39. void SetDataDirectory(
  40. const base::FilePath& path,
  41. mojo::PendingRemote<mojom::Directory> directory) override;
  42. #endif
  43. void BindPartition(const absl::optional<base::FilePath>& path,
  44. mojo::PendingReceiver<mojom::Partition> receiver) override;
  45. void BindTestApi(mojo::ScopedMessagePipeHandle test_api_receiver) override;
  46. private:
  47. friend class PartitionImpl;
  48. // Removes a partition from the set of tracked partitions.
  49. void RemovePartition(PartitionImpl* partition);
  50. #if !BUILDFLAG(IS_ANDROID)
  51. // Binds a Directory receiver to the same remote implementation to which
  52. // |remote_data_directory_| is bound. It is invalid to call this when
  53. // |remote_data_directory_| is unbound.
  54. void BindDataDirectoryReceiver(
  55. mojo::PendingReceiver<mojom::Directory> receiver);
  56. #endif
  57. const mojo::Receiver<mojom::StorageService> receiver_;
  58. const scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
  59. #if !BUILDFLAG(IS_ANDROID)
  60. // If bound, the service will assume it should not perform certain filesystem
  61. // operations directly and will instead go through this interface.
  62. base::FilePath remote_data_directory_path_;
  63. mojo::Remote<mojom::Directory> remote_data_directory_;
  64. #endif
  65. // The set of all isolated partitions owned by the service. This includes both
  66. // persistent and in-memory partitions.
  67. std::set<std::unique_ptr<PartitionImpl>, base::UniquePtrComparator>
  68. partitions_;
  69. // A mapping from FilePath to the corresponding PartitionImpl instance in
  70. // |partitions_|. The pointers stored here are not owned by this map and must
  71. // be removed when removed from |partitions_|. Only persistent partitions have
  72. // entries in this map.
  73. std::map<base::FilePath, PartitionImpl*> persistent_partition_map_;
  74. base::WeakPtrFactory<StorageServiceImpl> weak_ptr_factory_{this};
  75. };
  76. } // namespace storage
  77. #endif // COMPONENTS_SERVICES_STORAGE_STORAGE_SERVICE_IMPL_H_