Use a 1-byte enum class for TextDirection
[WebKit-https.git] / Source / WebCore / dom / ScriptExecutionContext.cpp
1 /*
2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3  * Copyright (C) 2012 Google Inc. All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27
28 #include "config.h"
29 #include "ScriptExecutionContext.h"
30
31 #include "CachedScript.h"
32 #include "CommonVM.h"
33 #include "DOMTimer.h"
34 #include "DOMWindow.h"
35 #include "DatabaseContext.h"
36 #include "Document.h"
37 #include "ErrorEvent.h"
38 #include "JSDOMExceptionHandling.h"
39 #include "JSDOMWindow.h"
40 #include "MessagePort.h"
41 #include "Navigator.h"
42 #include "PublicURLManager.h"
43 #include "RejectedPromiseTracker.h"
44 #include "ResourceRequest.h"
45 #include "SWClientConnection.h"
46 #include "SWContextManager.h"
47 #include "SchemeRegistry.h"
48 #include "ScriptDisallowedScope.h"
49 #include "ScriptState.h"
50 #include "ServiceWorker.h"
51 #include "ServiceWorkerGlobalScope.h"
52 #include "ServiceWorkerProvider.h"
53 #include "Settings.h"
54 #include "WorkerGlobalScope.h"
55 #include "WorkerNavigator.h"
56 #include "WorkerThread.h"
57 #include <JavaScriptCore/CatchScope.h>
58 #include <JavaScriptCore/Exception.h>
59 #include <JavaScriptCore/JSPromise.h>
60 #include <JavaScriptCore/ScriptCallStack.h>
61 #include <JavaScriptCore/StrongInlines.h>
62 #include <wtf/MainThread.h>
63 #include <wtf/Ref.h>
64 #include <wtf/SetForScope.h>
65
66 namespace WebCore {
67 using namespace Inspector;
68
69 static Lock allScriptExecutionContextsMapLock;
70 static HashMap<ScriptExecutionContextIdentifier, ScriptExecutionContext*>& allScriptExecutionContextsMap()
71 {
72     static NeverDestroyed<HashMap<ScriptExecutionContextIdentifier, ScriptExecutionContext*>> contexts;
73     ASSERT(allScriptExecutionContextsMapLock.isLocked());
74     return contexts;
75 }
76
77 struct ScriptExecutionContext::PendingException {
78     WTF_MAKE_FAST_ALLOCATED;
79 public:
80     PendingException(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL, RefPtr<ScriptCallStack>&& callStack)
81         : m_errorMessage(errorMessage)
82         , m_lineNumber(lineNumber)
83         , m_columnNumber(columnNumber)
84         , m_sourceURL(sourceURL)
85         , m_callStack(WTFMove(callStack))
86     {
87     }
88     String m_errorMessage;
89     int m_lineNumber;
90     int m_columnNumber;
91     String m_sourceURL;
92     RefPtr<ScriptCallStack> m_callStack;
93 };
94
95 ScriptExecutionContext::ScriptExecutionContext()
96 {
97 }
98
99 ScriptExecutionContextIdentifier ScriptExecutionContext::contextIdentifier() const
100 {
101     ASSERT(isContextThread());
102     if (!m_contextIdentifier) {
103         Locker<Lock> locker(allScriptExecutionContextsMapLock);
104
105         m_contextIdentifier = generateObjectIdentifier<ScriptExecutionContextIdentifierType>();
106
107         ASSERT(!allScriptExecutionContextsMap().contains(m_contextIdentifier));
108         allScriptExecutionContextsMap().add(m_contextIdentifier, const_cast<ScriptExecutionContext*>(this));
109     }
110     return m_contextIdentifier;
111 }
112
113 void ScriptExecutionContext::removeFromContextsMap()
114 {
115     if (m_contextIdentifier) {
116         Locker<Lock> locker(allScriptExecutionContextsMapLock);
117         ASSERT(allScriptExecutionContextsMap().contains(m_contextIdentifier));
118         allScriptExecutionContextsMap().remove(m_contextIdentifier);
119     }
120 }
121
122 #if ASSERT_DISABLED
123
124 inline void ScriptExecutionContext::checkConsistency() const
125 {
126 }
127
128 #else
129
130 void ScriptExecutionContext::checkConsistency() const
131 {
132     for (auto* messagePort : m_messagePorts)
133         ASSERT(messagePort->scriptExecutionContext() == this);
134
135     for (auto* destructionObserver : m_destructionObservers)
136         ASSERT(destructionObserver->scriptExecutionContext() == this);
137
138     for (auto* activeDOMObject : m_activeDOMObjects) {
139         ASSERT(activeDOMObject->scriptExecutionContext() == this);
140         activeDOMObject->assertSuspendIfNeededWasCalled();
141     }
142 }
143
144 #endif
145
146 ScriptExecutionContext::~ScriptExecutionContext()
147 {
148     checkConsistency();
149
150 #if !ASSERT_DISABLED
151     if (m_contextIdentifier) {
152         Locker<Lock> locker(allScriptExecutionContextsMapLock);
153         ASSERT_WITH_MESSAGE(!allScriptExecutionContextsMap().contains(m_contextIdentifier),
154             "A ScriptExecutionContext subclass instance implementing postTask should have already removed itself from the map");
155     }
156
157     m_inScriptExecutionContextDestructor = true;
158 #endif
159
160 #if ENABLE(SERVICE_WORKER)
161     setActiveServiceWorker(nullptr);
162 #endif
163
164     while (auto* destructionObserver = m_destructionObservers.takeAny())
165         destructionObserver->contextDestroyed();
166
167 #if !ASSERT_DISABLED
168     m_inScriptExecutionContextDestructor = false;
169 #endif
170 }
171
172 void ScriptExecutionContext::processMessageWithMessagePortsSoon()
173 {
174     if (m_willprocessMessageWithMessagePortsSoon)
175         return;
176
177     m_willprocessMessageWithMessagePortsSoon = true;
178     postTask([] (ScriptExecutionContext& context) {
179         context.dispatchMessagePortEvents();
180     });
181 }
182
183 void ScriptExecutionContext::dispatchMessagePortEvents()
184 {
185     checkConsistency();
186
187     Ref<ScriptExecutionContext> protectedThis(*this);
188     ASSERT(m_willprocessMessageWithMessagePortsSoon);
189     m_willprocessMessageWithMessagePortsSoon = false;
190
191     // Make a frozen copy of the ports so we can iterate while new ones might be added or destroyed.
192     for (auto* messagePort : copyToVector(m_messagePorts)) {
193         // The port may be destroyed, and another one created at the same address,
194         // but this is harmless. The worst that can happen as a result is that
195         // dispatchMessages() will be called needlessly.
196         if (m_messagePorts.contains(messagePort) && messagePort->started())
197             messagePort->dispatchMessages();
198     }
199 }
200
201 void ScriptExecutionContext::createdMessagePort(MessagePort& messagePort)
202 {
203     ASSERT((is<Document>(*this) && isMainThread())
204         || (is<WorkerGlobalScope>(*this) && downcast<WorkerGlobalScope>(*this).thread().thread() == &Thread::current()));
205
206     m_messagePorts.add(&messagePort);
207 }
208
209 void ScriptExecutionContext::destroyedMessagePort(MessagePort& messagePort)
210 {
211     ASSERT((is<Document>(*this) && isMainThread())
212         || (is<WorkerGlobalScope>(*this) && downcast<WorkerGlobalScope>(*this).thread().thread() == &Thread::current()));
213
214     m_messagePorts.remove(&messagePort);
215 }
216
217 void ScriptExecutionContext::didLoadResourceSynchronously()
218 {
219 }
220
221 bool ScriptExecutionContext::canSuspendActiveDOMObjectsForDocumentSuspension(Vector<ActiveDOMObject*>* unsuspendableObjects)
222 {
223     checkConsistency();
224
225     bool canSuspend = true;
226
227     forEachActiveDOMObject([&](auto& activeDOMObject) {
228         if (!activeDOMObject.canSuspendForDocumentSuspension()) {
229             canSuspend = false;
230             if (unsuspendableObjects)
231                 unsuspendableObjects->append(&activeDOMObject);
232             else
233                 return ShouldContinue::No;
234         }
235         return ShouldContinue::Yes;
236     });
237
238     if (unsuspendableObjects) {
239         // Remove activeDOMObjects that have been destroyed while we were iterating above.
240         unsuspendableObjects->removeAllMatching([&](auto* activeDOMObject) {
241             return !m_activeDOMObjects.contains(activeDOMObject);
242         });
243     }
244
245     return canSuspend;
246 }
247
248 void ScriptExecutionContext::forEachActiveDOMObject(const Function<ShouldContinue(ActiveDOMObject&)>& apply) const
249 {
250     // It is not allowed to run arbitrary script or construct new ActiveDOMObjects while we are iterating over ActiveDOMObjects.
251     // An ASSERT_WITH_SECURITY_IMPLICATION or RELEASE_ASSERT will fire if this happens, but it's important to code
252     // canSuspendActiveDOMObjectsForDocumentSuspension() / suspend() / resume() / stop() functions so it will not happen!
253     ScriptDisallowedScope scriptDisallowedScope;
254     SetForScope<bool> activeDOMObjectAdditionForbiddenScope(m_activeDOMObjectAdditionForbidden, true);
255
256     // Make a frozen copy of the objects so we can iterate while new ones might be destroyed.
257     auto possibleActiveDOMObjects = copyToVector(m_activeDOMObjects);
258
259     for (auto* activeDOMObject : possibleActiveDOMObjects) {
260         // Check if this object was deleted already. If so, just skip it.
261         // Calling contains on a possibly-already-deleted object is OK because we guarantee
262         // no new object can be added, so even if a new object ends up allocated with the
263         // same address, that will be *after* this function exits.
264         if (!m_activeDOMObjects.contains(activeDOMObject))
265             continue;
266
267         if (apply(*activeDOMObject) == ShouldContinue::No)
268             break;
269     }
270 }
271
272 void ScriptExecutionContext::suspendActiveDOMObjects(ReasonForSuspension why)
273 {
274     checkConsistency();
275
276     if (m_activeDOMObjectsAreSuspended) {
277         // A page may subsequently suspend DOM objects, say as part of entering the page cache, after the embedding
278         // client requested the page be suspended. We ignore such requests so long as the embedding client requested
279         // the suspension first. See <rdar://problem/13754896> for more details.
280         ASSERT(m_reasonForSuspendingActiveDOMObjects == ReasonForSuspension::PageWillBeSuspended);
281         return;
282     }
283
284     m_activeDOMObjectsAreSuspended = true;
285
286     forEachActiveDOMObject([why](auto& activeDOMObject) {
287         activeDOMObject.suspend(why);
288         return ShouldContinue::Yes;
289     });
290
291     m_reasonForSuspendingActiveDOMObjects = why;
292 }
293
294 void ScriptExecutionContext::resumeActiveDOMObjects(ReasonForSuspension why)
295 {
296     checkConsistency();
297
298     if (m_reasonForSuspendingActiveDOMObjects != why)
299         return;
300     m_activeDOMObjectsAreSuspended = false;
301
302     forEachActiveDOMObject([](auto& activeDOMObject) {
303         activeDOMObject.resume();
304         return ShouldContinue::Yes;
305     });
306 }
307
308 void ScriptExecutionContext::stopActiveDOMObjects()
309 {
310     checkConsistency();
311
312     if (m_activeDOMObjectsAreStopped)
313         return;
314     m_activeDOMObjectsAreStopped = true;
315
316     forEachActiveDOMObject([](auto& activeDOMObject) {
317         activeDOMObject.stop();
318         return ShouldContinue::Yes;
319     });
320 }
321
322 void ScriptExecutionContext::suspendActiveDOMObjectIfNeeded(ActiveDOMObject& activeDOMObject)
323 {
324     ASSERT(m_activeDOMObjects.contains(&activeDOMObject));
325     if (m_activeDOMObjectsAreSuspended)
326         activeDOMObject.suspend(m_reasonForSuspendingActiveDOMObjects);
327     if (m_activeDOMObjectsAreStopped)
328         activeDOMObject.stop();
329 }
330
331 void ScriptExecutionContext::didCreateActiveDOMObject(ActiveDOMObject& activeDOMObject)
332 {
333     // The m_activeDOMObjectAdditionForbidden check is a RELEASE_ASSERT because of the
334     // consequences of having an ActiveDOMObject that is not correctly reflected in the set.
335     // If we do have one of those, it can possibly be a security vulnerability. So we'd
336     // rather have a crash than continue running with the set possibly compromised.
337     ASSERT(!m_inScriptExecutionContextDestructor);
338     RELEASE_ASSERT(!m_activeDOMObjectAdditionForbidden);
339     m_activeDOMObjects.add(&activeDOMObject);
340 }
341
342 void ScriptExecutionContext::willDestroyActiveDOMObject(ActiveDOMObject& activeDOMObject)
343 {
344     m_activeDOMObjects.remove(&activeDOMObject);
345 }
346
347 void ScriptExecutionContext::didCreateDestructionObserver(ContextDestructionObserver& observer)
348 {
349     ASSERT(!m_inScriptExecutionContextDestructor);
350     m_destructionObservers.add(&observer);
351 }
352
353 void ScriptExecutionContext::willDestroyDestructionObserver(ContextDestructionObserver& observer)
354 {
355     m_destructionObservers.remove(&observer);
356 }
357
358 // FIXME: Should this function be in SecurityContext or SecurityOrigin instead?
359 bool ScriptExecutionContext::canIncludeErrorDetails(CachedScript* script, const String& sourceURL)
360 {
361     ASSERT(securityOrigin());
362     if (script) {
363         ASSERT(script->origin());
364         ASSERT(securityOrigin()->toString() == script->origin()->toString());
365         return script->isCORSSameOrigin();
366     }
367     return securityOrigin()->canRequest(completeURL(sourceURL));
368 }
369
370 void ScriptExecutionContext::reportException(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL, JSC::Exception* exception, RefPtr<ScriptCallStack>&& callStack, CachedScript* cachedScript)
371 {
372     if (m_inDispatchErrorEvent) {
373         if (!m_pendingExceptions)
374             m_pendingExceptions = std::make_unique<Vector<std::unique_ptr<PendingException>>>();
375         m_pendingExceptions->append(std::make_unique<PendingException>(errorMessage, lineNumber, columnNumber, sourceURL, WTFMove(callStack)));
376         return;
377     }
378
379     // First report the original exception and only then all the nested ones.
380     if (!dispatchErrorEvent(errorMessage, lineNumber, columnNumber, sourceURL, exception, cachedScript))
381         logExceptionToConsole(errorMessage, sourceURL, lineNumber, columnNumber, callStack.copyRef());
382
383     if (!m_pendingExceptions)
384         return;
385
386     auto pendingExceptions = WTFMove(m_pendingExceptions);
387     for (auto& exception : *pendingExceptions)
388         logExceptionToConsole(exception->m_errorMessage, exception->m_sourceURL, exception->m_lineNumber, exception->m_columnNumber, WTFMove(exception->m_callStack));
389 }
390
391 void ScriptExecutionContext::reportUnhandledPromiseRejection(JSC::ExecState& state, JSC::JSPromise& promise, RefPtr<Inspector::ScriptCallStack>&& callStack)
392 {
393     JSC::VM& vm = state.vm();
394     auto scope = DECLARE_CATCH_SCOPE(vm);
395
396     int lineNumber = 0;
397     int columnNumber = 0;
398     String sourceURL;
399
400     JSC::JSValue result = promise.result(vm);
401     String resultMessage = retrieveErrorMessage(state, vm, result, scope);
402     String errorMessage = makeString("Unhandled Promise Rejection: ", resultMessage);
403     if (callStack) {
404         if (const ScriptCallFrame* callFrame = callStack->firstNonNativeCallFrame()) {
405             lineNumber = callFrame->lineNumber();
406             columnNumber = callFrame->columnNumber();
407             sourceURL = callFrame->sourceURL();
408         }
409     }
410
411     logExceptionToConsole(errorMessage, sourceURL, lineNumber, columnNumber, WTFMove(callStack));
412 }
413
414 void ScriptExecutionContext::addConsoleMessage(MessageSource source, MessageLevel level, const String& message, const String& sourceURL, unsigned lineNumber, unsigned columnNumber, JSC::ExecState* state, unsigned long requestIdentifier)
415 {
416     addMessage(source, level, message, sourceURL, lineNumber, columnNumber, 0, state, requestIdentifier);
417 }
418
419 bool ScriptExecutionContext::dispatchErrorEvent(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL, JSC::Exception* exception, CachedScript* cachedScript)
420 {
421     auto* target = errorEventTarget();
422     if (!target)
423         return false;
424
425     RefPtr<ErrorEvent> errorEvent;
426     if (canIncludeErrorDetails(cachedScript, sourceURL))
427         errorEvent = ErrorEvent::create(errorMessage, sourceURL, lineNumber, columnNumber, { vm(), exception ? exception->value() : JSC::jsNull() });
428     else
429         errorEvent = ErrorEvent::create("Script error."_s, { }, 0, 0, { });
430
431     ASSERT(!m_inDispatchErrorEvent);
432     m_inDispatchErrorEvent = true;
433     target->dispatchEvent(*errorEvent);
434     m_inDispatchErrorEvent = false;
435     return errorEvent->defaultPrevented();
436 }
437
438 int ScriptExecutionContext::circularSequentialID()
439 {
440     ++m_circularSequentialID;
441     if (m_circularSequentialID <= 0)
442         m_circularSequentialID = 1;
443     return m_circularSequentialID;
444 }
445
446 PublicURLManager& ScriptExecutionContext::publicURLManager()
447 {
448     if (!m_publicURLManager)
449         m_publicURLManager = PublicURLManager::create(this);
450     return *m_publicURLManager;
451 }
452
453 void ScriptExecutionContext::adjustMinimumDOMTimerInterval(Seconds oldMinimumTimerInterval)
454 {
455     if (minimumDOMTimerInterval() != oldMinimumTimerInterval) {
456         for (auto& timer : m_timeouts.values())
457             timer->updateTimerIntervalIfNecessary();
458     }
459 }
460
461 Seconds ScriptExecutionContext::minimumDOMTimerInterval() const
462 {
463     // The default implementation returns the DOMTimer's default
464     // minimum timer interval. FIXME: to make it work with dedicated
465     // workers, we will have to override it in the appropriate
466     // subclass, and provide a way to enumerate a Document's dedicated
467     // workers so we can update them all.
468     return DOMTimer::defaultMinimumInterval();
469 }
470
471 void ScriptExecutionContext::didChangeTimerAlignmentInterval()
472 {
473     for (auto& timer : m_timeouts.values())
474         timer->didChangeAlignmentInterval();
475 }
476
477 Seconds ScriptExecutionContext::domTimerAlignmentInterval(bool) const
478 {
479     return DOMTimer::defaultAlignmentInterval();
480 }
481
482 JSC::VM& ScriptExecutionContext::vm()
483 {
484     if (is<Document>(*this))
485         return commonVM();
486
487     return downcast<WorkerGlobalScope>(*this).script()->vm();
488 }
489
490 RejectedPromiseTracker& ScriptExecutionContext::ensureRejectedPromiseTrackerSlow()
491 {
492     // ScriptExecutionContext::vm() in Worker is only available after WorkerGlobalScope initialization is done.
493     // When initializing ScriptExecutionContext, vm() is not ready.
494
495     ASSERT(!m_rejectedPromiseTracker);
496     m_rejectedPromiseTracker = std::make_unique<RejectedPromiseTracker>(*this, vm());
497     return *m_rejectedPromiseTracker.get();
498 }
499
500 void ScriptExecutionContext::setDatabaseContext(DatabaseContext* databaseContext)
501 {
502     m_databaseContext = databaseContext;
503 }
504
505 bool ScriptExecutionContext::hasPendingActivity() const
506 {
507     checkConsistency();
508
509     for (auto* activeDOMObject : m_activeDOMObjects) {
510         if (activeDOMObject->hasPendingActivity())
511             return true;
512     }
513
514     return false;
515 }
516
517 JSC::ExecState* ScriptExecutionContext::execState()
518 {
519     if (is<Document>(*this)) {
520         Document& document = downcast<Document>(*this);
521         return execStateFromPage(mainThreadNormalWorld(), document.page());
522     }
523
524     WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(this);
525     return execStateFromWorkerGlobalScope(workerGlobalScope);
526 }
527
528 String ScriptExecutionContext::domainForCachePartition() const
529 {
530     return m_domainForCachePartition.isNull() ? topOrigin().domainForCachePartition() : m_domainForCachePartition;
531 }
532
533 #if ENABLE(SERVICE_WORKER)
534
535 bool ScriptExecutionContext::hasServiceWorkerScheme()
536 {
537     ASSERT(securityOrigin());
538     return SchemeRegistry::isServiceWorkerContainerCustomScheme(securityOrigin()->protocol());
539 }
540
541 ServiceWorker* ScriptExecutionContext::activeServiceWorker() const
542 {
543     return m_activeServiceWorker.get();
544 }
545
546 void ScriptExecutionContext::setActiveServiceWorker(RefPtr<ServiceWorker>&& serviceWorker)
547 {
548     m_activeServiceWorker = WTFMove(serviceWorker);
549 }
550
551 void ScriptExecutionContext::registerServiceWorker(ServiceWorker& serviceWorker)
552 {
553     auto addResult = m_serviceWorkers.add(serviceWorker.identifier(), &serviceWorker);
554     ASSERT_UNUSED(addResult, addResult.isNewEntry);
555 }
556
557 void ScriptExecutionContext::unregisterServiceWorker(ServiceWorker& serviceWorker)
558 {
559     m_serviceWorkers.remove(serviceWorker.identifier());
560 }
561
562 ServiceWorkerContainer* ScriptExecutionContext::serviceWorkerContainer()
563 {
564     NavigatorBase* navigator = nullptr;
565     if (is<Document>(*this)) {
566         if (auto* window = downcast<Document>(*this).domWindow())
567             navigator = window->optionalNavigator();
568     } else
569         navigator = downcast<WorkerGlobalScope>(*this).optionalNavigator();
570
571     return navigator ? &navigator->serviceWorker() : nullptr;
572 }
573
574 bool ScriptExecutionContext::postTaskTo(const DocumentOrWorkerIdentifier& contextIdentifier, WTF::Function<void(ScriptExecutionContext&)>&& task)
575 {
576     ASSERT(isMainThread());
577
578     bool wasPosted = false;
579     switchOn(contextIdentifier, [&] (DocumentIdentifier identifier) {
580         auto* document = Document::allDocumentsMap().get(identifier);
581         if (!document)
582             return;
583         document->postTask([task = WTFMove(task)](auto& scope) {
584             task(scope);
585         });
586         wasPosted= true;
587     }, [&](ServiceWorkerIdentifier identifier) {
588         wasPosted = SWContextManager::singleton().postTaskToServiceWorker(identifier, [task = WTFMove(task)](auto& scope) {
589             task(scope);
590         });
591     });
592     return wasPosted;
593 }
594
595 #endif
596
597 bool ScriptExecutionContext::postTaskTo(ScriptExecutionContextIdentifier identifier, Task&& task)
598 {
599     Locker<Lock> locker(allScriptExecutionContextsMapLock);
600     auto* context = allScriptExecutionContextsMap().get(identifier);
601
602     if (!context)
603         return false;
604
605     context->postTask(WTFMove(task));
606     return true;
607 }
608
609 } // namespace WebCore