Skip to content

Commit

Permalink
ReasonForCallingCanExecuteScripts should be an enum class
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=256417

Reviewed by Alan Baradlay.

* Source/WebCore/bindings/js/JSEventListener.cpp:
(WebCore::JSEventListener::handleEvent):
* Source/WebCore/bindings/js/JSLazyEventListener.cpp:
(WebCore::JSLazyEventListener::initializeJSFunction const):
(WebCore::JSLazyEventListener::create):
* Source/WebCore/bindings/js/ScheduledAction.cpp:
(WebCore::ScheduledAction::execute):
* Source/WebCore/bindings/js/ScriptController.cpp:
(WebCore::ScriptController::cacheableBindingRootObject):
(WebCore::ScriptController::bindingRootObject):
(WebCore::ScriptController::jsObjectForPluginElement):
(WebCore::ScriptController::executeScriptInWorld):
(WebCore::ScriptController::canExecuteScripts):
* Source/WebCore/bindings/js/ScriptController.h:
* Source/WebCore/bindings/js/ScriptControllerMac.mm:
(WebCore::ScriptController::windowScriptObject):
(WebCore::ScriptController::javaScriptContext):
* Source/WebCore/dom/ScriptElement.cpp:
(WebCore::ScriptElement::prepareScript):
* Source/WebCore/editing/MarkupAccumulator.cpp:
(WebCore::isScriptEnabled):
* Source/WebCore/html/HTMLCanvasElement.cpp:
(WebCore::HTMLCanvasElement::createElementRenderer):
* Source/WebCore/html/HTMLElement.cpp:
(WebCore::HTMLElement::rendererIsEverNeeded):
* Source/WebCore/html/HTMLIFrameElement.cpp:
(WebCore::isFrameLazyLoadable):
* Source/WebCore/html/HTMLImageElement.cpp:
(WebCore::HTMLImageElement::isLazyLoadable const):
* Source/WebCore/html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::controls const):
* Source/WebCore/html/parser/HTMLParserOptions.cpp:
(WebCore::HTMLParserOptions::HTMLParserOptions):
* Source/WebCore/html/parser/HTMLParserScheduler.cpp:
(WebCore::HTMLParserScheduler::shouldYieldBeforeExecutingScript):
* Source/WebCore/inspector/PageDebugger.cpp:
(WebCore::PageDebugger::setJavaScriptPaused):
* Source/WebCore/inspector/agents/InspectorDOMAgent.cpp:
(WebCore::InspectorDOMAgent::buildObjectForEventListener):
* Source/WebCore/inspector/agents/page/PageRuntimeAgent.cpp:
(WebCore::PageRuntimeAgent::reportExecutionContextCreation):
* Source/WebCore/loader/FrameLoader.cpp:
(WebCore::FrameLoader::willRestoreFromCachedPage):
(WebCore::FrameLoader::dispatchDidClearWindowObjectsInAllWorlds):
(WebCore::FrameLoader::dispatchDidClearWindowObjectInWorld):

Canonical link: https://commits.webkit.org/263764@main
  • Loading branch information
