File: storage_service_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (240 lines) | stat: -rw-r--r-- 9,128 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/services/storage/storage_service_impl.h"

#include "base/functional/bind.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "components/services/storage/dom_storage/local_storage_impl.h"
#include "components/services/storage/dom_storage/session_storage_impl.h"
#include "components/services/storage/dom_storage/storage_area_impl.h"
#include "components/services/storage/filesystem_proxy_factory.h"
#include "components/services/storage/public/cpp/filesystem/filesystem_proxy.h"
#include "components/services/storage/sandboxed_vfs_delegate.h"
#include "components/services/storage/test_api_stubs.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "sql/database.h"
#include "sql/sandboxed_vfs.h"
#include "third_party/leveldatabase/env_chromium.h"

namespace storage {

namespace {

const char kSessionStorageDirectory[] = "Session Storage";

// We don't use out-of-process Storage Service on Android, so we can avoid
// pulling all the related code (including Directory mojom) into the build.
#if !BUILDFLAG(IS_ANDROID)
// The name under which we register our own sandboxed VFS instance when running
// out-of-process.
constexpr char kVfsName[] = "storage_service";

using DirectoryBinder =
    base::RepeatingCallback<void(mojo::PendingReceiver<mojom::Directory>)>;
std::unique_ptr<FilesystemProxy> CreateRestrictedFilesystemProxy(
    const base::FilePath& directory_path,
    scoped_refptr<base::SequencedTaskRunner> io_task_runner,
    DirectoryBinder binder,
    scoped_refptr<base::SequencedTaskRunner> binder_task_runner) {
  mojo::PendingRemote<mojom::Directory> directory;
  binder_task_runner->PostTask(
      FROM_HERE,
      base::BindOnce(binder, directory.InitWithNewPipeAndPassReceiver()));
  return std::make_unique<FilesystemProxy>(FilesystemProxy::RESTRICTED,
                                           directory_path, std::move(directory),
                                           std::move(io_task_runner));
}
#endif

template <typename T>
base::OnceClosure MakeDeferredDeleter(std::unique_ptr<T> object) {
  return base::BindOnce(
      [](scoped_refptr<base::SequencedTaskRunner> task_runner, T* object) {
        task_runner->DeleteSoon(FROM_HERE, object);
      },
      base::SequencedTaskRunner::GetCurrentDefault(),
      // NOTE: We release `object` immediately. In the case
      // where this task never runs, we prefer to leak the
      // object rather than potentially destroying it on the
      // wrong sequence.
      object.release());
}

template <typename T>
void ShutDown(std::unique_ptr<T> object) {
  if (T* ptr = object.get()) {
    ptr->ShutDown(MakeDeferredDeleter(std::move(object)));
  }
}

}  // namespace

StorageServiceImpl::StorageServiceImpl(
    mojo::PendingReceiver<mojom::StorageService> receiver,
    scoped_refptr<base::SequencedTaskRunner> io_task_runner)
    : receiver_(this, std::move(receiver)),
      io_task_runner_(std::move(io_task_runner)) {}

StorageServiceImpl::~StorageServiceImpl() {
  // ShutDown storages before we destroy the service. We transfer ownership of
  // the storages to the ShutDown function, which deletes them after ShutDown
  // completes.
  while (!local_storages_.empty()) {
    auto node = local_storages_.extract(local_storages_.begin());
    ShutDown(std::move(node.value()));
  }

  while (!session_storages_.empty()) {
    auto node = session_storages_.extract(session_storages_.begin());
    ShutDown(std::move(node.value()));
  }
}

void StorageServiceImpl::EnableAggressiveDomStorageFlushing() {
  StorageAreaImpl::EnableAggressiveCommitDelay();
}

#if !BUILDFLAG(IS_ANDROID)
void StorageServiceImpl::SetDataDirectory(
    const base::FilePath& path,
    mojo::PendingRemote<mojom::Directory> directory) {
  remote_data_directory_path_ = path;
  remote_data_directory_.Bind(std::move(directory));

  // We can assume we must be sandboxed if we're getting a remote data
  // directory handle. Override the default FilesystemProxy factory to produce
  // instances restricted to operations within |path|, which can operate
  // from within a sandbox.
  SetFilesystemProxyFactory(base::BindRepeating(
      &CreateRestrictedFilesystemProxy, remote_data_directory_path_,
      io_task_runner_,
      base::BindRepeating(&StorageServiceImpl::BindDataDirectoryReceiver,
                          weak_ptr_factory_.GetWeakPtr()),
      base::SequencedTaskRunner::GetCurrentDefault()));

  // SQLite needs our VFS implementation to work over a FilesystemProxy. This
  // installs it as the default implementation for the service process.
  sql::SandboxedVfs::Register(
      kVfsName, std::make_unique<SandboxedVfsDelegate>(CreateFilesystemProxy()),
      /*make_default=*/true);
}
#endif  // !BUILDFLAG(IS_ANDROID)

void StorageServiceImpl::BindLocalStorageControl(
    const std::optional<base::FilePath>& path,
    mojo::PendingReceiver<mojom::LocalStorageControl> receiver) {
  if (path.has_value()) {
    if (!path->IsAbsolute()) {
      // Refuse to bind LocalStorage for relative paths.
      return;
    }

    auto iter = persistent_local_storage_map_.find(*path);
    bool found = iter != persistent_local_storage_map_.end();
    // TODO(crbug.com/396030877): Remove this workaround to remove the
    // pre-existing LocalStorage once the issue is resolved.
    if (found) {
      ShutDownAndRemoveLocalStorage(iter->second);
    }
  }

  auto new_local_storage = std::make_unique<LocalStorageImpl>(
      path.value_or(base::FilePath()),
      base::SequencedTaskRunner::GetCurrentDefault(),
      base::BindOnce(&StorageServiceImpl::ShutDownAndRemoveLocalStorage,
                     weak_ptr_factory_.GetWeakPtr()),
      std::move(receiver));
  if (path.has_value()) {
    persistent_local_storage_map_[*path] = new_local_storage.get();
  }
  local_storages_.insert(std::move(new_local_storage));
}

void StorageServiceImpl::BindSessionStorageControl(
    const std::optional<base::FilePath>& path,
    mojo::PendingReceiver<mojom::SessionStorageControl> receiver) {
  if (path.has_value()) {
    if (!path->IsAbsolute()) {
      // Refuse to bind SessionStorage for relative paths.
      return;
    }

    auto iter = persistent_session_storage_map_.find(*path);
    bool found = iter != persistent_session_storage_map_.end();
    // TODO(crbug.com/396030877): Remove this workaround to remove the
    // pre-existing SessionStorage once the issue is resolved.
    if (found) {
      ShutDownAndRemoveSessionStorage(iter->second);
    }
  }

  auto new_session_storage = std::make_unique<SessionStorageImpl>(
      path.value_or(base::FilePath()),
      base::ThreadPool::CreateSequencedTaskRunner(
          {base::MayBlock(), base::WithBaseSyncPrimitives(),
           base::TaskShutdownBehavior::BLOCK_SHUTDOWN}),
      base::SequencedTaskRunner::GetCurrentDefault(),
#if BUILDFLAG(IS_ANDROID)
      // On Android there is no support for session storage restoring, and since
      // the restoring code is responsible for database cleanup, we must
      // manually delete the old database here before we open a new one.
      SessionStorageImpl::BackingMode::kClearDiskStateOnOpen,
#else
      path.has_value() ? SessionStorageImpl::BackingMode::kRestoreDiskState
                       : SessionStorageImpl::BackingMode::kNoDisk,
#endif
      std::string(kSessionStorageDirectory),
      base::OnceCallback<void(SessionStorageImpl*)>(
          base::BindOnce(&StorageServiceImpl::ShutDownAndRemoveSessionStorage,
                         weak_ptr_factory_.GetWeakPtr())),
      std::move(receiver));
  if (path.has_value()) {
    persistent_session_storage_map_[*path] = new_session_storage.get();
  }
  session_storages_.insert(std::move(new_session_storage));
}

void StorageServiceImpl::BindTestApi(
    mojo::ScopedMessagePipeHandle test_api_receiver) {
  GetTestApiBinderForTesting().Run(std::move(test_api_receiver));
}

void StorageServiceImpl::ShutDownAndRemoveSessionStorage(
    SessionStorageImpl* storage) {
  if (!storage->GetStoragePath().empty()) {
    persistent_session_storage_map_.erase(storage->GetStoragePath());
  }

  auto it = session_storages_.find(storage);
  if (it != session_storages_.end()) {
    auto node = session_storages_.extract(it);
    ShutDown(std::move(node.value()));
  }
}

void StorageServiceImpl::ShutDownAndRemoveLocalStorage(
    LocalStorageImpl* storage) {
  if (!storage->GetStoragePath().empty()) {
    persistent_local_storage_map_.erase(storage->GetStoragePath());
  }

  auto it = local_storages_.find(storage);
  if (it != local_storages_.end()) {
    auto node = local_storages_.extract(it);
    ShutDown(std::move(node.value()));
  }
}

#if !BUILDFLAG(IS_ANDROID)
void StorageServiceImpl::BindDataDirectoryReceiver(
    mojo::PendingReceiver<mojom::Directory> receiver) {
  DCHECK(remote_data_directory_.is_bound());
  remote_data_directory_->Clone(std::move(receiver));
}
#endif

}  // namespace storage