weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2000 Harri Porten (porten@kde.org) |
| 3 | * Copyright (C) 2006 Jon Shier (jshier@iastate.edu) |
weinig@apple.com | 6da3b70 | 2009-04-28 20:30:52 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reseved. |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 5 | * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org) |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009 Google Inc. All rights reseved. |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 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 | |
abarth@webkit.org | a51b999 | 2011-04-16 02:33:08 +0000 | [diff] [blame] | 27 | #include "ContentSecurityPolicy.h" |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 28 | #include "DOMWindow.h" |
| 29 | #include "Document.h" |
| 30 | #include "Frame.h" |
| 31 | #include "FrameLoader.h" |
ap@webkit.org | e9b6b59 | 2008-11-19 17:42:39 +0000 | [diff] [blame] | 32 | #include "JSDOMBinding.h" |
weinig@apple.com | 5f38e1a | 2008-03-04 03:08:30 +0000 | [diff] [blame] | 33 | #include "JSDOMWindow.h" |
yaar@chromium.org | 37eb477 | 2010-05-20 21:56:45 +0000 | [diff] [blame] | 34 | #include "JSMainThreadExecState.h" |
akling@apple.com | 4d3b989 | 2013-09-06 22:08:14 +0000 | [diff] [blame] | 35 | #include "JSMainThreadExecStateInstrumentation.h" |
commit-queue@webkit.org | f0eff20 | 2013-11-22 05:01:46 +0000 | [diff] [blame] | 36 | #include "JSWorkerGlobalScope.h" |
darin@apple.com | 92aaa2a | 2008-06-15 07:00:11 +0000 | [diff] [blame] | 37 | #include "ScriptController.h" |
ap@webkit.org | 3b2f2c3 | 2008-12-28 10:05:59 +0000 | [diff] [blame] | 38 | #include "ScriptExecutionContext.h" |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 39 | #include "ScriptSourceCode.h" |
weinig@apple.com | 771c7a2 | 2011-05-01 23:56:41 +0000 | [diff] [blame] | 40 | #include "ScriptValue.h" |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 41 | #include "WorkerGlobalScope.h" |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 42 | #include "WorkerThread.h" |
commit-queue@webkit.org | f0eff20 | 2013-11-22 05:01:46 +0000 | [diff] [blame] | 43 | #include <runtime/JSLock.h> |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 44 | |
cwzwarich@webkit.org | 3f782f6 | 2008-09-08 01:28:33 +0000 | [diff] [blame] | 45 | using namespace JSC; |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 46 | |
| 47 | namespace WebCore { |
| 48 | |
weinig@apple.com | 8f71603 | 2013-10-02 17:03:09 +0000 | [diff] [blame] | 49 | PassOwnPtr<ScheduledAction> ScheduledAction::create(ExecState* exec, DOMWrapperWorld& isolatedWorld, ContentSecurityPolicy* policy) |
weinig@apple.com | 6da3b70 | 2009-04-28 20:30:52 +0000 | [diff] [blame] | 50 | { |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 51 | JSValue v = exec->argument(0); |
weinig@apple.com | 6da3b70 | 2009-04-28 20:30:52 +0000 | [diff] [blame] | 52 | CallData callData; |
barraclough@apple.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 53 | if (getCallData(v, callData) == CallTypeNone) { |
mkwst@chromium.org | 109f7e7 | 2012-10-02 11:29:33 +0000 | [diff] [blame] | 54 | if (policy && !policy->allowEval(exec)) |
aroben@apple.com | 71e211b | 2011-05-03 13:54:58 +0000 | [diff] [blame] | 55 | return nullptr; |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 56 | String string = v.toString(exec)->value(exec); |
weinig@apple.com | bcd1883 | 2009-04-30 17:16:17 +0000 | [diff] [blame] | 57 | if (exec->hadException()) |
aroben@apple.com | 71e211b | 2011-05-03 13:54:58 +0000 | [diff] [blame] | 58 | return nullptr; |
benjamin@webkit.org | cff06e4 | 2012-08-30 21:23:51 +0000 | [diff] [blame] | 59 | return adoptPtr(new ScheduledAction(string, isolatedWorld)); |
weinig@apple.com | bcd1883 | 2009-04-30 17:16:17 +0000 | [diff] [blame] | 60 | } |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 61 | |
levin@chromium.org | 38484e0 | 2011-04-26 07:31:19 +0000 | [diff] [blame] | 62 | return adoptPtr(new ScheduledAction(exec, v, isolatedWorld)); |
weinig@apple.com | 6da3b70 | 2009-04-28 20:30:52 +0000 | [diff] [blame] | 63 | } |
| 64 | |
weinig@apple.com | 8f71603 | 2013-10-02 17:03:09 +0000 | [diff] [blame] | 65 | ScheduledAction::ScheduledAction(ExecState* exec, JSValue function, DOMWrapperWorld& isolatedWorld) |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 66 | : m_function(exec->vm(), function) |
weinig@apple.com | 8f71603 | 2013-10-02 17:03:09 +0000 | [diff] [blame] | 67 | , m_isolatedWorld(&isolatedWorld) |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 68 | { |
ggaren@apple.com | fea29f1 | 2010-05-29 06:33:05 +0000 | [diff] [blame] | 69 | // setTimeout(function, interval, arg0, arg1...). |
| 70 | // Start at 2 to skip function and interval. |
| 71 | for (size_t i = 2; i < exec->argumentCount(); ++i) |
darin@apple.com | d9b2213 | 2013-09-22 04:02:59 +0000 | [diff] [blame] | 72 | m_args.append(Strong<JSC::Unknown>(exec->vm(), exec->uncheckedArgument(i))); |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 73 | } |
| 74 | |
ap@webkit.org | 3b2f2c3 | 2008-12-28 10:05:59 +0000 | [diff] [blame] | 75 | void ScheduledAction::execute(ScriptExecutionContext* context) |
| 76 | { |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 77 | if (context->isDocument()) |
inferno@chromium.org | 8da49f1 | 2013-03-13 20:29:08 +0000 | [diff] [blame] | 78 | execute(toDocument(context)); |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 79 | else { |
akling@apple.com | 670fea1 | 2013-10-12 18:16:42 +0000 | [diff] [blame] | 80 | ASSERT_WITH_SECURITY_IMPLICATION(context->isWorkerGlobalScope()); |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 81 | execute(static_cast<WorkerGlobalScope*>(context)); |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 82 | } |
ap@webkit.org | 3b2f2c3 | 2008-12-28 10:05:59 +0000 | [diff] [blame] | 83 | } |
| 84 | |
yaar@chromium.org | 37eb477 | 2010-05-20 21:56:45 +0000 | [diff] [blame] | 85 | void ScheduledAction::executeFunctionInContext(JSGlobalObject* globalObject, JSValue thisValue, ScriptExecutionContext* context) |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 86 | { |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 87 | ASSERT(m_function); |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 88 | JSLockHolder lock(context->vm()); |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 89 | |
| 90 | CallData callData; |
barraclough@apple.com | 99ff343 | 2010-06-03 20:00:18 +0000 | [diff] [blame] | 91 | CallType callType = getCallData(m_function.get(), callData); |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 92 | if (callType == CallTypeNone) |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 93 | return; |
| 94 | |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 95 | ExecState* exec = globalObject->globalExec(); |
| 96 | |
oliver@apple.com | f32186e | 2009-04-30 01:21:52 +0000 | [diff] [blame] | 97 | MarkedArgumentBuffer args; |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 98 | size_t size = m_args.size(); |
| 99 | for (size_t i = 0; i < size; ++i) |
oliver@apple.com | 8d85705 | 2011-02-15 23:54:06 +0000 | [diff] [blame] | 100 | args.append(m_args[i].get()); |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 101 | |
timothy@apple.com | 7e72146 | 2012-05-11 23:40:47 +0000 | [diff] [blame] | 102 | InspectorInstrumentationCookie cookie = JSMainThreadExecState::instrumentFunctionCall(context, callType, callData); |
| 103 | |
yaar@chromium.org | 37eb477 | 2010-05-20 21:56:45 +0000 | [diff] [blame] | 104 | if (context->isDocument()) |
oliver@apple.com | 8d85705 | 2011-02-15 23:54:06 +0000 | [diff] [blame] | 105 | JSMainThreadExecState::call(exec, m_function.get(), callType, callData, thisValue, args); |
yaar@chromium.org | 37eb477 | 2010-05-20 21:56:45 +0000 | [diff] [blame] | 106 | else |
oliver@apple.com | 8d85705 | 2011-02-15 23:54:06 +0000 | [diff] [blame] | 107 | JSC::call(exec, m_function.get(), callType, callData, thisValue, args); |
timothy@apple.com | 7e72146 | 2012-05-11 23:40:47 +0000 | [diff] [blame] | 108 | |
| 109 | InspectorInstrumentation::didCallFunction(cookie); |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 110 | |
| 111 | if (exec->hadException()) |
| 112 | reportCurrentException(exec); |
| 113 | } |
| 114 | |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 115 | void ScheduledAction::execute(Document* document) |
| 116 | { |
weinig@apple.com | 8f71603 | 2013-10-02 17:03:09 +0000 | [diff] [blame] | 117 | JSDOMWindow* window = toJSDOMWindow(document->frame(), *m_isolatedWorld); |
ap@webkit.org | c1ff8e5 | 2009-01-11 08:06:58 +0000 | [diff] [blame] | 118 | if (!window) |
| 119 | return; |
| 120 | |
akling@apple.com | 51269d9 | 2013-10-10 12:01:51 +0000 | [diff] [blame] | 121 | RefPtr<Frame> frame = window->impl().frame(); |
psolanki@apple.com | c5b5dad | 2013-08-16 17:55:32 +0000 | [diff] [blame] | 122 | if (!frame || !frame->script().canExecuteScripts(AboutToExecuteScript)) |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 123 | return; |
| 124 | |
jamesr@google.com | 2b55f69 | 2012-01-26 22:24:10 +0000 | [diff] [blame] | 125 | if (m_function) |
yaar@chromium.org | 37eb477 | 2010-05-20 21:56:45 +0000 | [diff] [blame] | 126 | executeFunctionInContext(window, window->shell(), document); |
jamesr@google.com | 2b55f69 | 2012-01-26 22:24:10 +0000 | [diff] [blame] | 127 | else |
weinig@apple.com | 8f71603 | 2013-10-02 17:03:09 +0000 | [diff] [blame] | 128 | frame->script().executeScriptInWorld(*m_isolatedWorld, m_code); |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 129 | } |
| 130 | |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 131 | void ScheduledAction::execute(WorkerGlobalScope* workerGlobalScope) |
weinig@apple.com | 6da3b70 | 2009-04-28 20:30:52 +0000 | [diff] [blame] | 132 | { |
| 133 | // In a Worker, the execution should always happen on a worker thread. |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 134 | ASSERT(workerGlobalScope->thread()->threadID() == currentThread()); |
weinig@apple.com | 6da3b70 | 2009-04-28 20:30:52 +0000 | [diff] [blame] | 135 | |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 136 | WorkerScriptController* scriptController = workerGlobalScope->script(); |
weinig@apple.com | 6da3b70 | 2009-04-28 20:30:52 +0000 | [diff] [blame] | 137 | |
| 138 | if (m_function) { |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 139 | JSWorkerGlobalScope* contextWrapper = scriptController->workerGlobalScopeWrapper(); |
| 140 | executeFunctionInContext(contextWrapper, contextWrapper, workerGlobalScope); |
weinig@apple.com | 6da3b70 | 2009-04-28 20:30:52 +0000 | [diff] [blame] | 141 | } else { |
ch.dumez@sisa.samsung.com | 14792a6 | 2013-06-27 06:21:54 +0000 | [diff] [blame] | 142 | ScriptSourceCode code(m_code, workerGlobalScope->url()); |
weinig@apple.com | 6da3b70 | 2009-04-28 20:30:52 +0000 | [diff] [blame] | 143 | scriptController->evaluate(code); |
| 144 | } |
| 145 | } |
weinig@apple.com | 6da3b70 | 2009-04-28 20:30:52 +0000 | [diff] [blame] | 146 | |
weinig@apple.com | 007f830 | 2007-12-17 04:50:53 +0000 | [diff] [blame] | 147 | } // namespace WebCore |