PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/platform/external/webkit/WebCore/bindings/v8/V8LazyEventListener.cpp

https://github.com/aharish/totoro-gb-opensource-update2
C++ | 158 lines | 86 code | 23 blank | 49 comment | 12 complexity | 1b7fd6c9b447ffb23bea5094586df0c5 MD5 | raw file
  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#include "V8LazyEventListener.h"
 33
 34#include "Frame.h"
 35#include "V8Binding.h"
 36#include "V8HiddenPropertyName.h"
 37#include "V8Proxy.h"
 38#include "WorldContextHandle.h"
 39
 40#include <wtf/StdLibExtras.h>
 41
 42namespace WebCore {
 43
 44V8LazyEventListener::V8LazyEventListener(const String& functionName, bool isSVGEvent, const String& code, const String sourceURL, int lineNumber, int columnNumber, const WorldContextHandle& worldContext)
 45    : V8AbstractEventListener(true, worldContext)
 46    , m_functionName(functionName)
 47    , m_isSVGEvent(isSVGEvent)
 48    , m_code(code)
 49    , m_sourceURL(sourceURL)
 50    , m_lineNumber(lineNumber)
 51    , m_columnNumber(columnNumber)
 52{
 53}
 54
 55v8::Local<v8::Value> V8LazyEventListener::callListenerFunction(ScriptExecutionContext* context, v8::Handle<v8::Value> jsEvent, Event* event)
 56{
 57    v8::Local<v8::Object> listenerObject = getListenerObject(context);
 58    if (listenerObject.IsEmpty())
 59        return v8::Local<v8::Value>();
 60
 61    v8::Local<v8::Function> handlerFunction = v8::Local<v8::Function>::Cast(listenerObject);
 62    v8::Local<v8::Object> receiver = getReceiverObject(event);
 63    if (handlerFunction.IsEmpty() || receiver.IsEmpty())
 64        return v8::Local<v8::Value>();
 65
 66    v8::Handle<v8::Value> parameters[1] = { jsEvent };
 67
 68    if (V8Proxy* proxy = V8Proxy::retrieve(context))
 69        return proxy->callFunction(handlerFunction, receiver, 1, parameters);
 70
 71    return v8::Local<v8::Value>();
 72}
 73
 74static v8::Handle<v8::Value> V8LazyEventListenerToString(const v8::Arguments& args)
 75{
 76    return args.Holder()->GetHiddenValue(V8HiddenPropertyName::toStringString());
 77}
 78
 79void V8LazyEventListener::prepareListenerObject(ScriptExecutionContext* context)
 80{
 81    if (hasExistingListenerObject())
 82        return;
 83
 84    v8::HandleScope handleScope;
 85
 86    V8Proxy* proxy = V8Proxy::retrieve(context);
 87    if (!proxy)
 88        return;
 89
 90    // Use the outer scope to hold context.
 91    v8::Local<v8::Context> v8Context = worldContext().adjustedContext(proxy);
 92    // Bail out if we cannot get the context.
 93    if (v8Context.IsEmpty())
 94        return;
 95
 96    v8::Context::Scope scope(v8Context);
 97
 98    // FIXME: cache the wrapper function.
 99
100    // Nodes other than the document object, when executing inline event handlers push document, form, and the target node on the scope chain.
101    // We do this by using 'with' statement.
102    // See chrome/fast/forms/form-action.html
103    //     chrome/fast/forms/selected-index-value.html
104    //     base/fast/overflow/onscroll-layer-self-destruct.html
105    //
106    // Don't use new lines so that lines in the modified handler
107    // have the same numbers as in the original code.
108    String code = "(function (evt) {" \
109            "with (this.ownerDocument ? this.ownerDocument : {}) {" \
110            "with (this.form ? this.form : {}) {" \
111            "with (this) {" \
112            "return (function(evt){";
113    code.append(m_code);
114    // Insert '\n' otherwise //-style comments could break the handler.
115    code.append(  "\n}).call(this, evt);}}}})");
116    v8::Handle<v8::String> codeExternalString = v8ExternalString(code);
117    v8::Handle<v8::Script> script = V8Proxy::compileScript(codeExternalString, m_sourceURL, m_lineNumber);
118    if (!script.IsEmpty()) {
119        v8::Local<v8::Value> value = proxy->runScript(script, false);
120        if (!value.IsEmpty()) {
121            ASSERT(value->IsFunction());
122
123            v8::Local<v8::Function> wrappedFunction = v8::Local<v8::Function>::Cast(value);
124
125            // Change the toString function on the wrapper function to avoid it
126            // returning the source for the actual wrapper function. Instead it
127            // returns source for a clean wrapper function with the event
128            // argument wrapping the event source code. The reason for this is
129            // that some web sites use toString on event functions and eval the
130            // source returned (sometimes a RegExp is applied as well) for some
131            // other use. That fails miserably if the actual wrapper source is
132            // returned.
133            DEFINE_STATIC_LOCAL(v8::Persistent<v8::FunctionTemplate>, toStringTemplate, ());
134            if (toStringTemplate.IsEmpty())
135                toStringTemplate = v8::Persistent<v8::FunctionTemplate>::New(v8::FunctionTemplate::New(V8LazyEventListenerToString));
136            v8::Local<v8::Function> toStringFunction;
137            if (!toStringTemplate.IsEmpty())
138                toStringFunction = toStringTemplate->GetFunction();
139            if (!toStringFunction.IsEmpty()) {
140                String toStringResult = "function ";
141                toStringResult.append(m_functionName);
142                toStringResult.append("(");
143                toStringResult.append(m_isSVGEvent ? "evt" : "event");
144                toStringResult.append(") {\n  ");
145                toStringResult.append(m_code);
146                toStringResult.append("\n}");
147                wrappedFunction->SetHiddenValue(V8HiddenPropertyName::toStringString(), v8ExternalString(toStringResult));
148                wrappedFunction->Set(v8::String::New("toString"), toStringFunction);
149            }
150
151            wrappedFunction->SetName(v8::String::New(fromWebCoreString(m_functionName), m_functionName.length()));
152
153            setListenerObject(wrappedFunction);
154        }
155    }
156}
157
158} // namespace WebCore