OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions are | |
6 * met: | |
7 * | |
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 | |
13 * distribution. | |
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. | |
17 * | |
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. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 | |
33 #include "V8WorkerContext.h" | |
34 | |
35 #include "bindings/v8/ScheduledAction.h" | |
36 #include "bindings/v8/V8Binding.h" | |
37 #include "bindings/v8/V8Utilities.h" | |
38 #include "bindings/v8/V8WorkerContextEventListener.h" | |
39 #include "bindings/v8/WorkerScriptController.h" | |
40 #include "core/dom/ExceptionCode.h" | |
41 #include "core/inspector/ScriptCallStack.h" | |
42 #include "core/page/ContentSecurityPolicy.h" | |
43 #include "core/page/DOMTimer.h" | |
44 #include "core/workers/WorkerContext.h" | |
45 #include "modules/websockets/WebSocket.h" | |
46 | |
47 namespace WebCore { | |
48 | |
49 void SetTimeoutOrInterval(const v8::FunctionCallbackInfo<v8::Value>& args, bool
singleShot) | |
50 { | |
51 WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder()); | |
52 | |
53 int argumentCount = args.Length(); | |
54 if (argumentCount < 1) | |
55 return; | |
56 | |
57 v8::Handle<v8::Value> function = args[0]; | |
58 int32_t timeout = argumentCount >= 2 ? args[1]->Int32Value() : 0; | |
59 int timerId; | |
60 | |
61 WorkerScriptController* script = workerContext->script(); | |
62 if (!script) | |
63 return; | |
64 | |
65 v8::Handle<v8::Context> v8Context = script->context(); | |
66 if (function->IsString()) { | |
67 if (ContentSecurityPolicy* policy = workerContext->contentSecurityPolicy
()) { | |
68 if (!policy->allowEval()) { | |
69 v8SetReturnValue(args, 0); | |
70 return; | |
71 } | |
72 } | |
73 WTF::String stringFunction = toWebCoreString(function); | |
74 timerId = DOMTimer::install(workerContext, adoptPtr(new ScheduledAction(
v8Context, stringFunction, workerContext->url(), args.GetIsolate())), timeout, s
ingleShot); | |
75 } else if (function->IsFunction()) { | |
76 size_t paramCount = argumentCount >= 2 ? argumentCount - 2 : 0; | |
77 v8::Local<v8::Value>* params = 0; | |
78 if (paramCount > 0) { | |
79 params = new v8::Local<v8::Value>[paramCount]; | |
80 for (size_t i = 0; i < paramCount; ++i) | |
81 params[i] = args[i+2]; | |
82 } | |
83 // ScheduledAction takes ownership of actual params and releases them in
its destructor. | |
84 OwnPtr<ScheduledAction> action = adoptPtr(new ScheduledAction(v8Context,
v8::Handle<v8::Function>::Cast(function), paramCount, params, args.GetIsolate()
)); | |
85 // FIXME: We should use a OwnArrayPtr for params. | |
86 delete [] params; | |
87 timerId = DOMTimer::install(workerContext, action.release(), timeout, si
ngleShot); | |
88 } else | |
89 return; | |
90 | |
91 v8SetReturnValue(args, timerId); | |
92 } | |
93 | |
94 void V8WorkerContext::importScriptsMethodCustom(const v8::FunctionCallbackInfo<v
8::Value>& args) | |
95 { | |
96 if (!args.Length()) | |
97 return; | |
98 | |
99 Vector<String> urls; | |
100 for (int i = 0; i < args.Length(); i++) { | |
101 V8TRYCATCH_VOID(v8::Handle<v8::String>, scriptUrl, args[i]->ToString()); | |
102 if (scriptUrl.IsEmpty()) | |
103 return; | |
104 urls.append(toWebCoreString(scriptUrl)); | |
105 } | |
106 | |
107 WorkerContext* workerContext = V8WorkerContext::toNative(args.Holder()); | |
108 | |
109 ExceptionCode ec = 0; | |
110 workerContext->importScripts(urls, ec); | |
111 | |
112 if (!ec) | |
113 return; | |
114 setDOMException(ec, args.GetIsolate()); | |
115 } | |
116 | |
117 void V8WorkerContext::setTimeoutMethodCustom(const v8::FunctionCallbackInfo<v8::
Value>& args) | |
118 { | |
119 return SetTimeoutOrInterval(args, true); | |
120 } | |
121 | |
122 void V8WorkerContext::setIntervalMethodCustom(const v8::FunctionCallbackInfo<v8:
:Value>& args) | |
123 { | |
124 return SetTimeoutOrInterval(args, false); | |
125 } | |
126 | |
127 v8::Handle<v8::Value> toV8(WorkerContext* impl, v8::Handle<v8::Object> creationC
ontext, v8::Isolate* isolate) | |
128 { | |
129 // Notice that we explicitly ignore creationContext because the WorkerContex
t is its own creationContext. | |
130 | |
131 if (!impl) | |
132 return v8NullWithCheck(isolate); | |
133 | |
134 WorkerScriptController* script = impl->script(); | |
135 if (!script) | |
136 return v8NullWithCheck(isolate); | |
137 | |
138 v8::Handle<v8::Object> global = script->context()->Global(); | |
139 ASSERT(!global.IsEmpty()); | |
140 return global; | |
141 } | |
142 | |
143 v8::Handle<v8::Value> toV8ForMainWorld(WorkerContext* impl, v8::Handle<v8::Objec
t> creationContext, v8::Isolate* isolate) | |
144 { | |
145 return toV8(impl, creationContext, isolate); | |
146 } | |
147 | |
148 } // namespace WebCore | |
OLD | NEW |