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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_SHARED_STORAGE_SHARED_STORAGE_RUNTIME_MANAGER_H_
#define CONTENT_BROWSER_SHARED_STORAGE_SHARED_STORAGE_RUNTIME_MANAGER_H_
#include <map>
#include <memory>
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/scoped_observation_traits.h"
#include "base/time/time.h"
#include "content/browser/shared_storage/shared_storage_event_params.h"
#include "content/browser/shared_storage/shared_storage_lock_manager.h"
#include "content/common/content_export.h"
#include "content/public/browser/frame_tree_node_id.h"
#include "content/public/browser/global_routing_id.h"
#include "third_party/blink/public/common/shared_storage/shared_storage_utils.h"
#include "third_party/blink/public/mojom/origin_trials/origin_trial_feature.mojom-shared.h"
#include "third_party/blink/public/mojom/shared_storage/shared_storage.mojom.h"
namespace content {
class FencedFrameConfig;
class SharedStorageDocumentServiceImpl;
class SharedStorageWorkletHost;
class StoragePartitionImpl;
// Manages in-memory components related to shared storage, such as
// `SharedStorageWorkletHost` and `LockManager`. The manager is bound to the
// `StoragePartition`.
class CONTENT_EXPORT SharedStorageRuntimeManager {
public:
using AccessScope = blink::SharedStorageAccessScope;
using WorkletHosts = std::map<SharedStorageWorkletHost*,
std::unique_ptr<SharedStorageWorkletHost>>;
explicit SharedStorageRuntimeManager(StoragePartitionImpl& storage_partition);
virtual ~SharedStorageRuntimeManager();
class SharedStorageObserverInterface : public base::CheckedObserver {
public:
enum AccessMethod {
kAddModule,
kCreateWorklet,
kSelectURL,
kRun,
kBatchUpdate,
kSet,
kAppend,
kDelete,
kClear,
kGet,
kKeys,
kValues,
kEntries,
kLength,
kRemainingBudget,
};
virtual GlobalRenderFrameHostId AssociatedFrameHostId() const = 0;
virtual bool ShouldReceiveAllSharedStorageReports() const = 0;
virtual void OnSharedStorageAccessed(
base::Time access_time,
AccessScope scope,
AccessMethod method,
GlobalRenderFrameHostId main_frame_id,
const std::string& owner_origin,
const SharedStorageEventParams& params) = 0;
virtual void OnSharedStorageSelectUrlUrnUuidGenerated(
const GURL& urn_uuid) = 0;
virtual void OnSharedStorageSelectUrlConfigPopulated(
const std::optional<FencedFrameConfig>& config) = 0;
// TODO(crbug.com/401011862): Remove `worklet_ordinal_id` parameter.
virtual void OnSharedStorageWorkletOperationExecutionFinished(
base::Time finished_time,
base::TimeDelta execution_time,
AccessMethod method,
int operation_id,
int worklet_ordinal_id,
const base::UnguessableToken& worklet_devtools_token,
GlobalRenderFrameHostId main_frame_id,
const std::string& owner_origin) = 0;
};
void OnDocumentServiceDestroyed(
SharedStorageDocumentServiceImpl* document_service);
void ExpireWorkletHostForDocumentService(
SharedStorageDocumentServiceImpl* document_service,
SharedStorageWorkletHost* worklet_host);
void CreateWorkletHost(
SharedStorageDocumentServiceImpl* document_service,
const url::Origin& frame_origin,
const url::Origin& data_origin,
blink::mojom::SharedStorageDataOriginType data_origin_type,
const GURL& script_source_url,
network::mojom::CredentialsMode credentials_mode,
blink::mojom::SharedStorageWorkletCreationMethod creation_method,
const std::vector<blink::mojom::OriginTrialFeature>&
origin_trial_features,
mojo::PendingAssociatedReceiver<blink::mojom::SharedStorageWorkletHost>
worklet_host_receiver,
blink::mojom::SharedStorageDocumentService::CreateWorkletCallback
callback);
void AddSharedStorageObserver(SharedStorageObserverInterface* observer);
void RemoveSharedStorageObserver(SharedStorageObserverInterface* observer);
void NotifySharedStorageAccessed(
AccessScope scope,
SharedStorageObserverInterface::AccessMethod method,
GlobalRenderFrameHostId main_frame_id,
const std::string& owner_origin,
const SharedStorageEventParams& params);
// TODO(crbug.com/401011862): Remove `worklet_ordinal_id` parameter.
void NotifyWorkletOperationExecutionFinished(
base::TimeDelta execution_time,
SharedStorageObserverInterface::AccessMethod method,
int operation_id,
int worklet_ordinal_id,
const base::UnguessableToken& worklet_devtools_token,
GlobalRenderFrameHostId main_frame_id,
const std::string& owner_origin);
std::map<SharedStorageDocumentServiceImpl*, WorkletHosts>&
GetAttachedWorkletHostsForTesting() {
return attached_shared_storage_worklet_hosts_;
}
const std::map<SharedStorageWorkletHost*,
std::unique_ptr<SharedStorageWorkletHost>>&
GetKeepAliveWorkletHostsForTesting() {
return keep_alive_shared_storage_worklet_hosts_;
}
void NotifyUrnUuidGenerated(const GURL& urn_uuid);
void NotifyConfigPopulated(const std::optional<FencedFrameConfig>& config);
SharedStorageLockManager& lock_manager() { return lock_manager_; }
protected:
void OnWorkletKeepAliveFinished(SharedStorageWorkletHost*);
// virtual for testing
virtual std::unique_ptr<SharedStorageWorkletHost> CreateWorkletHostHelper(
SharedStorageDocumentServiceImpl& document_service,
const url::Origin& frame_origin,
const url::Origin& data_origin,
blink::mojom::SharedStorageDataOriginType data_origin_type,
const GURL& script_source_url,
network::mojom::CredentialsMode credentials_mode,
blink::mojom::SharedStorageWorkletCreationMethod creation_method,
int worklet_ordinal_id,
const std::vector<blink::mojom::OriginTrialFeature>&
origin_trial_features,
mojo::PendingAssociatedReceiver<blink::mojom::SharedStorageWorkletHost>
worklet_host,
blink::mojom::SharedStorageDocumentService::CreateWorkletCallback
callback);
private:
// The hosts that are attached to the worklet's owner document. Those hosts
// are created on demand when the `SharedStorageDocumentServiceImpl` requests
// it. When the corresponding document is destructed (where it will call
// `OnDocumentServiceDestroyed`), its associated worklet hosts will either be
// destroyed, or will be moved from this map to
// `keep_alive_shared_storage_worklet_hosts_`, depending on whether there are
// pending operations.
std::map<SharedStorageDocumentServiceImpl*, WorkletHosts>
attached_shared_storage_worklet_hosts_;
// The hosts that are detached from the worklet's owner document and have
// entered keep-alive phase.
WorkletHosts keep_alive_shared_storage_worklet_hosts_;
// Manages shared storage locks.
SharedStorageLockManager lock_manager_;
base::ObserverList<SharedStorageObserverInterface> observers_;
// A monotonically increasing ID assigned to each SharedStorageWorkletHost.
// This ID is assigned during construction of the SharedStorageWorkletHost.
// TODO(crbug.com/401011862): Use the worklet IDs generated in DevTools
// reporting.
int next_worklet_ordinal_id_ = 0;
};
} // namespace content
namespace base {
template <>
struct ScopedObservationTraits<
content::SharedStorageRuntimeManager,
content::SharedStorageRuntimeManager::SharedStorageObserverInterface> {
static void AddObserver(
content::SharedStorageRuntimeManager* source,
content::SharedStorageRuntimeManager::SharedStorageObserverInterface*
observer) {
source->AddSharedStorageObserver(observer);
}
static void RemoveObserver(
content::SharedStorageRuntimeManager* source,
content::SharedStorageRuntimeManager::SharedStorageObserverInterface*
observer) {
source->RemoveSharedStorageObserver(observer);
}
};
} // namespace base
#endif // CONTENT_BROWSER_SHARED_STORAGE_SHARED_STORAGE_RUNTIME_MANAGER_H_
|