1 /* -*- Mode: c++; c-basic-offset: 2; tab-width: 20; indent-tabs-mode: nil; -*-
2 * vim: set sw=2 ts=4 expandtab:
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/. */
7 #ifndef mozilla_widget_EventDispatcher_h
8 #define mozilla_widget_EventDispatcher_h
11 #include "nsClassHashtable.h"
12 #include "nsCOMArray.h"
13 #include "nsIAndroidBridge.h"
14 #include "nsHashKeys.h"
15 #include "nsPIDOMWindow.h"
17 #include "mozilla/java/EventDispatcherNatives.h"
18 #include "mozilla/java/GeckoBundleWrappers.h"
19 #include "mozilla/Mutex.h"
25 * EventDispatcher is the Gecko counterpart to the Java EventDispatcher class.
26 * Together, they make up a unified event bus. Events dispatched from the Java
27 * side may notify event listeners on the Gecko side, and vice versa.
29 class EventDispatcher final
30 : public nsIAndroidEventDispatcher
,
31 public java::EventDispatcher::Natives
<EventDispatcher
> {
32 using NativesBase
= java::EventDispatcher::Natives
<EventDispatcher
>;
36 NS_DECL_NSIANDROIDEVENTDISPATCHER
40 void Attach(java::EventDispatcher::Param aDispatcher
,
41 nsPIDOMWindowOuter
* aDOMWindow
);
44 nsresult
Dispatch(const char16_t
* aEvent
,
45 java::GeckoBundle::Param aData
= nullptr,
46 nsIAndroidEventCallback
* aCallback
= nullptr);
48 bool HasListener(const char16_t
* aEvent
);
49 bool HasGeckoListener(jni::String::Param aEvent
);
50 void DispatchToGecko(jni::String::Param aEvent
, jni::Object::Param aData
,
51 jni::Object::Param aCallback
);
53 static nsresult
UnboxBundle(JSContext
* aCx
, jni::Object::Param aData
,
54 JS::MutableHandleValue aOut
);
56 nsIGlobalObject
* GetGlobalObject();
58 using NativesBase::DisposeNative
;
61 java::EventDispatcher::WeakRef mDispatcher
;
62 nsCOMPtr
<nsPIDOMWindowOuter
> mDOMWindow
;
64 virtual ~EventDispatcher() {}
66 struct ListenersList
{
67 nsCOMArray
<nsIAndroidEventListener
> listeners
{/* count */ 1};
68 // 0 if the list can be modified
69 uint32_t lockCount
{0};
70 // true if this list has a listener that is being unregistered
71 bool unregistering
{false};
74 using ListenersMap
= nsClassHashtable
<nsStringHashKey
, ListenersList
>;
76 Mutex mLock
{"mozilla::widget::EventDispatcher"};
77 ListenersMap mListenersMap
;
79 using IterateEventsCallback
=
80 nsresult (EventDispatcher::*)(const nsAString
&, nsIAndroidEventListener
*);
82 nsresult
IterateEvents(JSContext
* aCx
, JS::HandleValue aEvents
,
83 IterateEventsCallback aCallback
,
84 nsIAndroidEventListener
* aListener
);
85 nsresult
RegisterEventLocked(const nsAString
&, nsIAndroidEventListener
*);
86 nsresult
UnregisterEventLocked(const nsAString
&, nsIAndroidEventListener
*);
88 nsresult
DispatchOnGecko(ListenersList
* list
, const nsAString
& aEvent
,
89 JS::HandleValue aData
,
90 nsIAndroidEventCallback
* aCallback
);
92 java::EventDispatcher::NativeCallbackDelegate::LocalRef
WrapCallback(
93 nsIAndroidEventCallback
* aCallback
,
94 nsIAndroidEventFinalizer
* aFinalizer
= nullptr);
98 } // namespace mozilla
100 #endif // mozilla_widget_EventDispatcher_h