From 24af1645ffc762e20367e8295d7d57f4282cf601 Mon Sep 17 00:00:00 2001 From: Masayuki Nakano Date: Wed, 2 Apr 2014 11:00:45 +0900 Subject: [PATCH] Bug 984253 part.3 Rename nsEventHandler to mozilla::TypedEventHandler r=smaug --- dom/events/EventListenerManager.cpp | 43 ++++++++-------- dom/events/EventListenerManager.h | 27 +++++----- dom/events/EventListenerService.cpp | 5 +- dom/events/JSEventHandler.cpp | 33 +++++++------ dom/events/JSEventHandler.h | 77 ++++++++++++++--------------- dom/xbl/nsXBLPrototypeHandler.cpp | 4 +- 6 files changed, 93 insertions(+), 96 deletions(-) diff --git a/dom/events/EventListenerManager.cpp b/dom/events/EventListenerManager.cpp index b9cc65c4e10..b5126426e10 100644 --- a/dom/events/EventListenerManager.cpp +++ b/dom/events/EventListenerManager.cpp @@ -599,7 +599,7 @@ EventListenerManager::Listener* EventListenerManager::SetEventHandlerInternal( nsIAtom* aName, const nsAString& aTypeString, - const nsEventHandler& aHandler, + const TypedEventHandler& aTypedHandler, bool aPermitUntrustedEvents) { MOZ_ASSERT(aName || !aTypeString.IsEmpty()); @@ -615,7 +615,7 @@ EventListenerManager::SetEventHandlerInternal( nsCOMPtr jsListener; NS_NewJSEventHandler(mTarget, aName, - aHandler, getter_AddRefs(jsListener)); + aTypedHandler, getter_AddRefs(jsListener)); EventListenerHolder listenerHolder(jsListener); AddEventListenerInternal(listenerHolder, eventType, aName, aTypeString, flags, true); @@ -626,9 +626,9 @@ EventListenerManager::SetEventHandlerInternal( MOZ_ASSERT(jsListener, "How can we have an event handler with no nsJSEventListener?"); - bool same = jsListener->GetHandler() == aHandler; + bool same = jsListener->GetTypedEventHandler() == aTypedHandler; // Possibly the same listener, but update still the context and scope. - jsListener->SetHandler(aHandler); + jsListener->SetHandler(aTypedHandler); if (mTarget && !same && aName) { mTarget->EventListenerRemoved(aName); mTarget->EventListenerAdded(aName); @@ -636,7 +636,7 @@ EventListenerManager::SetEventHandlerInternal( } // Set flag to indicate possible need for compilation later - listener->mHandlerIsString = !aHandler.HasEventHandler(); + listener->mHandlerIsString = !aTypedHandler.HasEventHandler(); if (aPermitUntrustedEvents) { listener->mFlags.mAllowUntrustedEvents = true; } @@ -747,7 +747,7 @@ EventListenerManager::SetEventHandler(nsIAtom* aName, Listener* listener = SetEventHandlerInternal(aName, EmptyString(), - nsEventHandler(), + TypedEventHandler(), aPermitUntrustedEvents); if (!aDeferCompilation) { @@ -786,7 +786,8 @@ EventListenerManager::CompileEventHandlerInternal(Listener* aListener, MOZ_ASSERT(aListener->GetJSListener()); MOZ_ASSERT(aListener->mHandlerIsString, "Why are we compiling a non-string JS listener?"); nsJSEventListener* jsListener = aListener->GetJSListener(); - MOZ_ASSERT(!jsListener->GetHandler().HasEventHandler(), "What is there to compile?"); + MOZ_ASSERT(!jsListener->GetTypedEventHandler().HasEventHandler(), + "What is there to compile?"); nsresult result = NS_OK; nsCOMPtr doc; @@ -1222,7 +1223,7 @@ EventListenerManager::SetEventHandler(nsIAtom* aEventName, // Untrusted events are always permitted for non-chrome script // handlers. - SetEventHandlerInternal(aEventName, aTypeString, nsEventHandler(aHandler), + SetEventHandlerInternal(aEventName, aTypeString, TypedEventHandler(aHandler), !mIsMainThreadELM || !nsContentUtils::IsCallerChrome()); } @@ -1239,7 +1240,7 @@ EventListenerManager::SetEventHandler(OnErrorEventHandlerNonNull* aHandler) // Untrusted events are always permitted for non-chrome script // handlers. SetEventHandlerInternal(nsGkAtoms::onerror, EmptyString(), - nsEventHandler(aHandler), + TypedEventHandler(aHandler), !nsContentUtils::IsCallerChrome()); } else { if (!aHandler) { @@ -1249,7 +1250,7 @@ EventListenerManager::SetEventHandler(OnErrorEventHandlerNonNull* aHandler) // Untrusted events are always permitted. SetEventHandlerInternal(nullptr, NS_LITERAL_STRING("error"), - nsEventHandler(aHandler), true); + TypedEventHandler(aHandler), true); } } @@ -1265,14 +1266,14 @@ EventListenerManager::SetEventHandler( // Untrusted events are always permitted for non-chrome script // handlers. SetEventHandlerInternal(nsGkAtoms::onbeforeunload, EmptyString(), - nsEventHandler(aHandler), + TypedEventHandler(aHandler), !mIsMainThreadELM || !nsContentUtils::IsCallerChrome()); } -const nsEventHandler* -EventListenerManager::GetEventHandlerInternal(nsIAtom* aEventName, - const nsAString& aTypeString) +const TypedEventHandler* +EventListenerManager::GetTypedEventHandler(nsIAtom* aEventName, + const nsAString& aTypeString) { uint32_t eventType = nsContentUtils::GetEventId(aEventName); Listener* listener = FindEventHandler(eventType, aEventName, aTypeString); @@ -1287,12 +1288,8 @@ EventListenerManager::GetEventHandlerInternal(nsIAtom* aEventName, CompileEventHandlerInternal(listener, nullptr, nullptr); } - const nsEventHandler& handler = jsListener->GetHandler(); - if (handler.HasEventHandler()) { - return &handler; - } - - return nullptr; + const TypedEventHandler& typedHandler = jsListener->GetTypedEventHandler(); + return typedHandler.HasEventHandler() ? &typedHandler : nullptr; } size_t @@ -1318,8 +1315,10 @@ EventListenerManager::MarkForCC() const Listener& listener = mListeners.ElementAt(i); nsJSEventListener* jsListener = listener.GetJSListener(); if (jsListener) { - if (jsListener->GetHandler().HasEventHandler()) { - JS::ExposeObjectToActiveJS(jsListener->GetHandler().Ptr()->Callable()); + const TypedEventHandler& typedHandler = + jsListener->GetTypedEventHandler(); + if (typedHandler.HasEventHandler()) { + JS::ExposeObjectToActiveJS(typedHandler.Ptr()->Callable()); } } else if (listener.mListenerType == Listener::eWrappedJSListener) { xpc_TryUnmarkWrappedGrayObject(listener.mListener.GetXPCOMCallback()); diff --git a/dom/events/EventListenerManager.h b/dom/events/EventListenerManager.h index a1cbd8ff5b2..b2e2c03423b 100644 --- a/dom/events/EventListenerManager.h +++ b/dom/events/EventListenerManager.h @@ -449,7 +449,7 @@ protected: */ Listener* SetEventHandlerInternal(nsIAtom* aName, const nsAString& aTypeString, - const nsEventHandler& aHandler, + const TypedEventHandler& aHandler, bool aPermitUntrustedEvents); bool IsDeviceType(uint32_t aType); @@ -479,27 +479,24 @@ public: dom::EventHandlerNonNull* GetEventHandler(nsIAtom* aEventName, const nsAString& aTypeString) { - const nsEventHandler* handler = - GetEventHandlerInternal(aEventName, aTypeString); - return handler ? handler->EventHandler() : nullptr; + const TypedEventHandler* typedHandler = + GetTypedEventHandler(aEventName, aTypeString); + return typedHandler ? typedHandler->NormalEventHandler() : nullptr; } dom::OnErrorEventHandlerNonNull* GetOnErrorEventHandler() { - const nsEventHandler* handler; - if (mIsMainThreadELM) { - handler = GetEventHandlerInternal(nsGkAtoms::onerror, EmptyString()); - } else { - handler = GetEventHandlerInternal(nullptr, NS_LITERAL_STRING("error")); - } - return handler ? handler->OnErrorEventHandler() : nullptr; + const TypedEventHandler* typedHandler = mIsMainThreadELM ? + GetTypedEventHandler(nsGkAtoms::onerror, EmptyString()) : + GetTypedEventHandler(nullptr, NS_LITERAL_STRING("error")); + return typedHandler ? typedHandler->OnErrorEventHandler() : nullptr; } dom::OnBeforeUnloadEventHandlerNonNull* GetOnBeforeUnloadEventHandler() { - const nsEventHandler* handler = - GetEventHandlerInternal(nsGkAtoms::onbeforeunload, EmptyString()); - return handler ? handler->OnBeforeUnloadEventHandler() : nullptr; + const TypedEventHandler* typedHandler = + GetTypedEventHandler(nsGkAtoms::onbeforeunload, EmptyString()); + return typedHandler ? typedHandler->OnBeforeUnloadEventHandler() : nullptr; } protected: @@ -507,7 +504,7 @@ protected: * Helper method for implementing the various Get*EventHandler above. Will * return null if we don't have an event handler for this event name. */ - const nsEventHandler* GetEventHandlerInternal(nsIAtom* aEventName, + const TypedEventHandler* GetTypedEventHandler(nsIAtom* aEventName, const nsAString& aTypeString); void AddEventListener(const nsAString& aType, diff --git a/dom/events/EventListenerService.cpp b/dom/events/EventListenerService.cpp index 4c6f820919b..6da06fc4da8 100644 --- a/dom/events/EventListenerService.cpp +++ b/dom/events/EventListenerService.cpp @@ -102,8 +102,9 @@ EventListenerInfo::GetJSVal(JSContext* aCx, } nsCOMPtr jsl = do_QueryInterface(mListener); - if (jsl && jsl->GetHandler().HasEventHandler()) { - JS::Handle handler(jsl->GetHandler().Ptr()->Callable()); + if (jsl && jsl->GetTypedEventHandler().HasEventHandler()) { + JS::Handle handler = + jsl->GetTypedEventHandler().Ptr()->Callable(); if (handler) { aAc.construct(aCx, handler); aJSVal.setObject(*handler); diff --git a/dom/events/JSEventHandler.cpp b/dom/events/JSEventHandler.cpp index 0f40bffc997..13eac790be8 100644 --- a/dom/events/JSEventHandler.cpp +++ b/dom/events/JSEventHandler.cpp @@ -45,9 +45,9 @@ using namespace mozilla::dom; */ nsJSEventListener::nsJSEventListener(nsISupports *aTarget, nsIAtom* aType, - const nsEventHandler& aHandler) + const TypedEventHandler& aTypedHandler) : mEventName(aType) - , mHandler(aHandler) + , mTypedHandler(aTypedHandler) { nsCOMPtr base = do_QueryInterface(aTarget); mTarget = base.get(); @@ -56,7 +56,7 @@ nsJSEventListener::nsJSEventListener(nsISupports *aTarget, NS_IMPL_CYCLE_COLLECTION_CLASS(nsJSEventListener) NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsJSEventListener) - tmp->mHandler.ForgetHandler(); + tmp->mTypedHandler.ForgetHandler(); NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsJSEventListener) if (MOZ_UNLIKELY(cb.WantDebugInfo()) && tmp->mEventName) { @@ -68,7 +68,7 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsJSEventListener) } else { NS_IMPL_CYCLE_COLLECTION_DESCRIBE(nsJSEventListener, tmp->mRefCnt.get()) } - NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mHandler.Ptr()) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mTypedHandler.Ptr()) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END @@ -113,15 +113,16 @@ nsJSEventListener::IsBlackForCC() { // We can claim to be black if all the things we reference are // effectively black already. - return !mHandler.HasEventHandler() || !mHandler.Ptr()->HasGrayCallable(); + return !mTypedHandler.HasEventHandler() || + !mTypedHandler.Ptr()->HasGrayCallable(); } nsresult nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent) { nsCOMPtr target = do_QueryInterface(mTarget); - if (!target || !mHandler.HasEventHandler() || - !GetHandler().Ptr()->CallbackPreserveColor()) { + if (!target || !mTypedHandler.HasEventHandler() || + !GetTypedEventHandler().Ptr()->CallbackPreserveColor()) { return NS_ERROR_FAILURE; } @@ -130,11 +131,11 @@ nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent) bool isChromeHandler = isMainThread ? nsContentUtils::GetObjectPrincipal( - GetHandler().Ptr()->CallbackPreserveColor()) == + GetTypedEventHandler().Ptr()->CallbackPreserveColor()) == nsContentUtils::GetSystemPrincipal() : mozilla::dom::workers::IsCurrentThreadRunningChromeWorker(); - if (mHandler.Type() == nsEventHandler::eOnError) { + if (mTypedHandler.Type() == TypedEventHandler::eOnError) { MOZ_ASSERT_IF(mEventName, mEventName == nsGkAtoms::onerror); nsString errorMsg, file; @@ -167,7 +168,7 @@ nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent) } nsRefPtr handler = - mHandler.OnErrorEventHandler(); + mTypedHandler.OnErrorEventHandler(); ErrorResult rv; bool handled = handler->Call(mTarget, msgOrEvent, fileName, lineNumber, columnNumber, error, rv); @@ -181,11 +182,11 @@ nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent) return NS_OK; } - if (mHandler.Type() == nsEventHandler::eOnBeforeUnload) { + if (mTypedHandler.Type() == TypedEventHandler::eOnBeforeUnload) { MOZ_ASSERT(mEventName == nsGkAtoms::onbeforeunload); nsRefPtr handler = - mHandler.OnBeforeUnloadEventHandler(); + mTypedHandler.OnBeforeUnloadEventHandler(); ErrorResult rv; nsString retval; handler->Call(mTarget, *(aEvent->InternalDOMEvent()), retval, rv); @@ -213,9 +214,9 @@ nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent) return NS_OK; } - MOZ_ASSERT(mHandler.Type() == nsEventHandler::eNormal); + MOZ_ASSERT(mTypedHandler.Type() == TypedEventHandler::eNormal); ErrorResult rv; - nsRefPtr handler = mHandler.EventHandler(); + nsRefPtr handler = mTypedHandler.NormalEventHandler(); JS::Value retval = handler->Call(mTarget, *(aEvent->InternalDOMEvent()), rv); if (rv.Failed()) { @@ -241,12 +242,12 @@ nsJSEventListener::HandleEvent(nsIDOMEvent* aEvent) nsresult NS_NewJSEventHandler(nsISupports* aTarget, nsIAtom* aEventType, - const nsEventHandler& aHandler, + const TypedEventHandler& aTypedHandler, nsJSEventListener** aReturn) { NS_ENSURE_ARG(aEventType || !NS_IsMainThread()); nsJSEventListener* it = - new nsJSEventListener(aTarget, aEventType, aHandler); + new nsJSEventListener(aTarget, aEventType, aTypedHandler); NS_ADDREF(*aReturn = it); return NS_OK; diff --git a/dom/events/JSEventHandler.h b/dom/events/JSEventHandler.h index 729cf54ed13..cb592f60514 100644 --- a/dom/events/JSEventHandler.h +++ b/dom/events/JSEventHandler.h @@ -16,15 +16,11 @@ #include "nsIDOMEventListener.h" #include "nsIScriptContext.h" -class nsEventHandler +namespace mozilla { + +class TypedEventHandler { public: - typedef mozilla::dom::EventHandlerNonNull EventHandlerNonNull; - typedef mozilla::dom::OnBeforeUnloadEventHandlerNonNull - OnBeforeUnloadEventHandlerNonNull; - typedef mozilla::dom::OnErrorEventHandlerNonNull OnErrorEventHandlerNonNull; - typedef mozilla::dom::CallbackFunction CallbackFunction; - enum HandlerType { eUnset = 0, @@ -34,27 +30,27 @@ public: eTypeBits = 0x3 }; - nsEventHandler() + TypedEventHandler() : mBits(0) { } - nsEventHandler(EventHandlerNonNull* aHandler) + TypedEventHandler(dom::EventHandlerNonNull* aHandler) { Assign(aHandler, eNormal); } - nsEventHandler(OnErrorEventHandlerNonNull* aHandler) + TypedEventHandler(dom::OnErrorEventHandlerNonNull* aHandler) { Assign(aHandler, eOnError); } - nsEventHandler(OnBeforeUnloadEventHandlerNonNull* aHandler) + TypedEventHandler(dom::OnBeforeUnloadEventHandlerNonNull* aHandler) { Assign(aHandler, eOnBeforeUnload); } - nsEventHandler(const nsEventHandler& aOther) + TypedEventHandler(const TypedEventHandler& aOther) { if (aOther.HasEventHandler()) { // Have to make sure we take our own ref @@ -64,7 +60,7 @@ public: } } - ~nsEventHandler() + ~TypedEventHandler() { ReleaseHandler(); } @@ -79,7 +75,7 @@ public: return !!Ptr(); } - void SetHandler(const nsEventHandler& aHandler) + void SetHandler(const TypedEventHandler& aHandler) { if (aHandler.HasEventHandler()) { ReleaseHandler(); @@ -89,47 +85,48 @@ public: } } - EventHandlerNonNull* EventHandler() const + dom::EventHandlerNonNull* NormalEventHandler() const { MOZ_ASSERT(Type() == eNormal && Ptr()); - return reinterpret_cast(Ptr()); + return reinterpret_cast(Ptr()); } - void SetHandler(EventHandlerNonNull* aHandler) + void SetHandler(dom::EventHandlerNonNull* aHandler) { ReleaseHandler(); Assign(aHandler, eNormal); } - OnBeforeUnloadEventHandlerNonNull* OnBeforeUnloadEventHandler() const + dom::OnBeforeUnloadEventHandlerNonNull* OnBeforeUnloadEventHandler() const { MOZ_ASSERT(Type() == eOnBeforeUnload); - return reinterpret_cast(Ptr()); + return reinterpret_cast(Ptr()); } - void SetHandler(OnBeforeUnloadEventHandlerNonNull* aHandler) + void SetHandler(dom::OnBeforeUnloadEventHandlerNonNull* aHandler) { ReleaseHandler(); Assign(aHandler, eOnBeforeUnload); } - OnErrorEventHandlerNonNull* OnErrorEventHandler() const + dom::OnErrorEventHandlerNonNull* OnErrorEventHandler() const { MOZ_ASSERT(Type() == eOnError); - return reinterpret_cast(Ptr()); + return reinterpret_cast(Ptr()); } - void SetHandler(OnErrorEventHandlerNonNull* aHandler) + void SetHandler(dom::OnErrorEventHandlerNonNull* aHandler) { ReleaseHandler(); Assign(aHandler, eOnError); } - CallbackFunction* Ptr() const + dom::CallbackFunction* Ptr() const { // Have to cast eTypeBits so we don't have to worry about // promotion issues after the bitflip. - return reinterpret_cast(mBits & ~uintptr_t(eTypeBits)); + return reinterpret_cast(mBits & + ~uintptr_t(eTypeBits)); } void ForgetHandler() @@ -138,7 +135,7 @@ public: mBits = 0; } - bool operator==(const nsEventHandler& aOther) const + bool operator==(const TypedEventHandler& aOther) const { return Ptr() && aOther.Ptr() && @@ -146,7 +143,7 @@ public: } private: - void operator=(const nsEventHandler&) MOZ_DELETE; + void operator=(const TypedEventHandler&) MOZ_DELETE; void ReleaseHandler() { @@ -164,6 +161,8 @@ private: uintptr_t mBits; }; +} // namespace mozilla + /** * Implemented by script event listeners. Used to retrieve the script object * corresponding to the event target and the handler itself. @@ -182,7 +181,7 @@ public: NS_DECLARE_STATIC_IID_ACCESSOR(NS_JSEVENTLISTENER_IID) nsJSEventListener(nsISupports* aTarget, nsIAtom* aType, - const nsEventHandler& aHandler); + const mozilla::TypedEventHandler& aTypedHandler); virtual ~nsJSEventListener() { @@ -204,14 +203,14 @@ public: mTarget = nullptr; } - const nsEventHandler& GetHandler() const + const mozilla::TypedEventHandler& GetTypedEventHandler() const { - return mHandler; + return mTypedHandler; } void ForgetHandler() { - mHandler.ForgetHandler(); + mTypedHandler.ForgetHandler(); } nsIAtom* EventName() const @@ -221,21 +220,21 @@ public: // Set a handler for this event listener. The handler must already // be bound to the right target. - void SetHandler(const nsEventHandler& aHandler) + void SetHandler(const mozilla::TypedEventHandler& aTypedHandler) { - mHandler.SetHandler(aHandler); + mTypedHandler.SetHandler(aTypedHandler); } void SetHandler(mozilla::dom::EventHandlerNonNull* aHandler) { - mHandler.SetHandler(aHandler); + mTypedHandler.SetHandler(aHandler); } void SetHandler(mozilla::dom::OnBeforeUnloadEventHandlerNonNull* aHandler) { - mHandler.SetHandler(aHandler); + mTypedHandler.SetHandler(aHandler); } void SetHandler(mozilla::dom::OnErrorEventHandlerNonNull* aHandler) { - mHandler.SetHandler(aHandler); + mTypedHandler.SetHandler(aHandler); } size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const @@ -247,7 +246,7 @@ public: // - mTarget // // The following members are not measured: - // - mHandler: may be shared with others + // - mTypedHandler: may be shared with others // - mEventName: shared with others } @@ -263,7 +262,7 @@ public: protected: nsISupports* mTarget; nsCOMPtr mEventName; - nsEventHandler mHandler; + mozilla::TypedEventHandler mTypedHandler; }; NS_DEFINE_STATIC_IID_ACCESSOR(nsJSEventListener, NS_JSEVENTLISTENER_IID) @@ -274,7 +273,7 @@ NS_DEFINE_STATIC_IID_ACCESSOR(nsJSEventListener, NS_JSEVENTLISTENER_IID) */ nsresult NS_NewJSEventHandler(nsISupports* aTarget, nsIAtom* aType, - const nsEventHandler& aHandler, + const mozilla::TypedEventHandler& aTypedHandler, nsJSEventListener** aReturn); #endif // mozilla_JSEventHandler_h_ diff --git a/dom/xbl/nsXBLPrototypeHandler.cpp b/dom/xbl/nsXBLPrototypeHandler.cpp index aee4065f1e1..d120092d73e 100644 --- a/dom/xbl/nsXBLPrototypeHandler.cpp +++ b/dom/xbl/nsXBLPrototypeHandler.cpp @@ -322,12 +322,12 @@ nsXBLPrototypeHandler::ExecuteHandler(EventTarget* aTarget, nsRefPtr handlerCallback = new EventHandlerNonNull(bound, /* aIncumbentGlobal = */ nullptr); - nsEventHandler eventHandler(handlerCallback); + TypedEventHandler typedHandler(handlerCallback); // Execute it. nsCOMPtr eventListener; rv = NS_NewJSEventHandler(scriptTarget, onEventAtom, - eventHandler, + typedHandler, getter_AddRefs(eventListener)); NS_ENSURE_SUCCESS(rv, rv);