Bug 665609: Remove Add/RemoveEventListenerByIID from nsTextEditorState. r=jst
authorJonas Sicking <jonas@sicking.cc>
Fri, 24 Jun 2011 22:56:13 -0700
changeset 71689 ce10fd5d82c64d7c1fda532619e89ddd4a8526ad
parent 71688 8904812b90a7721eabf0d497fd2615456f6b92d1
child 71690 b7439d5baf498cfe625ba8b7b4af1ebc2bb39dae
push id20598
push usersicking@mozilla.com
push dateSat, 25 Jun 2011 05:57:51 +0000
treeherdermozilla-central@ce10fd5d82c6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjst
bugs665609
milestone7.0a1
first release with
nightly linux32
ce10fd5d82c6 / 7.0a1 / 20110625030743 / files
nightly linux64
ce10fd5d82c6 / 7.0a1 / 20110625030743 / files
nightly mac
ce10fd5d82c6 / 7.0a1 / 20110625030743 / files
nightly win32
ce10fd5d82c6 / 7.0a1 / 20110625030743 / files
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly mac
nightly win32
Bug 665609: Remove Add/RemoveEventListenerByIID from nsTextEditorState. r=jst
content/html/content/src/nsTextEditorState.cpp
--- a/content/html/content/src/nsTextEditorState.cpp
+++ b/content/html/content/src/nsTextEditorState.cpp
@@ -49,17 +49,17 @@
 #include "nsTextControlFrame.h"
 #include "nsIControllers.h"
 #include "nsIDOMHTMLInputElement.h"
 #include "nsIDOMHTMLTextAreaElement.h"
 #include "nsITransactionManager.h"
 #include "nsIControllerContext.h"
 #include "nsAttrValue.h"
 #include "nsGenericHTMLElement.h"
-#include "nsIDOMKeyListener.h"
+#include "nsIDOMEventListener.h"
 #include "nsIEditorObserver.h"
 #include "nsINativeKeyBindings.h"
 #include "nsIDocumentEncoder.h"
 #include "nsISelectionPrivate.h"
 #include "nsPIDOMWindow.h"
 #include "nsServiceManagerUtils.h"
 #include "nsIEditor.h"
 #include "nsTextEditRules.h"
@@ -604,17 +604,17 @@ nsTextInputSelectionImpl::CheckVisibilit
   {
     return shell->CheckVisibility(node,startOffset,EndOffset, _retval);
   }
   return NS_ERROR_FAILURE;
 
 }
 
 class nsTextInputListener : public nsISelectionListener,
