Bug 1330561 - Check if a relevant field is already focused when attaching autocomplete. r=MattN
authorJared Wein <jwein@mozilla.com>
Tue, 26 Feb 2019 15:43:31 -0500
changeset 519984 c5219b2bbf2b401c6d74e220df02636cc67d8a24
parent 519983 57624be2e55cefabe21feb5e0169f33b283f9ddd
child 519985 5e4f3a3033e9e63528fe4e68685296aaf5deed60
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN
bugs1330561
milestone67.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 1330561 - Check if a relevant field is already focused when attaching autocomplete. r=MattN Differential Revision: https://phabricator.services.mozilla.com/D21267
toolkit/components/satchel/nsFormFillController.cpp
toolkit/components/satchel/nsFormFillController.h
--- a/toolkit/components/satchel/nsFormFillController.cpp
+++ b/toolkit/components/satchel/nsFormFillController.cpp
@@ -218,16 +218,22 @@ nsFormFillController::AttachToBrowser(ns
 
   mDocShells.AppendElement(aDocShell);
   mPopups.AppendElement(aPopup);
 
   // Listen for focus events on the domWindow of the docShell
   nsCOMPtr<nsPIDOMWindowOuter> window = GetWindowForDocShell(aDocShell);
   AddWindowListeners(window);
 
+  nsFocusManager* fm = nsFocusManager::GetFocusManager();
+  if (fm) {
+    nsCOMPtr<nsIContent> focusedContent = fm->GetFocusedElement();
+    this->HandleFocus(HTMLInputElement::FromNodeOrNull(focusedContent));
+  }
+
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsFormFillController::AttachPopupElementToBrowser(nsIDocShell* aDocShell,
                                                   dom::Element* aPopupEl) {
   MOZ_LOG(sLogger, LogLevel::Debug,
           ("AttachPopupElementToBrowser for docShell %p with popup %p",
@@ -938,19 +944,18 @@ void nsFormFillController::MaybeStartCon
   // Trigger an asynchronous login reputation query when user focuses on the
   // password field.
   if (aInput->ControlType() == NS_FORM_INPUT_PASSWORD) {
     StartQueryLoginReputation(aInput);
   }
 #endif
 }
 
-nsresult nsFormFillController::Focus(Event* aEvent) {
-  nsCOMPtr<nsIContent> input = do_QueryInterface(aEvent->GetComposedTarget());
-  MaybeStartControllingInput(HTMLInputElement::FromNodeOrNull(input));
+nsresult nsFormFillController::HandleFocus(HTMLInputElement* aInput) {
+  MaybeStartControllingInput(aInput);
 
   // Bail if we didn't start controlling the input.
   if (!mFocusedInput) {
     return NS_OK;
   }
 
 #ifndef ANDROID
   // If this focus doesn't follow a right click within our specified
@@ -976,16 +981,21 @@ nsresult nsFormFillController::Focus(Eve
   if (timeDiff > mFocusAfterRightClickThreshold) {
     ShowPopup();
   }
 #endif
 
   return NS_OK;
 }
 
+nsresult nsFormFillController::Focus(Event* aEvent) {
+  nsCOMPtr<nsIContent> input = do_QueryInterface(aEvent->GetComposedTarget());
+  return this->HandleFocus(HTMLInputElement::FromNodeOrNull(input));
+}
+
 nsresult nsFormFillController::KeyDown(Event* aEvent) {
   NS_ASSERTION(mController, "should have a controller!");
   if (!mFocusedInput || !mController) {
     return NS_OK;
   }
 
   RefPtr<KeyboardEvent> keyEvent = aEvent->AsKeyboardEvent();
   if (!keyEvent) {
--- a/toolkit/components/satchel/nsFormFillController.h
+++ b/toolkit/components/satchel/nsFormFillController.h
@@ -68,16 +68,19 @@ class nsFormFillController final : publi
   void AddWindowListeners(nsPIDOMWindowOuter* aWindow);
   void RemoveWindowListeners(nsPIDOMWindowOuter* aWindow);
 
   void AddKeyListener(nsINode* aInput);
   void RemoveKeyListener();
 
   void StartControllingInput(mozilla::dom::HTMLInputElement* aInput);
   void StopControllingInput();
+
+  nsresult HandleFocus(mozilla::dom::HTMLInputElement* aInput);
+
   /**
    * Checks that aElement is a type of element we want to fill, then calls
    * StartControllingInput on it.
    */
   void MaybeStartControllingInput(mozilla::dom::HTMLInputElement* aElement);
 
   nsresult PerformInputListAutoComplete(const nsAString& aSearch,
                                         nsIAutoCompleteResult** aResult);