1/* 2 * Copyright (C) 2000 Harri Porten (porten@kde.org) 3 * Copyright (C) 2006 Jon Shier (jshier@iastate.edu) 4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reseved. 5 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org) 6 * Copyright (C) 2009 Google Inc. All rights reseved. 7 * 8 * This library is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU Lesser General Public 10 * License as published by the Free Software Foundation; either 11 * version 2 of the License, or (at your option) any later version. 12 * 13 * This library is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * Lesser General Public License for more details. 17 * 18 * You should have received a copy of the GNU Lesser General Public 19 * License along with this library; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 21 * USA 22 */ 23 24#include "config.h" 25#include "ScheduledAction.h" 26 27#include "ContentSecurityPolicy.h" 28#include "DOMWindow.h" 29#include "Document.h" 30#include "Frame.h" 31#include "FrameLoader.h" 32#include "JSDOMBinding.h" 33#include "JSDOMWindow.h" 34#include "JSMainThreadExecState.h" 35#include "ScriptController.h" 36#include "ScriptExecutionContext.h" 37#include "ScriptSourceCode.h" 38#include "ScriptValue.h" 39#include <runtime/JSLock.h> 40 41#if ENABLE(WORKERS) 42#include "JSWorkerContext.h" 43#include "WorkerContext.h" 44#include "WorkerThread.h" 45#endif 46 47using namespace JSC; 48 49namespace WebCore { 50 51PassOwnPtr<ScheduledAction> ScheduledAction::create(ExecState* exec, DOMWrapperWorld* isolatedWorld, ContentSecurityPolicy* policy) 52{ 53 JSValue v = exec->argument(0); 54 CallData callData; 55 if (getCallData(v, callData) == CallTypeNone) { 56 if (policy && !policy->allowEval(exec)) 57 return nullptr; 58 String string = v.toString(exec)->value(exec); 59 if (exec->hadException()) 60 return nullptr; 61 return adoptPtr(new ScheduledAction(string, isolatedWorld)); 62 } 63 64 return adoptPtr(new ScheduledAction(exec, v, isolatedWorld)); 65} 66 67ScheduledAction::ScheduledAction(ExecState* exec, JSValue function, DOMWrapperWorld* isolatedWorld) 68 : m_function(exec->vm(), function) 69 , m_isolatedWorld(isolatedWorld) 70{ 71 // setTimeout(function, interval, arg0, arg1...). 72 // Start at 2 to skip function and interval. 73 for (size_t i = 2; i < exec->argumentCount(); ++i) 74 m_args.append(Strong<JSC::Unknown>(exec->vm(), exec->argument(i))); 75} 76 77void ScheduledAction::execute(ScriptExecutionContext* context) 78{ 79 if (context->isDocument()) 80 execute(toDocument(context)); 81#if ENABLE(WORKERS) 82 else { 83 ASSERT(context->isWorkerContext()); 84 execute(static_cast<WorkerContext*>(context)); 85 } 86#else 87 ASSERT(context->isDocument()); 88#endif 89} 90 91void ScheduledAction::executeFunctionInContext(JSGlobalObject* globalObject, JSValue thisValue, ScriptExecutionContext* context) 92{ 93 ASSERT(m_function); 94 JSLockHolder lock(context->vm()); 95 96 CallData callData; 97 CallType callType = getCallData(m_function.get(), callData); 98 if (callType == CallTypeNone) 99 return; 100 101 ExecState* exec = globalObject->globalExec(); 102 103 MarkedArgumentBuffer args; 104 size_t size = m_args.size(); 105 for (size_t i = 0; i < size; ++i) 106 args.append(m_args[i].get()); 107 108 InspectorInstrumentationCookie cookie = JSMainThreadExecState::instrumentFunctionCall(context, callType, callData); 109 110 if (context->isDocument()) 111 JSMainThreadExecState::call(exec, m_function.get(), callType, callData, thisValue, args); 112 else 113 JSC::call(exec, m_function.get(), callType, callData, thisValue, args); 114 115 InspectorInstrumentation::didCallFunction(cookie); 116 117 if (exec->hadException()) 118 reportCurrentException(exec); 119} 120 121void ScheduledAction::execute(Document* document) 122{ 123 JSDOMWindow* window = toJSDOMWindow(document->frame(), m_isolatedWorld.get()); 124 if (!window) 125 return; 126 127 RefPtr<Frame> frame = window->impl()->frame(); 128 if (!frame || !frame->script()->canExecuteScripts(AboutToExecuteScript)) 129 return; 130 131 if (m_function) 132 executeFunctionInContext(window, window->shell(), document); 133 else 134 frame->script()->executeScriptInWorld(m_isolatedWorld.get(), m_code); 135} 136 137#if ENABLE(WORKERS) 138void ScheduledAction::execute(WorkerContext* workerContext) 139{ 140 // In a Worker, the execution should always happen on a worker thread. 141 ASSERT(workerContext->thread()->threadID() == currentThread()); 142 143 WorkerScriptController* scriptController = workerContext->script(); 144 145 if (m_function) { 146 JSWorkerContext* contextWrapper = scriptController->workerContextWrapper(); 147 executeFunctionInContext(contextWrapper, contextWrapper, workerContext); 148 } else { 149 ScriptSourceCode code(m_code, workerContext->url()); 150 scriptController->evaluate(code); 151 } 152} 153#endif // ENABLE(WORKERS) 154 155} // namespace WebCore 156