File: storage_service_impl.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (108 lines) | stat: -rw-r--r-- 4,680 bytes parent folder | download | duplicates (3)
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
// 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.

#ifndef COMPONENTS_SERVICES_STORAGE_STORAGE_SERVICE_IMPL_H_
#define COMPONENTS_SERVICES_STORAGE_STORAGE_SERVICE_IMPL_H_

#include <memory>
#include <set>

#include "base/containers/unique_ptr_adapters.h"
#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "build/build_config.h"
#include "components/services/storage/public/mojom/filesystem/directory.mojom.h"
#include "components/services/storage/public/mojom/storage_service.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace storage {

class LocalStorageImpl;
class SessionStorageImpl;
// Implementation of the main StorageService Mojo interface. This is the root
// owner of all Storage service instance state, managing the set of active
// persistent and in-memory local and session storage instances.
class StorageServiceImpl : public mojom::StorageService {
 public:
  // NOTE: |io_task_runner| is only used in sandboxed environments and can be
  // null otherwise. If non-null, it should specify a task runner that will
  // never block and is thus capable of reliably facilitating IPC to the
  // browser.
  StorageServiceImpl(mojo::PendingReceiver<mojom::StorageService> receiver,
                     scoped_refptr<base::SequencedTaskRunner> io_task_runner);

  StorageServiceImpl(const StorageServiceImpl&) = delete;
  StorageServiceImpl& operator=(const StorageServiceImpl&) = delete;

  ~StorageServiceImpl() override;

  // mojom::StorageService implementation:
  void EnableAggressiveDomStorageFlushing() override;
#if !BUILDFLAG(IS_ANDROID)
  void SetDataDirectory(
      const base::FilePath& path,
      mojo::PendingRemote<mojom::Directory> directory) override;
#endif
  void BindLocalStorageControl(
      const std::optional<base::FilePath>& path,
      mojo::PendingReceiver<mojom::LocalStorageControl> receiver) override;
  void BindSessionStorageControl(
      const std::optional<base::FilePath>& path,
      mojo::PendingReceiver<mojom::SessionStorageControl> receiver) override;
  void BindTestApi(mojo::ScopedMessagePipeHandle test_api_receiver) override;

  // These transfer ownership of the storage instance to a DeferredDeleter when
  // performing ShutDown. This allows the storage instance to be deleted after
  // ShutDown is complete. This prevents race conditions where a storage
  // instance for a user data directory is rebound while we wait for the
  // previous instance to ShutDown.
  void ShutDownAndRemoveSessionStorage(SessionStorageImpl* storage);
  void ShutDownAndRemoveLocalStorage(LocalStorageImpl* storage);

 private:
#if !BUILDFLAG(IS_ANDROID)
  // Binds a Directory receiver to the same remote implementation to which
  // |remote_data_directory_| is bound. It is invalid to call this when
  // |remote_data_directory_| is unbound.
  void BindDataDirectoryReceiver(
      mojo::PendingReceiver<mojom::Directory> receiver);
#endif

  const mojo::Receiver<mojom::StorageService> receiver_;
  const scoped_refptr<base::SequencedTaskRunner> io_task_runner_;

#if !BUILDFLAG(IS_ANDROID)
  // If bound, the service will assume it should not perform certain filesystem
  // operations directly and will instead go through this interface.
  base::FilePath remote_data_directory_path_;
  mojo::Remote<mojom::Directory> remote_data_directory_;
#endif

  // Sets of all isolated local and session storages owned by the service. This
  // includes both persistent and in-memory storages.
  std::set<std::unique_ptr<LocalStorageImpl>, base::UniquePtrComparator>
      local_storages_;
  std::set<std::unique_ptr<SessionStorageImpl>, base::UniquePtrComparator>
      session_storages_;

  // Mappings from a profile directory within the user data directory to the
  // corresponding storage instance in `local_storages` or `session_storages_`.
  // The pointers in these maps are not owned by the map and must be removed
  // when removed from `local_storages_` or `session_storages_`. Only persistent
  // storages have entries in these maps.
  std::map<base::FilePath, raw_ptr<LocalStorageImpl, CtnExperimental>>
      persistent_local_storage_map_;
  std::map<base::FilePath, raw_ptr<SessionStorageImpl, CtnExperimental>>
      persistent_session_storage_map_;

  base::WeakPtrFactory<StorageServiceImpl> weak_ptr_factory_{this};
};

}  // namespace storage

#endif  // COMPONENTS_SERVICES_STORAGE_STORAGE_SERVICE_IMPL_H_