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