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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/shared_storage/test_shared_storage_runtime_manager.h"
#include <cstddef>
#include <map>
#include <memory>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/check_op.h"
#include "content/browser/shared_storage/shared_storage_document_service_impl.h"
#include "content/browser/shared_storage/shared_storage_runtime_manager.h"
#include "content/browser/shared_storage/test_shared_storage_runtime_manager.h"
#include "content/browser/shared_storage/test_shared_storage_worklet_host.h"
#include "content/public/browser/document_user_data.h"
#include "mojo/public/cpp/bindings/pending_associated_receiver.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.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 {
TestSharedStorageRuntimeManager::TestSharedStorageRuntimeManager(
StoragePartitionImpl& storage_partition)
: SharedStorageRuntimeManager(storage_partition) {}
TestSharedStorageRuntimeManager::~TestSharedStorageRuntimeManager() = default;
std::unique_ptr<SharedStorageWorkletHost>
TestSharedStorageRuntimeManager::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) {
auto test_worklet_host = std::make_unique<TestSharedStorageWorkletHost>(
document_service, frame_origin, data_origin, data_origin_type,
script_source_url, credentials_mode, creation_method, worklet_ordinal_id,
origin_trial_features, std::move(worklet_host), std::move(callback),
should_defer_worklet_messages_);
cached_worklet_host_devtools_tokens_.emplace(
worklet_ordinal_id,
test_worklet_host->GetWorkletDevToolsTokenForTesting());
return std::move(test_worklet_host);
}
// Precondition: there's only one eligible worklet host.
TestSharedStorageWorkletHost*
TestSharedStorageRuntimeManager::GetAttachedWorkletHost() {
std::vector<TestSharedStorageWorkletHost*> worklet_hosts =
GetAttachedWorkletHosts();
DCHECK_EQ(worklet_hosts.size(), 1u);
return worklet_hosts[0];
}
std::vector<TestSharedStorageWorkletHost*>
TestSharedStorageRuntimeManager::GetAttachedWorkletHosts() {
std::vector<TestSharedStorageWorkletHost*> results;
for (auto& [document_service, worklet_hosts] :
GetAttachedWorkletHostsForTesting()) {
for (auto& [raw_worklet_host, worklet_host] : worklet_hosts) {
results.push_back(
static_cast<TestSharedStorageWorkletHost*>(raw_worklet_host));
}
}
return results;
}
TestSharedStorageWorkletHost*
TestSharedStorageRuntimeManager::GetKeepAliveWorkletHost() {
DCHECK_EQ(1u, GetKeepAliveWorkletHostsCount());
return static_cast<TestSharedStorageWorkletHost*>(
GetKeepAliveWorkletHostsForTesting().begin()->second.get());
}
TestSharedStorageWorkletHost*
TestSharedStorageRuntimeManager::GetAttachedWorkletHostForFrame(
RenderFrameHost* frame) {
std::vector<TestSharedStorageWorkletHost*> worklet_hosts =
GetAttachedWorkletHostsForFrame(frame);
DCHECK_EQ(worklet_hosts.size(), 1u);
return worklet_hosts[0];
}
TestSharedStorageWorkletHost* TestSharedStorageRuntimeManager::
GetLastAttachedWorkletHostForFrameWithScriptSrc(
RenderFrameHost* frame,
const GURL& script_src_url) {
std::vector<TestSharedStorageWorkletHost*> worklet_hosts =
GetAttachedWorkletHostsForFrame(frame);
if (worklet_hosts.empty()) {
return nullptr;
}
for (size_t i = worklet_hosts.size(); i > 0; --i) {
auto* host = worklet_hosts[i - 1];
if (host->script_source_url() == script_src_url) {
return host;
}
}
return nullptr;
}
std::vector<TestSharedStorageWorkletHost*>
TestSharedStorageRuntimeManager::GetAttachedWorkletHostsForFrame(
RenderFrameHost* frame) {
SharedStorageDocumentServiceImpl* document_service =
DocumentUserData<SharedStorageDocumentServiceImpl>::GetForCurrentDocument(
frame);
DCHECK(document_service);
WorkletHosts& worklet_hosts =
GetAttachedWorkletHostsForTesting().at(document_service);
std::vector<TestSharedStorageWorkletHost*> results;
for (auto& [raw_worklet_host, worklet_host] : worklet_hosts) {
results.push_back(
static_cast<TestSharedStorageWorkletHost*>(raw_worklet_host));
}
return results;
}
void TestSharedStorageRuntimeManager::
ConfigureShouldDeferWorkletMessagesOnWorkletHostCreation(
bool should_defer_worklet_messages) {
should_defer_worklet_messages_ = should_defer_worklet_messages;
}
size_t TestSharedStorageRuntimeManager::GetAttachedWorkletHostsCount() {
size_t count = 0;
for (const auto& [document_service, worklet_hosts] :
GetAttachedWorkletHostsForTesting()) {
count += worklet_hosts.size();
}
return count;
}
size_t TestSharedStorageRuntimeManager::GetKeepAliveWorkletHostsCount() {
return GetKeepAliveWorkletHostsForTesting().size();
}
std::map<int, base::UnguessableToken>&
TestSharedStorageRuntimeManager::GetCachedWorkletHostDevToolsTokens() {
return cached_worklet_host_devtools_tokens_;
}
} // namespace content
|