Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (C) 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 #include "bindings/core/v8/ScriptEventListener.h"
     33 
     34 #include "bindings/core/v8/ScriptController.h"
     35 #include "bindings/core/v8/ScriptState.h"
     36 #include "bindings/core/v8/V8AbstractEventListener.h"
     37 #include "bindings/core/v8/V8Binding.h"
     38 #include "bindings/core/v8/WindowProxy.h"
     39 #include "core/dom/Document.h"
     40 #include "core/dom/DocumentParser.h"
     41 #include "core/dom/QualifiedName.h"
     42 #include "core/events/EventListener.h"
     43 #include "core/frame/LocalFrame.h"
     44 #include <v8.h>
     45 
     46 namespace blink {
     47 
     48 PassRefPtr<V8LazyEventListener> createAttributeEventListener(Node* node, const QualifiedName& name, const AtomicString& value, const AtomicString& eventParameterName)
     49 {
     50     ASSERT(node);
     51     if (value.isNull())
     52         return nullptr;
     53 
     54     // FIXME: Very strange: we initialize zero-based number with '1'.
     55     TextPosition position(OrdinalNumber::fromZeroBasedInt(1), OrdinalNumber::first());
     56     String sourceURL;
     57 
     58     v8::Isolate* isolate;
     59     if (LocalFrame* frame = node->document().frame()) {
     60         isolate = toIsolate(frame);
     61         ScriptController& scriptController = frame->script();
     62         if (!scriptController.canExecuteScripts(AboutToExecuteScript))
     63             return nullptr;
     64         position = scriptController.eventHandlerPosition();
     65         sourceURL = node->document().url().string();
     66     } else {
     67         isolate = v8::Isolate::GetCurrent();
     68     }
     69 
     70     return V8LazyEventListener::create(name.localName(), eventParameterName, value, sourceURL, position, node, isolate);
     71 }
     72 
     73 PassRefPtr<V8LazyEventListener> createAttributeEventListener(LocalFrame* frame, const QualifiedName& name, const AtomicString& value, const AtomicString& eventParameterName)
     74 {
     75     if (!frame)
     76         return nullptr;
     77 
     78     if (value.isNull())
     79         return nullptr;
     80 
     81     ScriptController& scriptController = frame->script();
     82     if (!scriptController.canExecuteScripts(AboutToExecuteScript))
     83         return nullptr;
     84 
     85     TextPosition position = scriptController.eventHandlerPosition();
     86     String sourceURL = frame->document()->url().string();
     87 
     88     return V8LazyEventListener::create(name.localName(), eventParameterName, value, sourceURL, position, 0, toIsolate(frame));
     89 }
     90 
     91 static v8::Handle<v8::Function> eventListenerEffectiveFunction(v8::Isolate* isolate, v8::Handle<v8::Object> listenerObject)
     92 {
     93     v8::Handle<v8::Function> function;
     94     if (listenerObject->IsFunction()) {
     95         function = v8::Handle<v8::Function>::Cast(listenerObject);
     96     } else if (listenerObject->IsObject()) {
     97         // Try the "handleEvent" method (EventListener interface).
     98         v8::Handle<v8::Value> property = listenerObject->Get(v8AtomicString(isolate, "handleEvent"));
     99         if (property.IsEmpty() || !property->IsFunction()) {
    100             // Fall back to the "constructor" property.
    101             property = listenerObject->Get(v8AtomicString(isolate, "constructor"));
    102         }
    103         if (!property.IsEmpty() && property->IsFunction())
    104             function = v8::Handle<v8::Function>::Cast(property);
    105     }
    106     return function;
    107 }
    108 
    109 String eventListenerHandlerBody(Document* document, EventListener* listener)
    110 {
    111     if (listener->type() != EventListener::JSEventListenerType)
    112         return "";
    113 
    114     v8::HandleScope scope(toIsolate(document));
    115     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
    116     v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
    117     v8::Context::Scope contextScope(context);
    118     v8::Handle<v8::Object> object = v8Listener->getListenerObject(document);
    119     if (object.IsEmpty())
    120         return "";
    121     v8::Handle<v8::Function> function = eventListenerEffectiveFunction(scope.GetIsolate(), object);
    122     if (function.IsEmpty())
    123         return "";
    124 
    125     TOSTRING_DEFAULT(V8StringResource<TreatNullAsNullString>, functionString, function, "");
    126     return functionString;
    127 }
    128 
    129 ScriptValue eventListenerHandler(Document* document, EventListener* listener)
    130 {
    131     if (listener->type() != EventListener::JSEventListenerType)
    132         return ScriptValue();
    133 
    134     v8::Isolate* isolate = toIsolate(document);
    135     v8::HandleScope scope(isolate);
    136     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
    137     v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
    138     v8::Context::Scope contextScope(context);
    139     v8::Handle<v8::Object> function = v8Listener->getListenerObject(document);
    140     if (function.IsEmpty())
    141         return ScriptValue();
    142     return ScriptValue(ScriptState::from(context), function);
    143 }
    144 
    145 ScriptState* eventListenerHandlerScriptState(LocalFrame* frame, EventListener* listener)
    146 {
    147     if (listener->type() != EventListener::JSEventListenerType)
    148         return 0;
    149     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
    150     v8::HandleScope scope(toIsolate(frame));
    151     v8::Handle<v8::Context> v8Context = frame->script().windowProxy(v8Listener->world())->context();
    152     return ScriptState::from(v8Context);
    153 }
    154 
    155 bool eventListenerHandlerLocation(Document* document, EventListener* listener, String& sourceName, String& scriptId, int& lineNumber, int& columnNumber)
    156 {
    157     if (listener->type() != EventListener::JSEventListenerType)
    158         return false;
    159 
    160     v8::HandleScope scope(toIsolate(document));
    161     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
    162     v8::Handle<v8::Context> context = toV8Context(document, v8Listener->world());
    163     v8::Context::Scope contextScope(context);
    164     v8::Local<v8::Object> object = v8Listener->getListenerObject(document);
    165     if (object.IsEmpty())
    166         return false;
    167     v8::Handle<v8::Function> function = eventListenerEffectiveFunction(scope.GetIsolate(), object);
    168     if (function.IsEmpty())
    169         return false;
    170     v8::Handle<v8::Function> originalFunction = getBoundFunction(function);
    171     int scriptIdValue = originalFunction->ScriptId();
    172     scriptId = String::number(scriptIdValue);
    173     v8::ScriptOrigin origin = originalFunction->GetScriptOrigin();
    174     if (!origin.ResourceName().IsEmpty() && origin.ResourceName()->IsString())
    175         sourceName = toCoreString(origin.ResourceName().As<v8::String>());
    176     else
    177         sourceName = "";
    178     lineNumber = originalFunction->GetScriptLineNumber();
    179     columnNumber = originalFunction->GetScriptColumnNumber();
    180     return true;
    181 }
    182 
    183 } // namespace blink
    184