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
|
/*
* Copyright (C) 2021 Apple 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:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``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 APPLE INC. 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 "config.h"
#include "BroadcastChannel.h"
#include "BroadcastChannelRegistry.h"
#include "EventNames.h"
#include "MessageEvent.h"
#include "Page.h"
#include "PartitionedSecurityOrigin.h"
#include "SecurityOrigin.h"
#include "SerializedScriptValue.h"
#include "WorkerGlobalScope.h"
#include "WorkerLoaderProxy.h"
#include "WorkerThread.h"
#include <wtf/CallbackAggregator.h>
#include <wtf/HashMap.h>
#include <wtf/Identified.h>
#include <wtf/MainThread.h>
#include <wtf/Scope.h>
#include <wtf/TZoneMallocInlines.h>
namespace WebCore {
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(BroadcastChannel);
static Lock allBroadcastChannelsLock;
static HashMap<BroadcastChannelIdentifier, ThreadSafeWeakPtr<BroadcastChannel>>& allBroadcastChannels() WTF_REQUIRES_LOCK(allBroadcastChannelsLock)
{
static NeverDestroyed<HashMap<BroadcastChannelIdentifier, ThreadSafeWeakPtr<BroadcastChannel>>> map;
return map;
}
static HashMap<BroadcastChannelIdentifier, ScriptExecutionContextIdentifier>& channelToContextIdentifier()
{
ASSERT(isMainThread());
static NeverDestroyed<HashMap<BroadcastChannelIdentifier, ScriptExecutionContextIdentifier>> map;
return map;
}
static PartitionedSecurityOrigin partitionedSecurityOriginFromContext(ScriptExecutionContext& context)
{
Ref securityOrigin { *context.securityOrigin() };
Ref topOrigin { context.settingsValues().broadcastChannelOriginPartitioningEnabled ? context.topOrigin() : securityOrigin.get() };
return { WTFMove(topOrigin), WTFMove(securityOrigin) };
}
class BroadcastChannel::MainThreadBridge : public ThreadSafeRefCounted<MainThreadBridge, WTF::DestructionThread::Main>, public Identified<BroadcastChannelIdentifier> {
public:
static Ref<MainThreadBridge> create(BroadcastChannel& channel, const String& name)
{
return adoptRef(*new MainThreadBridge(channel, name));
}
void registerChannel();
void unregisterChannel();
void postMessage(Ref<SerializedScriptValue>&&);
void detach() { m_broadcastChannel = nullptr; }
String name() const { return m_name.isolatedCopy(); }
private:
MainThreadBridge(BroadcastChannel&, const String& name);
void ensureOnMainThread(Function<void(Page*)>&&);
WeakPtr<BroadcastChannel, WeakPtrImplWithEventTargetData> m_broadcastChannel;
const String m_name; // Main thread only.
PartitionedSecurityOrigin m_origin; // Main thread only.
};
BroadcastChannel::MainThreadBridge::MainThreadBridge(BroadcastChannel& channel, const String& name)
: m_broadcastChannel(channel)
, m_name(name.isolatedCopy())
, m_origin(partitionedSecurityOriginFromContext(*channel.protectedScriptExecutionContext()).isolatedCopy())
{
}
void BroadcastChannel::MainThreadBridge::ensureOnMainThread(Function<void(Page*)>&& task)
{
ASSERT(m_broadcastChannel);
if (!m_broadcastChannel)
return;
RefPtr context = m_broadcastChannel->scriptExecutionContext();
if (!context)
return;
ASSERT(context->isContextThread());
if (auto* document = dynamicDowncast<Document>(*context)) {
task(document->protectedPage().get());
return;
}
auto* workerLoaderProxy = downcast<WorkerGlobalScope>(*context).thread().workerLoaderProxy();
if (!workerLoaderProxy)
return;
workerLoaderProxy->postTaskToLoader([task = WTFMove(task)](auto& context) {
task(downcast<Document>(context).protectedPage().get());
});
}
void BroadcastChannel::MainThreadBridge::registerChannel()
{
ensureOnMainThread([this, contextIdentifier = m_broadcastChannel->scriptExecutionContext()->identifier()](auto* page) mutable {
if (page)
page->protectedBroadcastChannelRegistry()->registerChannel(m_origin, m_name, identifier());
channelToContextIdentifier().add(identifier(), contextIdentifier);
});
}
void BroadcastChannel::MainThreadBridge::unregisterChannel()
{
ensureOnMainThread([this](auto* page) {
if (page)
page->protectedBroadcastChannelRegistry()->unregisterChannel(m_origin, m_name, identifier());
channelToContextIdentifier().remove(identifier());
});
}
void BroadcastChannel::MainThreadBridge::postMessage(Ref<SerializedScriptValue>&& message)
{
ensureOnMainThread([this, message = WTFMove(message)](auto* page) mutable {
if (!page)
return;
auto blobHandles = message->blobHandles();
page->protectedBroadcastChannelRegistry()->postMessage(m_origin, m_name, identifier(), WTFMove(message), [blobHandles = WTFMove(blobHandles)] {
// Keeps Blob data inside messageData alive until the message has been delivered.
});
});
}
BroadcastChannel::BroadcastChannel(ScriptExecutionContext& context, const String& name)
: ActiveDOMObject(&context)
, m_mainThreadBridge(MainThreadBridge::create(*this, name))
{
Ref mainThreadBridge = m_mainThreadBridge;
{
Locker locker { allBroadcastChannelsLock };
allBroadcastChannels().add(mainThreadBridge->identifier(), *this);
}
mainThreadBridge->registerChannel();
}
BroadcastChannel::~BroadcastChannel()
{
close();
m_mainThreadBridge->detach();
{
Locker locker { allBroadcastChannelsLock };
allBroadcastChannels().remove(m_mainThreadBridge->identifier());
}
}
auto BroadcastChannel::protectedMainThreadBridge() const -> Ref<MainThreadBridge>
{
return m_mainThreadBridge;
}
BroadcastChannelIdentifier BroadcastChannel::identifier() const
{
return m_mainThreadBridge->identifier();
}
String BroadcastChannel::name() const
{
return m_mainThreadBridge->name();
}
ExceptionOr<void> BroadcastChannel::postMessage(JSC::JSGlobalObject& globalObject, JSC::JSValue message)
{
if (!isEligibleForMessaging())
return { };
if (m_isClosed)
return Exception { ExceptionCode::InvalidStateError, "This BroadcastChannel is closed"_s };
Vector<Ref<MessagePort>> ports;
auto messageData = SerializedScriptValue::create(globalObject, message, { }, ports, SerializationForStorage::No, SerializationContext::WorkerPostMessage);
if (messageData.hasException())
return messageData.releaseException();
ASSERT(ports.isEmpty());
protectedMainThreadBridge()->postMessage(messageData.releaseReturnValue());
return { };
}
void BroadcastChannel::close()
{
if (m_isClosed)
return;
m_isClosed = true;
protectedMainThreadBridge()->unregisterChannel();
}
void BroadcastChannel::dispatchMessageTo(BroadcastChannelIdentifier channelIdentifier, Ref<SerializedScriptValue>&& message, CompletionHandler<void()>&& completionHandler)
{
ASSERT(isMainThread());
auto completionHandlerCallingScope = makeScopeExit([completionHandler = WTFMove(completionHandler)]() mutable {
callOnMainThread(WTFMove(completionHandler));
});
auto contextIdentifier = channelToContextIdentifier().get(channelIdentifier);
if (!contextIdentifier)
return;
ScriptExecutionContext::ensureOnContextThread(contextIdentifier, [channelIdentifier, message = WTFMove(message), completionHandlerCallingScope = WTFMove(completionHandlerCallingScope)](auto&) mutable {
RefPtr<BroadcastChannel> channel;
{
Locker locker { allBroadcastChannelsLock };
channel = allBroadcastChannels().get(channelIdentifier).get();
}
if (channel)
channel->dispatchMessage(WTFMove(message));
});
}
void BroadcastChannel::dispatchMessage(Ref<SerializedScriptValue>&& message)
{
if (!isEligibleForMessaging())
return;
if (m_isClosed)
return;
queueTaskKeepingObjectAlive(*this, TaskSource::PostedMessageQueue, [this, message = WTFMove(message)]() mutable {
if (m_isClosed || !scriptExecutionContext())
return;
auto* globalObject = scriptExecutionContext()->globalObject();
if (!globalObject)
return;
auto& vm = globalObject->vm();
auto scope = DECLARE_CATCH_SCOPE(vm);
auto event = MessageEvent::create(*globalObject, WTFMove(message), scriptExecutionContext()->securityOrigin()->toString());
if (UNLIKELY(scope.exception())) {
// Currently, we assume that the only way we can get here is if we have a termination.
RELEASE_ASSERT(vm.hasPendingTerminationException());
return;
}
dispatchEvent(event.event);
});
}
void BroadcastChannel::eventListenersDidChange()
{
m_hasRelevantEventListener = hasEventListeners(eventNames().messageEvent);
}
bool BroadcastChannel::virtualHasPendingActivity() const
{
return !m_isClosed && m_hasRelevantEventListener;
}
// https://html.spec.whatwg.org/#eligible-for-messaging
bool BroadcastChannel::isEligibleForMessaging() const
{
RefPtr context = scriptExecutionContext();
if (!context)
return false;
if (auto document = dynamicDowncast<Document>(*context))
return document->isFullyActive();
return !downcast<WorkerGlobalScope>(*context).isClosing();
}
} // namespace WebCore
|