Add checked casts for ScriptExecutionContext.
authorakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 16 Feb 2014 00:26:53 +0000 (00:26 +0000)
committerakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 16 Feb 2014 00:26:53 +0000 (00:26 +0000)
<https://webkit.org/b/128874>

Generate casting helpers for casting from ScriptExecutionContext to
Document and WorkerGlobalScope. Apply heartily.

Reviewed by Antti Koivisto.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@164182 268f45cc-cd09-0410-ab3c-d52691b4dbfc

21 files changed:
Source/WebCore/ChangeLog
Source/WebCore/Modules/indexeddb/IDBFactory.cpp
Source/WebCore/Modules/indexeddb/WorkerGlobalScopeIndexedDatabase.cpp
Source/WebCore/Modules/websockets/ThreadableWebSocketChannel.cpp
Source/WebCore/bindings/js/JSDOMGlobalObject.cpp
Source/WebCore/bindings/js/JSEventListener.cpp
Source/WebCore/bindings/js/ScheduledAction.cpp
Source/WebCore/dom/Document.h
Source/WebCore/dom/MessagePort.cpp
Source/WebCore/dom/ScriptExecutionContext.cpp
Source/WebCore/dom/ScriptExecutionContext.h
Source/WebCore/inspector/InspectorInstrumentation.cpp
Source/WebCore/loader/ThreadableLoader.cpp
Source/WebCore/loader/cache/MemoryCache.cpp
Source/WebCore/workers/DefaultSharedWorkerRepository.cpp
Source/WebCore/workers/WorkerGlobalScope.cpp
Source/WebCore/workers/WorkerGlobalScope.h
Source/WebCore/workers/WorkerMessagingProxy.cpp
Source/WebCore/workers/WorkerRunLoop.cpp
Source/WebCore/workers/WorkerScriptLoader.cpp
Source/WebCore/workers/WorkerThread.cpp

index b5ccf9d90f05d3610f02eca5560e6b38b8dc8baf..d0f4f9da50cf0cd9dbd23a9f649835655b4a0020 100644 (file)
@@ -1,3 +1,13 @@
+2014-02-15  Andreas Kling  <akling@apple.com>
+
+        Add checked casts for ScriptExecutionContext.
+        <https://webkit.org/b/128874>
+
+        Generate casting helpers for casting from ScriptExecutionContext to
+        Document and WorkerGlobalScope. Apply heartily.
+
+        Reviewed by Antti Koivisto.
+
 2014-02-15  Alexey Proskuryakov  <ap@apple.com>
 
         [Mac] All WebKit clients should encrypt WebCrypto keys automatically
index 835937129de583923a250ec3da2537a54d969a41..c04a528baed36e766e1906ec635bfcf6e22af6e5 100644 (file)
@@ -84,8 +84,7 @@ static String getIndexedDBDatabasePath(ScriptExecutionContext* context)
         Document* document = toDocument(context);
         return document->page()->group().groupSettings().indexedDBDatabasePath();
     }
-    WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(context);
-    const GroupSettings* groupSettings = workerGlobalScope->groupSettings();
+    const GroupSettings* groupSettings = toWorkerGlobalScope(context)->groupSettings();
     if (groupSettings)
         return groupSettings->indexedDBDatabasePath();
     return String();
