1 | /* |
2 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "InjectedBundleScriptWorld.h" |
28 | |
29 | #include <WebCore/DOMWrapperWorld.h> |
30 | #include <WebCore/ScriptController.h> |
31 | #include <wtf/HashMap.h> |
32 | #include <wtf/NeverDestroyed.h> |
33 | #include <wtf/text/StringConcatenate.h> |
34 | #include <wtf/text/WTFString.h> |
35 | |
36 | namespace WebKit { |
37 | using namespace WebCore; |
38 | |
39 | typedef HashMap<DOMWrapperWorld*, InjectedBundleScriptWorld*> WorldMap; |
40 | |
41 | static WorldMap& allWorlds() |
42 | { |
43 | static NeverDestroyed<WorldMap> map; |
44 | return map; |
45 | } |
46 | |
47 | static String uniqueWorldName() |
48 | { |
49 | static uint64_t = 0; |
50 | return makeString("UniqueWorld_" , uniqueWorldNameNumber++); |
51 | } |
52 | |
53 | Ref<InjectedBundleScriptWorld> InjectedBundleScriptWorld::create() |
54 | { |
55 | return adoptRef(*new InjectedBundleScriptWorld(ScriptController::createWorld(), uniqueWorldName())); |
56 | } |
57 | |
58 | Ref<InjectedBundleScriptWorld> InjectedBundleScriptWorld::create(const String& name) |
59 | { |
60 | return adoptRef(*new InjectedBundleScriptWorld(ScriptController::createWorld(), name)); |
61 | } |
62 | |
63 | Ref<InjectedBundleScriptWorld> InjectedBundleScriptWorld::getOrCreate(DOMWrapperWorld& world) |
64 | { |
65 | if (&world == &mainThreadNormalWorld()) |
66 | return normalWorld(); |
67 | |
68 | if (InjectedBundleScriptWorld* existingWorld = allWorlds().get(&world)) |
69 | return *existingWorld; |
70 | |
71 | return adoptRef(*new InjectedBundleScriptWorld(world, uniqueWorldName())); |
72 | } |
73 | |
74 | InjectedBundleScriptWorld* InjectedBundleScriptWorld::find(const String& name) |
75 | { |
76 | for (auto* world : allWorlds().values()) { |
77 | if (world->name() == name) |
78 | return world; |
79 | } |
80 | return nullptr; |
81 | } |
82 | |
83 | InjectedBundleScriptWorld& InjectedBundleScriptWorld::normalWorld() |
84 | { |
85 | static InjectedBundleScriptWorld& world = adoptRef(*new InjectedBundleScriptWorld(mainThreadNormalWorld(), String())).leakRef(); |
86 | return world; |
87 | } |
88 | |
89 | InjectedBundleScriptWorld::InjectedBundleScriptWorld(DOMWrapperWorld& world, const String& name) |
90 | : m_world(world) |
91 | , m_name(name) |
92 | { |
93 | ASSERT(!allWorlds().contains(m_world.ptr())); |
94 | allWorlds().add(m_world.ptr(), this); |
95 | } |
96 | |
97 | InjectedBundleScriptWorld::~InjectedBundleScriptWorld() |
98 | { |
99 | ASSERT(allWorlds().contains(m_world.ptr())); |
100 | allWorlds().remove(m_world.ptr()); |
101 | } |
102 | |
103 | const DOMWrapperWorld& InjectedBundleScriptWorld::coreWorld() const |
104 | { |
105 | return m_world; |
106 | } |
107 | |
108 | DOMWrapperWorld& InjectedBundleScriptWorld::coreWorld() |
109 | { |
110 | return m_world; |
111 | } |
112 | |
113 | void InjectedBundleScriptWorld::clearWrappers() |
114 | { |
115 | m_world->clearWrappers(); |
116 | } |
117 | |
118 | void InjectedBundleScriptWorld::makeAllShadowRootsOpen() |
119 | { |
120 | m_world->setShadowRootIsAlwaysOpen(); |
121 | } |
122 | |
123 | void InjectedBundleScriptWorld::disableOverrideBuiltinsBehavior() |
124 | { |
125 | m_world->disableOverrideBuiltinsBehavior(); |
126 | } |
127 | |
128 | } // namespace WebKit |
129 | |