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 <runtime/JSLock.h> 39 40#if ENABLE(WORKERS) 41#include "JSWorkerContext.h" 42#include "WorkerContext.h" 43#include "WorkerThread.h" 44#endif 45 46using namespace JSC; 47 48namespace WebCore { 49 50PassOwnPtr<ScheduledAction> ScheduledAction::create(ExecState* exec, DOMWrapperWorld* isolatedWorld, ContentSecurityPolicy* policy) 51{ 52 JSValue v = exec->argument(0); 53 CallData callData; 54 if (getCallData(v, callData) == CallTypeNone) { 55 if (policy && !policy->allowEval()) 56 return 0; 57 UString string = v.toString(exec); 58 if (exec->hadException()) 59 return 0; 60 return new ScheduledAction(ustringToString(string), isolatedWorld); 61 } 62 63 return new ScheduledAction(exec, v, isolatedWorld); 64} 65 66ScheduledAction::ScheduledAction(ExecState* exec, JSValue function, DOMWrapperWorld* isolatedWorld) 67 : m_function(exec->globalData(), function) 68 , m_isolatedWorld(isolatedWorld) 69{ 70 // setTimeout(function, interval, arg0, arg1...). 71 // Start at 2 to skip function and interval. 72 for (size_t i = 2; i < exec->argumentCount(); ++i) 73 m_args.append(Strong<JSC::Unknown>(exec->globalData(), exec->argument(i))); 74} 75 76void ScheduledAction::execute(ScriptExecutionContext* context) 77{ 78 if (context->isDocument()) 79 execute(static_cast<Document*>(context)); 80#if ENABLE(WORKERS) 81 else { 82 ASSERT(context->isWorkerContext()); 83 execute(static_cast<WorkerContext*>(context)); 84 } 85#else 86 ASSERT(context->isDocument()); 87#endif 88} 89 90void ScheduledAction::executeFunctionInContext(JSGlobalObject* globalObject, JSValue thisValue, ScriptExecutionContext* context) 91{ 92 ASSERT(m_function); 93 JSLock lock(SilenceAssertionsOnly); 94 95 CallData callData; 96 CallType callType = getCallData(m_function.get(), callData); 97 if (callType == CallTypeNone) 98 return; 99 100 ExecState* exec = globalObject->globalExec(); 101 102 MarkedArgumentBuffer args; 103 size_t size = m_args.size(); 104 for (size_t i = 0; i < size; ++i) 105 args.append(m_args[i].get()); 106 107 globalObject->globalData().timeoutChecker.start(); 108 if (context->isDocument()) 109 JSMainThreadExecState::call(exec, m_function.get(), callType, callData, thisValue, args); 110 else 111 JSC::call(exec, m_function.get(), callType, callData, thisValue, args); 112 globalObject->globalData().timeoutChecker.stop(); 113 114 if (exec->hadException()) 115 reportCurrentException(exec); 116} 117 118void ScheduledAction::execute(Document* document) 119{ 120 JSDOMWindow* window = toJSDOMWindow(document->frame(), m_isolatedWorld.get()); 121 if (!window) 122 return; 123 124 RefPtr<Frame> frame = window->impl()->frame(); 125 if (!frame || !frame->script()->canExecuteScripts(AboutToExecuteScript)) 126 return; 127 128 frame->script()->setProcessingTimerCallback(true); 129 130 if (m_function) { 131 executeFunctionInContext(window, window->shell(), document); 132 Document::updateStyleForAllDocuments(); 133 } else 134 frame->script()->executeScriptInWorld(m_isolatedWorld.get(), m_code); 135 136 frame->script()->setProcessingTimerCallback(false); 137} 138 139#if ENABLE(WORKERS) 140void ScheduledAction::execute(WorkerContext* workerContext) 141{ 142 // In a Worker, the execution should always happen on a worker thread. 143 ASSERT(workerContext->thread()->threadID() == currentThread()); 144 145 WorkerScriptController* scriptController = workerContext->script(); 146 147 if (m_function) { 148 JSWorkerContext* contextWrapper = scriptController->workerContextWrapper(); 149 executeFunctionInContext(contextWrapper, contextWrapper, workerContext); 150 } else { 151 ScriptSourceCode code(m_code, workerContext->url()); 152 scriptController->evaluate(code); 153 } 154} 155#endif // ENABLE(WORKERS) 156 157} // namespace WebCore 158