index 2074b15c53b40abf6f4ab176b622ca9dd5ef6979..8c7e94c98dc4fa52261cf98ff0f701e196df042c 100644 (file)
@@ -57,8 +57,7 @@ WorkerGlobalScopeIndexedDatabase* WorkerGlobalScopeIndexedDatabase::from(ScriptE
     WorkerGlobalScopeIndexedDatabase* supplement = static_cast<WorkerGlobalScopeIndexedDatabase*>(Supplement<ScriptExecutionContext>::from(context, supplementName()));
     if (!supplement) {
         String databaseDirectoryIdentifier;
-        WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(context);
-        const GroupSettings* groupSettings = workerGlobalScope->groupSettings();
+        const GroupSettings* groupSettings = toWorkerGlobalScope(context)->groupSettings();
         if (groupSettings)
             databaseDirectoryIdentifier = groupSettings->indexedDBDatabasePath();
 
index 9828b95571fea12c302a411285df7890b58a7cf9..9ffdf5e06972b4628d04f96a2698d35f412ba845 100644 (file)
@@ -56,7 +56,7 @@ PassRefPtr<ThreadableWebSocketChannel> ThreadableWebSocketChannel::create(Script
     ASSERT(client);
 
     if (context->isWorkerGlobalScope()) {
-        WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(context);
+        WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context);
         WorkerRunLoop& runLoop = workerGlobalScope->thread().runLoop();
         String mode = webSocketChannelMode;
         mode.append(String::number(runLoop.createUniqueId()));
index 27bd7894f962f5ccf428132eedbd1b5493061a4c..5e2683e90f0f06408c0f390844fda81fa9970c56 100644 (file)
@@ -120,7 +120,7 @@ JSDOMGlobalObject* toJSDOMGlobalObject(ScriptExecutionContext* scriptExecutionCo
         return toJSDOMGlobalObject(toDocument(scriptExecutionContext), exec);
 
     if (scriptExecutionContext->isWorkerGlobalScope())
-        return static_cast<WorkerGlobalScope*>(scriptExecutionContext)->script()->workerGlobalScopeWrapper();
+        return toWorkerGlobalScope(scriptExecutionContext)->script()->workerGlobalScopeWrapper();
 
     ASSERT_NOT_REACHED();
     return 0;
@@ -137,7 +137,7 @@ JSDOMGlobalObject* toJSDOMGlobalObject(ScriptExecutionContext* scriptExecutionCo
         return toJSDOMGlobalObject(toDocument(scriptExecutionContext), world);
 
     if (scriptExecutionContext->isWorkerGlobalScope())
-        return static_cast<WorkerGlobalScope*>(scriptExecutionContext)->script()->workerGlobalScopeWrapper();
+        return toWorkerGlobalScope(scriptExecutionContext)->script()->workerGlobalScopeWrapper();
 
     ASSERT_NOT_REACHED();
     return 0;
index 6f6662712f1f8d5c6d8bb5f519ac2b0f07a5fc72..9a4bf000b5d49658ea2efdc52b5ad45ab101b484 100644 (file)
@@ -140,7 +140,7 @@ void JSEventListener::handleEvent(ScriptExecutionContext* scriptExecutionContext
         if (scriptExecutionContext->isWorkerGlobalScope()) {
             bool terminatorCausedException = (exec->hadException() && isTerminatedExecutionException(exec->exception()));
             if (terminatorCausedException || vm.watchdog.didFire())
-                static_cast<WorkerGlobalScope*>(scriptExecutionContext)->script()->forbidExecution();
+                toWorkerGlobalScope(scriptExecutionContext)->script()->forbidExecution();
         }
 
         if (exec->hadException()) {
index 7aec1019834fb20515c041e7bf9f3804ed36b7ad..eb0e31a8ba8eae4b2d03e290e830f4806f960275 100644 (file)
@@ -76,10 +76,8 @@ void ScheduledAction::execute(ScriptExecutionContext* context)
 {
     if (context->isDocument())
         execute(toDocument(context));
-    else {
-        ASSERT_WITH_SECURITY_IMPLICATION(context->isWorkerGlobalScope());
-        execute(static_cast<WorkerGlobalScope*>(context));
-    }
+    else
+        execute(toWorkerGlobalScope(context));
 }
 
 void ScheduledAction::executeFunctionInContext(JSGlobalObject* globalObject, JSValue thisValue, ScriptExecutionContext* context)
index ffa38676f68a76e3fea058365b73e13707876c4e..92f6e5473f9d8a2b6c6eed30d5b0947d3752d5a1 100644 (file)
@@ -1705,29 +1705,7 @@ inline ScriptExecutionContext* Node::scriptExecutionContext() const
 
 Element* eventTargetElementForDocument(Document*);
 
-inline Document& toDocument(ScriptExecutionContext& scriptExecutionContext)
-{
-    ASSERT_WITH_SECURITY_IMPLICATION(scriptExecutionContext.isDocument());
-    return static_cast<Document&>(scriptExecutionContext);
-}
-
-inline const Document& toDocument(const ScriptExecutionContext& scriptExecutionContext)
-{
-    ASSERT_WITH_SECURITY_IMPLICATION(scriptExecutionContext.isDocument());
-    return static_cast<const Document&>(scriptExecutionContext);
-}
-
-inline Document* toDocument(ScriptExecutionContext* scriptExecutionContext)
-{
-    ASSERT_WITH_SECURITY_IMPLICATION(!scriptExecutionContext || scriptExecutionContext->isDocument());
-    return static_cast<Document*>(scriptExecutionContext);
-}
-
-inline const Document* toDocument(const ScriptExecutionContext* scriptExecutionContext)
-{
-    ASSERT_WITH_SECURITY_IMPLICATION(!scriptExecutionContext || scriptExecutionContext->isDocument());
-    return static_cast<const Document*>(scriptExecutionContext);
-}
+SCRIPT_EXECUTION_CONTEXT_TYPE_CASTS(Document)
 
 inline bool isDocument(const Node& node) { return node.isDocumentNode(); }
 void isDocument(const Document&); // Catch unnecessary runtime check of type known at compile time.
index 8e5b29d8f98921d2e0957891b62fd10a336ed22d..125e42f1d341d7c571c7c804788813b23132950b 100644 (file)
@@ -157,7 +157,7 @@ void MessagePort::dispatchMessages()
     while (m_entangledChannel && m_entangledChannel->tryGetMessageFromRemote(message, channels)) {
 
         // close() in Worker onmessage handler should prevent next message from dispatching.
-        if (m_scriptExecutionContext->isWorkerGlobalScope() && static_cast<WorkerGlobalScope*>(m_scriptExecutionContext)->isClosing())
+        if (m_scriptExecutionContext->isWorkerGlobalScope() && toWorkerGlobalScope(m_scriptExecutionContext)->isClosing())
             return;
 
         std::unique_ptr<MessagePortArray> ports = MessagePort::entanglePorts(*m_scriptExecutionContext, std::move(channels));
index c305d48ef4c48810f816b010ac1e7a8589b32cf2..2d74a381a6d74073328920a21f57a4a78b5dba80 100644 (file)
@@ -146,7 +146,7 @@ void ScriptExecutionContext::createdMessagePort(MessagePort* port)
 {
     ASSERT(port);
     ASSERT((isDocument() && isMainThread())
-        || (isWorkerGlobalScope() && currentThread() == static_cast<WorkerGlobalScope*>(this)->thread().threadID()));
+        || (isWorkerGlobalScope() && currentThread() == toWorkerGlobalScope(this)->thread().threadID()));
 
     m_messagePorts.add(port);
 }
@@ -155,7 +155,7 @@ void ScriptExecutionContext::destroyedMessagePort(MessagePort* port)
 {
     ASSERT(port);
     ASSERT((isDocument() && isMainThread())
-        || (isWorkerGlobalScope() && currentThread() == static_cast<WorkerGlobalScope*>(this)->thread().threadID()));
+        || (isWorkerGlobalScope() && currentThread() == toWorkerGlobalScope(this)->thread().threadID()));
 
     m_messagePorts.remove(port);
 }
@@ -411,7 +411,7 @@ JSC::VM* ScriptExecutionContext::vm()
         return JSDOMWindow::commonVM();
 
     if (isWorkerGlobalScope())
-        return static_cast<WorkerGlobalScope*>(this)->script()->vm();
+        return toWorkerGlobalScope(this)->script()->vm();
 
     ASSERT_NOT_REACHED();
     return 0;
index 8bd9ece1a0775e954a5932c643902929a7e12e1b..b0406167bd5d367354394286b3009dc55b8b157f 100644 (file)
@@ -223,6 +223,9 @@ private:
 #endif
 };
 
+#define SCRIPT_EXECUTION_CONTEXT_TYPE_CASTS(ToValueTypeName) \
+    TYPE_CASTS_BASE(ToValueTypeName, ScriptExecutionContext, context, context->is##ToValueTypeName(), context.is##ToValueTypeName())
+
 } // namespace WebCore
 
 #endif // ScriptExecutionContext_h
index f158923de48faf70f3d477d9c3cb3af547e32e89..30512ab3b5de44c1d3fca577853b2efa05f20d3e 100644 (file)
@@ -1177,7 +1177,7 @@ InstrumentingAgents* InspectorInstrumentation::instrumentingAgentsForWorkerGloba
 InstrumentingAgents* InspectorInstrumentation::instrumentingAgentsForNonDocumentContext(ScriptExecutionContext* context)
 {
     if (context->isWorkerGlobalScope())
-        return instrumentationForWorkerGlobalScope(static_cast<WorkerGlobalScope*>(context));
+        return instrumentationForWorkerGlobalScope(toWorkerGlobalScope(context));
     return nullptr;
 }
 
index 55efefaa5940ed411f75374e1fb6c06e4e5d835e..41a7c1a589862165bed430001da83df55f9c9c8e 100644 (file)
@@ -57,7 +57,7 @@ PassRefPtr<ThreadableLoader> ThreadableLoader::create(ScriptExecutionContext* co
     ASSERT(context);
 
     if (context->isWorkerGlobalScope())
-        return WorkerThreadableLoader::create(static_cast<WorkerGlobalScope*>(context), client, WorkerRunLoop::defaultMode(), request, options);
+        return WorkerThreadableLoader::create(toWorkerGlobalScope(context), client, WorkerRunLoop::defaultMode(), request, options);
 
     return DocumentThreadableLoader::create(toDocument(*context), *client, request, options);
 }
@@ -67,7 +67,7 @@ void ThreadableLoader::loadResourceSynchronously(ScriptExecutionContext* context
     ASSERT(context);
 
     if (context->isWorkerGlobalScope()) {
-        WorkerThreadableLoader::loadResourceSynchronously(static_cast<WorkerGlobalScope*>(context), request, client, options);
+        WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(context), request, client, options);
         return;
     }
 
index 1b45a253554bb9502d23b3d5b9023e6536bcdc69..cc6a225744232f9b444e7986952d23191eb4b59c 100644 (file)
@@ -816,8 +816,7 @@ void MemoryCache::removeUrlFromCache(ScriptExecutionContext* context, const Stri
 void MemoryCache::removeRequestFromCache(ScriptExecutionContext* context, const ResourceRequest& request)
 {
     if (context->isWorkerGlobalScope()) {
-        WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(context);
-        workerGlobalScope->thread().workerLoaderProxy().postTaskToLoader(createCallbackTask(&crossThreadRemoveRequestFromCache, request));
+        toWorkerGlobalScope(context)->thread().workerLoaderProxy().postTaskToLoader(createCallbackTask(&crossThreadRemoveRequestFromCache, request));
         return;
     }
 
index a675f131803ef1b5e5beced4dc4145e0f56883c8..c59d563533ea0c65b4734b4b4422455ce7aa6002 100644 (file)
@@ -270,7 +270,7 @@ private:
         RefPtr<MessagePort> port = MessagePort::create(*scriptContext);
         port->entangle(std::move(m_channel));
         ASSERT_WITH_SECURITY_IMPLICATION(scriptContext->isWorkerGlobalScope());
-        WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(scriptContext);
+        WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(scriptContext);
         // Since close() stops the thread event loop, this should not ever get called while closing.
         ASSERT(!workerGlobalScope->isClosing());
         ASSERT_WITH_SECURITY_IMPLICATION(workerGlobalScope->isSharedWorkerGlobalScope());
index 9a3f9655e728f06799bb365c7b5645b9c502ba2d..7451c9fcd760a5b9cd41417fcebb0660c87183f5 100644 (file)
@@ -73,10 +73,8 @@ public:
 
     virtual void performTask(ScriptExecutionContext *context)
     {
-        ASSERT_WITH_SECURITY_IMPLICATION(context->isWorkerGlobalScope());
-        WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(context);
         // Notify parent that this context is closed. Parent is responsible for calling WorkerThread::stop().
-        workerGlobalScope->thread().workerReportingProxy().workerGlobalScopeClosed();
+        toWorkerGlobalScope(context)->thread().workerReportingProxy().workerGlobalScopeClosed();
     }
 
     virtual bool isCleanupTask() const { return true; }
index 9c4ed4a661d2983c8ed49b684a540577da074820..3928ea4ff7ddcb22014c41c1eb197e96e6183ea7 100644 (file)
@@ -182,6 +182,8 @@ namespace WebCore {
         RefPtr<SecurityOrigin> m_topOrigin;
     };
 
+SCRIPT_EXECUTION_CONTEXT_TYPE_CASTS(WorkerGlobalScope)
+
 } // namespace WebCore
 
 #endif // WorkerGlobalScope_h
index c94a6d8dfcdaa3665dcdb5d5e534a5df5808e711..5b8e7ebd12b2534203a9a38f3032e4d5c26de7a1 100644 (file)
@@ -259,8 +259,7 @@ private:
     virtual void performTask(ScriptExecutionContext *context)
     {
         AtomicString eventName = m_isOnLine ? eventNames().onlineEvent : eventNames().offlineEvent;
-        WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(context);
-        workerGlobalScope->dispatchEvent(Event::create(eventName, false, false));
+        toWorkerGlobalScope(context)->dispatchEvent(Event::create(eventName, false, false));
     }
 
     bool m_isOnLine;
@@ -285,14 +284,14 @@ WorkerMessagingProxy::WorkerMessagingProxy(Worker* workerObject)
 {
     ASSERT(m_workerObject);
     ASSERT((m_scriptExecutionContext->isDocument() && isMainThread())
-           || (m_scriptExecutionContext->isWorkerGlobalScope() && currentThread() == static_cast<WorkerGlobalScope*>(m_scriptExecutionContext.get())->thread().threadID()));
+           || (m_scriptExecutionContext->isWorkerGlobalScope() && currentThread() == toWorkerGlobalScope(*m_scriptExecutionContext).thread().threadID()));
 }
 
 WorkerMessagingProxy::~WorkerMessagingProxy()
 {
     ASSERT(!m_workerObject);
     ASSERT((m_scriptExecutionContext->isDocument() && isMainThread())
-           || (m_scriptExecutionContext->isWorkerGlobalScope() && currentThread() == static_cast<WorkerGlobalScope*>(m_scriptExecutionContext.get())->thread().threadID()));
+           || (m_scriptExecutionContext->isWorkerGlobalScope() && currentThread() == toWorkerGlobalScope(*m_scriptExecutionContext).thread().threadID()));
 }
 
 void WorkerMessagingProxy::startWorkerGlobalScope(const URL& scriptURL, const String& userAgent, const String& sourceCode, WorkerThreadStartMode startMode)
@@ -408,8 +407,7 @@ void WorkerMessagingProxy::workerObjectDestroyedInternal(ScriptExecutionContext*
 #if ENABLE(INSPECTOR)
 static void connectToWorkerGlobalScopeInspectorTask(ScriptExecutionContext* context, bool)
 {
-    ASSERT_WITH_SECURITY_IMPLICATION(context->isWorkerGlobalScope());
-    static_cast<WorkerGlobalScope*>(context)->workerInspectorController().connectFrontend();
+    toWorkerGlobalScope(context)->workerInspectorController().connectFrontend();
 }
 
 void WorkerMessagingProxy::connectToInspector(WorkerGlobalScopeProxy::PageInspector* pageInspector)
@@ -423,8 +421,7 @@ void WorkerMessagingProxy::connectToInspector(WorkerGlobalScopeProxy::PageInspec
 
 static void disconnectFromWorkerGlobalScopeInspectorTask(ScriptExecutionContext* context, bool)
 {
-    ASSERT_WITH_SECURITY_IMPLICATION(context->isWorkerGlobalScope());
-    static_cast<WorkerGlobalScope*>(context)->workerInspectorController().disconnectFrontend(Inspector::InspectorDisconnectReason::InspectorDestroyed);
+    toWorkerGlobalScope(context)->workerInspectorController().disconnectFrontend(Inspector::InspectorDisconnectReason::InspectorDestroyed);
 }
 
 void WorkerMessagingProxy::disconnectFromInspector()
@@ -437,8 +434,7 @@ void WorkerMessagingProxy::disconnectFromInspector()
 
 static void dispatchOnInspectorBackendTask(ScriptExecutionContext* context, const String& message)
 {
-    ASSERT_WITH_SECURITY_IMPLICATION(context->isWorkerGlobalScope());
-    static_cast<WorkerGlobalScope*>(context)->workerInspectorController().dispatchMessageFromFrontend(message);
+    toWorkerGlobalScope(context)->workerInspectorController().dispatchMessageFromFrontend(message);
 }
 
 void WorkerMessagingProxy::sendMessageToInspector(const String& message)
index 67fafebfe515470e3b7a19991ec6da8f942a722c..49e061c0a233b4aefa9b057953289a69c92a9784 100644 (file)
@@ -214,8 +214,7 @@ std::unique_ptr<WorkerRunLoop::Task> WorkerRunLoop::Task::create(PassOwnPtr<Scri
 
 void WorkerRunLoop::Task::performTask(const WorkerRunLoop& runLoop, ScriptExecutionContext* context)
 {
-    WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(context);
-    if ((!workerGlobalScope->isClosing() && !runLoop.terminated()) || m_task->isCleanupTask())
+    if ((!toWorkerGlobalScope(context)->isClosing() && !runLoop.terminated()) || m_task->isCleanupTask())
         m_task->performTask(context);
 }
 
index aec3e84d26855b50258417e9145a265e52230abe..884087f8bdea28151626c11aa00f10975a271dd2 100644 (file)
@@ -70,7 +70,7 @@ void WorkerScriptLoader::loadSynchronously(ScriptExecutionContext* scriptExecuti
     options.crossOriginRequestPolicy = crossOriginRequestPolicy;
     options.sendLoadCallbacks = SendCallbacks;
 
-    WorkerThreadableLoader::loadResourceSynchronously(static_cast<WorkerGlobalScope*>(scriptExecutionContext), *request, *this, options);
+    WorkerThreadableLoader::loadResourceSynchronously(toWorkerGlobalScope(scriptExecutionContext), *request, *this, options);
 }
     
 void WorkerScriptLoader::loadAsynchronously(ScriptExecutionContext* scriptExecutionContext, const URL& url, CrossOriginRequestPolicy crossOriginRequestPolicy, WorkerScriptLoaderClient* client)
index fca606ec233ff34196de9dc0d415b39a82ad024b..ce813415d602780bca08f8cb7b12f5ea4328ba14 100644 (file)
@@ -214,10 +214,8 @@ public:
 
     virtual void performTask(ScriptExecutionContext *context)
     {
-        ASSERT_WITH_SECURITY_IMPLICATION(context->isWorkerGlobalScope());
-        WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(context);
         // It's not safe to call clearScript until all the cleanup tasks posted by functions initiated by WorkerThreadShutdownStartTask have completed.
-        workerGlobalScope->clearScript();
+        toWorkerGlobalScope(context)->clearScript();
     }
 
     virtual bool isCleanupTask() const { return true; }
@@ -232,8 +230,7 @@ public:
 
     virtual void performTask(ScriptExecutionContext *context)
     {
-        ASSERT_WITH_SECURITY_IMPLICATION(context->isWorkerGlobalScope());
-        WorkerGlobalScope* workerGlobalScope = static_cast<WorkerGlobalScope*>(context);
+        WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context);
 
 #if ENABLE(SQL_DATABASE)
         // FIXME: Should we stop the databases as part of stopActiveDOMObjects() below?