| OLD | NEW |
| (Empty) |
| 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 "V8Proxy.h" | |
| 37 | |
| 38 namespace WebCore { | |
| 39 | |
| 40 V8LazyEventListener::V8LazyEventListener(Frame *frame, const String& code, const
String& functionName) | |
| 41 : V8AbstractEventListener(frame, true) | |
| 42 , m_code(code) | |
| 43 , m_functionName(functionName) | |
| 44 , m_compiled(false) | |
| 45 , m_wrappedFunctionCompiled(false) | |
| 46 { | |
| 47 } | |
| 48 | |
| 49 V8LazyEventListener::~V8LazyEventListener() | |
| 50 { | |
| 51 disposeListenerObject(); | |
| 52 | |
| 53 // Dispose wrapped function | |
| 54 if (!m_wrappedFunction.IsEmpty()) { | |
| 55 #ifndef NDEBUG | |
| 56 V8Proxy::UnregisterGlobalHandle(this, m_wrappedFunction); | |
| 57 #endif | |
| 58 m_wrappedFunction.Dispose(); | |
| 59 m_wrappedFunction.Clear(); | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 v8::Local<v8::Function> V8LazyEventListener::getListenerFunction() | |
| 64 { | |
| 65 if (m_compiled) { | |
| 66 ASSERT(m_listener.IsEmpty() || m_listener->IsFunction()); | |
| 67 return m_listener.IsEmpty() ? v8::Local<v8::Function>() : v8::Local<v8::
Function>::New(v8::Persistent<v8::Function>::Cast(m_listener)); | |
| 68 } | |
| 69 | |
| 70 m_compiled = true; | |
| 71 | |
| 72 ASSERT(m_frame); | |
| 73 | |
| 74 { | |
| 75 // Switch to the context of m_frame. | |
| 76 v8::HandleScope handleScope; | |
| 77 | |
| 78 // Use the outer scope to hold context. | |
| 79 v8::Handle<v8::Context> context = V8Proxy::GetContext(m_frame); | |
| 80 // Bail out if we could not get the context. | |
| 81 if (context.IsEmpty()) | |
| 82 return v8::Local<v8::Function>(); | |
| 83 | |
| 84 v8::Context::Scope scope(context); | |
| 85 | |
| 86 // Wrap function around the event code. The parenthesis around the func
tion are needed so that evaluating the code yields | |
| 87 // the function value. Without the parenthesis the function value is th
rown away. | |
| 88 | |
| 89 // Make it an anonymous function to avoid name conflict for cases like | |
| 90 // <body onload='onload()'> | |
| 91 // <script> function onload() { alert('hi'); } </script>. | |
| 92 // Set function name to function object instead. | |
| 93 // See issue 944690. | |
| 94 // | |
| 95 // The ECMAScript spec says (very obliquely) that the parameter to an ev
ent handler is named "evt". | |
| 96 String code = "(function (evt) {\n"; | |
| 97 code.append(m_code); | |
| 98 code.append("})"); | |
| 99 | |
| 100 v8::Handle<v8::String> codeExternalString = v8ExternalString(code); | |
| 101 v8::Handle<v8::Script> script = V8Proxy::CompileScript(codeExternalStrin
g, m_frame->document()->url(), m_lineNumber - 1); | |
| 102 if (!script.IsEmpty()) { | |
| 103 V8Proxy* proxy = V8Proxy::retrieve(m_frame); | |
| 104 ASSERT(proxy); | |
| 105 v8::Local<v8::Value> value = proxy->RunScript(script, false); | |
| 106 if (!value.IsEmpty()) { | |
| 107 ASSERT(value->IsFunction()); | |
| 108 v8::Local<v8::Function> listenerFunction = v8::Local<v8::Functio
n>::Cast(value); | |
| 109 listenerFunction->SetName(v8::String::New(FromWebCoreString(m_fu
nctionName), m_functionName.length())); | |
| 110 | |
| 111 m_listener = v8::Persistent<v8::Function>::New(listenerFunction)
; | |
| 112 #ifndef NDEBUG | |
| 113 V8Proxy::RegisterGlobalHandle(EVENT_LISTENER, this, m_listener); | |
| 114 #endif | |
| 115 } | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 ASSERT(m_listener.IsEmpty() || m_listener->IsFunction()); | |
| 120 return m_listener.IsEmpty() ? v8::Local<v8::Function>() : v8::Local<v8::Func
tion>::New(v8::Persistent<v8::Function>::Cast(m_listener)); | |
| 121 } | |
| 122 | |
| 123 v8::Local<v8::Value> V8LazyEventListener::callListenerFunction(v8::Handle<v8::Va
lue> jsEvent, Event* event, bool isWindowEvent) | |
| 124 { | |
| 125 v8::Local<v8::Function> handlerFunction = getWrappedListenerFunction(); | |
| 126 v8::Local<v8::Object> receiver = getReceiverObject(event, isWindowEvent); | |
| 127 if (handlerFunction.IsEmpty() || receiver.IsEmpty()) | |
| 128 return v8::Local<v8::Value>(); | |
| 129 | |
| 130 v8::Handle<v8::Value> parameters[1] = { jsEvent }; | |
| 131 | |
| 132 V8Proxy* proxy = V8Proxy::retrieve(m_frame); | |
| 133 return proxy->CallFunction(handlerFunction, receiver, 1, parameters); | |
| 134 } | |
| 135 | |
| 136 v8::Local<v8::Function> V8LazyEventListener::getWrappedListenerFunction() | |
| 137 { | |
| 138 if (m_wrappedFunctionCompiled) { | |
| 139 ASSERT(m_wrappedFunction.IsEmpty() || m_wrappedFunction->IsFunction()); | |
| 140 return m_wrappedFunction.IsEmpty() ? v8::Local<v8::Function>() : v8::Loc
al<v8::Function>::New(m_wrappedFunction); | |
| 141 } | |
| 142 | |
| 143 m_wrappedFunctionCompiled = true; | |
| 144 | |
| 145 { | |
| 146 // Switch to the context of m_frame. | |
| 147 v8::HandleScope handleScope; | |
| 148 | |
| 149 // Use the outer scope to hold context. | |
| 150 v8::Handle<v8::Context> context = V8Proxy::GetContext(m_frame); | |
| 151 // Bail out if we cannot get the context. | |
| 152 if (context.IsEmpty()) | |
| 153 return v8::Local<v8::Function>(); | |
| 154 | |
| 155 v8::Context::Scope scope(context); | |
| 156 | |
| 157 // FIXME: cache the wrapper function. | |
| 158 String code = "(function (evt) {\n"; | |
| 159 | |
| 160 // Nodes other than the document object, when executing inline event han
dlers push document, form, and the target node on the scope chain. | |
| 161 // We do this by using 'with' statement. | |
| 162 // See chrome/fast/forms/form-action.html | |
| 163 // chrome/fast/forms/selected-index-value.html | |
| 164 // base/fast/overflow/onscroll-layer-self-destruct.html | |
| 165 code.append(" with (this.ownerDocument ? this.ownerDocument : {}) {\n")
; | |
| 166 code.append(" with (this.form ? this.form : {}) {\n"); | |
| 167 code.append(" with (this) {\n"); | |
| 168 code.append(" return (function(evt){"); | |
| 169 code.append(m_code); | |
| 170 code.append("}).call(this, evt);\n"); | |
| 171 code.append(" }\n"); | |
| 172 code.append(" }\n"); | |
| 173 code.append(" }\n"); | |
| 174 code.append("})"); | |
| 175 v8::Handle<v8::String> codeExternalString = v8ExternalString(code); | |
| 176 v8::Handle<v8::Script> script = V8Proxy::CompileScript(codeExternalStrin
g, m_frame->document()->url(), m_lineNumber - 4); | |
| 177 if (!script.IsEmpty()) { | |
| 178 V8Proxy* proxy = V8Proxy::retrieve(m_frame); | |
| 179 ASSERT(proxy); | |
| 180 v8::Local<v8::Value> value = proxy->RunScript(script, false); | |
| 181 if (!value.IsEmpty()) { | |
| 182 ASSERT(value->IsFunction()); | |
| 183 | |
| 184 m_wrappedFunction = v8::Persistent<v8::Function>::New(v8::Local<
v8::Function>::Cast(value)); | |
| 185 #ifndef NDEBUG | |
| 186 V8Proxy::RegisterGlobalHandle(EVENT_LISTENER, this, m_wrappedFun
ction); | |
| 187 #endif | |
| 188 m_wrappedFunction->SetName(v8::String::New(fromWebCoreString(m_f
unctionName), m_functionName.length())); | |
| 189 } | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 return v8::Local<v8::Function>::New(m_wrappedFunction); | |
| 194 } | |
| 195 | |
| 196 } // namespace WebCore | |
| OLD | NEW |