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.
34
#include "V8WorkerContext.h"
36
#include "ContentSecurityPolicy.h"
38
#include "ExceptionCode.h"
39
#include "ScheduledAction.h"
40
#include "ScriptCallStack.h"
41
#include "V8Binding.h"
42
#include "V8Utilities.h"
43
#include "V8WorkerContextEventListener.h"
44
#include "WebSocket.h"
45
#include "WorkerContext.h"
46
#include "WorkerScriptController.h"
50
v8::Handle<v8::Value> SetTimeoutOrInterval(const v8::Arguments& args, bool singleShot)
52
WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder());
54
int argumentCount = args.Length();
55
if (argumentCount < 1)
56
return v8::Undefined();
58
v8::Handle<v8::Value> function = args[0];
59
int32_t timeout = argumentCount >= 2 ? args[1]->Int32Value() : 0;
62
WorkerScriptController* script = workerContext->script();
64
return v8::Undefined();
66
v8::Handle<v8::Context> v8Context = script->context();
67
if (function->IsString()) {
68
if (ContentSecurityPolicy* policy = workerContext->contentSecurityPolicy()) {
69
if (!policy->allowEval())
70
return v8Integer(0, args.GetIsolate());
72
WTF::String stringFunction = toWebCoreString(function);
73
timerId = DOMTimer::install(workerContext, adoptPtr(new ScheduledAction(v8Context, stringFunction, workerContext->url())), timeout, singleShot);
74
} else if (function->IsFunction()) {
75
size_t paramCount = argumentCount >= 2 ? argumentCount - 2 : 0;
76
v8::Local<v8::Value>* params = 0;
78
params = new v8::Local<v8::Value>[paramCount];
79
for (size_t i = 0; i < paramCount; ++i)
80
params[i] = args[i+2];
82
// ScheduledAction takes ownership of actual params and releases them in its destructor.
83
OwnPtr<ScheduledAction> action = adoptPtr(new ScheduledAction(v8Context, v8::Handle<v8::Function>::Cast(function), paramCount, params));
84
// FIXME: We should use a OwnArrayPtr for params.
86
timerId = DOMTimer::install(workerContext, action.release(), timeout, singleShot);
88
return v8::Undefined();
90
return v8Integer(timerId, args.GetIsolate());
93
v8::Handle<v8::Value> V8WorkerContext::importScriptsCallback(const v8::Arguments& args)
95
INC_STATS("DOM.WorkerContext.importScripts()");
97
return v8::Undefined();
100
for (int i = 0; i < args.Length(); i++) {
101
V8TRYCATCH(v8::Handle<v8::String>, scriptUrl, args[i]->ToString());
102
if (scriptUrl.IsEmpty())
103
return v8::Undefined();
104
urls.append(toWebCoreString(scriptUrl));
107
WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder());
109
ExceptionCode ec = 0;
110
workerContext->importScripts(urls, ec);
113
return setDOMException(ec, args.GetIsolate());
115
return v8::Undefined();
118
v8::Handle<v8::Value> V8WorkerContext::setTimeoutCallback(const v8::Arguments& args)
120
INC_STATS("DOM.WorkerContext.setTimeout()");
121
return SetTimeoutOrInterval(args, true);
124
v8::Handle<v8::Value> V8WorkerContext::setIntervalCallback(const v8::Arguments& args)
126
INC_STATS("DOM.WorkerContext.setInterval()");
127
return SetTimeoutOrInterval(args, false);
130
v8::Handle<v8::Value> toV8(WorkerContext* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
132
// Notice that we explicitly ignore creationContext because the WorkerContext is its own creationContext.
135
return v8NullWithCheck(isolate);
137
WorkerScriptController* script = impl->script();
139
return v8NullWithCheck(isolate);
141
v8::Handle<v8::Object> global = script->context()->Global();
142
ASSERT(!global.IsEmpty());
146
} // namespace WebCore
148
#endif // ENABLE(WORKERS)