[go: nahoru, domu]

blob: 6dd85cb5a2b3ea558ea2bffd23531e76d42783f4 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2019 The Chromium Authors
Ken Rockot1962d932019-08-21 01:47:282// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "components/services/storage/partition_impl.h"
6
Ken rockot6e4416c32020-11-23 20:10:087#include <memory>
Ken Rockot1962d932019-08-21 01:47:288#include <utility>
9
Avi Drissman12be0312023-01-11 09:16:0910#include "base/functional/bind.h"
Ken rockot6e4416c32020-11-23 20:10:0811#include "base/synchronization/waitable_event.h"
Patrick Monette643cdf62021-10-15 19:13:4212#include "base/task/sequenced_task_runner.h"
Gabriel Charettedd8d5985e2020-02-26 18:38:3513#include "base/task/thread_pool.h"
Ken Rockotaf15f1d2020-02-14 23:08:1114#include "build/build_config.h"
15#include "components/services/storage/dom_storage/local_storage_impl.h"
16#include "components/services/storage/dom_storage/session_storage_impl.h"
Kenichi Ishibashidf2e01d0f2021-02-08 07:07:0317#include "components/services/storage/service_worker/service_worker_storage_control_impl.h"
Ken Rockot1962d932019-08-21 01:47:2818#include "components/services/storage/storage_service_impl.h"
19
20namespace storage {
21
Ken Rockotaf15f1d2020-02-14 23:08:1122namespace {
23
24const char kSessionStorageDirectory[] = "Session Storage";
25
Ken rockot6e4416c32020-11-23 20:10:0826template <typename T>
27base::OnceClosure MakeDeferredDeleter(std::unique_ptr<T> object) {
28 return base::BindOnce(
29 [](scoped_refptr<base::SequencedTaskRunner> task_runner, T* object) {
30 task_runner->DeleteSoon(FROM_HERE, object);
31 },
Sean Maher52fa5a72022-11-14 15:53:2532 base::SequencedTaskRunner::GetCurrentDefault(),
Ken rockot6e4416c32020-11-23 20:10:0833 // NOTE: We release `object` immediately. In the case
34 // where this task never runs, we prefer to leak the
35 // object rather than potentilaly destroying it on the
36 // wrong sequence.
37 object.release());
38}
39
40template <typename T>
41void ShutDown(std::unique_ptr<T> object) {
42 if (T* ptr = object.get())
43 ptr->ShutDown(MakeDeferredDeleter(std::move(object)));
44}
45
Ken Rockotaf15f1d2020-02-14 23:08:1146} // namespace
47
Ken Rockot1962d932019-08-21 01:47:2848PartitionImpl::PartitionImpl(StorageServiceImpl* service,
Arthur Sonzognic571efb2024-01-26 20:26:1849 const std::optional<base::FilePath>& path)
Ken Rockot1962d932019-08-21 01:47:2850 : service_(service), path_(path) {
51 receivers_.set_disconnect_handler(base::BindRepeating(
52 &PartitionImpl::OnDisconnect, base::Unretained(this)));
53}
54
Ken rockot6e4416c32020-11-23 20:10:0855PartitionImpl::~PartitionImpl() {
56 ShutDown(std::move(local_storage_));
57 ShutDown(std::move(session_storage_));
58}
Ken Rockot1962d932019-08-21 01:47:2859
60void PartitionImpl::BindReceiver(
61 mojo::PendingReceiver<mojom::Partition> receiver) {
62 DCHECK(receivers_.empty() || path_.has_value())
63 << "In-memory partitions must have at most one client.";
64
65 receivers_.Add(this, std::move(receiver));
66}
67
68void PartitionImpl::BindOriginContext(
69 const url::Origin& origin,
70 mojo::PendingReceiver<mojom::OriginContext> receiver) {
71 auto iter = origin_contexts_.find(origin);
72 if (iter == origin_contexts_.end()) {
73 auto result = origin_contexts_.emplace(
74 origin, std::make_unique<OriginContextImpl>(this, origin));
75 iter = result.first;
76 }
77
78 iter->second->BindReceiver(std::move(receiver));
79}
80
Ken Rockotaf15f1d2020-02-14 23:08:1181void PartitionImpl::BindSessionStorageControl(
82 mojo::PendingReceiver<mojom::SessionStorageControl> receiver) {
Ken rockot6e4416c32020-11-23 20:10:0883 session_storage_ = std::make_unique<SessionStorageImpl>(
Ken Rockotaf15f1d2020-02-14 23:08:1184 path_.value_or(base::FilePath()),
Gabriel Charettedd8d5985e2020-02-26 18:38:3585 base::ThreadPool::CreateSequencedTaskRunner(
Ken Rockot4e284d32020-03-06 06:26:4986 {base::MayBlock(), base::WithBaseSyncPrimitives(),
87 base::TaskShutdownBehavior::BLOCK_SHUTDOWN}),
Sean Maher52fa5a72022-11-14 15:53:2588 base::SequencedTaskRunner::GetCurrentDefault(),
Xiaohan Wang8bef6b52022-01-14 14:59:1689#if BUILDFLAG(IS_ANDROID)
Ken Rockotaf15f1d2020-02-14 23:08:1190 // On Android there is no support for session storage restoring, and since
91 // the restoring code is responsible for database cleanup, we must
92 // manually delete the old database here before we open a new one.
93 SessionStorageImpl::BackingMode::kClearDiskStateOnOpen,
94#else
95 path_.has_value() ? SessionStorageImpl::BackingMode::kRestoreDiskState
96 : SessionStorageImpl::BackingMode::kNoDisk,
97#endif
98 std::string(kSessionStorageDirectory), std::move(receiver));
99}
100
101void PartitionImpl::BindLocalStorageControl(
102 mojo::PendingReceiver<mojom::LocalStorageControl> receiver) {
Ken rockot6e4416c32020-11-23 20:10:08103 local_storage_ = std::make_unique<LocalStorageImpl>(
Sean Maher52fa5a72022-11-14 15:53:25104 path_.value_or(base::FilePath()),
105 base::SequencedTaskRunner::GetCurrentDefault(), std::move(receiver));
Ken Rockotaf15f1d2020-02-14 23:08:11106}
107
Kenichi Ishibashidf2e01d0f2021-02-08 07:07:03108void PartitionImpl::BindServiceWorkerStorageControl(
109 mojo::PendingReceiver<mojom::ServiceWorkerStorageControl> receiver) {
110 service_worker_storage_ = std::make_unique<ServiceWorkerStorageControlImpl>(
111 path_.value_or(base::FilePath()),
112 base::ThreadPool::CreateSequencedTaskRunner(
113 {base::MayBlock(), base::WithBaseSyncPrimitives(),
114 base::TaskShutdownBehavior::BLOCK_SHUTDOWN}),
115 std::move(receiver));
116}
117
Ken Rockot1962d932019-08-21 01:47:28118void PartitionImpl::OnDisconnect() {
119 if (receivers_.empty()) {
120 // Deletes |this|.
121 service_->RemovePartition(this);
122 }
123}
124
125void PartitionImpl::RemoveOriginContext(const url::Origin& origin) {
126 origin_contexts_.erase(origin);
127}
128
129} // namespace storage