Bug 1640914 [wpt PR 23771] - Python 3: port tests in resource-timing, a=testonly
[gecko.git] / widget / android / EventDispatcher.h
bloba4b1521c67608e3671dcc0f1946713e271c5f9bf
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
10 #include "jsapi.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"
21 namespace mozilla {
22 namespace widget {
24 /**
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>;
34 public:
35 NS_DECL_ISUPPORTS
36 NS_DECL_NSIANDROIDEVENTDISPATCHER
38 EventDispatcher() {}
40 void Attach(java::EventDispatcher::Param aDispatcher,
41 nsPIDOMWindowOuter* aDOMWindow);
42 void Detach();
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;
60 private:
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);
97 } // namespace widget
98 } // namespace mozilla
100 #endif // mozilla_widget_EventDispatcher_h