Bug 1653421 - Part 1: Refactor AccessibleWrap event handling. r=morgan
authorEitan Isaacson <eitan@monotonous.org>
Tue, 21 Jul 2020 23:02:53 +0000
changeset 541523 daeda032926d1fb85aca0d9117d9e77ea677d2b4
parent 541522 978c7a6ddb55e53f9230112f5455a4970ee5d7b4
child 541524 1d37ebf3d20ce8071c7482fbf7bcdc37834e950c
push id37625
push usercsabou@mozilla.com
push dateWed, 22 Jul 2020 04:32:41 +0000
treeherdermozilla-central@6dfc866efa7a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmorgan
bugs1653421
milestone80.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1653421 - Part 1: Refactor AccessibleWrap event handling. r=morgan I split the switch statement into two: the first switch determines the MOX target accessible. The second one does all the rest. This makes it more readable and scalable when we add more events that need to be accompanied with data. Differential Revision: https://phabricator.services.mozilla.com/D84053
accessible/mac/AccessibleWrap.mm
--- a/accessible/mac/AccessibleWrap.mm
+++ b/accessible/mac/AccessibleWrap.mm
@@ -105,106 +105,95 @@ nsresult AccessibleWrap::HandleAccEvent(
   nsresult rv = Accessible::HandleAccEvent(aEvent);
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (IPCAccessibilityActive()) {
     return NS_OK;
   }
 
   uint32_t eventType = aEvent->GetEventType();
+  Accessible* eventTarget = nullptr;
+
+  switch (eventType) {
+    case nsIAccessibleEvent::EVENT_SELECTION:
+    case nsIAccessibleEvent::EVENT_SELECTION_ADD:
+    case nsIAccessibleEvent::EVENT_SELECTION_REMOVE: {
+      AccSelChangeEvent* selEvent = downcast_accEvent(aEvent);
+      // The "widget" is the selected widget's container. In OSX
+      // it is the target of the selection changed event.
+      eventTarget = selEvent->Widget();
+      break;
+    }
+    default:
+      eventTarget = aEvent->GetAccessible();
+      break;
+  }
 
   mozAccessible* nativeAcc = nil;
+  eventTarget->GetNativeInterface((void**)&nativeAcc);
+  if (!nativeAcc) {
+    return NS_ERROR_FAILURE;
+  }
 
   switch (eventType) {
+    case nsIAccessibleEvent::EVENT_STATE_CHANGE: {
+      AccStateChangeEvent* event = downcast_accEvent(aEvent);
+      [nativeAcc stateChanged:event->GetState() isEnabled:event->IsStateEnabled()];
+      break;
+    }
+
+    case nsIAccessibleEvent::EVENT_TEXT_SELECTION_CHANGED: {
+      MOXTextMarkerDelegate* delegate =
+          [MOXTextMarkerDelegate getOrCreateForDoc:aEvent->Document()];
+      AccTextSelChangeEvent* event = downcast_accEvent(aEvent);
+      AutoTArray<TextRange, 1> ranges;
+      event->SelectionRanges(&ranges);
+
+      if (ranges.Length()) {
+        // Cache selection in delegate.
+        [delegate setSelectionFrom:ranges[0].StartContainer()
+                                at:ranges[0].StartOffset()
+                                to:ranges[0].EndContainer()
+                                at:ranges[0].EndOffset()];
+      }
+
+      [nativeAcc handleAccessibleEvent:eventType];
+      break;
+    }
+
+    case nsIAccessibleEvent::EVENT_TEXT_CARET_MOVED: {
+      AccCaretMoveEvent* event = downcast_accEvent(aEvent);
+      if (event->IsSelectionCollapsed()) {
+        // If the selection is collapsed, invalidate our text selection cache.
+        MOXTextMarkerDelegate* delegate =
+            [MOXTextMarkerDelegate getOrCreateForDoc:aEvent->Document()];
+        [delegate invalidateSelection];
+      }
+
+      [nativeAcc handleAccessibleEvent:eventType];
+      break;
+    }
+
     case nsIAccessibleEvent::EVENT_FOCUS:
     case nsIAccessibleEvent::EVENT_VALUE_CHANGE:
     case nsIAccessibleEvent::EVENT_TEXT_VALUE_CHANGE:
     case nsIAccessibleEvent::EVENT_DOCUMENT_LOAD_COMPLETE:
     case nsIAccessibleEvent::EVENT_MENUPOPUP_START:
     case nsIAccessibleEvent::EVENT_MENUPOPUP_END:
     case nsIAccessibleEvent::EVENT_REORDER:
-      if (Accessible* accessible = aEvent->GetAccessible()) {
-        accessible->GetNativeInterface((void**)&nativeAcc);
-        if (!nativeAcc) {
-          return NS_ERROR_FAILURE;
-        }
-      }
-      break;
     case nsIAccessibleEvent::EVENT_SELECTION:
     case nsIAccessibleEvent::EVENT_SELECTION_ADD:
-    case nsIAccessibleEvent::EVENT_SELECTION_REMOVE: {
-      AccSelChangeEvent* selEvent = downcast_accEvent(aEvent);
-      // The "widget" is the selected widget's container. In OSX
-      // it is the target of the selection changed event.
-      if (Accessible* accessible = selEvent->Widget()) {
-        accessible->GetNativeInterface((void**)&nativeAcc);
-        if (!nativeAcc) {
-          return NS_ERROR_FAILURE;
-        }
-      }
+    case nsIAccessibleEvent::EVENT_SELECTION_REMOVE:
+      [nativeAcc handleAccessibleEvent:eventType];
       break;
-    }
-    case nsIAccessibleEvent::EVENT_STATE_CHANGE:
-      if (Accessible* accessible = aEvent->GetAccessible()) {
-        accessible->GetNativeInterface((void**)&nativeAcc);
-        if (nativeAcc) {
-          AccStateChangeEvent* event = downcast_accEvent(aEvent);
-          [nativeAcc stateChanged:event->GetState() isEnabled:event->IsStateEnabled()];
-          return NS_OK;
-        } else {
-          return NS_ERROR_FAILURE;
-        }
-      }
-      break;
-    case nsIAccessibleEvent::EVENT_TEXT_SELECTION_CHANGED:
-      if (Accessible* accessible = aEvent->GetAccessible()) {
-        accessible->GetNativeInterface((void**)&nativeAcc);
-        if (!nativeAcc) {
-          return NS_ERROR_FAILURE;
-        }
 
-        MOXTextMarkerDelegate* delegate =
-            [MOXTextMarkerDelegate getOrCreateForDoc:aEvent->Document()];
-        AccTextSelChangeEvent* event = downcast_accEvent(aEvent);
-        AutoTArray<TextRange, 1> ranges;
-        event->SelectionRanges(&ranges);
-
-        if (ranges.Length()) {
-          // Cache selection in delegate.
-          [delegate setSelectionFrom:ranges[0].StartContainer()
-                                  at:ranges[0].StartOffset()
-                                  to:ranges[0].EndContainer()
-                                  at:ranges[0].EndOffset()];
-        }
-      }
-      break;
-    case nsIAccessibleEvent::EVENT_TEXT_CARET_MOVED:
-      if (Accessible* accessible = aEvent->GetAccessible()) {
-        accessible->GetNativeInterface((void**)&nativeAcc);
-        if (!nativeAcc) {
-          return NS_ERROR_FAILURE;
-        }
-
-        AccCaretMoveEvent* event = downcast_accEvent(aEvent);
-        if (event->IsSelectionCollapsed()) {
-          // If the selection is collapsed, invalidate our text selection cache.
-          MOXTextMarkerDelegate* delegate =
-              [MOXTextMarkerDelegate getOrCreateForDoc:aEvent->Document()];
-          [delegate invalidateSelection];
-        }
-      }
-      break;
     default:
       break;
   }
 
-  if (nativeAcc) {
-    [nativeAcc handleAccessibleEvent:eventType];
-  }
-
   return NS_OK;
 
   NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 // AccessibleWrap protected