Bug 1909577 - Make some nsFocusManager::GetFocusedElement() users use its static version instead r=emilio,credential-management-reviewers,issammani

Now, we have `nsFocusManager::GetFocusedElementStatic()` which returns focused
element if the `nsFocusManager` instance is available.  Therefore, if
`nsFocusManager::GetFocusedElement()` users do not use other methods of
`nsFocusManager`, they can use `nsFocusManager::GetFocusedElementStatic()` and
make themselves simpler.

Note that some callers return early if `nsFocusManager` is not available, but
they do not return error and `nsFocusManager` instance is available in most
time of the life time of the process.  Therefore, we can simply stop using the
early return.

Differential Revision: https://phabricator.services.mozilla.com/D217527
This commit is contained in:
Masayuki Nakano
2024-07-25 00:33:58 +00:00
parent 88087a209e
commit 44aae6e962
14 changed files with 53 additions and 112 deletions

View File

@@ -1877,15 +1877,12 @@ bool RemoteAccessible::HasPrimaryAction() const {
void RemoteAccessible::TakeFocus() const {
Unused << mDoc->SendTakeFocus(mID);
if (nsFocusManager* fm = nsFocusManager::GetFocusManager()) {
auto* bp = static_cast<dom::BrowserParent*>(mDoc->Manager());
MOZ_ASSERT(bp);
dom::Element* owner = bp->GetOwnerElement();
if (fm->GetFocusedElement() == owner) {
// This remote document tree is already focused. We don't need to do
// anything else.
return;
}
auto* bp = static_cast<dom::BrowserParent*>(mDoc->Manager());
MOZ_ASSERT(bp);
if (nsFocusManager::GetFocusedElementStatic() == bp->GetOwnerElement()) {
// This remote document tree is already focused. We don't need to do
// anything else.
return;
}
// Otherwise, we need to focus the <browser> or <iframe> element embedding the
// remote document in the parent process. If `this` is in an OOP iframe, we

View File

@@ -6087,12 +6087,11 @@ nsresult Document::TurnEditingOff() {
// Editor resets selection since it is being destroyed. But if focus is
// still into editable control, we have to initialize selection again.
if (nsFocusManager* fm = nsFocusManager::GetFocusManager()) {
if (RefPtr<TextControlElement> textControlElement =
TextControlElement::FromNodeOrNull(fm->GetFocusedElement())) {
if (RefPtr<TextEditor> textEditor = textControlElement->GetTextEditor()) {
textEditor->ReinitializeSelection(*textControlElement);
}
if (RefPtr<TextControlElement> textControlElement =
TextControlElement::FromNodeOrNull(
nsFocusManager::GetFocusedElementStatic())) {
if (RefPtr<TextEditor> textEditor = textControlElement->GetTextEditor()) {
textEditor->ReinitializeSelection(*textControlElement);
}
}
@@ -6157,10 +6156,7 @@ nsresult Document::EditingStateChanged() {
// Note that even if focusedElement is an editable text control element,
// it becomes not editable from HTMLEditor point of view since text
// control elements are manged by TextEditor.
RefPtr<Element> focusedElement =
nsFocusManager::GetFocusManager()
? nsFocusManager::GetFocusManager()->GetFocusedElement()
: nullptr;
RefPtr<Element> focusedElement = nsFocusManager::GetFocusedElementStatic();
DebugOnly<nsresult> rvIgnored =
HTMLEditor::FocusedElementOrDocumentBecomesNotEditable(
htmlEditor, *this, focusedElement);
@@ -6444,9 +6440,8 @@ void Document::ChangeContentEditableCount(Element* aElement, int32_t aChange) {
}
void Document::DeferredContentEditableCountChange(Element* aElement) {
const RefPtr<nsFocusManager> fm = nsFocusManager::GetFocusManager();
const bool elementHasFocus =
aElement && fm && fm->GetFocusedElement() == aElement;
aElement && nsFocusManager::GetFocusedElementStatic() == aElement;
if (elementHasFocus) {
MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
// When contenteditable of aElement is changed and HTMLEditor works with it
@@ -6522,7 +6517,7 @@ void Document::DeferredContentEditableCountChange(Element* aElement) {
// having focus, the HTMLEditor won't receive `focus` event. Therefore, we
// need to notify HTMLEditor of it becomes editable.
if (elementHasFocus && aElement->HasFlag(NODE_IS_EDITABLE) &&
fm->GetFocusedElement() == aElement) {
nsFocusManager::GetFocusedElementStatic() == aElement) {
if (RefPtr<HTMLEditor> htmlEditor = GetHTMLEditor()) {
DebugOnly<nsresult> rvIgnored =
htmlEditor->FocusedElementOrDocumentBecomesEditable(*this, aElement);

View File

@@ -7044,13 +7044,6 @@ void* nsContentUtils::AllocClassMatchingInfo(nsINode* aRootNode,
return info;
}
// static
bool nsContentUtils::IsFocusedContent(const nsIContent* aContent) {
nsFocusManager* fm = nsFocusManager::GetFocusManager();
return fm && fm->GetFocusedElement() == aContent;
}
bool nsContentUtils::HasScrollgrab(nsIContent* aContent) {
// If we ever standardize this feature we'll want to hook this up properly
// again. For now we're removing all the DOM-side code related to it but

View File

@@ -2549,14 +2549,6 @@ class nsContentUtils {
static mozilla::WindowRenderer* WindowRendererForContent(
const nsIContent* aContent);
/**
* Determine whether a content node is focused or not,
*
* @param aContent the content node to check
* @return true if the content node is focused, false otherwise.
*/
static bool IsFocusedContent(const nsIContent* aContent);
/**
* Returns true if calling execCommand with 'cut' or 'copy' arguments is
* allowed for the given subject principal. These are only allowed if the user

View File

@@ -3078,12 +3078,7 @@ nsDOMWindowUtils::ZoomToFocusedInput() {
return NS_OK;
}
nsFocusManager* fm = nsFocusManager::GetFocusManager();
if (!fm) {
return NS_OK;
}
RefPtr<Element> element = fm->GetFocusedElement();
const RefPtr<Element> element = nsFocusManager::GetFocusedElementStatic();
if (!element) {
return NS_OK;
}

View File

@@ -3969,14 +3969,14 @@ nsresult HTMLInputElement::PostHandleEvent(EventChainPostVisitor& aVisitor) {
wheelEvent->mDeltaY != 0 &&
wheelEvent->mDeltaMode != WheelEvent_Binding::DOM_DELTA_PIXEL) {
if (mType == FormControlType::InputNumber) {
if (nsContentUtils::IsFocusedContent(this)) {
if (nsFocusManager::GetFocusedElementStatic() == this) {
StepNumberControlForUserEvent(wheelEvent->mDeltaY > 0 ? -1
: 1);
FireChangeEventIfNeeded();
aVisitor.mEvent->PreventDefault();
}
} else if (mType == FormControlType::InputRange &&
nsContentUtils::IsFocusedContent(this) &&
nsFocusManager::GetFocusedElementStatic() == this &&
GetMinimum() < GetMaximum()) {
Decimal value = GetValueAsDecimal();
Decimal step = GetStep();

View File

@@ -15,6 +15,7 @@
#include "nsCOMPtr.h"
#include "nsView.h"
#include "nsCaret.h"
#include "nsFocusManager.h"
#include "nsContentCreatorFunctions.h"
#include "nsTextControlFrame.h"
#include "nsIControllers.h"
@@ -858,7 +859,7 @@ void TextInputListener::OnSelectionChange(Selection& aSelection,
mSelectionWasCollapsed = collapsed;
if (!weakFrame.IsAlive() || !mFrame ||
!nsContentUtils::IsFocusedContent(mFrame->GetContent())) {
nsFocusManager::GetFocusedElementStatic() != mFrame->GetContent()) {
return;
}

View File

@@ -5841,12 +5841,7 @@ Element* EditorBase::GetFocusedElement() const {
return nullptr;
}
nsFocusManager* focusManager = nsFocusManager::GetFocusManager();
if (NS_WARN_IF(!focusManager)) {
return nullptr;
}
Element* focusedElement = focusManager->GetFocusedElement();
Element* const focusedElement = nsFocusManager::GetFocusedElementStatic();
MOZ_ASSERT((focusedElement == eventTarget) ==
SameCOMIdentity(focusedElement, eventTarget));
@@ -5956,11 +5951,6 @@ bool EditorBase::CanKeepHandlingFocusEvent(
return false;
}
nsFocusManager* focusManager = nsFocusManager::GetFocusManager();
if (MOZ_UNLIKELY(!focusManager)) {
return false;
}
// If the event target is document mode, we only need to handle the focus
// event when the document is still in designMode. Otherwise, the
// mode has been disabled by somebody while we're handling the focus event.
@@ -5972,7 +5962,9 @@ bool EditorBase::CanKeepHandlingFocusEvent(
// If nobody has focus, the focus event target has been blurred by somebody
// else. So the editor shouldn't initialize itself to start to handle
// anything.
if (!focusManager->GetFocusedElement()) {
const Element* const focusedElement =
nsFocusManager::GetFocusedElementStatic();
if (!focusedElement) {
return false;
}
@@ -5992,7 +5984,7 @@ bool EditorBase::CanKeepHandlingFocusEvent(
aOriginalEventTargetNode.AsContent()
->FindFirstNonChromeOnlyAccessContent();
const nsIContent* exposedFocusedContent =
focusManager->GetFocusedElement()->FindFirstNonChromeOnlyAccessContent();
focusedElement->FindFirstNonChromeOnlyAccessContent();
return exposedTargetContent && exposedFocusedContent &&
exposedTargetContent == exposedFocusedContent;
}

View File

@@ -209,19 +209,15 @@ void EditorEventListener::Disconnect() {
const OwningNonNull<EditorBase> editorBase = *mEditorBase;
mEditorBase = nullptr;
nsFocusManager* fm = nsFocusManager::GetFocusManager();
if (fm) {
nsIContent* focusedContent = fm->GetFocusedElement();
mozilla::dom::Element* root = editorBase->GetRoot();
if (focusedContent && root &&
focusedContent->IsInclusiveDescendantOf(root)) {
// Reset the Selection ancestor limiter and SelectionController state
// that EditorBase::InitializeSelection set up.
DebugOnly<nsresult> rvIgnored = editorBase->FinalizeSelection();
NS_WARNING_ASSERTION(
NS_SUCCEEDED(rvIgnored),
"EditorBase::FinalizeSelection() failed, but ignored");
}
const Element* const focusedElement =
nsFocusManager::GetFocusedElementStatic();
mozilla::dom::Element* root = editorBase->GetRoot();
if (focusedElement && root && focusedElement->IsInclusiveDescendantOf(root)) {
// Reset the Selection ancestor limiter and SelectionController state
// that EditorBase::InitializeSelection set up.
DebugOnly<nsresult> rvIgnored = editorBase->FinalizeSelection();
NS_WARNING_ASSERTION(NS_SUCCEEDED(rvIgnored),
"EditorBase::FinalizeSelection() failed, but ignored");
}
}

View File

@@ -6894,12 +6894,7 @@ NS_IMETHODIMP HTMLEditor::SetWrapWidth(int32_t aWrapColumn) {
}
Element* HTMLEditor::GetFocusedElement() const {
nsFocusManager* focusManager = nsFocusManager::GetFocusManager();
if (NS_WARN_IF(!focusManager)) {
return nullptr;
}
Element* const focusedElement = focusManager->GetFocusedElement();
Element* const focusedElement = nsFocusManager::GetFocusedElementStatic();
Document* document = GetDocument();
if (NS_WARN_IF(!document)) {
@@ -7183,14 +7178,12 @@ nsINode* HTMLEditor::GetFocusedNode() const {
return nullptr;
}
// focusedElement might be non-null even focusManager->GetFocusedElement()
// is null. That's the designMode case, and in that case our
// FocusedContent() returns the root element, but we want to return
// the document.
// focusedElement might be non-null even
// nsFocusManager::GetFocusedElementStatic() returns null. That's the
// designMode case, and in that case our GetFocusedElement() returns the root
// element, but we want to return the document.
nsFocusManager* focusManager = nsFocusManager::GetFocusManager();
NS_ASSERTION(focusManager, "Focus manager is null");
if ((focusedElement = focusManager->GetFocusedElement())) {
if ((focusedElement = nsFocusManager::GetFocusedElementStatic())) {
return focusedElement;
}

View File

@@ -772,15 +772,10 @@ nsresult TextEditor::OnFocus(const nsINode& aOriginalEventTargetNode) {
nsresult TextEditor::OnBlur(const EventTarget* aEventTarget) {
// check if something else is focused. If another element is focused, then
// we should not change the selection.
nsFocusManager* focusManager = nsFocusManager::GetFocusManager();
if (MOZ_UNLIKELY(!focusManager)) {
return NS_OK;
}
// If another element already has focus, we should not maintain the selection
// because we may not have the rights doing it.
if (focusManager->GetFocusedElement()) {
// we should not change the selection. If another element already has focus,
// we should not maintain the selection because we may not have the rights
// doing it.
if (nsFocusManager::GetFocusedElementStatic()) {
return NS_OK;
}

View File

@@ -9208,11 +9208,7 @@ bool PresShell::EventHandler::AdjustContextMenuKeyEvent(
// If we're here because of the key-equiv for showing context menus, we
// have to reset the event target to the currently focused element. Get it
// from the focus controller.
RefPtr<Element> currentFocus;
nsFocusManager* fm = nsFocusManager::GetFocusManager();
if (fm) {
currentFocus = fm->GetFocusedElement();
}
RefPtr<Element> currentFocus = nsFocusManager::GetFocusedElementStatic();
// Reset event coordinates relative to focused frame in view
if (currentFocus) {

View File

@@ -981,12 +981,12 @@ void nsTextControlFrame::HandleReadonlyOrDisabledChange() {
return;
}
if (el->IsDisabledOrReadOnly()) {
if (nsContentUtils::IsFocusedContent(el)) {
if (nsFocusManager::GetFocusedElementStatic() == el) {
selCon->SetCaretEnabled(false);
}
editor->AddFlags(nsIEditor::eEditorReadonlyMask);
} else {
if (nsContentUtils::IsFocusedContent(el)) {
if (nsFocusManager::GetFocusedElementStatic() == el) {
selCon->SetCaretEnabled(true);
}
editor->RemoveFlags(nsIEditor::eEditorReadonlyMask);

View File

@@ -249,17 +249,13 @@ nsFormFillController::MarkAsAutoCompletableField(HTMLInputElement* aInput) {
aInput->EnablePreview();
nsFocusManager* fm = nsFocusManager::GetFocusManager();
if (fm) {
nsCOMPtr<nsIContent> focusedContent = fm->GetFocusedElement();
if (focusedContent == aInput) {
if (!mFocusedInput) {
MaybeStartControllingInput(aInput);
} else {
// See `MarkAsLoginManagerField` for why this is needed.
nsCOMPtr<nsIAutoCompleteController> controller = mController;
controller->ResetInternalState();
}
if (nsFocusManager::GetFocusedElementStatic() == aInput) {
if (!mFocusedInput) {
MaybeStartControllingInput(aInput);
} else {
// See `MarkAsLoginManagerField` for why this is needed.
nsCOMPtr<nsIAutoCompleteController> controller = mController;
controller->ResetInternalState();
}
}