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 "ScriptEventListener.h"
     33 
     34 #include "Attribute.h"
     35 #include "Document.h"
     36 #include "EventListener.h"
     37 #include "Frame.h"
     38 #include "ScriptScope.h"
     39 #include "DocumentParser.h"
     40 #include "V8AbstractEventListener.h"
     41 #include "V8Binding.h"
     42 
     43 namespace WebCore {
     44 
     45 PassRefPtr<V8LazyEventListener> createAttributeEventListener(Node* node, Attribute* attr)
     46 {
     47     ASSERT(node);
     48     ASSERT(attr);
     49     if (attr->isNull())
     50         return 0;
     51 
     52     // FIXME: Very strange: we initialize zero-based number with '1'.
     53     TextPosition0 position(WTF::ZeroBasedNumber::fromZeroBasedInt(1), WTF::ZeroBasedNumber::base());
     54     String sourceURL;
     55 
     56     if (Frame* frame = node->document()->frame()) {
     57         ScriptController* scriptController = frame->script();
     58         if (!scriptController->canExecuteScripts(AboutToExecuteScript))
     59             return 0;
     60 
     61         position = scriptController->eventHandlerPosition();
     62         sourceURL = node->document()->url().string();
     63     }
     64 
     65     return V8LazyEventListener::create(attr->localName().string(), node->isSVGElement(), attr->value(), sourceURL, position, WorldContextHandle(UseMainWorld));
     66 }
     67 
     68 PassRefPtr<V8LazyEventListener> createAttributeEventListener(Frame* frame, Attribute* attr)
     69 {
     70     if (!frame)
     71         return 0;
     72 
     73     ASSERT(attr);
     74     if (attr->isNull())
     75         return 0;
     76 
     77     ScriptController* scriptController = frame->script();
     78     if (!scriptController->canExecuteScripts(AboutToExecuteScript))
     79         return 0;
     80 
     81     TextPosition0 position = scriptController->eventHandlerPosition();
     82     String sourceURL = frame->document()->url().string();
     83     return V8LazyEventListener::create(attr->localName().string(), frame->document()->isSVGDocument(), attr->value(), sourceURL, position, WorldContextHandle(UseMainWorld));
     84 }
     85 
     86 String eventListenerHandlerBody(Document* document, EventListener* listener)
     87 {
     88     if (listener->type() != EventListener::JSEventListenerType)
     89         return "";
     90 
     91     v8::HandleScope scope;
     92     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
     93     v8::Handle<v8::Context> context = toV8Context(document, v8Listener->worldContext());
     94     v8::Context::Scope contextScope(context);
     95     v8::Handle<v8::Object> function = v8Listener->getListenerObject(document);
     96     if (function.IsEmpty())
     97         return "";
     98 
     99     return toWebCoreStringWithNullCheck(function);
    100 }
    101 
    102 bool eventListenerHandlerLocation(Document* document, EventListener* listener, String& sourceName, int& lineNumber)
    103 {
    104     if (listener->type() != EventListener::JSEventListenerType)
    105         return false;
    106 
    107     v8::HandleScope scope;
    108     V8AbstractEventListener* v8Listener = static_cast<V8AbstractEventListener*>(listener);
    109     v8::Handle<v8::Context> context = toV8Context(document, v8Listener->worldContext());
    110     v8::Context::Scope contextScope(context);
    111     v8::Handle<v8::Object> object = v8Listener->getListenerObject(document);
    112     if (object.IsEmpty() || !object->IsFunction())
    113         return false;
    114 
    115     v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(object);
    116     v8::ScriptOrigin origin = function->GetScriptOrigin();
    117     if (!origin.ResourceName().IsEmpty()) {
    118         sourceName = toWebCoreString(origin.ResourceName());
    119         lineNumber = function->GetScriptLineNumber() + 1;
    120         return true;
    121     }
    122     return false;
    123 }
    124 
    125 } // namespace WebCore
    126