rniwa committed May 6, 2023
1 parent 126453e commit e40c62f
Show file tree
Hide file tree
Showing 19 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Source/WebCore/bindings/js/JSEventListener.cpp
Expand Up @@ -161,7 +161,7 @@ void JSEventListener::handleEvent(ScriptExecutionContext& scriptExecutionContext
}
// FIXME: Is this check needed for other contexts?
auto& script = window->wrapped().frame()->script();
if (!script.canExecuteScripts(AboutToExecuteScript) || script.isPaused())
if (!script.canExecuteScripts(ReasonForCallingCanExecuteScripts::AboutToExecuteScript) || script.isPaused())
return;
}

Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/bindings/js/JSLazyEventListener.cpp
Expand Up @@ -129,7 +129,7 @@ JSObject* JSLazyEventListener::initializeJSFunction(ScriptExecutionContext& exec
return nullptr;

auto& script = document.frame()->script();
if (!script.canExecuteScripts(AboutToCreateEventListener) || script.isPaused())
if (!script.canExecuteScripts(ReasonForCallingCanExecuteScripts::AboutToCreateEventListener) || script.isPaused())
return nullptr;

ASSERT_WITH_MESSAGE(document.settings().scriptMarkupEnabled(), "Scripting element attributes should have been stripped during parsing");
Expand Down Expand Up @@ -197,7 +197,7 @@ RefPtr<JSLazyEventListener> JSLazyEventListener::create(CreationArguments&& argu
TextPosition position;
URL sourceURL;
if (auto* frame = arguments.document.frame()) {
if (!frame->script().canExecuteScripts(AboutToCreateEventListener))
if (!frame->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::AboutToCreateEventListener))
return nullptr;
position = frame->script().eventHandlerPosition();
sourceURL = arguments.document.url();
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/bindings/js/ScheduledAction.cpp
Expand Up @@ -132,7 +132,7 @@ void ScheduledAction::execute(Document& document)
return;

RefPtr frame = window->wrapped().frame();
if (!frame || !frame->script().canExecuteScripts(AboutToExecuteScript))
if (!frame || !frame->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::AboutToExecuteScript))
return;

if (m_function)
Expand Down
12 changes: 6 additions & 6 deletions Source/WebCore/bindings/js/ScriptController.cpp
Expand Up @@ -464,7 +464,7 @@ void ScriptController::updateDocument()

Bindings::RootObject* ScriptController::cacheableBindingRootObject()
{
if (!canExecuteScripts(NotAboutToExecuteScript))
if (!canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return nullptr;

if (!m_cacheableBindingRootObject) {
Expand All @@ -476,7 +476,7 @@ Bindings::RootObject* ScriptController::cacheableBindingRootObject()

Bindings::RootObject* ScriptController::bindingRootObject()
{
if (!canExecuteScripts(NotAboutToExecuteScript))
if (!canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return nullptr;

if (!m_bindingRootObject) {
Expand Down Expand Up @@ -517,7 +517,7 @@ RefPtr<JSC::Bindings::Instance> ScriptController::createScriptInstanceForWidget(
JSObject* ScriptController::jsObjectForPluginElement(HTMLPlugInElement* plugin)
{
// Can't create JSObjects when JavaScript is disabled
if (!canExecuteScripts(NotAboutToExecuteScript))
if (!canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return nullptr;

JSLockHolder lock(commonVM());
Expand Down Expand Up @@ -594,7 +594,7 @@ ValueOrException ScriptController::executeScriptInWorld(DOMWrapperWorld& world,

UserGestureIndicator gestureIndicator(parameters.forceUserGesture == ForceUserGesture::Yes ? std::optional<ProcessingUserGestureState>(ProcessingUserGesture) : std::nullopt, m_frame.document());

if (!canExecuteScripts(AboutToExecuteScript) || isPaused())
if (!canExecuteScripts(ReasonForCallingCanExecuteScripts::AboutToExecuteScript) || isPaused())
return makeUnexpected(ExceptionDetails { "Cannot execute JavaScript in this document"_s });

auto sourceURL = parameters.sourceURL;
Expand Down Expand Up @@ -773,12 +773,12 @@ void ScriptController::executeAsynchronousUserAgentScriptInWorld(DOMWrapperWorld

bool ScriptController::canExecuteScripts(ReasonForCallingCanExecuteScripts reason)
{
if (reason == AboutToExecuteScript)
if (reason == ReasonForCallingCanExecuteScripts::AboutToExecuteScript)
RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(ScriptDisallowedScope::InMainThread::isScriptAllowed() || !isInWebProcess());

if (m_frame.document() && m_frame.document()->isSandboxed(SandboxScripts)) {
// FIXME: This message should be moved off the console once a solution to https://bugs.webkit.org/show_bug.cgi?id=103274 exists.
if (reason == AboutToExecuteScript || reason == AboutToCreateEventListener)
if (reason == ReasonForCallingCanExecuteScripts::AboutToExecuteScript || reason == ReasonForCallingCanExecuteScripts::AboutToCreateEventListener)
m_frame.document()->addConsoleMessage(MessageSource::Security, MessageLevel::Error, "Blocked script execution in '" + m_frame.document()->url().stringCenterEllipsizedToLength() + "' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.");
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/bindings/js/ScriptController.h
Expand Up @@ -69,7 +69,7 @@ enum class RunAsAsyncFunction : bool;
struct ExceptionDetails;
struct RunJavaScriptParameters;

enum ReasonForCallingCanExecuteScripts {
enum class ReasonForCallingCanExecuteScripts : uint8_t {
AboutToCreateEventListener,
AboutToExecuteScript,
NotAboutToExecuteScript
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/bindings/js/ScriptControllerMac.mm
Expand Up @@ -75,7 +75,7 @@ - (id)objectForWebScript;

WebScriptObject *ScriptController::windowScriptObject()
{
if (!canExecuteScripts(NotAboutToExecuteScript))
if (!canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return nil;

if (!m_windowScriptObject) {
Expand All @@ -90,7 +90,7 @@ - (id)objectForWebScript;
JSContext *ScriptController::javaScriptContext()
{
#if JSC_OBJC_API_ENABLED
if (!canExecuteScripts(NotAboutToExecuteScript))
if (!canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return 0;
JSContext *context = [JSContext contextWithJSGlobalContextRef:toGlobalRef(bindingRootObject()->globalObject())];
return context;
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/dom/ScriptElement.cpp
Expand Up @@ -229,7 +229,7 @@ bool ScriptElement::prepareScript(const TextPosition& scriptStartPosition, Legac

m_preparationTimeDocumentIdentifier = document.identifier();

if (!document.frame()->script().canExecuteScripts(AboutToExecuteScript))
if (!document.frame()->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::AboutToExecuteScript))
return false;

if (scriptType == ScriptType::Classic && !isScriptForEventSupported())
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/editing/MarkupAccumulator.cpp
Expand Up @@ -366,7 +366,7 @@ void MarkupAccumulator::appendNamespace(StringBuilder& result, const AtomString&
static inline bool isScriptEnabled(Node& node)
{
RefPtr frame = node.document().frame();
return frame && frame->script().canExecuteScripts(NotAboutToExecuteScript);
return frame && frame->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript);
}

EntityMask MarkupAccumulator::entityMaskForText(const Text& text) const
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/html/HTMLCanvasElement.cpp
Expand Up @@ -178,7 +178,7 @@ void HTMLCanvasElement::attributeChanged(const QualifiedName& name, const AtomSt
RenderPtr<RenderElement> HTMLCanvasElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition& insertionPosition)
{
RefPtr frame { document().frame() };
if (frame && frame->script().canExecuteScripts(NotAboutToExecuteScript))
if (frame && frame->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return createRenderer<RenderHTMLCanvas>(*this, WTFMove(style));
return HTMLElement::createElementRenderer(WTFMove(style), insertionPosition);
}
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/html/HTMLElement.cpp
Expand Up @@ -781,7 +781,7 @@ bool HTMLElement::rendererIsEverNeeded()
{
if (hasTagName(noscriptTag)) {
RefPtr frame { document().frame() };
if (frame && frame->script().canExecuteScripts(NotAboutToExecuteScript))
if (frame && frame->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return false;
} else if (hasTagName(noembedTag)) {
RefPtr frame { document().frame() };
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/html/HTMLIFrameElement.cpp
Expand Up @@ -195,7 +195,7 @@ static bool isFrameLazyLoadable(const Document& document, const URL& url, const
if (!url.isValid() || url.isAboutBlank())
return false;

if (!document.frame() || !document.frame()->script().canExecuteScripts(NotAboutToExecuteScript))
if (!document.frame() || !document.frame()->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return false;

return equalLettersIgnoringASCIICase(loadingAttributeValue, "lazy"_s);
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/html/HTMLImageElement.cpp
Expand Up @@ -996,7 +996,7 @@ bool HTMLImageElement::isDeferred() const

bool HTMLImageElement::isLazyLoadable() const
{
if (!document().frame() || !document().frame()->script().canExecuteScripts(NotAboutToExecuteScript))
if (!document().frame() || !document().frame()->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return false;
return hasLazyLoadableAttributeValue(attributeWithoutSynchronization(HTMLNames::loadingAttr));
}
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/html/HTMLMediaElement.cpp
Expand Up @@ -4164,7 +4164,7 @@ bool HTMLMediaElement::controls() const
RefPtr frame = document().frame();

// always show controls when scripting is disabled
if (frame && !frame->script().canExecuteScripts(NotAboutToExecuteScript))
if (frame && !frame->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return true;

return hasAttributeWithoutSynchronization(controlsAttr);
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/html/parser/HTMLParserOptions.cpp
Expand Up @@ -49,7 +49,7 @@ HTMLParserOptions::HTMLParserOptions(Document& document)
if (document.settings().htmlParserScriptingFlagPolicy() == HTMLParserScriptingFlagPolicy::Enabled)
scriptingFlag = true;
else
scriptingFlag = frame && frame->script().canExecuteScripts(NotAboutToExecuteScript);
scriptingFlag = frame && frame->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript);

usePreHTML5ParserQuirks = document.settings().usePreHTML5ParserQuirks();
maximumDOMTreeDepth = document.settings().maximumHTMLParserDOMTreeDepth();
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/html/parser/HTMLParserScheduler.cpp
Expand Up @@ -122,7 +122,7 @@ bool HTMLParserScheduler::shouldYieldBeforeExecutingScript(const ScriptElement*
if (!document->body())
return false;

if (!document->frame() || !document->frame()->script().canExecuteScripts(NotAboutToExecuteScript))
if (!document->frame() || !document->frame()->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return false;

if (!document->haveStylesheetsLoaded())
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/inspector/PageDebugger.cpp
Expand Up @@ -166,7 +166,7 @@ void PageDebugger::setJavaScriptPaused(const PageGroup& pageGroup, bool paused)

void PageDebugger::setJavaScriptPaused(LocalFrame& frame, bool paused)
{
if (!frame.script().canExecuteScripts(NotAboutToExecuteScript))
if (!frame.script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return;

frame.script().setPaused(paused);
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/inspector/agents/InspectorDOMAgent.cpp
Expand Up @@ -2097,7 +2097,7 @@ Ref<Protocol::DOM::EventListener> InspectorDOMAgent::buildObjectForEventListener
handlerObject = scriptListener->ensureJSFunction(*document);
if (auto frame = document->frame()) {
// FIXME: Why do we need the canExecuteScripts check here?
if (frame->script().canExecuteScripts(NotAboutToExecuteScript))
if (frame->script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
globalObject = frame->script().globalObject(*scriptListener->isolatedWorld());
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/inspector/agents/page/PageRuntimeAgent.cpp
Expand Up @@ -139,7 +139,7 @@ void PageRuntimeAgent::reportExecutionContextCreation()
return;

m_inspectedPage.forEachFrame([&](LocalFrame& frame) {
if (!frame.script().canExecuteScripts(NotAboutToExecuteScript))
if (!frame.script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return;

auto frameId = pageAgent->frameId(&frame);
Expand Down
6 changes: 3 additions & 3 deletions Source/WebCore/loader/FrameLoader.cpp
Expand Up @@ -2401,7 +2401,7 @@ void FrameLoader::willRestoreFromCachedPage()
closeURL();

// Delete old status bar messages (if it _was_ activated on last URL).
if (m_frame.script().canExecuteScripts(NotAboutToExecuteScript)) {
if (m_frame.script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript)) {
auto* window = m_frame.document()->domWindow();
window->setStatus(String());
window->setDefaultStatus(String());
Expand Down Expand Up @@ -4198,7 +4198,7 @@ String FrameLoader::referrer() const

void FrameLoader::dispatchDidClearWindowObjectsInAllWorlds()
{
if (!m_frame.script().canExecuteScripts(NotAboutToExecuteScript))
if (!m_frame.script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript))
return;

Vector<Ref<DOMWrapperWorld>> worlds;
Expand All @@ -4209,7 +4209,7 @@ void FrameLoader::dispatchDidClearWindowObjectsInAllWorlds()

void FrameLoader::dispatchDidClearWindowObjectInWorld(DOMWrapperWorld& world)
{
if (!m_frame.script().canExecuteScripts(NotAboutToExecuteScript) || !m_frame.windowProxy().existingJSWindowProxy(world))
if (!m_frame.script().canExecuteScripts(ReasonForCallingCanExecuteScripts::NotAboutToExecuteScript) || !m_frame.windowProxy().existingJSWindowProxy(world))
return;

m_client->dispatchDidClearWindowObjectInWorld(world);
Expand Down

0 comments on commit e40c62f

Please sign in to comment.