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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
/*
* Copyright (C) 2009 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/platform/bindings/dom_wrapper_world.h"
#include <memory>
#include <optional>
#include <utility>
#include "base/notreached.h"
#include "third_party/blink/public/platform/web_isolated_world_info.h"
#include "third_party/blink/renderer/platform/bindings/dom_data_store.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h"
#include "third_party/blink/renderer/platform/heap/disallow_new_wrapper.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/hash_traits.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h"
#include "third_party/blink/renderer/platform/wtf/thread_specific.h"
namespace blink {
namespace {
constexpr bool IsMainWorldId(int32_t world_id) {
return world_id == DOMWrapperWorld::kMainWorldId;
}
static_assert(IsMainWorldId(kMainDOMWorldId),
"The publicly-exposed kMainWorldId constant must match "
"the internal blink value.");
// This does not contain the main world because the WorldMap needs
// non-default hashmap traits (WTF::IntWithZeroKeyHashTraits) to contain
// it for the main world's id (0), and it may change the performance trends.
// (see https://crbug.com/704778#c6).
using WorldMap = HeapHashMap<int, WeakMember<DOMWrapperWorld>>;
static WorldMap& GetWorldMap() {
using WorldMapWrapper = DisallowNewWrapper<WorldMap>;
DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<Persistent<WorldMapWrapper>>,
map, ());
Persistent<WorldMapWrapper>& persistent_map = *map;
if (!persistent_map) {
persistent_map = MakeGarbageCollected<WorldMapWrapper>();
}
return persistent_map->Value();
}
} // namespace
unsigned DOMWrapperWorld::number_of_non_main_worlds_in_main_thread_ = 0;
DOMWrapperWorld* DOMWrapperWorld::Create(v8::Isolate* isolate,
WorldType world_type,
bool is_default_world_of_isolate) {
DCHECK(isolate);
DCHECK_NE(WorldType::kIsolated, world_type);
const auto world_id = GenerateWorldIdForType(world_type);
if (world_id.has_value()) [[likely]] {
return MakeGarbageCollected<DOMWrapperWorld>(PassKey(), isolate, world_type,
world_id.value(),
is_default_world_of_isolate);
}
return nullptr;
}
DOMWrapperWorld* DOMWrapperWorld::EnsureIsolatedWorld(v8::Isolate* isolate,
int32_t world_id) {
DCHECK(IsIsolatedWorldId(world_id));
WorldMap& map = GetWorldMap();
auto it = map.find(world_id);
if (it != map.end()) {
DOMWrapperWorld* world = it->value.Get();
DCHECK(world->IsIsolatedWorld());
DCHECK_EQ(world_id, world->GetWorldId());
return world;
}
return MakeGarbageCollected<DOMWrapperWorld>(
PassKey(), isolate, WorldType::kIsolated, world_id,
/*is_default_world_of_isolate=*/false);
}
DOMWrapperWorld::DOMWrapperWorld(PassKey,
v8::Isolate* isolate,
WorldType world_type,
int32_t world_id,
bool is_default_world_of_isolate)
: world_type_(world_type),
world_id_(world_id),
dom_data_store_(
MakeGarbageCollected<DOMDataStore>(isolate,
is_default_world_of_isolate)),
isolate_(isolate) {
switch (world_type_) {
case WorldType::kMain:
// The main world is managed separately from worldMap(). See worldMap().
break;
case WorldType::kIsolated:
case WorldType::kInspectorIsolated:
case WorldType::kRegExp:
case WorldType::kForV8ContextSnapshotNonMain:
case WorldType::kWorkerOrWorklet:
case WorldType::kShadowRealm: {
WorldMap& map = GetWorldMap();
DCHECK(!map.Contains(world_id_));
map.insert(world_id_, this);
if (IsMainThread())
number_of_non_main_worlds_in_main_thread_++;
break;
}
}
}
DOMWrapperWorld& DOMWrapperWorld::MainWorld(v8::Isolate* isolate) {
DCHECK(IsMainThread());
return V8PerIsolateData::From(isolate)->GetMainWorld();
}
void DOMWrapperWorld::AllWorldsInIsolate(
v8::Isolate* isolate,
HeapVector<Member<DOMWrapperWorld>>& worlds) {
DCHECK(worlds.empty());
worlds.assign(GetWorldMap().Values());
if (IsMainThread()) {
worlds.push_back(&MainWorld(isolate));
}
}
DOMWrapperWorld::~DOMWrapperWorld() {
if (IsMainThread() && !IsMainWorld()) {
number_of_non_main_worlds_in_main_thread_--;
}
}
void DOMWrapperWorld::Dispose() {
CHECK(!IsMainWorld());
if (dom_data_store_) {
// The data_store_ might be cleared on thread termination in the same
// garbage collection cycle which prohibits accessing the references from
// the dtor.
dom_data_store_->Dispose();
}
}
typedef HashMap<int, scoped_refptr<SecurityOrigin>>
IsolatedWorldSecurityOriginMap;
static IsolatedWorldSecurityOriginMap& IsolatedWorldSecurityOrigins() {
DCHECK(IsMainThread());
DEFINE_STATIC_LOCAL(IsolatedWorldSecurityOriginMap, map, ());
return map;
}
static scoped_refptr<SecurityOrigin> GetIsolatedWorldSecurityOrigin(
int32_t world_id,
const base::UnguessableToken& cluster_id) {
IsolatedWorldSecurityOriginMap& origins = IsolatedWorldSecurityOrigins();
auto it = origins.find(world_id);
if (it == origins.end())
return nullptr;
return it->value->GetOriginForAgentCluster(cluster_id);
}
scoped_refptr<SecurityOrigin> DOMWrapperWorld::IsolatedWorldSecurityOrigin(
const base::UnguessableToken& cluster_id) {
DCHECK(IsIsolatedWorld());
return GetIsolatedWorldSecurityOrigin(GetWorldId(), cluster_id);
}
scoped_refptr<const SecurityOrigin>
DOMWrapperWorld::IsolatedWorldSecurityOrigin(
const base::UnguessableToken& cluster_id) const {
DCHECK(IsIsolatedWorld());
return GetIsolatedWorldSecurityOrigin(GetWorldId(), cluster_id);
}
void DOMWrapperWorld::SetIsolatedWorldSecurityOrigin(
int32_t world_id,
scoped_refptr<SecurityOrigin> security_origin) {
DCHECK(IsIsolatedWorldId(world_id));
if (security_origin)
IsolatedWorldSecurityOrigins().Set(world_id, std::move(security_origin));
else
IsolatedWorldSecurityOrigins().erase(world_id);
}
typedef HashMap<int, String> IsolatedWorldStableIdMap;
static IsolatedWorldStableIdMap& IsolatedWorldStableIds() {
DCHECK(IsMainThread());
DEFINE_STATIC_LOCAL(IsolatedWorldStableIdMap, map, ());
return map;
}
String DOMWrapperWorld::NonMainWorldStableId() const {
DCHECK(!IsMainWorld());
const auto& map = IsolatedWorldStableIds();
const auto it = map.find(GetWorldId());
return it != map.end() ? it->value : String();
}
void DOMWrapperWorld::SetNonMainWorldStableId(int32_t world_id,
const String& stable_id) {
DCHECK(!IsMainWorldId(world_id));
IsolatedWorldStableIds().Set(world_id, stable_id);
}
typedef HashMap<int, String> IsolatedWorldHumanReadableNameMap;
static IsolatedWorldHumanReadableNameMap& IsolatedWorldHumanReadableNames() {
DCHECK(IsMainThread());
DEFINE_STATIC_LOCAL(IsolatedWorldHumanReadableNameMap, map, ());
return map;
}
String DOMWrapperWorld::NonMainWorldHumanReadableName() const {
DCHECK(!IsMainWorld());
const auto& map = IsolatedWorldHumanReadableNames();
const auto it = map.find(GetWorldId());
return it != map.end() ? it->value : String();
}
void DOMWrapperWorld::SetNonMainWorldHumanReadableName(
int32_t world_id,
const String& human_readable_name) {
DCHECK(!IsMainWorldId(world_id));
IsolatedWorldHumanReadableNames().Set(world_id, human_readable_name);
}
constinit thread_local int next_world_id =
DOMWrapperWorld::kUnspecifiedWorldIdStart;
// static
std::optional<int> DOMWrapperWorld::GenerateWorldIdForType(
WorldType world_type) {
switch (world_type) {
case WorldType::kMain:
return kMainWorldId;
case WorldType::kIsolated:
// This function should not be called for IsolatedWorld because an
// identifier for the world is given from out of DOMWrapperWorld.
NOTREACHED();
case WorldType::kInspectorIsolated: {
DCHECK(IsMainThread());
static int next_devtools_isolated_world_id =
IsolatedWorldId::kDevToolsFirstIsolatedWorldId;
if (next_devtools_isolated_world_id >
IsolatedWorldId::kDevToolsLastIsolatedWorldId) {
return std::nullopt;
}
return next_devtools_isolated_world_id++;
}
case WorldType::kRegExp:
case WorldType::kForV8ContextSnapshotNonMain:
case WorldType::kWorkerOrWorklet:
case WorldType::kShadowRealm: {
CHECK_GE(next_world_id, kUnspecifiedWorldIdStart);
return next_world_id++;
}
}
NOTREACHED();
}
// static
bool DOMWrapperWorld::ClearWrapperInAnyNonInlineStorageWorldIfEqualTo(
ScriptWrappable* object,
const v8::Local<v8::Object>& handle) {
for (DOMWrapperWorld* world : GetWorldMap().Values()) {
DOMDataStore& data_store = world->DomDataStore();
if (data_store.ClearInMapIfEqualTo(object, handle)) {
return true;
}
}
return false;
}
// static
bool DOMWrapperWorld::ClearWrapperInAnyNonInlineStorageWorldIfEqualTo(
ScriptWrappable* object,
const v8::TracedReference<v8::Object>& handle) {
for (DOMWrapperWorld* world : GetWorldMap().Values()) {
DOMDataStore& data_store = world->DomDataStore();
if (data_store.ClearInMapIfEqualTo(object, handle)) {
return true;
}
}
return false;
}
void DOMWrapperWorld::Trace(Visitor* visitor) const {
visitor->Trace(dom_data_store_);
}
} // namespace blink
|