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/core/v8/V8WorkerGlobalScopeEventListener.h"
     34 
     35 #include "bindings/core/v8/V8Binding.h"
     36 #include "bindings/core/v8/V8DOMWrapper.h"
     37 #include "bindings/core/v8/V8Event.h"
     38 #include "bindings/core/v8/V8EventTarget.h"
     39 #include "bindings/core/v8/V8GCController.h"
     40 #include "bindings/core/v8/V8ScriptRunner.h"
     41 #include "bindings/core/v8/WorkerScriptController.h"
     42 #include "core/inspector/InspectorInstrumentation.h"
     43 #include "core/inspector/InspectorTraceEvents.h"
     44 #include "core/workers/WorkerGlobalScope.h"
     45 
     46 namespace blink {
     47 
     48 V8WorkerGlobalScopeEventListener::V8WorkerGlobalScopeEventListener(v8::Local<v8::Object> listener, bool isInline, ScriptState* scriptState)
     49     : V8EventListener(listener, isInline, scriptState)
     50 {
     51 }
     52 
     53 void V8WorkerGlobalScopeEventListener::handleEvent(ExecutionContext*, Event* event)
     54 {
     55     // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it.
     56     // See issue 889829.
     57     RefPtr<V8AbstractEventListener> protect(this);
     58 
     59     WorkerScriptController* script = toWorkerGlobalScope(scriptState()->executionContext())->script();
     60     if (!script)
     61         return;
     62 
     63     if (scriptState()->contextIsValid())
     64         return;
     65     ScriptState::Scope scope(scriptState());
     66 
     67     // Get the V8 wrapper for the event object.
     68     v8::Handle<v8::Value> jsEvent = toV8(event, scriptState()->context()->Global(), isolate());
     69 
     70     invokeEventHandler(event, v8::Local<v8::Value>::New(isolate(), jsEvent));
     71 }
     72 
     73 v8::Local<v8::Value> V8WorkerGlobalScopeEventListener::callListenerFunction(v8::Handle<v8::Value> jsEvent, Event* event)
     74 {
     75     v8::Local<v8::Function> handlerFunction = getListenerFunction(scriptState()->executionContext());
     76     v8::Local<v8::Object> receiver = getReceiverObject(event);
     77     if (handlerFunction.IsEmpty() || receiver.IsEmpty())
     78         return v8::Local<v8::Value>();
     79 
     80     TRACE_EVENT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "FunctionCall", "data", devToolsTraceEventData(scriptState()->executionContext(), handlerFunction, isolate()));
     81     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline.stack"), "CallStack", "stack", InspectorCallStackEvent::currentCallStack());
     82     // FIXME(361045): remove InspectorInstrumentation calls once DevTools Timeline migrates to tracing.
     83     InspectorInstrumentationCookie cookie;
     84     if (InspectorInstrumentation::timelineAgentEnabled(scriptState()->executionContext())) {
     85         int scriptId = 0;
     86         String resourceName;
     87         int lineNumber = 1;
     88         GetDevToolsFunctionInfo(handlerFunction, isolate(), scriptId, resourceName, lineNumber);
     89         cookie = InspectorInstrumentation::willCallFunction(scriptState()->executionContext(), scriptId, resourceName, lineNumber);
     90     }
     91 
     92     v8::Handle<v8::Value> parameters[1] = { jsEvent };
     93     v8::Local<v8::Value> result = V8ScriptRunner::callFunction(handlerFunction, scriptState()->executionContext(), receiver, WTF_ARRAY_LENGTH(parameters), parameters, isolate());
     94 
     95     InspectorInstrumentation::didCallFunction(cookie);
     96     TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "UpdateCounters", "data", InspectorUpdateCountersEvent::data());
     97 
     98     return result;
     99 }
    100 
    101 v8::Local<v8::Object> V8WorkerGlobalScopeEventListener::getReceiverObject(Event* event)
    102 {
    103     v8::Local<v8::Object> listener = getListenerObject(scriptState()->executionContext());
    104 
    105     if (!listener.IsEmpty() && !listener->IsFunction())
    106         return listener;
    107 
    108     EventTarget* target = event->currentTarget();
    109     v8::Handle<v8::Value> value = toV8(target, scriptState()->context()->Global(), isolate());
    110     if (value.IsEmpty())
    111         return v8::Local<v8::Object>();
    112     return v8::Local<v8::Object>::New(isolate(), v8::Handle<v8::Object>::Cast(value));
    113 }
    114 
    115 } // namespace blink
    116