15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2010 Google Inc. All rights reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met:
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution.
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission.
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h"
325267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "bindings/v8/V8ErrorHandler.h"
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
34f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include "bindings/core/v8/V8ErrorEvent.h"
3553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "bindings/v8/ScriptController.h"
3653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "bindings/v8/V8Binding.h"
37d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "bindings/v8/V8HiddenValue.h"
385267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "bindings/v8/V8ScriptRunner.h"
39e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)#include "core/dom/Document.h"
401e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "core/dom/ExecutionContext.h"
4106f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)#include "core/events/ErrorEvent.h"
42d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "core/frame/LocalFrame.h"
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace WebCore {
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
46f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)V8ErrorHandler::V8ErrorHandler(v8::Local<v8::Object> listener, bool isInline, ScriptState* scriptState)
47f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    : V8EventListener(listener, isInline, scriptState)
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
51f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)v8::Local<v8::Value> V8ErrorHandler::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event)
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
531e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    if (!event->hasInterface(EventNames::ErrorEvent))
54f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        return V8EventListener::callListenerFunction(jsEvent, event);
555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ErrorEvent* errorEvent = static_cast<ErrorEvent*>(event);
578abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
58f5f02c98d2d694be40c6182631462fff13b4a479Ben Murdoch    if (errorEvent->world() && errorEvent->world() != &world())
59f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        return v8::Null(isolate());
608abfc5808a4e34d6e03867af8bc440dee641886fTorne (Richard Coles)
61f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    v8::Local<v8::Object> listener = getListenerObject(scriptState()->executionContext());
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    v8::Local<v8::Value> returnValue;
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (!listener.IsEmpty() && listener->IsFunction()) {
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        v8::Local<v8::Function> callFunction = v8::Local<v8::Function>::Cast(listener);
65f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        v8::Local<v8::Object> thisValue = scriptState()->context()->Global();
66f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
67f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        v8::Local<v8::Value> error = V8HiddenValue::getHiddenValue(isolate(), jsEvent->ToObject(), V8HiddenValue::error(isolate()));
68f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)        if (error.IsEmpty())
69f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)            error = v8::Null(isolate());
70f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
71f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        v8::Handle<v8::Value> parameters[5] = { v8String(isolate(), errorEvent->message()), v8String(isolate(), errorEvent->filename()), v8::Integer::New(isolate(), errorEvent->lineno()), v8::Integer::New(isolate(), errorEvent->colno()), error };
725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        v8::TryCatch tryCatch;
735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        tryCatch.SetVerbose(true);
74f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)        if (scriptState()->executionContext()->isWorkerGlobalScope())
75f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)            returnValue = V8ScriptRunner::callFunction(callFunction, scriptState()->executionContext(), thisValue, WTF_ARRAY_LENGTH(parameters), parameters, isolate());
765267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)        else
77f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)            returnValue = ScriptController::callFunction(scriptState()->executionContext(), callFunction, thisValue, WTF_ARRAY_LENGTH(parameters), parameters, isolate());
785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return returnValue;
805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
82e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)// static
83f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)void V8ErrorHandler::storeExceptionOnErrorEventWrapper(ErrorEvent* event, v8::Handle<v8::Value> data, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
84e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles){
85f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    v8::Local<v8::Value> wrappedEvent = toV8(event, creationContext, isolate);
86e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)    if (!wrappedEvent.IsEmpty()) {
87e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)        ASSERT(wrappedEvent->IsObject());
88d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)        V8HiddenValue::setHiddenValue(isolate, v8::Local<v8::Object>::Cast(wrappedEvent), V8HiddenValue::error(isolate), data);
89e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)    }
90e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)}
91e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)
925267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)bool V8ErrorHandler::shouldPreventDefault(v8::Local<v8::Value> returnValue)
935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    return returnValue->IsBoolean() && returnValue->BooleanValue();
955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} // namespace WebCore
98