OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2011 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 "DartEventListener.h" |
| 33 |
| 34 #include "DartController.h" |
| 35 #include "DartEvent.h" |
| 36 #include "DartIsolateState.h" |
| 37 #include "DartUtilities.h" |
| 38 #include "ScriptExecutionContext.h" |
| 39 |
| 40 namespace WebCore { |
| 41 |
| 42 DartEventListener::DartEventListener(Dart_Isolate isolate, Dart_Handle listener) |
| 43 : EventListener(JSEventListenerType) |
| 44 , m_listener(0) |
| 45 { |
| 46 setListenerObject(isolate, listener); |
| 47 } |
| 48 |
| 49 DartEventListener::~DartEventListener() |
| 50 { |
| 51 disposeListenerObject(); |
| 52 } |
| 53 |
| 54 PassRefPtr<DartEventListener> DartEventListener::createOrFetch(Dart_Handle liste
ner) |
| 55 { |
| 56 ASSERT(Dart_IsClosure(listener)); |
| 57 DartEventListener* nativeListener = reinterpret_cast<DartEventListener*>(Dar
t_ClosureSmrck(listener)); |
| 58 if (nativeListener) |
| 59 return nativeListener; |
| 60 return adoptRef(new DartEventListener(Dart_CurrentIsolate(), listener)); |
| 61 } |
| 62 |
| 63 void DartEventListener::handleEvent(ScriptExecutionContext* context, Event* even
t) |
| 64 { |
| 65 // Don't reenter Dart if execution was terminated in this instance of Dart. |
| 66 // FIXME: we probably need isDartExecutionForbidden. |
| 67 if (context->isJSExecutionForbidden()) |
| 68 return; |
| 69 |
| 70 // FIXME: Validate that this isolate hasn't been shutdown. |
| 71 DartIsolateState::push(m_isolate); |
| 72 ASSERT(event); |
| 73 |
| 74 // The callback function on XMLHttpRequest can clear the event listener and
destroys 'this' object. Keep a local reference to it. |
| 75 // See issue 889829. |
| 76 RefPtr<DartEventListener> protect(this); |
| 77 |
| 78 { |
| 79 DartApiScope scope; |
| 80 |
| 81 // Get the Dart wrapper for the event object. |
| 82 Dart_Handle dartEvent = toDartValue(event); |
| 83 ASSERT(dartEvent); |
| 84 |
| 85 invokeEventHandler(context, event, dartEvent); |
| 86 } |
| 87 DartIsolateState::pop(); |
| 88 } |
| 89 |
| 90 void DartEventListener::invokeEventHandler(ScriptExecutionContext* context, Even
t* event, Dart_Handle dartEvent) |
| 91 { |
| 92 if (!dartEvent) |
| 93 return; |
| 94 |
| 95 // FIXME: consider if DateExtension manipulations are necessary and if yes (
most probably), |
| 96 // factor out common logic. For example by introducing EventProcessScope RAI
I to manage DateExtension. |
| 97 Dart_Handle result = callListenerFunction(context, dartEvent, event); |
| 98 if (Dart_IsError(result)) { |
| 99 DartUtilities::reportProblem(context, result); |
| 100 return; |
| 101 } |
| 102 |
| 103 if (Dart_IsString(result)) { |
| 104 if (event->storesResultAsString()) |
| 105 event->storeResult(DartUtilities::dartStringToString(result)); |
| 106 } |
| 107 } |
| 108 |
| 109 Dart_Handle DartEventListener::callListenerFunction(ScriptExecutionContext* cont
ext, Dart_Handle dartEvent, Event* event) |
| 110 { |
| 111 if (!m_listener) |
| 112 return Dart_Error("No listener to call"); |
| 113 |
| 114 DartController* dartController = DartController::retrieve(context); |
| 115 if (!dartController) |
| 116 return Dart_Error("Internal error: failed to fetch Dart controller"); |
| 117 |
| 118 Dart_Handle parameters[1] = { dartEvent }; |
| 119 Dart_Handle result = dartController->callFunction(m_listener, 1, parameters)
; |
| 120 if (Dart_IsError(result)) |
| 121 DartUtilities::reportProblem(context, result); |
| 122 return result; |
| 123 } |
| 124 |
| 125 void DartEventListener::setListenerObject(Dart_Isolate isolate, Dart_Handle list
ener) |
| 126 { |
| 127 disposeListenerObject(); |
| 128 ASSERT(Dart_IsClosure(listener)); |
| 129 m_isolate = isolate; |
| 130 // FIXME: Validate isolate. |
| 131 ASSERT(m_isolate); |
| 132 // NOTE: DartEventListener destructor will clean wild smrck so |
| 133 // Dart wrapper never keeps the pointer to destroyed object. |
| 134 ASSERT(!Dart_ClosureSmrck(listener)); |
| 135 Dart_ClosureSetSmrck(listener, reinterpret_cast<int64_t>(this)); |
| 136 |
| 137 m_listener = Dart_NewPersistentHandle(listener); |
| 138 } |
| 139 |
| 140 void DartEventListener::disposeListenerObject() |
| 141 { |
| 142 if (!m_listener) |
| 143 return; |
| 144 |
| 145 DartIsolateState::push(m_isolate); |
| 146 |
| 147 { |
| 148 DartApiScope scope; |
| 149 |
| 150 ASSERT(this == reinterpret_cast<DartEventListener*>(Dart_ClosureSmrck(m_
listener))); |
| 151 |
| 152 // DartEventListener is going to be disposed, remove a pointer to it fro
m Dart wrapper. |
| 153 Dart_ClosureSetSmrck(m_listener, 0); |
| 154 |
| 155 // FIXME: implement persistent handles tracking a la V8GCController::unr
egisterGlobalHandle. |
| 156 Dart_DeletePersistentHandle(m_listener); |
| 157 m_listener = 0; |
| 158 } |
| 159 |
| 160 // FIXME: We probably should ref count the isolate and |
| 161 // shutdown if this is the last one. This can happen if the |
| 162 // isolate sets a listener on another frame and its original |
| 163 // frame is closed. |
| 164 DartIsolateState::pop(); |
| 165 } |
| 166 |
| 167 } |
OLD | NEW |