summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/storage/storage_controller.h
blob: 63c631f639eae8e21d5ce766b37f011474abc0de (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_STORAGE_STORAGE_CONTROLLER_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_STORAGE_STORAGE_CONTROLLER_H_

#include "base/callback.h"
#include "base/sequence_checker.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "third_party/blink/public/common/dom_storage/session_storage_namespace_id.h"
#include "third_party/blink/public/mojom/dom_storage/dom_storage.mojom-blink.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/modules/storage/storage_area.h"
#include "third_party/blink/renderer/platform/heap/heap_allocator.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"

namespace base {
class SingleThreadTaskRunner;
}

namespace blink {

class CachedStorageArea;
class InspectorDOMStorageAgent;
class LocalFrame;
class SecurityOrigin;
class StorageNamespace;

// Singleton that manages the creation & accounting for DOMStorage objects. It
// does this by holding weak references to all session storage namespaces, and
// owning the local storage namespace internally. The total cache size is
// exposed with `TotalCacheSize()`, and `ClearAreasIfNeeded()` will - if our
// total cache size is larger than `total_cache_limit` - clear away any cache
// areas in live namespaces that no longer have references from Blink objects.
//
// SessionStorage StorageNamespace objects are created with
// `CreateSessionStorageNamespace` and live as a supplement on the Page.
//
// The LocalStorage StorageNamespace object is owned internally, and
// StorageController delegates the following methods to that namespace:
// GetLocalStorageArea, AddLocalStorageInspectorStorageAgent,
// RemoveLocalStorageInspectorStorageAgent
class MODULES_EXPORT StorageController : public mojom::blink::DomStorageClient {
  USING_FAST_MALLOC(StorageController);

 public:
  // Returns the one global StorageController instance.
  static StorageController* GetInstance();

  static bool CanAccessStorageArea(LocalFrame* frame,
                                   StorageArea::StorageType type);

  // Visible for testing.
  struct DomStorageConnection {
    mojo::Remote<mojom::blink::DomStorage> dom_storage_remote;
    mojo::PendingReceiver<mojom::blink::DomStorageClient> client_receiver;
  };
  StorageController(DomStorageConnection connection,
                    scoped_refptr<base::SingleThreadTaskRunner> task_runner,
                    size_t total_cache_limit);

  // Creates a MakeGarbageCollected<StorageNamespace> for Session storage, and
  // holds a weak reference for accounting & clearing. If there is already a
  // StorageNamespace created for the given id, it is returned.
  StorageNamespace* CreateSessionStorageNamespace(const String& namespace_id);

  // Returns the total size of all cached areas in namespaces this controller
  // knows of.
  size_t TotalCacheSize() const;

  // Cleans up unused areas if the total cache size is over the cache limit.
  void ClearAreasIfNeeded();

  // Methods that delegate to the internal StorageNamespace used for
  // LocalStorage:

  scoped_refptr<CachedStorageArea> GetLocalStorageArea(
      const SecurityOrigin*,
      mojo::PendingRemote<mojom::blink::StorageArea> local_storage_area = {});
  void AddLocalStorageInspectorStorageAgent(InspectorDOMStorageAgent* agent);
  void RemoveLocalStorageInspectorStorageAgent(InspectorDOMStorageAgent* agent);

  mojom::blink::DomStorage* dom_storage() const {
    return dom_storage_remote_.get();
  }

  scoped_refptr<base::SingleThreadTaskRunner> TaskRunner() {
    return task_runner_;
  }

 private:
  void EnsureLocalStorageNamespaceCreated();

  // mojom::blink::DomStorageClient:
  void ResetStorageAreaAndNamespaceConnections() override;

  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  Persistent<HeapHashMap<String, WeakMember<StorageNamespace>>> namespaces_;
  Persistent<StorageNamespace> local_storage_namespace_;
  size_t total_cache_limit_;

  mojo::Remote<mojom::blink::DomStorage> dom_storage_remote_;
  mojo::Receiver<mojom::blink::DomStorageClient> dom_storage_client_receiver_{
      this};

  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_STORAGE_STORAGE_CONTROLLER_H_