DOMEventDispatch.cpp (2028B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "js/DOMEventDispatch.h" 8 #include "js/Printf.h" 9 #include "vm/JSContext.h" 10 #include "vm/JSScript.h" 11 #include "vm/StringType.h" 12 13 JS_PUBLIC_API void JS::SetDispatchDOMEventCallback( 14 JSContext* cx, DispatchDOMEventCallback callback) { 15 if (cx && cx->runtime()) { 16 cx->runtime()->dispatchDOMEventCallback = callback; 17 } 18 } 19 20 void js::TestingDispatchDOMEvent(JSContext* cx, const char* eventType) { 21 // Dispatch callback is available when `dom.expose_test_interfaces = true` 22 if (cx->runtime()->dispatchDOMEventCallback) { 23 cx->runtime()->dispatchDOMEventCallback(cx, eventType); 24 } 25 } 26 27 void js::TestingDispatchDOMEvent(JSContext* cx, const char* eventType, 28 JSScript* script) { 29 // Dispatch callback is available when `dom.expose_test_interfaces = true` 30 if (!cx->runtime()->dispatchDOMEventCallback) { 31 return; 32 } 33 34 if (!script || !script->function()) { 35 // Fallback to just event name if script is not a function. 36 cx->runtime()->dispatchDOMEventCallback(cx, eventType); 37 return; 38 } 39 40 JS::Rooted<JSAtom*> atom(cx); 41 if (!script->function()->getDisplayAtom(cx, &atom)) { 42 // If failed, then simply just return. 43 cx->clearPendingException(); 44 return; 45 } 46 47 if (!atom) { 48 return; 49 } 50 51 JS::UniqueChars nameStr = AtomToPrintableString(cx, atom); 52 if (!nameStr) { 53 // If failed, then simply just return. 54 cx->clearPendingException(); 55 return; 56 } 57 58 // Construct event name with function name: "eventType: functionName" 59 JS::UniqueChars eventWithName = 60 JS_smprintf("%s: %s", eventType, nameStr.get()); 61 if (eventWithName) { 62 cx->runtime()->dispatchDOMEventCallback(cx, eventWithName.get()); 63 return; 64 } 65 }