-                            public nsIDOMKeyListener,
+                            public nsIDOMEventListener,
                             public nsIEditorObserver,
                             public nsSupportsWeakReference
 {
 public:
   /** the default constructor
    */ 
   explicit nsTextInputListener(nsITextControlElement* aTxtCtrlElement);
   /** the default destructor. virtual due to the possibility of derivation.
@@ -627,22 +627,17 @@ public:
   void SetFrame(nsTextControlFrame *aFrame){mFrame = aFrame;}
 
   void SettingValue(PRBool aValue) { mSettingValue = aValue; }
 
   NS_DECL_ISUPPORTS
 
   NS_DECL_NSISELECTIONLISTENER
 
-  NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent);
-
-  // nsIDOMKeyListener
-  NS_IMETHOD KeyDown(nsIDOMEvent *aKeyEvent);
-  NS_IMETHOD KeyPress(nsIDOMEvent *aKeyEvent);
-  NS_IMETHOD KeyUp(nsIDOMEvent *aKeyEvent);
+  NS_DECL_NSIDOMEVENTLISTENER
 
   NS_DECL_NSIEDITOROBSERVER
 
 protected:
 
   nsresult  UpdateTextInputCommands(const nsAString& commandsToUpdate);
 
   NS_HIDDEN_(nsINativeKeyBindings*) GetKeyBindings();
@@ -685,27 +680,21 @@ nsTextInputListener::nsTextInputListener
 , mSettingValue(PR_FALSE)
 {
 }
 
 nsTextInputListener::~nsTextInputListener() 
 {
 }
 
-NS_IMPL_ADDREF(nsTextInputListener)
-NS_IMPL_RELEASE(nsTextInputListener)
-
-NS_INTERFACE_MAP_BEGIN(nsTextInputListener)
-  NS_INTERFACE_MAP_ENTRY(nsISelectionListener)
-  NS_INTERFACE_MAP_ENTRY(nsIEditorObserver)
-  NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
-  NS_INTERFACE_MAP_ENTRY(nsIDOMKeyListener)
-  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIDOMEventListener, nsIDOMKeyListener)
-  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMKeyListener)
-NS_INTERFACE_MAP_END
+NS_IMPL_ISUPPORTS4(nsTextInputListener,
+                   nsISelectionListener,
+                   nsIEditorObserver,
+                   nsISupportsWeakReference,
+                   nsIDOMEventListener)
 
 // BEGIN nsIDOMSelectionListener
 
 NS_IMETHODIMP
 nsTextInputListener::NotifySelectionChanged(nsIDOMDocument* aDoc, nsISelection* aSel, PRInt16 aReason)
 {
   PRBool collapsed;
   nsWeakFrame weakFrame = mFrame;
@@ -758,24 +747,16 @@ nsTextInputListener::NotifySelectionChan
   if (!weakFrame.IsAlive() || !nsContentUtils::IsFocusedContent(mFrame->GetContent()))
     return NS_OK;
 
   return UpdateTextInputCommands(NS_LITERAL_STRING("select"));
 }
 
 // END nsIDOMSelectionListener
 
-// BEGIN nsIDOMKeyListener
-
-NS_IMETHODIMP
-nsTextInputListener::HandleEvent(nsIDOMEvent* aEvent)
-{
-  return NS_OK;
-}
-
 static void
 DoCommandCallback(const char *aCommand, void *aData)
 {
   nsTextControlFrame *frame = static_cast<nsTextControlFrame*>(aData);
   nsIContent *content = frame->GetContent();
 
   nsCOMPtr<nsIControllers> controllers;
   nsCOMPtr<nsIDOMHTMLInputElement> input = do_QueryInterface(content);
@@ -797,72 +778,51 @@ DoCommandCallback(const char *aCommand, 
 
   nsCOMPtr<nsIController> controller;
   controllers->GetControllerForCommand(aCommand, getter_AddRefs(controller));
   if (controller) {
     controller->DoCommand(aCommand);
   }
 }
 
+NS_IMETHODIMP
+nsTextInputListener::HandleEvent(nsIDOMEvent* aEvent)
+{
+  nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aEvent));
+  NS_ENSURE_TRUE(keyEvent, NS_ERROR_INVALID_ARG);
 
-NS_IMETHODIMP
-nsTextInputListener::KeyDown(nsIDOMEvent *aDOMEvent)
-{
-  nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aDOMEvent));
-  NS_ENSURE_TRUE(keyEvent, NS_ERROR_INVALID_ARG);
+  nsAutoString eventType;
+  aEvent->GetType(eventType);
 
   nsNativeKeyEvent nativeEvent;
   nsINativeKeyBindings *bindings = GetKeyBindings();
   if (bindings &&
       nsContentUtils::DOMEventToNativeKeyEvent(keyEvent, &nativeEvent, PR_FALSE)) {
-    if (bindings->KeyDown(nativeEvent, DoCommandCallback, mFrame)) {
-      aDOMEvent->PreventDefault();
+
+    PRBool handled = PR_FALSE;
+    if (eventType.EqualsLiteral("keydown")) {
+      handled = bindings->KeyDown(nativeEvent, DoCommandCallback, mFrame);
+    }
+    else if (eventType.EqualsLiteral("keyup")) {
+      handled = bindings->KeyUp(nativeEvent, DoCommandCallback, mFrame);
+    }
+    else if (eventType.EqualsLiteral("keypress")) {
+      handled = bindings->KeyPress(nativeEvent, DoCommandCallback, mFrame);
+    }
+    else {
+      NS_ABORT();
+    }
+    if (handled) {
+      aEvent->PreventDefault();
     }
   }
 
   return NS_OK;
 }
 
-NS_IMETHODIMP
-nsTextInputListener::KeyPress(nsIDOMEvent *aDOMEvent)
-{
-  nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aDOMEvent));
-  NS_ENSURE_TRUE(keyEvent, NS_ERROR_INVALID_ARG);
-
-  nsNativeKeyEvent nativeEvent;
-  nsINativeKeyBindings *bindings = GetKeyBindings();
-  if (bindings &&
-      nsContentUtils::DOMEventToNativeKeyEvent(keyEvent, &nativeEvent, PR_TRUE)) {
-    if (bindings->KeyPress(nativeEvent, DoCommandCallback, mFrame)) {
-      aDOMEvent->PreventDefault();
-    }
-  }
-
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-nsTextInputListener::KeyUp(nsIDOMEvent *aDOMEvent)
-{
-  nsCOMPtr<nsIDOMKeyEvent> keyEvent(do_QueryInterface(aDOMEvent));
-  NS_ENSURE_TRUE(keyEvent, NS_ERROR_INVALID_ARG);
-
-  nsNativeKeyEvent nativeEvent;
-  nsINativeKeyBindings *bindings = GetKeyBindings();
-  if (bindings &&
-      nsContentUtils::DOMEventToNativeKeyEvent(keyEvent, &nativeEvent, PR_FALSE)) {
-    if (bindings->KeyUp(nativeEvent, DoCommandCallback, mFrame)) {
-      aDOMEvent->PreventDefault();
-    }
-  }
-
-  return NS_OK;
-}
-// END nsIDOMKeyListener
-
 // BEGIN nsIEditorObserver
 
 NS_IMETHODIMP
 nsTextInputListener::EditAction()
 {
   nsWeakFrame weakFrame = mFrame;
 
   nsITextControlFrame* frameBase = do_QueryFrame(mFrame);
@@ -1512,29 +1472,25 @@ nsTextEditorState::UnbindFromFrame(nsTex
   if (mTextListener)
   {
     mTextListener->SetFrame(nsnull);
 
     nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(mTextCtrlElement);
     nsEventListenerManager* manager =
       target->GetListenerManager(PR_FALSE);
     if (manager) {
-      // cast because of ambiguous base
-      nsIDOMEventListener *listener = static_cast<nsIDOMKeyListener*>
-                                                 (mTextListener);
-
-      manager->RemoveEventListenerByType(listener,
+      manager->RemoveEventListenerByType(mTextListener,
                                          NS_LITERAL_STRING("keydown"),
                                          NS_EVENT_FLAG_BUBBLE |
                                          NS_EVENT_FLAG_SYSTEM_EVENT);
-      manager->RemoveEventListenerByType(listener,
+      manager->RemoveEventListenerByType(mTextListener,
                                          NS_LITERAL_STRING("keypress"),
                                          NS_EVENT_FLAG_BUBBLE |
                                          NS_EVENT_FLAG_SYSTEM_EVENT);
-      manager->RemoveEventListenerByType(listener,
+      manager->RemoveEventListenerByType(mTextListener,
                                          NS_LITERAL_STRING("keyup"),
                                          NS_EVENT_FLAG_BUBBLE |
                                          NS_EVENT_FLAG_SYSTEM_EVENT);
     }
 
     NS_RELEASE(mTextListener);
     mTextListener = nsnull;
   }
@@ -1941,27 +1897,26 @@ nsTextEditorState::SetValue(const nsAStr
 
 void
 nsTextEditorState::InitializeKeyboardEventListeners()
 {
   //register key listeners
   nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(mTextCtrlElement);
   nsEventListenerManager* manager = target->GetListenerManager(PR_TRUE);
   if (manager) {
-    // cast because of ambiguous base
-    nsIDOMEventListener *listener = static_cast<nsIDOMKeyListener*>
-                                               (mTextListener);
-
-    manager->AddEventListenerByType(listener, NS_LITERAL_STRING("keydown"),
+    manager->AddEventListenerByType(mTextListener,
+                                    NS_LITERAL_STRING("keydown"),
                                     NS_EVENT_FLAG_BUBBLE |
                                     NS_EVENT_FLAG_SYSTEM_EVENT);
-    manager->AddEventListenerByType(listener, NS_LITERAL_STRING("keypress"),
+    manager->AddEventListenerByType(mTextListener,
+                                    NS_LITERAL_STRING("keypress"),
                                     NS_EVENT_FLAG_BUBBLE |
                                     NS_EVENT_FLAG_SYSTEM_EVENT);
-    manager->AddEventListenerByType(listener, NS_LITERAL_STRING("keyup"),
+    manager->AddEventListenerByType(mTextListener,
+                                    NS_LITERAL_STRING("keyup"),
                                     NS_EVENT_FLAG_BUBBLE |
                                     NS_EVENT_FLAG_SYSTEM_EVENT);
   }
 
   mSelCon->SetScrollableFrame(do_QueryFrame(mBoundFrame->GetFirstChild(nsnull)));
 }
 
 /* static */ void