Index: Source/WebCore/bindings/dart/DartEventListener.cpp |
diff --git a/Source/WebCore/bindings/dart/DartEventListener.cpp b/Source/WebCore/bindings/dart/DartEventListener.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..683b37468c2e7593449d04b4b8378143f68ebeaf |
--- /dev/null |
+++ b/Source/WebCore/bindings/dart/DartEventListener.cpp |
@@ -0,0 +1,167 @@ |
+/* |
+ * Copyright (C) 2011 Google Inc. All rights reserved. |
+ * |
+ * Redistribution and use in source and binary forms, with or without |
+ * modification, are permitted provided that the following conditions are |
+ * met: |
+ * |
+ * * Redistributions of source code must retain the above copyright |
+ * notice, this list of conditions and the following disclaimer. |
+ * * Redistributions in binary form must reproduce the above |
+ * copyright notice, this list of conditions and the following disclaimer |
+ * in the documentation and/or other materials provided with the |
+ * distribution. |
+ * * Neither the name of Google Inc. nor the names of its |
+ * contributors may be used to endorse or promote products derived from |
+ * this software without specific prior written permission. |
+ * |
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ */ |
+ |
+#include "config.h" |
+#include "DartEventListener.h" |
+ |
+#include "DartController.h" |
+#include "DartEvent.h" |
+#include "DartIsolateState.h" |
+#include "DartUtilities.h" |
+#include "ScriptExecutionContext.h" |
+ |
+namespace WebCore { |
+ |
+DartEventListener::DartEventListener(Dart_Isolate isolate, Dart_Handle listener) |
+ : EventListener(JSEventListenerType) |
+ , m_listener(0) |
+{ |
+ setListenerObject(isolate, listener); |
+} |
+ |
+DartEventListener::~DartEventListener() |
+{ |
+ disposeListenerObject(); |
+} |
+ |
+PassRefPtr<DartEventListener> DartEventListener::createOrFetch(Dart_Handle listener) |
+{ |
+ ASSERT(Dart_IsClosure(listener)); |
+ DartEventListener* nativeListener = reinterpret_cast<DartEventListener*>(Dart_ClosureSmrck(listener)); |
+ if (nativeListener) |
+ return nativeListener; |
+ return adoptRef(new DartEventListener(Dart_CurrentIsolate(), listener)); |
+} |
+ |
+void DartEventListener::handleEvent(ScriptExecutionContext* context, Event* event) |
+{ |
+ // Don't reenter Dart if execution was terminated in this instance of Dart. |
+ // FIXME: we probably need isDartExecutionForbidden. |
+ if (context->isJSExecutionForbidden()) |
+ return; |
+ |
+ // FIXME: Validate that this isolate hasn't been shutdown. |
+ DartIsolateState::push(m_isolate); |
+ ASSERT(event); |
+ |
+ // The callback function on XMLHttpRequest can clear the event listener and destroys 'this' object. Keep a local reference to it. |
+ // See issue 889829. |
+ RefPtr<DartEventListener> protect(this); |
+ |
+ { |
+ DartApiScope scope; |
+ |
+ // Get the Dart wrapper for the event object. |
+ Dart_Handle dartEvent = toDartValue(event); |
+ ASSERT(dartEvent); |
+ |
+ invokeEventHandler(context, event, dartEvent); |
+ } |
+ DartIsolateState::pop(); |
+} |
+ |
+void DartEventListener::invokeEventHandler(ScriptExecutionContext* context, Event* event, Dart_Handle dartEvent) |
+{ |
+ if (!dartEvent) |
+ return; |
+ |
+ // FIXME: consider if DateExtension manipulations are necessary and if yes (most probably), |
+ // factor out common logic. For example by introducing EventProcessScope RAII to manage DateExtension. |
+ Dart_Handle result = callListenerFunction(context, dartEvent, event); |
+ if (Dart_IsError(result)) { |
+ DartUtilities::reportProblem(context, result); |
+ return; |
+ } |
+ |
+ if (Dart_IsString(result)) { |
+ if (event->storesResultAsString()) |
+ event->storeResult(DartUtilities::dartStringToString(result)); |
+ } |
+} |
+ |
+Dart_Handle DartEventListener::callListenerFunction(ScriptExecutionContext* context, Dart_Handle dartEvent, Event* event) |
+{ |
+ if (!m_listener) |
+ return Dart_Error("No listener to call"); |
+ |
+ DartController* dartController = DartController::retrieve(context); |
+ if (!dartController) |
+ return Dart_Error("Internal error: failed to fetch Dart controller"); |
+ |
+ Dart_Handle parameters[1] = { dartEvent }; |
+ Dart_Handle result = dartController->callFunction(m_listener, 1, parameters); |
+ if (Dart_IsError(result)) |
+ DartUtilities::reportProblem(context, result); |
+ return result; |
+} |
+ |
+void DartEventListener::setListenerObject(Dart_Isolate isolate, Dart_Handle listener) |
+{ |
+ disposeListenerObject(); |
+ ASSERT(Dart_IsClosure(listener)); |
+ m_isolate = isolate; |
+ // FIXME: Validate isolate. |
+ ASSERT(m_isolate); |
+ // NOTE: DartEventListener destructor will clean wild smrck so |
+ // Dart wrapper never keeps the pointer to destroyed object. |
+ ASSERT(!Dart_ClosureSmrck(listener)); |
+ Dart_ClosureSetSmrck(listener, reinterpret_cast<int64_t>(this)); |
+ |
+ m_listener = Dart_NewPersistentHandle(listener); |
+} |
+ |
+void DartEventListener::disposeListenerObject() |
+{ |
+ if (!m_listener) |
+ return; |
+ |
+ DartIsolateState::push(m_isolate); |
+ |
+ { |
+ DartApiScope scope; |
+ |
+ ASSERT(this == reinterpret_cast<DartEventListener*>(Dart_ClosureSmrck(m_listener))); |
+ |
+ // DartEventListener is going to be disposed, remove a pointer to it from Dart wrapper. |
+ Dart_ClosureSetSmrck(m_listener, 0); |
+ |
+ // FIXME: implement persistent handles tracking a la V8GCController::unregisterGlobalHandle. |
+ Dart_DeletePersistentHandle(m_listener); |
+ m_listener = 0; |
+ } |
+ |
+ // FIXME: We probably should ref count the isolate and |
+ // shutdown if this is the last one. This can happen if the |
+ // isolate sets a listener on another frame and its original |
+ // frame is closed. |
+ DartIsolateState::pop(); |
+} |
+ |
+} |