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 "V8Binding.h"
41 #include "V8WorkerContextEventListener.h"
42 #include "V8WorkerContextObjectEventListener.h"
44 #include "WorkerContext.h"
45 #include "WorkerLocation.h"
46 #include "WorkerNavigator.h"
47 #include "WorkerScriptController.h"
51 static bool isWorkersEnabled = false;
53 static void reportFatalErrorInV8(const char* location, const char* message)
55 // FIXME: We temporarily deal with V8 internal error situations such as out-of-memory by crashing the worker.
59 static void handleConsoleMessage(v8::Handle<v8::Message> message, v8::Handle<v8::Value> data)
61 WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve();
65 WorkerContext* workerContext = proxy->workerContext();
69 v8::Handle<v8::String> errorMessageString = message->Get();
70 ASSERT(!errorMessageString.IsEmpty());
71 String errorMessage = ToWebCoreString(errorMessageString);
73 v8::Handle<v8::Value> resourceName = message->GetScriptResourceName();
74 bool useURL = (resourceName.IsEmpty() || !resourceName->IsString());
75 String resourceNameString = useURL ? workerContext->url() : ToWebCoreString(resourceName);
77 workerContext->addMessage(ConsoleDestination, JSMessageSource, ErrorMessageLevel, errorMessage, message->GetLineNumber(), resourceNameString);
80 bool WorkerContextExecutionProxy::isWebWorkersEnabled()
82 return isWorkersEnabled;
85 void WorkerContextExecutionProxy::setIsWebWorkersEnabled(bool value)
87 isWorkersEnabled = value;
90 WorkerContextExecutionProxy::WorkerContextExecutionProxy(WorkerContext* workerContext)
91 : m_workerContext(workerContext)
97 WorkerContextExecutionProxy::~WorkerContextExecutionProxy()
102 void WorkerContextExecutionProxy::dispose()
104 // Disconnect all event listeners.
105 if (m_listeners.get())
107 for (V8EventListenerList::iterator iterator(m_listeners->begin()); iterator != m_listeners->end(); ++iterator)
108 static_cast<V8WorkerContextEventListener*>(*iterator)->disconnect();
110 m_listeners->clear();
113 // Detach all events from their JS wrappers.
114 for (size_t eventIndex = 0; eventIndex < m_events.size(); ++eventIndex) {
115 Event* event = m_events[eventIndex];
116 if (forgetV8EventObject(event))
121 // Dispose the context.
122 if (!m_context.IsEmpty()) {
128 WorkerContextExecutionProxy* WorkerContextExecutionProxy::retrieve()
130 v8::Handle<v8::Context> context = v8::Context::GetCurrent();
131 v8::Handle<v8::Object> global = context->Global();
132 global = V8Proxy::LookupDOMWrapper(V8ClassIndex::WORKERCONTEXT, global);
133 // Return 0 if the current executing context is not the worker context.
134 if (global.IsEmpty())
136 WorkerContext* workerContext = V8Proxy::ToNativeObject<WorkerContext>(V8ClassIndex::WORKERCONTEXT, global);
137 return workerContext->script()->proxy();
140 void WorkerContextExecutionProxy::initV8IfNeeded()
142 static bool v8Initialized = false;
147 // Tell V8 not to call the default OOM handler, binding code will handle it.
148 v8::V8::IgnoreOutOfMemoryException();
149 v8::V8::SetFatalErrorHandler(reportFatalErrorInV8);
151 // Set up the handler for V8 error message.
152 v8::V8::AddMessageListener(handleConsoleMessage);
154 v8Initialized = true;
157 void WorkerContextExecutionProxy::initContextIfNeeded()
159 // Bail out if the context has already been initialized.
160 if (!m_context.IsEmpty())
163 // Create a new environment
164 v8::Persistent<v8::ObjectTemplate> globalTemplate;
165 m_context = v8::Context::New(0, globalTemplate);
167 // Starting from now, use local context only.
168 v8::Local<v8::Context> context = v8::Local<v8::Context>::New(m_context);
169 v8::Context::Scope scope(context);
171 // Allocate strings used during initialization.
172 v8::Handle<v8::String> implicitProtoString = v8::String::New("__proto__");
174 // Create a new JS object and use it as the prototype for the shadow global object.
175 v8::Handle<v8::Function> workerContextConstructor = GetConstructor(V8ClassIndex::WORKERCONTEXT);
176 v8::Local<v8::Object> jsWorkerContext = SafeAllocation::newInstance(workerContextConstructor);
177 // Bail out if allocation failed.
178 if (jsWorkerContext.IsEmpty()) {
184 V8Proxy::SetDOMWrapper(jsWorkerContext, V8ClassIndex::ToInt(V8ClassIndex::WORKERCONTEXT), m_workerContext);
186 V8Proxy::SetJSWrapperForDOMObject(m_workerContext, v8::Persistent<v8::Object>::New(jsWorkerContext));
187 m_workerContext->ref();
189 // Insert the object instance as the prototype of the shadow object.
190 v8::Handle<v8::Object> globalObject = m_context->Global();
191 globalObject->Set(implicitProtoString, jsWorkerContext);
193 m_listeners.set(new V8EventListenerList());
196 v8::Local<v8::Function> WorkerContextExecutionProxy::GetConstructor(V8ClassIndex::V8WrapperType type)
198 // Enter the context of the proxy to make sure that the function is
199 // constructed in the context corresponding to this proxy.
200 v8::Context::Scope scope(m_context);
201 v8::Handle<v8::FunctionTemplate> functionTemplate = V8Proxy::GetTemplate(type);
203 // Getting the function might fail if we're running out of stack or memory.
204 v8::TryCatch tryCatch;
205 v8::Local<v8::Function> value = functionTemplate->GetFunction();
207 return v8::Local<v8::Function>();
212 v8::Handle<v8::Value> WorkerContextExecutionProxy::ToV8Object(V8ClassIndex::V8WrapperType type, void* impl)
217 if (type == V8ClassIndex::WORKERCONTEXT)
218 return WorkerContextToV8Object(static_cast<WorkerContext*>(impl));
220 if (type == V8ClassIndex::WORKER || type == V8ClassIndex::XMLHTTPREQUEST) {
221 v8::Persistent<v8::Object> result = getActiveDOMObjectMap().get(impl);
222 if (!result.IsEmpty())
225 v8::Local<v8::Object> object = toV8(type, type, impl);
226 if (!object.IsEmpty())
227 static_cast<Worker*>(impl)->ref();
228 result = v8::Persistent<v8::Object>::New(object);
229 V8Proxy::SetJSWrapperForDOMObject(impl, result);
234 v8::Persistent<v8::Object> result = domObjectMap().get(impl);
235 if (result.IsEmpty()) {
236 v8::Local<v8::Object> object = toV8(type, type, impl);
237 if (!object.IsEmpty()) {
239 case V8ClassIndex::WORKERLOCATION:
240 static_cast<WorkerLocation*>(impl)->ref();
242 case V8ClassIndex::WORKERNAVIGATOR:
243 static_cast<WorkerNavigator*>(impl)->ref();
248 result = v8::Persistent<v8::Object>::New(object);
249 V8Proxy::SetJSWrapperForDOMObject(impl, result);
255 v8::Handle<v8::Value> WorkerContextExecutionProxy::EventToV8Object(Event* event)
260 v8::Handle<v8::Object> wrapper = domObjectMap().get(event);
261 if (!wrapper.IsEmpty())
264 V8ClassIndex::V8WrapperType type = V8ClassIndex::EVENT;
266 if (event->isMessageEvent())
267 type = V8ClassIndex::MESSAGEEVENT;
269 v8::Handle<v8::Object> result = toV8(type, V8ClassIndex::EVENT, event);
270 if (result.IsEmpty()) {
271 // Instantiation failed. Avoid updating the DOM object map and return null which
272 // is already handled by callers of this function in case the event is NULL.
276 event->ref(); // fast ref
277 V8Proxy::SetJSWrapperForDOMObject(event, v8::Persistent<v8::Object>::New(result));
282 // A JS object of type EventTarget in the worker context can only be Worker or WorkerContext.
283 v8::Handle<v8::Value> WorkerContextExecutionProxy::EventTargetToV8Object(EventTarget* target)
288 WorkerContext* workerContext = target->toWorkerContext();
290 return WorkerContextToV8Object(workerContext);
292 Worker* worker = target->toWorker();
294 return ToV8Object(V8ClassIndex::WORKER, worker);
296 XMLHttpRequest* xhr = target->toXMLHttpRequest();
298 return ToV8Object(V8ClassIndex::XMLHTTPREQUEST, xhr);
300 ASSERT_NOT_REACHED();
301 return v8::Handle<v8::Value>();
304 v8::Handle<v8::Value> WorkerContextExecutionProxy::WorkerContextToV8Object(WorkerContext* workerContext)
309 v8::Handle<v8::Context> context = workerContext->script()->proxy()->GetContext();
311 v8::Handle<v8::Object> global = context->Global();
312 ASSERT(!global.IsEmpty());
316 v8::Local<v8::Object> WorkerContextExecutionProxy::toV8(V8ClassIndex::V8WrapperType descType, V8ClassIndex::V8WrapperType cptrType, void* impl)
318 v8::Local<v8::Function> function;
319 WorkerContextExecutionProxy* proxy = retrieve();
321 function = proxy->GetConstructor(descType);
323 function = V8Proxy::GetTemplate(descType)->GetFunction();
325 v8::Local<v8::Object> instance = SafeAllocation::newInstance(function);
326 if (!instance.IsEmpty()) {
327 // Avoid setting the DOM wrapper for failed allocations.
328 V8Proxy::SetDOMWrapper(instance, V8ClassIndex::ToInt(cptrType), impl);
333 bool WorkerContextExecutionProxy::forgetV8EventObject(Event* event)
335 if (domObjectMap().contains(event)) {
336 domObjectMap().forget(event);
342 v8::Local<v8::Value> WorkerContextExecutionProxy::evaluate(const String& script, const String& fileName, int baseLine)
346 initContextIfNeeded();
347 v8::Context::Scope scope(m_context);
349 v8::Local<v8::String> scriptString = v8ExternalString(script);
350 v8::Handle<v8::Script> compiledScript = V8Proxy::CompileScript(scriptString, fileName, baseLine);
351 return runScript(compiledScript);
354 v8::Local<v8::Value> WorkerContextExecutionProxy::runScript(v8::Handle<v8::Script> script)
356 if (script.IsEmpty())
357 return v8::Local<v8::Value>();
359 // Compute the source string and prevent against infinite recursion.
360 if (m_recursion >= kMaxRecursionDepth) {
361 v8::Local<v8::String> code = v8ExternalString("throw RangeError('Recursion too deep')");
362 script = V8Proxy::CompileScript(code, "", 0);
365 if (V8Proxy::HandleOutOfMemory())
366 ASSERT(script.IsEmpty());
368 if (script.IsEmpty())
369 return v8::Local<v8::Value>();
371 // Run the script and keep track of the current recursion depth.
372 v8::Local<v8::Value> result;
375 result = script->Run();
379 // Handle V8 internal error situation (Out-of-memory).
380 if (result.IsEmpty())
381 return v8::Local<v8::Value>();
386 PassRefPtr<V8EventListener> WorkerContextExecutionProxy::findOrCreateEventListenerHelper(v8::Local<v8::Value> object, bool isInline, bool findOnly, bool createObjectEventListener)
388 if (!object->IsObject())
391 V8EventListener* listener = m_listeners->find(object->ToObject(), isInline);
395 // Create a new one, and add to cache.
396 RefPtr<V8EventListener> newListener;
397 if (createObjectEventListener)
398 newListener = V8WorkerContextObjectEventListener::create(this, v8::Local<v8::Object>::Cast(object), isInline);
400 newListener = V8WorkerContextEventListener::create(this, v8::Local<v8::Object>::Cast(object), isInline);
401 m_listeners->add(newListener.get());
403 return newListener.release();
406 PassRefPtr<V8EventListener> WorkerContextExecutionProxy::findOrCreateEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly)
408 return findOrCreateEventListenerHelper(object, isInline, findOnly, false);
411 PassRefPtr<V8EventListener> WorkerContextExecutionProxy::findOrCreateObjectEventListener(v8::Local<v8::Value> object, bool isInline, bool findOnly)
413 return findOrCreateEventListenerHelper(object, isInline, findOnly, true);
416 void WorkerContextExecutionProxy::RemoveEventListener(V8EventListener* listener)
418 m_listeners->remove(listener);
421 void WorkerContextExecutionProxy::trackEvent(Event* event)
423 m_events.append(event);
426 } // namespace WebCore
428 #endif // ENABLE(WORKERS)