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.
36 #include "WorkerContextExecutionProxy.h"
38 #include "DOMCoreException.h"
40 #include "EventException.h"
41 #include "RangeException.h"
42 #include "V8Binding.h"
45 #include "V8WorkerContextEventListener.h"
46 #include "V8WorkerContextObjectEventListener.h"
48 #include "WorkerContext.h"
49 #include "WorkerLocation.h"
50 #include "WorkerNavigator.h"
51 #include "WorkerScriptController.h"
52 #include "XMLHttpRequestException.h"
56 static void reportFatalErrorInV8(const char* location, const char* message)
58 // FIXME: We temporarily deal with V8 internal error situations such as out-of-memory by crashing the worker.
62 static void handleConsoleMessage(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data)
64 WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve();
68 WorkerContext* workerContext = proxy->workerContext();
72 v8::Handle<v8::String> errorMessageString = message->Get();
73 ASSERT(!errorMessageString.IsEmpty());
74 String errorMessage = toWebCoreString(errorMessageString);
76 v8::Handle<v8::Value> resourceName = message->GetScriptResourceName();
77 bool useURL = (resourceName.IsEmpty() || !resourceName->IsString());
78 String resourceNameString = useURL ? workerContext->url() : toWebCoreString(resourceName);
80 workerContext->addMessage(ConsoleDestination, JSMessageSource, LogMessageType, ErrorMessageLevel, errorMessage, message->GetLineNumber(), resourceNameString);
83 WorkerContextExecutionProxy::WorkerContextExecutionProxy(WorkerContext* workerContext)
84 : m_workerContext(workerContext)
90 WorkerContextExecutionProxy::~WorkerContextExecutionProxy()
95 void WorkerContextExecutionProxy::dispose()
97 // Disconnect all event listeners.
98 if (m_listeners.get())
100 for (V8EventListenerList::iterator iterator(m_listeners->begin()); iterator != m_listeners->end(); ++iterator)
101 static_cast<V8WorkerContextEventListener*>(*iterator)->disconnect();
103 m_listeners->clear();
106 // Detach all events from their JS wrappers.
107 for (size_t eventIndex = 0; eventIndex < m_events.size(); ++eventIndex) {
108 Event* event = m_events[eventIndex];
109 if (forgetV8EventObject(event))
114 // Dispose the context.
115 if (!m_context.IsEmpty()) {
121 WorkerContextExecutionProxy* WorkerContextExecutionProxy::retrieve()
123 v8::Handle<v8::Context> context = v8::Context::GetCurrent();
124 v8::Handle<v8::Object> global = context->Global();
125 global = V8DOMWrapper::lookupDOMWrapper(V8ClassIndex::WORKERCONTEXT, global);
126 // Return 0 if the current executing context is not the worker context.
127 if (global.IsEmpty())
129 WorkerContext* workerContext = V8DOMWrapper::convertToNativeObject<WorkerContext>(V8ClassIndex::WORKERCONTEXT, global);
130 return workerContext->script()->proxy();
133 void WorkerContextExecutionProxy::initV8IfNeeded()
135 static bool v8Initialized = false;
140 // Tell V8 not to call the default OOM handler, binding code will handle it.
141 v8::V8::IgnoreOutOfMemoryException();
142 v8::V8::SetFatalErrorHandler(reportFatalErrorInV8);
144 // Set up the handler for V8 error message.
145 v8::V8::AddMessageListener(handleConsoleMessage);
147 v8Initialized = true;
150 void WorkerContextExecutionProxy::initContextIfNeeded()
152 // Bail out if the context has already been initialized.
153 if (!m_context.IsEmpty())
156 // Create a new environment
157 v8::Persistent<v8::ObjectTemplate> globalTemplate;
158 m_context = v8::Context::New(0, globalTemplate);
160 // Starting from now, use local context only.
161 v8::Local<v8::Context> context = v8::Local<v8::Context>::New(m_context);
162 v8::Context::Scope scope(context);
164 // Allocate strings used during initialization.
165 v8::Handle<v8::String> implicitProtoString = v8::String::New("__proto__");
167 // Create a new JS object and use it as the prototype for the shadow global object.
168 v8::Handle<v8::Function> workerContextConstructor = GetConstructor(V8ClassIndex::WORKERCONTEXT);
169 v8::Local<v8::Object> jsWorkerContext = SafeAllocation::newInstance(workerContextConstructor);
170 // Bail out if allocation failed.
171 if (jsWorkerContext.IsEmpty()) {
177 V8DOMWrapper::setDOMWrapper(jsWorkerContext, V8ClassIndex::ToInt(V8ClassIndex::WORKERCONTEXT), m_workerContext);
179 V8DOMWrapper::setJSWrapperForDOMObject(m_workerContext, v8::Persistent<v8::Object>::New(jsWorkerContext));
180 m_workerContext->ref();
182 // Insert the object instance as the prototype of the shadow object.
183 v8::Handle<v8::Object> globalObject = m_context->Global();
184 globalObject->Set(implicitProtoString, jsWorkerContext);
186 m_listeners.set(new V8EventListenerList());
189 v8::Local<v8::Function> WorkerContextExecutionProxy::GetConstructor(V8ClassIndex::V8WrapperType type)
191 // Enter the context of the proxy to make sure that the function is
192 // constructed in the context corresponding to this proxy.
193 v8::Context::Scope scope(m_context);
194 v8::Handle<v8::FunctionTemplate> functionTemplate = V8DOMWrapper::getTemplate(type);
196 // Getting the function might fail if we're running out of stack or memory.
197 v8::TryCatch tryCatch;
198 v8::Local<v8::Function> value = functionTemplate->GetFunction();
200 return v8::Local<v8::Function>();
205 v8::Handle<v8::Value> WorkerContextExecutionProxy::ToV8Object(V8ClassIndex::V8WrapperType type, void* impl)
210 if (type == V8ClassIndex::WORKERCONTEXT)
211 return WorkerContextToV8Object(static_cast<WorkerContext*>(impl));
213 if (type == V8ClassIndex::WORKER || type == V8ClassIndex::XMLHTTPREQUEST) {
214 v8::Persistent<v8::Object> result = getActiveDOMObjectMap().get(impl);
215 if (!result.IsEmpty())
218 v8::Local<v8::Object> object = toV8(type, type, impl);
219 if (!object.IsEmpty())
220 static_cast<Worker*>(impl)->ref();
221 result = v8::Persistent<v8::Object>::New(object);
222 V8DOMWrapper::setJSWrapperForDOMObject(impl, result);
227 v8::Persistent<v8::Object> result = getDOMObjectMap().get(impl);
228 if (result.IsEmpty()) {
229 v8::Local<v8::Object> object = toV8(type, type, impl);
230 if (!object.IsEmpty()) {
232 case V8ClassIndex::WORKERLOCATION:
233 static_cast<WorkerLocation*>(impl)->ref();
235 case V8ClassIndex::WORKERNAVIGATOR:
236 static_cast<WorkerNavigator*>(impl)->ref();
238 case V8ClassIndex::DOMCOREEXCEPTION:
239 static_cast<DOMCoreException*>(impl)->ref();
241 case V8ClassIndex::RANGEEXCEPTION:
242 static_cast<RangeException*>(impl)->ref();
244 case V8ClassIndex::EVENTEXCEPTION:
245 static_cast<EventException*>(impl)->ref();
247 case V8ClassIndex::XMLHTTPREQUESTEXCEPTION:
248 static_cast<XMLHttpRequestException*>(impl)->ref();
253 result = v8::Persistent<v8::Object>::New(object);
254 V8DOMWrapper::setJSWrapperForDOMObject(impl, result);
260 v8::Handle<v8::Value> WorkerContextExecutionProxy::EventToV8Object(Event* event)
265 v8::Handle<v8::Object> wrapper = getDOMObjectMap().get(event);
266 if (!wrapper.IsEmpty())
269 V8ClassIndex::V8WrapperType type = V8ClassIndex::EVENT;
271 if (event->isMessageEvent())
272 type = V8ClassIndex::MESSAGEEVENT;
274 v8::Handle<v8::Object> result = toV8(type, V8ClassIndex::EVENT, event);
275 if (result.IsEmpty()) {
276 // Instantiation failed. Avoid updating the DOM object map and return null which
277 // is already handled by callers of this function in case the event is NULL.
281 event->ref(); // fast ref
282 V8DOMWrapper::setJSWrapperForDOMObject(event, v8::Persistent<v8::Object>::New(result));
287 // A JS object of type EventTarget in the worker context can only be Worker or WorkerContext.
288 v8::Handle<v8::Value> WorkerContextExecutionProxy::EventTargetToV8Object(EventTarget* target)
293 WorkerContext* workerContext = target->toWorkerContext();
295 return WorkerContextToV8Object(workerContext);
297 Worker* worker = target->toWorker();
299 return ToV8Object(V8ClassIndex::WORKER, worker);
301 XMLHttpRequest* xhr = target->toXMLHttpRequest();
303 return ToV8Object(V8ClassIndex::XMLHTTPREQUEST, xhr);
305 ASSERT_NOT_REACHED();
306 return v8::Handle<v8::Value>();
309 v8::Handle<v8::Value> WorkerContextExecutionProxy::WorkerContextToV8Object(WorkerContext* workerContext)
314 v8::Handle<v8::Context> context = workerContext->script()->proxy()->GetContext();
316 v8::Handle<v8::Object> global = context->Global();
317 ASSERT(!global.IsEmpty());
321 v8::Local<v8::Object> WorkerContextExecutionProxy::toV8(V8ClassIndex::V8WrapperType descType, V8ClassIndex::V8WrapperType cptrType, void* impl)
323 v8::Local<v8::Function> function;
324 WorkerContextExecutionProxy* proxy = retrieve();
326 function = proxy->GetConstructor(descType);
328 function = V8DOMWrapper::getTemplate(descType)->GetFunction();
330 v8::Local<v8::Object> instance = SafeAllocation::newInstance(function);
331 if (!instance.IsEmpty()) {
332 // Avoid setting the DOM wrapper for failed allocations.
333 V8DOMWrapper::setDOMWrapper(instance, V8ClassIndex::ToInt(cptrType), impl);
338 bool WorkerContextExecutionProxy::forgetV8EventObject(Event* event)
340 if (getDOMObjectMap().contains(event)) {
341 getDOMObjectMap().forget(event);
347 ScriptValue WorkerContextExecutionProxy::evaluate(const String& script, const String& fileName, int baseLine, WorkerContextExecutionState* state)
351 initContextIfNeeded();
352 v8::Context::Scope scope(m_context);
354 v8::TryCatch exceptionCatcher;
356 v8::Local<v8::String> scriptString = v8ExternalString(script);
357 v8::Handle<v8::Script> compiledScript = V8Proxy::compileScript(scriptString, fileName, baseLine);
358 v8::Local<v8::Value> result = runScript(compiledScript);
360 if (exceptionCatcher.HasCaught()) {
361 v8::Local<v8::Message> message = exceptionCatcher.Message();
362 state->hadException = true;
363 state->exception = ScriptValue(exceptionCatcher.Exception());
364 state->errorMessage = toWebCoreString(message->Get());
365 state->lineNumber = message->GetLineNumber();
366 state->sourceURL = toWebCoreString(message->GetScriptResourceName());
367 exceptionCatcher.Reset();
369 state->hadException = false;
371 if (result.IsEmpty() || result->IsUndefined())
372 return ScriptValue();
374 return ScriptValue(result);
377 v8::Local<v8::Value> WorkerContextExecutionProxy::runScript(v8::Handle<v8::Script> script)
379 if (script.IsEmpty())
380 return v8::Local<v8::Value>();
382 // Compute the source string and prevent against infinite recursion.
383 if (m_recursion >= kMaxRecursionDepth) {
384 v8::Local<v8::String> code = v8ExternalString("throw RangeError('Recursion too deep')");
385 script = V8Proxy::compileScript(code, "", 0);
388 if (V8Proxy::handleOutOfMemory())
389 ASSERT(script.IsEmpty());
391 if (script.IsEmpty())
392 return v8::Local<v8::Value>();
394 // Run the script and keep track of the current recursion depth.
395 v8::Local<v8::Value> result;
398 result = script->Run();
402 // Handle V8 internal error situation (Out-of-memory).
403 if (result.IsEmpty())
404 return v8::Local<v8::Value>();
409 PassRefPtr<V8EventListener> WorkerContextExecutionProxy::findOrCreateEventListenerHelper(v8::Local<v8::Value> object, bool isInline, bool findOnly, bool createObjectEventListener)
411 if (!object->IsObject())
414 V8EventListener* listener = m_listeners->find(object->ToObject(), isInline);
418 // Create a new one, and add to cache.
419 RefPtr<V8EventListener> newListener;
420 if (createObjectEventListener)
421 newListener = V8WorkerContextObjectEventListener::create(this, v8::Local<v8::Object>::Cast(object), isInline);
423 newListener = V8WorkerContextEventListener::create(this, v8::Local<v8::Object>::Cast(object), isInline);
424 m_listeners->add(newListener.get());
426 return newListener.release();
429 PassRefPtr<V8EventListener> WorkerContextExecutionProxy::findOrCreateEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly)
431 return findOrCreateEventListenerHelper(object, isInline, findOnly, false);
434 PassRefPtr<V8EventListener> WorkerContextExecutionProxy::findOrCreateObjectEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly)
436 return findOrCreateEventListenerHelper(object, isInline, findOnly, true);
439 void WorkerContextExecutionProxy::RemoveEventListener(V8EventListener* listener)
441 m_listeners->remove(listener);
444 void WorkerContextExecutionProxy::trackEvent(Event* event)
446 m_events.append(event);
449 } // namespace WebCore
451 #endif // ENABLE(WORKERS)