EventListenerService.h (3402B)
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 #ifndef mozilla_EventListenerService_h_ 8 #define mozilla_EventListenerService_h_ 9 10 #include "EventListenerManager.h" 11 #include "jsapi.h" 12 #include "nsCycleCollectionParticipant.h" 13 #include "nsGkAtoms.h" 14 #include "nsIEventListenerService.h" 15 #include "nsString.h" 16 #include "nsTHashMap.h" 17 #include "nsTObserverArray.h" 18 19 class nsIMutableArray; 20 21 namespace mozilla { 22 namespace dom { 23 class EventTarget; 24 } // namespace dom 25 26 template <typename T> 27 class Maybe; 28 29 class EventListenerChange final : public nsIEventListenerChange { 30 public: 31 explicit EventListenerChange(dom::EventTarget* aTarget); 32 33 void AddChangedListenerName(nsAtom* aEventName); 34 35 NS_DECL_ISUPPORTS 36 NS_DECL_NSIEVENTLISTENERCHANGE 37 38 protected: 39 virtual ~EventListenerChange(); 40 nsCOMPtr<dom::EventTarget> mTarget; 41 nsTArray<RefPtr<nsAtom>> mChangedListenerNames; 42 }; 43 44 class EventListenerInfo final : public nsIEventListenerInfo { 45 public: 46 EventListenerInfo(EventListenerManager* aListenerManager, 47 const nsAString& aType, 48 JS::Handle<JSObject*> aScriptedListener, 49 JS::Handle<JSObject*> aScriptedListenerGlobal, 50 bool aCapturing, bool aAllowsUntrusted, 51 bool aInSystemEventGroup, bool aIsHandler); 52 53 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 54 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(EventListenerInfo) 55 NS_DECL_NSIEVENTLISTENERINFO 56 57 protected: 58 virtual ~EventListenerInfo(); 59 60 bool GetJSVal(JSContext* aCx, Maybe<JSAutoRealm>& aAr, 61 JS::MutableHandle<JS::Value> aJSVal); 62 63 RefPtr<EventListenerManager> mListenerManager; 64 nsString mType; 65 JS::Heap<JSObject*> mScriptedListener; // May be null. 66 // mScriptedListener may be a cross-compartment wrapper so we cannot use it 67 // with JSAutoRealm because CCWs are not associated with a single realm. We 68 // use this global instead (must be same-compartment with mScriptedListener 69 // and must be non-null if mScriptedListener is non-null). 70 JS::Heap<JSObject*> mScriptedListenerGlobal; 71 bool mCapturing; 72 bool mAllowsUntrusted; 73 bool mInSystemEventGroup; 74 bool mIsHandler; 75 }; 76 77 class EventListenerService final : public nsIEventListenerService { 78 ~EventListenerService(); 79 80 public: 81 EventListenerService(); 82 NS_DECL_ISUPPORTS 83 NS_DECL_NSIEVENTLISTENERSERVICE 84 85 static void NotifyAboutMainThreadListenerChange(dom::EventTarget* aTarget, 86 nsAtom* aName) { 87 if (sInstance) { 88 sInstance->NotifyAboutMainThreadListenerChangeInternal(aTarget, aName); 89 } 90 } 91 92 void NotifyPendingChanges(); 93 94 private: 95 void NotifyAboutMainThreadListenerChangeInternal(dom::EventTarget* aTarget, 96 nsAtom* aName); 97 nsTObserverArray<nsCOMPtr<nsIListenerChangeListener>> mChangeListeners; 98 nsCOMPtr<nsIMutableArray> mPendingListenerChanges; 99 nsTHashMap<nsISupportsHashKey, RefPtr<EventListenerChange>> 100 mPendingListenerChangesSet; 101 102 static EventListenerService* sInstance; 103 }; 104 105 } // namespace mozilla 106 107 #endif // mozilla_EventListenerService_h_