From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- widget/android/EventDispatcher.h | 104 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 widget/android/EventDispatcher.h (limited to 'widget/android/EventDispatcher.h') diff --git a/widget/android/EventDispatcher.h b/widget/android/EventDispatcher.h new file mode 100644 index 0000000000..a7daa18d7d --- /dev/null +++ b/widget/android/EventDispatcher.h @@ -0,0 +1,104 @@ +/* -*- Mode: c++; c-basic-offset: 2; tab-width: 20; indent-tabs-mode: nil; -*- + * vim: set sw=2 ts=4 expandtab: + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_widget_EventDispatcher_h +#define mozilla_widget_EventDispatcher_h + +#include "jsapi.h" +#include "nsClassHashtable.h" +#include "nsCOMArray.h" +#include "nsIAndroidBridge.h" +#include "nsHashKeys.h" +#include "nsPIDOMWindow.h" + +#include "mozilla/java/EventDispatcherNatives.h" +#include "mozilla/java/GeckoBundleWrappers.h" +#include "mozilla/Mutex.h" + +namespace mozilla { +namespace widget { + +/** + * EventDispatcher is the Gecko counterpart to the Java EventDispatcher class. + * Together, they make up a unified event bus. Events dispatched from the Java + * side may notify event listeners on the Gecko side, and vice versa. + */ +class EventDispatcher final + : public nsIAndroidEventDispatcher, + public java::EventDispatcher::Natives { + using NativesBase = java::EventDispatcher::Natives; + + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIANDROIDEVENTDISPATCHER + + EventDispatcher() {} + + void Attach(java::EventDispatcher::Param aDispatcher, + nsPIDOMWindowOuter* aDOMWindow); + void Detach(); + + nsresult Dispatch(const char16_t* aEvent, + java::GeckoBundle::Param aData = nullptr, + nsIAndroidEventCallback* aCallback = nullptr); + + bool HasListener(const char16_t* aEvent); + bool HasGeckoListener(jni::String::Param aEvent); + void DispatchToGecko(jni::String::Param aEvent, jni::Object::Param aData, + jni::Object::Param aCallback); + + static nsresult UnboxBundle(JSContext* aCx, jni::Object::Param aData, + JS::MutableHandle aOut); + + nsIGlobalObject* GetGlobalObject(); + + using NativesBase::DisposeNative; + + private: + friend class java::EventDispatcher::Natives; + + java::EventDispatcher::WeakRef mDispatcher; + nsCOMPtr mDOMWindow; + + virtual ~EventDispatcher() {} + + void Shutdown(); + + struct ListenersList { + nsCOMArray listeners{/* count */ 1}; + // 0 if the list can be modified + uint32_t lockCount{0}; + // true if this list has a listener that is being unregistered + bool unregistering{false}; + }; + + using ListenersMap = nsClassHashtable; + + Mutex mLock MOZ_UNANNOTATED{"mozilla::widget::EventDispatcher"}; + ListenersMap mListenersMap; + + using IterateEventsCallback = + nsresult (EventDispatcher::*)(const nsAString&, nsIAndroidEventListener*); + + nsresult IterateEvents(JSContext* aCx, JS::Handle aEvents, + IterateEventsCallback aCallback, + nsIAndroidEventListener* aListener); + nsresult RegisterEventLocked(const nsAString&, nsIAndroidEventListener*); + nsresult UnregisterEventLocked(const nsAString&, nsIAndroidEventListener*); + + nsresult DispatchOnGecko(ListenersList* list, const nsAString& aEvent, + JS::Handle aData, + nsIAndroidEventCallback* aCallback); + + java::EventDispatcher::NativeCallbackDelegate::LocalRef WrapCallback( + nsIAndroidEventCallback* aCallback, + nsIAndroidEventFinalizer* aFinalizer = nullptr); +}; + +} // namespace widget +} // namespace mozilla + +#endif // mozilla_widget_EventDispatcher_h -- cgit v1.2.3