Changeset 284226 in webkit


Ignore:
Timestamp:
Oct 14, 2021, 7:02:14 PM (4 years ago)
Author:
Chris Dumez
Message:

Service workers running on the main thread should use the main VM
https://bugs.webkit.org/show_bug.cgi?id=231753

Reviewed by Geoffrey Garen.

Source/WebCore:

ervice workers running on the main thread should use the main VM. This makes life easier for injected
bundle clients and there is no strong reason to use a separate VM since VMs are mainly used for thread
safety / isolation.

No new tests, extended existing API test.

  • workers/WorkerOrWorkletGlobalScope.cpp:

(WebCore::WorkerOrWorkletGlobalScope::isContextThread const):
WorkerOrWorkletThread::thread() returns null when the service worker is running on the main thread.
Update WorkerOrWorkletGlobalScope::isContextThread() to deal with that and properly treats the
main thread as the context thread in this case.

  • workers/WorkerOrWorkletScriptController.cpp:

(WebCore::WorkerOrWorkletScriptController::WorkerOrWorkletScriptController):
(WebCore::WorkerOrWorkletScriptController::scheduleExecutionTermination):

Tools:

Extend API test coverage.

  • TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerPagePlugIn.mm:

(-[ServiceWorkerPagePlugIn webProcessPlugInBrowserContextController:serviceWorkerGlobalObjectIsAvailableForFrame:inScriptWorld:]):

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r284225 r284226  
     12021-10-14  Chris Dumez  <cdumez@apple.com>
     2
     3        Service workers running on the main thread should use the main VM
     4        https://bugs.webkit.org/show_bug.cgi?id=231753
     5
     6        Reviewed by Geoffrey Garen.
     7
     8        ervice workers running on the main thread should use the main VM. This makes life easier for injected
     9        bundle clients and there is no strong reason to use a separate VM since VMs are mainly used for thread
     10        safety / isolation.
     11
     12        No new tests, extended existing API test.
     13
     14        * workers/WorkerOrWorkletGlobalScope.cpp:
     15        (WebCore::WorkerOrWorkletGlobalScope::isContextThread const):
     16        WorkerOrWorkletThread::thread() returns null when the service worker is running on the main thread.
     17        Update WorkerOrWorkletGlobalScope::isContextThread() to deal with that and properly treats the
     18        main thread as the context thread in this case.
     19
     20        * workers/WorkerOrWorkletScriptController.cpp:
     21        (WebCore::WorkerOrWorkletScriptController::WorkerOrWorkletScriptController):
     22        (WebCore::WorkerOrWorkletScriptController::scheduleExecutionTermination):
     23
    1242021-10-14  Aditya Keerthi  <akeerthi@apple.com>
    225
  • trunk/Source/WebCore/workers/WorkerGlobalScope.cpp

    r284093 r284226  
    3232#include "CSSValueList.h"
    3333#include "CSSValuePool.h"
     34#include "CommonVM.h"
    3435#include "ContentSecurityPolicy.h"
    3536#include "Crypto.h"
     
    8081
    8182WorkerGlobalScope::WorkerGlobalScope(WorkerThreadType type, const WorkerParameters& params, Ref<SecurityOrigin>&& origin, WorkerThread& thread, Ref<SecurityOrigin>&& topOrigin, IDBClient::IDBConnectionProxy* connectionProxy, SocketProvider* socketProvider)
    82     : WorkerOrWorkletGlobalScope(type, JSC::VM::create(), &thread)
     83    : WorkerOrWorkletGlobalScope(type, isMainThread() ? Ref { commonVM() } : JSC::VM::create(), &thread)
    8384    , m_url(params.scriptURL)
    8485    , m_identifier(params.identifier)
  • trunk/Source/WebCore/workers/WorkerOrWorkletGlobalScope.cpp

    r273203 r284226  
    112112{
    113113    auto* thread = workerOrWorkletThread();
    114     return thread ? thread->thread() == &Thread::current() : isMainThread();
     114    return thread && thread->thread() ? thread->thread() == &Thread::current() : isMainThread();
    115115}
    116116
  • trunk/Source/WebCore/workers/WorkerOrWorkletScriptController.cpp

    r284093 r284226  
    2828#include "WorkerOrWorkletScriptController.h"
    2929
     30#include "CommonVM.h"
    3031#include "DedicatedWorkerGlobalScope.h"
    3132#include "EventLoop.h"
     
    6970    , m_globalScopeWrapper(*m_vm)
    7071{
    71     m_vm->heap.acquireAccess(); // It's not clear that we have good discipline for heap access, so turn it on permanently.
    72     {
    73         JSLockHolder lock(m_vm.get());
    74         m_vm->ensureTerminationException();
    75     }
    76 
    77     JSVMClientData::initNormalWorld(m_vm.get(), type);
     72    if (!isMainThread() || m_vm != &commonVM()) {
     73        m_vm->heap.acquireAccess(); // It's not clear that we have good discipline for heap access, so turn it on permanently.
     74        {
     75            JSLockHolder lock(m_vm.get());
     76            m_vm->ensureTerminationException();
     77        }
     78
     79        JSVMClientData::initNormalWorld(m_vm.get(), type);
     80    }
    7881}
    7982
     
    130133        m_isTerminatingExecution = true;
    131134    }
    132     m_vm->notifyNeedTermination();
     135    if (m_vm != &commonVM())
     136        m_vm->notifyNeedTermination();
    133137}
    134138
  • trunk/Tools/ChangeLog

    r284223 r284226  
     12021-10-14  Chris Dumez  <cdumez@apple.com>
     2
     3        Service workers running on the main thread should use the main VM
     4        https://bugs.webkit.org/show_bug.cgi?id=231753
     5
     6        Reviewed by Geoffrey Garen.
     7
     8        Extend API test coverage.
     9
     10        * TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerPagePlugIn.mm:
     11        (-[ServiceWorkerPagePlugIn webProcessPlugInBrowserContextController:serviceWorkerGlobalObjectIsAvailableForFrame:inScriptWorld:]):
     12
    1132021-10-14  Wenson Hsieh  <wenson_hsieh@apple.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerPagePlugIn.mm

    r283522 r284226  
    2727
    2828#import "ServiceWorkerPageProtocol.h"
     29#import <JavaScriptCore/JSContextRef.h>
    2930#import <WebKit/WKBundlePage.h>
    3031#import <WebKit/WKWebProcessPlugIn.h>
     
    3334#import <WebKit/WKWebProcessPlugInFramePrivate.h>
    3435#import <WebKit/WKWebProcessPlugInLoadDelegate.h>
     36#import <WebKit/WKWebProcessPlugInScriptWorld.h>
    3537#import <WebKit/_WKRemoteObjectInterface.h>
    3638#import <WebKit/_WKRemoteObjectRegistry.h>
     
    4850    RELEASE_ASSERT(RunLoop::isMain());
    4951    RELEASE_ASSERT(frame);
    50     JSContext *jsContext = [frame jsContextForServiceWorkerWorld:scriptWorld];
    51     RELEASE_ASSERT(jsContext);
    52     RELEASE_ASSERT([WKWebProcessPlugInFrame lookUpFrameFromJSContext:jsContext] == frame);
     52    JSContext *serviceWorkerJSContext = [frame jsContextForServiceWorkerWorld:scriptWorld];
     53    RELEASE_ASSERT(serviceWorkerJSContext);
     54    RELEASE_ASSERT([WKWebProcessPlugInFrame lookUpFrameFromJSContext:serviceWorkerJSContext] == frame);
    5355
    54     JSValue *globalIsServiceWorkerGlobalScope = [jsContext evaluateScript:@"self.__proto__ === ServiceWorkerGlobalScope.prototype"];
     56    JSContext *mainFrameJSContext = [frame jsContextForWorld:[WKWebProcessPlugInScriptWorld normalWorld]];
     57    RELEASE_ASSERT(mainFrameJSContext);
     58
     59    // The main frame and the service worker should have different JSContexts but should use the same VM.
     60    RELEASE_ASSERT(mainFrameJSContext != serviceWorkerJSContext);
     61    RELEASE_ASSERT(JSContextGetGroup(mainFrameJSContext.JSGlobalContextRef) == JSContextGetGroup(serviceWorkerJSContext.JSGlobalContextRef));
     62
     63    RELEASE_ASSERT(scriptWorld == [WKWebProcessPlugInScriptWorld normalWorld]);
     64
     65    JSValue *globalIsServiceWorkerGlobalScope = [serviceWorkerJSContext evaluateScript:@"self.__proto__ === ServiceWorkerGlobalScope.prototype"];
    5566    RELEASE_ASSERT(!!globalIsServiceWorkerGlobalScope);
    5667    RELEASE_ASSERT([globalIsServiceWorkerGlobalScope toBool]);
Note: See TracChangeset for help on using the changeset viewer.