ScheduledAction.cpp [plain text]
#include "config.h"
#include "ScheduledAction.h"
#include "ContentSecurityPolicy.h"
#include "DOMWindow.h"
#include "Document.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "JSDOMBinding.h"
#include "JSDOMWindow.h"
#include "JSMainThreadExecState.h"
#include "ScriptController.h"
#include "ScriptExecutionContext.h"
#include "ScriptSourceCode.h"
#include "ScriptValue.h"
#include <runtime/JSLock.h>
#if ENABLE(WORKERS)
#include "JSWorkerContext.h"
#include "WorkerContext.h"
#include "WorkerThread.h"
#endif
using namespace JSC;
namespace WebCore {
PassOwnPtr<ScheduledAction> ScheduledAction::create(ExecState* exec, DOMWrapperWorld* isolatedWorld, ContentSecurityPolicy* policy)
{
JSValue v = exec->argument(0);
CallData callData;
if (getCallData(v, callData) == CallTypeNone) {
if (policy && !policy->allowEval())
return nullptr;
UString string = v.toString(exec)->value(exec);
if (exec->hadException())
return nullptr;
return adoptPtr(new ScheduledAction(ustringToString(string), isolatedWorld));
}
return adoptPtr(new ScheduledAction(exec, v, isolatedWorld));
}
ScheduledAction::ScheduledAction(ExecState* exec, JSValue function, DOMWrapperWorld* isolatedWorld)
: m_function(exec->globalData(), function)
, m_isolatedWorld(isolatedWorld)
{
for (size_t i = 2; i < exec->argumentCount(); ++i)
m_args.append(Strong<JSC::Unknown>(exec->globalData(), exec->argument(i)));
}
void ScheduledAction::execute(ScriptExecutionContext* context)
{
if (context->isDocument())
execute(static_cast<Document*>(context));
#if ENABLE(WORKERS)
else {
ASSERT(context->isWorkerContext());
execute(static_cast<WorkerContext*>(context));
}
#else
ASSERT(context->isDocument());
#endif
}
void ScheduledAction::executeFunctionInContext(JSGlobalObject* globalObject, JSValue thisValue, ScriptExecutionContext* context)
{
ASSERT(m_function);
JSLockHolder lock(context->globalData());
CallData callData;
CallType callType = getCallData(m_function.get(), callData);
if (callType == CallTypeNone)
return;
ExecState* exec = globalObject->globalExec();
MarkedArgumentBuffer args;
size_t size = m_args.size();
for (size_t i = 0; i < size; ++i)
args.append(m_args[i].get());
globalObject->globalData().timeoutChecker.start();
InspectorInstrumentationCookie cookie = JSMainThreadExecState::instrumentFunctionCall(context, callType, callData);
if (context->isDocument())
JSMainThreadExecState::call(exec, m_function.get(), callType, callData, thisValue, args);
else
JSC::call(exec, m_function.get(), callType, callData, thisValue, args);
InspectorInstrumentation::didCallFunction(cookie);
globalObject->globalData().timeoutChecker.stop();
if (exec->hadException())
reportCurrentException(exec);
}
void ScheduledAction::execute(Document* document)
{
JSDOMWindow* window = toJSDOMWindow(document->frame(), m_isolatedWorld.get());
if (!window)
return;
RefPtr<Frame> frame = window->impl()->frame();
if (!frame || !frame->script()->canExecuteScripts(AboutToExecuteScript))
return;
if (m_function)
executeFunctionInContext(window, window->shell(), document);
else
frame->script()->executeScriptInWorld(m_isolatedWorld.get(), m_code);
}
#if ENABLE(WORKERS)
void ScheduledAction::execute(WorkerContext* workerContext)
{
ASSERT(workerContext->thread()->threadID() == currentThread());
WorkerScriptController* scriptController = workerContext->script();
if (m_function) {
JSWorkerContext* contextWrapper = scriptController->workerContextWrapper();
executeFunctionInContext(contextWrapper, contextWrapper, workerContext);
} else {
ScriptSourceCode code(m_code, workerContext->url());
scriptController->evaluate(code);
}
}
#endif // ENABLE(WORKERS)
}