Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 2006, 2007, 2008, 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 "bindings/v8/V8WorkerGlobalScopeEventListener.h"
     34 
     35 #include "V8Event.h"
     36 #include "V8EventTarget.h"
     37 #include "bindings/v8/V8Binding.h"
     38 #include "bindings/v8/V8DOMWrapper.h"
     39 #include "bindings/v8/V8GCController.h"
     40 #include "bindings/v8/V8ScriptRunner.h"
     41 #include "bindings/v8/WorkerScriptController.h"
     42 #include "core/inspector/InspectorInstrumentation.h"
     43 #include "core/workers/WorkerGlobalScope.h"
     44 
     45 namespace WebCore {
     46 
     47 V8WorkerGlobalScopeEventListener::V8WorkerGlobalScopeEventListener(v8::Local<v8::Object> listener, bool isInline)
     48     : V8EventListener(listener, isInline)
     49 {
     50 }
     51 
     52 void V8WorkerGlobalScopeEventListener::handleEvent(ScriptExecutionContext* context, Event* event)
     53 {
     54     if (!context)
     55         return;
     56 
     57     // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it.
     58     // See issue 889829.
     59     RefPtr<V8AbstractEventListener> protect(this);
     60 
     61     v8::HandleScope handleScope;
     62 
     63     WorkerScriptController* script = toWorkerGlobalScope(context)->script();
     64     if (!script)
     65         return;
     66 
     67     v8::Handle<v8::Context> v8Context = script->context();
     68     if (v8Context.IsEmpty())
     69         return;
     70 
     71     // Enter the V8 context in which to perform the event handling.
     72     v8::Context::Scope scope(v8Context);
     73 
     74     // Get the V8 wrapper for the event object.
     75     v8::Isolate* isolate = v8Context->GetIsolate();
     76     v8::Handle<v8::Value> jsEvent = toV8(event, v8::Handle<v8::Object>(), isolate);
     77 
     78     invokeEventHandler(context, event, v8::Local<v8::Value>::New(isolate, jsEvent));
     79 }
     80 
     81 v8::Local<v8::Value> V8WorkerGlobalScopeEventListener::callListenerFunction(ScriptExecutionContext* context, v8::Handle<v8::Value> jsEvent, Event* event)
     82 {
     83     v8::Local<v8::Function> handlerFunction = getListenerFunction(context);
     84     v8::Local<v8::Object> receiver = getReceiverObject(context, event);
     85     if (handlerFunction.IsEmpty() || receiver.IsEmpty())
     86         return v8::Local<v8::Value>();
     87 
     88     InspectorInstrumentationCookie cookie;
     89     if (InspectorInstrumentation::timelineAgentEnabled(context)) {
     90         String resourceName("undefined");
     91         int lineNumber = 1;
     92         v8::ScriptOrigin origin = handlerFunction->GetScriptOrigin();
     93         if (!origin.ResourceName().IsEmpty()) {
     94             resourceName = toWebCoreString(origin.ResourceName());
     95             lineNumber = handlerFunction->GetScriptLineNumber() + 1;
     96         }
     97         cookie = InspectorInstrumentation::willCallFunction(context, resourceName, lineNumber);
     98     }
     99 
    100     v8::Handle<v8::Value> parameters[1] = { jsEvent };
    101     v8::Local<v8::Value> result = V8ScriptRunner::callFunction(handlerFunction, context, receiver, WTF_ARRAY_LENGTH(parameters), parameters);
    102 
    103     InspectorInstrumentation::didCallFunction(cookie);
    104 
    105     return result;
    106 }
    107 
    108 v8::Local<v8::Object> V8WorkerGlobalScopeEventListener::getReceiverObject(ScriptExecutionContext* context, Event* event)
    109 {
    110     v8::Local<v8::Object> listener = getListenerObject(context);
    111 
    112     if (!listener.IsEmpty() && !listener->IsFunction())
    113         return listener;
    114 
    115     EventTarget* target = event->currentTarget();
    116     v8::Handle<v8::Value> value = toV8(target, v8::Handle<v8::Object>(), toV8Context(context, world())->GetIsolate());
    117     if (value.IsEmpty())
    118         return v8::Local<v8::Object>();
    119     return v8::Local<v8::Object>::New(v8::Handle<v8::Object>::Cast(value));
    120 }
    121 
    122 } // namespace WebCore
    123