2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "bindings/core/v8/V8WorkerGlobalScope.h"
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "bindings/core/v8/ScheduledAction.h"
36 #include "bindings/core/v8/V8Binding.h"
37 #include "bindings/core/v8/V8WorkerGlobalScopeEventListener.h"
38 #include "bindings/core/v8/WorkerScriptController.h"
39 #include "core/frame/DOMTimer.h"
40 #include "core/frame/DOMWindowTimers.h"
41 #include "core/frame/csp/ContentSecurityPolicy.h"
42 #include "core/inspector/ScriptCallStack.h"
43 #include "core/workers/WorkerGlobalScope.h"
44 #include "wtf/OwnPtr.h"
48 static void setTimeoutOrInterval(const v8::FunctionCallbackInfo<v8::Value>& info, bool singleShot)
50 WorkerGlobalScope* workerGlobalScope = V8WorkerGlobalScope::toImpl(info.Holder());
51 ASSERT(workerGlobalScope);
53 int argumentCount = info.Length();
54 if (argumentCount < 1)
57 v8::Handle<v8::Value> function = info[0];
59 WorkerScriptController* script = workerGlobalScope->script();
63 ScriptState* scriptState = ScriptState::current(info.GetIsolate());
64 OwnPtr<ScheduledAction> action;
65 if (function->IsString()) {
66 if (ContentSecurityPolicy* policy = workerGlobalScope->contentSecurityPolicy()) {
67 if (!policy->allowEval()) {
68 v8SetReturnValue(info, 0);
72 action = adoptPtr(new ScheduledAction(scriptState, toCoreString(function.As<v8::String>()), KURL(), info.GetIsolate()));
73 } else if (function->IsFunction()) {
74 size_t paramCount = argumentCount >= 2 ? argumentCount - 2 : 0;
75 OwnPtr<v8::Local<v8::Value>[]> params;
77 params = adoptArrayPtr(new v8::Local<v8::Value>[paramCount]);
78 for (size_t i = 0; i < paramCount; ++i)
79 params[i] = info[i+2];
81 // ScheduledAction takes ownership of actual params and releases them in its destructor.
82 action = adoptPtr(new ScheduledAction(scriptState, v8::Handle<v8::Function>::Cast(function), paramCount, params.get(), info.GetIsolate()));
87 int32_t timeout = argumentCount >= 2 ? info[1]->Int32Value() : 0;
90 timerId = DOMWindowTimers::setTimeout(*workerGlobalScope, action.release(), timeout);
92 timerId = DOMWindowTimers::setInterval(*workerGlobalScope, action.release(), timeout);
94 v8SetReturnValue(info, timerId);
97 void V8WorkerGlobalScope::setTimeoutMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
99 return setTimeoutOrInterval(info, true);
102 void V8WorkerGlobalScope::setIntervalMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
104 return setTimeoutOrInterval(info, false);
107 v8::Handle<v8::Value> toV8(WorkerGlobalScope* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
109 // Notice that we explicitly ignore creationContext because the WorkerGlobalScope is its own creationContext.
112 return v8::Null(isolate);
114 WorkerScriptController* script = impl->script();
116 return v8::Null(isolate);
118 v8::Handle<v8::Object> global = script->context()->Global();
119 ASSERT(!global.IsEmpty());