Modified all places that check the current focus to allow for the case when the focus is unknown

1. Modified BrowserAccessibilityManager and its subclasses to return nullptr if the current tree is disconnected from the top document and the focus node is retrieved
This is because from Blink, we get one focused element per renderer - one focused node per iframe.
If the current accessibility tree is disconnected from its parent tree, we have no way of knowing where the document's global focus is and might erroneously get back the locally focused node, i.e. the focused node in the current iframe.
This will cause the focus to appear to jump from the globally focused node to the locally focused one, until the trees containing each iframe are connected again on the browser side.
The user-facing bug is that focus will seem to randomly jump around the page, mostly jumping back to the top, preventing the user from reading anything.

2. Ensured that the static variable in BrowserAccessibilityManager in which the previously focused node is stored is cleared when:
A) The tree managed by the current BrowserAccessibilityManager is deleted.
B) The tree managed by the current BrowserAccessibilityManager changes its tree ID.
C) The subtree containing the previously focused node is deleted.

3. Audited all places in the code were certain focus related methods from BrowserAccessibilityManager are called, and accounted for the possibility of those methods returning nullptr.
The list of methods is as follows:

4. Audited all the methods in BrowserAccessibilityManager and its subclasses that were using the BrowserAccessibilityManagerDelegate.
Some of those methods were calling methods on the delegate without first checking if it was nullptr, which could be the case in unit tests.

5. Took the opportunity to add "const" to a bunch of focus related methods, since retrieving the focus should not be modifying any state on any class instance.

6. A followup patch should modify the deserializer in AXTree to ensure that the same node ID is used when the root of the tree has been updated after some of its children got added or removed.
This will remove even more spurious focus events from being fired.

Change-Id: I4a75312632bf0b66e346fc5cae683edd9e63933a
Bug: 956748
Commit-Queue: Nektarios Paisios <>
Reviewed-by: Avi Drissman <>
Auto-Submit: Nektarios Paisios <>
Cr-Commit-Position: refs/heads/master@{#672125}
23 files changed