toolkit/components/satchel/nsFormFillController.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/satchel/nsFormFillController.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,1222 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsFormFillController.h"
    1.10 +
    1.11 +#include "mozilla/dom/Element.h"
    1.12 +#include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent()
    1.13 +#include "nsIFormAutoComplete.h"
    1.14 +#include "nsIInputListAutoComplete.h"
    1.15 +#include "nsIAutoCompleteSimpleResult.h"
    1.16 +#include "nsString.h"
    1.17 +#include "nsReadableUtils.h"
    1.18 +#include "nsIServiceManager.h"
    1.19 +#include "nsIInterfaceRequestor.h"
    1.20 +#include "nsIInterfaceRequestorUtils.h"
    1.21 +#include "nsIDocShellTreeItem.h"
    1.22 +#include "nsPIDOMWindow.h"
    1.23 +#include "nsIWebNavigation.h"
    1.24 +#include "nsIContentViewer.h"
    1.25 +#include "nsIDOMKeyEvent.h"
    1.26 +#include "nsIDOMDocument.h"
    1.27 +#include "nsIDOMElement.h"
    1.28 +#include "nsIFormControl.h"
    1.29 +#include "nsIDocument.h"
    1.30 +#include "nsIContent.h"
    1.31 +#include "nsIPresShell.h"
    1.32 +#include "nsRect.h"
    1.33 +#include "nsIDOMHTMLFormElement.h"
    1.34 +#include "nsILoginManager.h"
    1.35 +#include "nsIDOMMouseEvent.h"
    1.36 +#include "mozilla/ModuleUtils.h"
    1.37 +#include "nsToolkitCompsCID.h"
    1.38 +#include "nsEmbedCID.h"
    1.39 +#include "nsIDOMNSEditableElement.h"
    1.40 +#include "nsContentUtils.h"
    1.41 +#include "nsILoadContext.h"
    1.42 +
    1.43 +using namespace mozilla::dom;
    1.44 +
    1.45 +NS_IMPL_ISUPPORTS(nsFormFillController,
    1.46 +                  nsIFormFillController,
    1.47 +                  nsIAutoCompleteInput,
    1.48 +                  nsIAutoCompleteSearch,
    1.49 +                  nsIDOMEventListener,
    1.50 +                  nsIFormAutoCompleteObserver,
    1.51 +                  nsIMutationObserver)
    1.52 +
    1.53 +nsFormFillController::nsFormFillController() :
    1.54 +  mFocusedInput(nullptr),
    1.55 +  mFocusedInputNode(nullptr),
    1.56 +  mListNode(nullptr),
    1.57 +  mTimeout(50),
    1.58 +  mMinResultsForPopup(1),
    1.59 +  mMaxRows(0),
    1.60 +  mDisableAutoComplete(false),
    1.61 +  mCompleteDefaultIndex(false),
    1.62 +  mCompleteSelectedIndex(false),
    1.63 +  mForceComplete(false),
    1.64 +  mSuppressOnInput(false)
    1.65 +{
    1.66 +  mController = do_GetService("@mozilla.org/autocomplete/controller;1");
    1.67 +}
    1.68 +
    1.69 +struct PwmgrInputsEnumData
    1.70 +{
    1.71 +  PwmgrInputsEnumData(nsFormFillController* aFFC, nsIDocument* aDoc)
    1.72 +  : mFFC(aFFC), mDoc(aDoc) {}
    1.73 +
    1.74 +  nsFormFillController* mFFC;
    1.75 +  nsCOMPtr<nsIDocument> mDoc;
    1.76 +};
    1.77 +
    1.78 +nsFormFillController::~nsFormFillController()
    1.79 +{
    1.80 +  if (mListNode) {
    1.81 +    mListNode->RemoveMutationObserver(this);
    1.82 +    mListNode = nullptr;
    1.83 +  }
    1.84 +  if (mFocusedInputNode) {
    1.85 +    MaybeRemoveMutationObserver(mFocusedInputNode);
    1.86 +    mFocusedInputNode = nullptr;
    1.87 +    mFocusedInput = nullptr;
    1.88 +  }
    1.89 +  PwmgrInputsEnumData ed(this, nullptr);
    1.90 +  mPwmgrInputs.Enumerate(RemoveForDocumentEnumerator, &ed);
    1.91 +
    1.92 +  // Remove ourselves as a focus listener from all cached docShells
    1.93 +  uint32_t count = mDocShells.Length();
    1.94 +  for (uint32_t i = 0; i < count; ++i) {
    1.95 +    nsCOMPtr<nsIDOMWindow> domWindow = GetWindowForDocShell(mDocShells[i]);
    1.96 +    RemoveWindowListeners(domWindow);
    1.97 +  }
    1.98 +}
    1.99 +
   1.100 +////////////////////////////////////////////////////////////////////////
   1.101 +//// nsIMutationObserver
   1.102 +//
   1.103 +
   1.104 +void
   1.105 +nsFormFillController::AttributeChanged(nsIDocument* aDocument,
   1.106 +                                       mozilla::dom::Element* aElement,
   1.107 +                                       int32_t aNameSpaceID,
   1.108 +                                       nsIAtom* aAttribute, int32_t aModType)
   1.109 +{
   1.110 +  if (mListNode && mListNode->Contains(aElement)) {
   1.111 +    RevalidateDataList();
   1.112 +  }
   1.113 +}
   1.114 +
   1.115 +void
   1.116 +nsFormFillController::ContentAppended(nsIDocument* aDocument,
   1.117 +                                      nsIContent* aContainer,
   1.118 +                                      nsIContent* aChild,
   1.119 +                                      int32_t aIndexInContainer)
   1.120 +{
   1.121 +  if (mListNode && mListNode->Contains(aContainer)) {
   1.122 +    RevalidateDataList();
   1.123 +  }
   1.124 +}
   1.125 +
   1.126 +void
   1.127 +nsFormFillController::ContentInserted(nsIDocument* aDocument,
   1.128 +                                      nsIContent* aContainer,
   1.129 +                                      nsIContent* aChild,
   1.130 +                                      int32_t aIndexInContainer)
   1.131 +{
   1.132 +  if (mListNode && mListNode->Contains(aContainer)) {
   1.133 +    RevalidateDataList();
   1.134 +  }
   1.135 +}
   1.136 +
   1.137 +void
   1.138 +nsFormFillController::ContentRemoved(nsIDocument* aDocument,
   1.139 +                                     nsIContent* aContainer,
   1.140 +                                     nsIContent* aChild,
   1.141 +                                     int32_t aIndexInContainer,
   1.142 +                                     nsIContent* aPreviousSibling)
   1.143 +{
   1.144 +  if (mListNode && mListNode->Contains(aContainer)) {
   1.145 +    RevalidateDataList();
   1.146 +  }
   1.147 +}
   1.148 +
   1.149 +void
   1.150 +nsFormFillController::CharacterDataWillChange(nsIDocument* aDocument,
   1.151 +                                              nsIContent* aContent,
   1.152 +                                              CharacterDataChangeInfo* aInfo)
   1.153 +{
   1.154 +}
   1.155 +
   1.156 +void
   1.157 +nsFormFillController::CharacterDataChanged(nsIDocument* aDocument,
   1.158 +                                           nsIContent* aContent,
   1.159 +                                           CharacterDataChangeInfo* aInfo)
   1.160 +{
   1.161 +}
   1.162 +
   1.163 +void
   1.164 +nsFormFillController::AttributeWillChange(nsIDocument* aDocument,
   1.165 +                                          mozilla::dom::Element* aElement,
   1.166 +                                          int32_t aNameSpaceID,
   1.167 +                                          nsIAtom* aAttribute, int32_t aModType)
   1.168 +{
   1.169 +}
   1.170 +
   1.171 +void
   1.172 +nsFormFillController::ParentChainChanged(nsIContent* aContent)
   1.173 +{
   1.174 +}
   1.175 +
   1.176 +void
   1.177 +nsFormFillController::NodeWillBeDestroyed(const nsINode* aNode)
   1.178 +{
   1.179 +  mPwmgrInputs.Remove(aNode);
   1.180 +  if (aNode == mListNode) {
   1.181 +    mListNode = nullptr;
   1.182 +    RevalidateDataList();
   1.183 +  } else if (aNode == mFocusedInputNode) {
   1.184 +    mFocusedInputNode = nullptr;
   1.185 +    mFocusedInput = nullptr;
   1.186 +  }
   1.187 +}
   1.188 +
   1.189 +void
   1.190 +nsFormFillController::MaybeRemoveMutationObserver(nsINode* aNode)
   1.191 +{
   1.192 +  // Nodes being tracked in mPwmgrInputs will have their observers removed when
   1.193 +  // they stop being tracked. 
   1.194 +  bool dummy;
   1.195 +  if (!mPwmgrInputs.Get(aNode, &dummy)) {
   1.196 +    aNode->RemoveMutationObserver(this);
   1.197 +  }
   1.198 +}
   1.199 +
   1.200 +////////////////////////////////////////////////////////////////////////
   1.201 +//// nsIFormFillController
   1.202 +
   1.203 +NS_IMETHODIMP
   1.204 +nsFormFillController::AttachToBrowser(nsIDocShell *aDocShell, nsIAutoCompletePopup *aPopup)
   1.205 +{
   1.206 +  NS_ENSURE_TRUE(aDocShell && aPopup, NS_ERROR_ILLEGAL_VALUE);
   1.207 +
   1.208 +  mDocShells.AppendElement(aDocShell);
   1.209 +  mPopups.AppendElement(aPopup);
   1.210 +
   1.211 +  // Listen for focus events on the domWindow of the docShell
   1.212 +  nsCOMPtr<nsIDOMWindow> domWindow = GetWindowForDocShell(aDocShell);
   1.213 +  AddWindowListeners(domWindow);
   1.214 +
   1.215 +  return NS_OK;
   1.216 +}
   1.217 +
   1.218 +NS_IMETHODIMP
   1.219 +nsFormFillController::DetachFromBrowser(nsIDocShell *aDocShell)
   1.220 +{
   1.221 +  int32_t index = GetIndexOfDocShell(aDocShell);
   1.222 +  NS_ENSURE_TRUE(index >= 0, NS_ERROR_FAILURE);
   1.223 +
   1.224 +  // Stop listening for focus events on the domWindow of the docShell
   1.225 +  nsCOMPtr<nsIDOMWindow> domWindow =
   1.226 +    GetWindowForDocShell(mDocShells.SafeElementAt(index));
   1.227 +  RemoveWindowListeners(domWindow);
   1.228 +
   1.229 +  mDocShells.RemoveElementAt(index);
   1.230 +  mPopups.RemoveElementAt(index);
   1.231 +
   1.232 +  return NS_OK;
   1.233 +}
   1.234 +
   1.235 +
   1.236 +NS_IMETHODIMP
   1.237 +nsFormFillController::MarkAsLoginManagerField(nsIDOMHTMLInputElement *aInput)
   1.238 +{
   1.239 +  /*
   1.240 +   * The Login Manager can supply autocomplete results for username fields,
   1.241 +   * when a user has multiple logins stored for a site. It uses this
   1.242 +   * interface to indicate that the form manager shouldn't handle the
   1.243 +   * autocomplete. The form manager also checks for this tag when saving
   1.244 +   * form history (so it doesn't save usernames).
   1.245 +   */
   1.246 +  nsCOMPtr<nsINode> node = do_QueryInterface(aInput);
   1.247 +  NS_ENSURE_STATE(node);
   1.248 +  mPwmgrInputs.Put(node, true);
   1.249 +  node->AddMutationObserverUnlessExists(this);
   1.250 +
   1.251 +  if (!mLoginManager)
   1.252 +    mLoginManager = do_GetService("@mozilla.org/login-manager;1");
   1.253 +
   1.254 +  return NS_OK;
   1.255 +}
   1.256 +
   1.257 +
   1.258 +////////////////////////////////////////////////////////////////////////
   1.259 +//// nsIAutoCompleteInput
   1.260 +
   1.261 +NS_IMETHODIMP
   1.262 +nsFormFillController::GetPopup(nsIAutoCompletePopup **aPopup)
   1.263 +{
   1.264 +  *aPopup = mFocusedPopup;
   1.265 +  NS_IF_ADDREF(*aPopup);
   1.266 +  return NS_OK;
   1.267 +}
   1.268 +
   1.269 +NS_IMETHODIMP
   1.270 +nsFormFillController::GetController(nsIAutoCompleteController **aController)
   1.271 +{
   1.272 +  *aController = mController;
   1.273 +  NS_IF_ADDREF(*aController);
   1.274 +  return NS_OK;
   1.275 +}
   1.276 +
   1.277 +NS_IMETHODIMP
   1.278 +nsFormFillController::GetPopupOpen(bool *aPopupOpen)
   1.279 +{
   1.280 +  if (mFocusedPopup)
   1.281 +    mFocusedPopup->GetPopupOpen(aPopupOpen);
   1.282 +  else
   1.283 +    *aPopupOpen = false;
   1.284 +  return NS_OK;
   1.285 +}
   1.286 +
   1.287 +NS_IMETHODIMP
   1.288 +nsFormFillController::SetPopupOpen(bool aPopupOpen)
   1.289 +{
   1.290 +  if (mFocusedPopup) {
   1.291 +    if (aPopupOpen) {
   1.292 +      // make sure input field is visible before showing popup (bug 320938)
   1.293 +      nsCOMPtr<nsIContent> content = do_QueryInterface(mFocusedInput);
   1.294 +      NS_ENSURE_STATE(content);
   1.295 +      nsCOMPtr<nsIDocShell> docShell = GetDocShellForInput(mFocusedInput);
   1.296 +      NS_ENSURE_STATE(docShell);
   1.297 +      nsCOMPtr<nsIPresShell> presShell = docShell->GetPresShell();
   1.298 +      NS_ENSURE_STATE(presShell);
   1.299 +      presShell->ScrollContentIntoView(content,
   1.300 +                                       nsIPresShell::ScrollAxis(
   1.301 +                                         nsIPresShell::SCROLL_MINIMUM,
   1.302 +                                         nsIPresShell::SCROLL_IF_NOT_VISIBLE),
   1.303 +                                       nsIPresShell::ScrollAxis(
   1.304 +                                         nsIPresShell::SCROLL_MINIMUM,
   1.305 +                                         nsIPresShell::SCROLL_IF_NOT_VISIBLE),
   1.306 +                                       nsIPresShell::SCROLL_OVERFLOW_HIDDEN);
   1.307 +      // mFocusedPopup can be destroyed after ScrollContentIntoView, see bug 420089
   1.308 +      if (mFocusedPopup) {
   1.309 +        nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mFocusedInput);
   1.310 +        mFocusedPopup->OpenAutocompletePopup(this, element);
   1.311 +      }
   1.312 +    } else
   1.313 +      mFocusedPopup->ClosePopup();
   1.314 +  }
   1.315 +
   1.316 +  return NS_OK;
   1.317 +}
   1.318 +
   1.319 +NS_IMETHODIMP
   1.320 +nsFormFillController::GetDisableAutoComplete(bool *aDisableAutoComplete)
   1.321 +{
   1.322 +  *aDisableAutoComplete = mDisableAutoComplete;
   1.323 +  return NS_OK;
   1.324 +}
   1.325 +
   1.326 +NS_IMETHODIMP
   1.327 +nsFormFillController::SetDisableAutoComplete(bool aDisableAutoComplete)
   1.328 +{
   1.329 +  mDisableAutoComplete = aDisableAutoComplete;
   1.330 +  return NS_OK;
   1.331 +}
   1.332 +
   1.333 +NS_IMETHODIMP
   1.334 +nsFormFillController::GetCompleteDefaultIndex(bool *aCompleteDefaultIndex)
   1.335 +{
   1.336 +  *aCompleteDefaultIndex = mCompleteDefaultIndex;
   1.337 +  return NS_OK;
   1.338 +}
   1.339 +
   1.340 +NS_IMETHODIMP
   1.341 +nsFormFillController::SetCompleteDefaultIndex(bool aCompleteDefaultIndex)
   1.342 +{
   1.343 +  mCompleteDefaultIndex = aCompleteDefaultIndex;
   1.344 +  return NS_OK;
   1.345 +}
   1.346 +
   1.347 +NS_IMETHODIMP
   1.348 +nsFormFillController::GetCompleteSelectedIndex(bool *aCompleteSelectedIndex)
   1.349 +{
   1.350 +  *aCompleteSelectedIndex = mCompleteSelectedIndex;
   1.351 +  return NS_OK;
   1.352 +}
   1.353 +
   1.354 +NS_IMETHODIMP
   1.355 +nsFormFillController::SetCompleteSelectedIndex(bool aCompleteSelectedIndex)
   1.356 +{
   1.357 +  mCompleteSelectedIndex = aCompleteSelectedIndex;
   1.358 +  return NS_OK;
   1.359 +}
   1.360 +
   1.361 +NS_IMETHODIMP
   1.362 +nsFormFillController::GetForceComplete(bool *aForceComplete)
   1.363 +{
   1.364 +  *aForceComplete = mForceComplete;
   1.365 +  return NS_OK;
   1.366 +}
   1.367 +
   1.368 +NS_IMETHODIMP nsFormFillController::SetForceComplete(bool aForceComplete)
   1.369 +{
   1.370 +  mForceComplete = aForceComplete;
   1.371 +  return NS_OK;
   1.372 +}
   1.373 +
   1.374 +NS_IMETHODIMP
   1.375 +nsFormFillController::GetMinResultsForPopup(uint32_t *aMinResultsForPopup)
   1.376 +{
   1.377 +  *aMinResultsForPopup = mMinResultsForPopup;
   1.378 +  return NS_OK;
   1.379 +}
   1.380 +
   1.381 +NS_IMETHODIMP nsFormFillController::SetMinResultsForPopup(uint32_t aMinResultsForPopup)
   1.382 +{
   1.383 +  mMinResultsForPopup = aMinResultsForPopup;
   1.384 +  return NS_OK;
   1.385 +}
   1.386 +
   1.387 +NS_IMETHODIMP
   1.388 +nsFormFillController::GetMaxRows(uint32_t *aMaxRows)
   1.389 +{
   1.390 +  *aMaxRows = mMaxRows;
   1.391 +  return NS_OK;
   1.392 +}
   1.393 +
   1.394 +NS_IMETHODIMP
   1.395 +nsFormFillController::SetMaxRows(uint32_t aMaxRows)
   1.396 +{
   1.397 +  mMaxRows = aMaxRows;
   1.398 +  return NS_OK;
   1.399 +}
   1.400 +
   1.401 +NS_IMETHODIMP
   1.402 +nsFormFillController::GetShowImageColumn(bool *aShowImageColumn)
   1.403 +{
   1.404 +  *aShowImageColumn = false;
   1.405 +  return NS_OK;
   1.406 +}
   1.407 +
   1.408 +NS_IMETHODIMP nsFormFillController::SetShowImageColumn(bool aShowImageColumn)
   1.409 +{
   1.410 +  return NS_ERROR_NOT_IMPLEMENTED;
   1.411 +}
   1.412 +
   1.413 +
   1.414 +NS_IMETHODIMP
   1.415 +nsFormFillController::GetShowCommentColumn(bool *aShowCommentColumn)
   1.416 +{
   1.417 +  *aShowCommentColumn = false;
   1.418 +  return NS_OK;
   1.419 +}
   1.420 +
   1.421 +NS_IMETHODIMP nsFormFillController::SetShowCommentColumn(bool aShowCommentColumn)
   1.422 +{
   1.423 +  return NS_ERROR_NOT_IMPLEMENTED;
   1.424 +}
   1.425 +
   1.426 +NS_IMETHODIMP
   1.427 +nsFormFillController::GetTimeout(uint32_t *aTimeout)
   1.428 +{
   1.429 +  *aTimeout = mTimeout;
   1.430 +  return NS_OK;
   1.431 +}
   1.432 +
   1.433 +NS_IMETHODIMP nsFormFillController::SetTimeout(uint32_t aTimeout)
   1.434 +{
   1.435 +  mTimeout = aTimeout;
   1.436 +  return NS_OK;
   1.437 +}
   1.438 +
   1.439 +NS_IMETHODIMP
   1.440 +nsFormFillController::SetSearchParam(const nsAString &aSearchParam)
   1.441 +{
   1.442 +  return NS_ERROR_NOT_IMPLEMENTED;
   1.443 +}
   1.444 +
   1.445 +NS_IMETHODIMP
   1.446 +nsFormFillController::GetSearchParam(nsAString &aSearchParam)
   1.447 +{
   1.448 +  if (!mFocusedInput) {
   1.449 +    NS_WARNING("mFocusedInput is null for some reason! avoiding a crash. should find out why... - ben");
   1.450 +    return NS_ERROR_FAILURE; // XXX why? fix me.
   1.451 +  }
   1.452 +
   1.453 +  mFocusedInput->GetName(aSearchParam);
   1.454 +  if (aSearchParam.IsEmpty()) {
   1.455 +    nsCOMPtr<nsIDOMHTMLElement> element = do_QueryInterface(mFocusedInput);
   1.456 +    element->GetId(aSearchParam);
   1.457 +  }
   1.458 +
   1.459 +  return NS_OK;
   1.460 +}
   1.461 +
   1.462 +NS_IMETHODIMP
   1.463 +nsFormFillController::GetSearchCount(uint32_t *aSearchCount)
   1.464 +{
   1.465 +  *aSearchCount = 1;
   1.466 +  return NS_OK;
   1.467 +}
   1.468 +
   1.469 +NS_IMETHODIMP
   1.470 +nsFormFillController::GetSearchAt(uint32_t index, nsACString & _retval)
   1.471 +{
   1.472 +  _retval.Assign("form-history");
   1.473 +  return NS_OK;
   1.474 +}
   1.475 +
   1.476 +NS_IMETHODIMP
   1.477 +nsFormFillController::GetTextValue(nsAString & aTextValue)
   1.478 +{
   1.479 +  if (mFocusedInput) {
   1.480 +    mFocusedInput->GetValue(aTextValue);
   1.481 +  } else {
   1.482 +    aTextValue.Truncate();
   1.483 +  }
   1.484 +  return NS_OK;
   1.485 +}
   1.486 +
   1.487 +NS_IMETHODIMP
   1.488 +nsFormFillController::SetTextValue(const nsAString & aTextValue)
   1.489 +{
   1.490 +  nsCOMPtr<nsIDOMNSEditableElement> editable = do_QueryInterface(mFocusedInput);
   1.491 +  if (editable) {
   1.492 +    mSuppressOnInput = true;
   1.493 +    editable->SetUserInput(aTextValue);
   1.494 +    mSuppressOnInput = false;
   1.495 +  }
   1.496 +  return NS_OK;
   1.497 +}
   1.498 +
   1.499 +NS_IMETHODIMP
   1.500 +nsFormFillController::GetSelectionStart(int32_t *aSelectionStart)
   1.501 +{
   1.502 +  if (mFocusedInput)
   1.503 +    mFocusedInput->GetSelectionStart(aSelectionStart);
   1.504 +  return NS_OK;
   1.505 +}
   1.506 +
   1.507 +NS_IMETHODIMP
   1.508 +nsFormFillController::GetSelectionEnd(int32_t *aSelectionEnd)
   1.509 +{
   1.510 +  if (mFocusedInput)
   1.511 +    mFocusedInput->GetSelectionEnd(aSelectionEnd);
   1.512 +  return NS_OK;
   1.513 +}
   1.514 +
   1.515 +NS_IMETHODIMP
   1.516 +nsFormFillController::SelectTextRange(int32_t aStartIndex, int32_t aEndIndex)
   1.517 +{
   1.518 + if (mFocusedInput)
   1.519 +    mFocusedInput->SetSelectionRange(aStartIndex, aEndIndex, EmptyString());
   1.520 +  return NS_OK;
   1.521 +}
   1.522 +
   1.523 +NS_IMETHODIMP
   1.524 +nsFormFillController::OnSearchBegin()
   1.525 +{
   1.526 +  return NS_OK;
   1.527 +}
   1.528 +
   1.529 +NS_IMETHODIMP
   1.530 +nsFormFillController::OnSearchComplete()
   1.531 +{
   1.532 +  return NS_OK;
   1.533 +}
   1.534 +
   1.535 +NS_IMETHODIMP
   1.536 +nsFormFillController::OnTextEntered(bool* aPrevent)
   1.537 +{
   1.538 +  NS_ENSURE_ARG(aPrevent);
   1.539 +  NS_ENSURE_TRUE(mFocusedInput, NS_OK);
   1.540 +  // Fire off a DOMAutoComplete event
   1.541 +  nsCOMPtr<nsIDOMDocument> domDoc;
   1.542 +  nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mFocusedInput);
   1.543 +  element->GetOwnerDocument(getter_AddRefs(domDoc));
   1.544 +  NS_ENSURE_STATE(domDoc);
   1.545 +
   1.546 +  nsCOMPtr<nsIDOMEvent> event;
   1.547 +  domDoc->CreateEvent(NS_LITERAL_STRING("Events"), getter_AddRefs(event));
   1.548 +  NS_ENSURE_STATE(event);
   1.549 +
   1.550 +  event->InitEvent(NS_LITERAL_STRING("DOMAutoComplete"), true, true);
   1.551 +
   1.552 +  // XXXjst: We mark this event as a trusted event, it's up to the
   1.553 +  // callers of this to ensure that it's only called from trusted
   1.554 +  // code.
   1.555 +  event->SetTrusted(true);
   1.556 +
   1.557 +  nsCOMPtr<EventTarget> targ = do_QueryInterface(mFocusedInput);
   1.558 +
   1.559 +  bool defaultActionEnabled;
   1.560 +  targ->DispatchEvent(event, &defaultActionEnabled);
   1.561 +  *aPrevent = !defaultActionEnabled;
   1.562 +  return NS_OK;
   1.563 +}
   1.564 +
   1.565 +NS_IMETHODIMP
   1.566 +nsFormFillController::OnTextReverted(bool *_retval)
   1.567 +{
   1.568 +  return NS_OK;
   1.569 +}
   1.570 +
   1.571 +NS_IMETHODIMP
   1.572 +nsFormFillController::GetConsumeRollupEvent(bool *aConsumeRollupEvent)
   1.573 +{
   1.574 +  *aConsumeRollupEvent = false;
   1.575 +  return NS_OK;
   1.576 +}
   1.577 +
   1.578 +NS_IMETHODIMP
   1.579 +nsFormFillController::GetInPrivateContext(bool *aInPrivateContext)
   1.580 +{
   1.581 +  if (!mFocusedInput) {
   1.582 +    *aInPrivateContext = false;
   1.583 +    return NS_OK;
   1.584 +  }
   1.585 +
   1.586 +  nsCOMPtr<nsIDOMDocument> inputDoc;
   1.587 +  nsCOMPtr<nsIDOMElement> element = do_QueryInterface(mFocusedInput);
   1.588 +  element->GetOwnerDocument(getter_AddRefs(inputDoc));
   1.589 +  nsCOMPtr<nsIDocument> doc = do_QueryInterface(inputDoc);
   1.590 +  nsCOMPtr<nsIDocShell> docShell = doc->GetDocShell();
   1.591 +  nsCOMPtr<nsILoadContext> loadContext = doc->GetLoadContext();
   1.592 +  *aInPrivateContext = loadContext && loadContext->UsePrivateBrowsing();
   1.593 +  return NS_OK;
   1.594 +}
   1.595 +
   1.596 +
   1.597 +////////////////////////////////////////////////////////////////////////
   1.598 +//// nsIAutoCompleteSearch
   1.599 +
   1.600 +NS_IMETHODIMP
   1.601 +nsFormFillController::StartSearch(const nsAString &aSearchString, const nsAString &aSearchParam,
   1.602 +                                  nsIAutoCompleteResult *aPreviousResult, nsIAutoCompleteObserver *aListener)
   1.603 +{
   1.604 +  nsresult rv;
   1.605 +  nsCOMPtr<nsIAutoCompleteResult> result;
   1.606 +
   1.607 +  // If the login manager has indicated it's responsible for this field, let it
   1.608 +  // handle the autocomplete. Otherwise, handle with form history.
   1.609 +  bool dummy;
   1.610 +  if (mPwmgrInputs.Get(mFocusedInputNode, &dummy)) {
   1.611 +    // XXX aPreviousResult shouldn't ever be a historyResult type, since we're not letting
   1.612 +    // satchel manage the field?
   1.613 +    rv = mLoginManager->AutoCompleteSearch(aSearchString,
   1.614 +                                           aPreviousResult,
   1.615 +                                           mFocusedInput,
   1.616 +                                           getter_AddRefs(result));
   1.617 +    NS_ENSURE_SUCCESS(rv, rv);
   1.618 +    if (aListener) {
   1.619 +      aListener->OnSearchResult(this, result);
   1.620 +    }
   1.621 +  } else {
   1.622 +    mLastListener = aListener;
   1.623 +
   1.624 +    // It appears that mFocusedInput is always null when we are focusing a XUL
   1.625 +    // element. Scary :)
   1.626 +    if (!mFocusedInput || nsContentUtils::IsAutocompleteEnabled(mFocusedInput)) {
   1.627 +      nsCOMPtr <nsIFormAutoComplete> formAutoComplete =
   1.628 +        do_GetService("@mozilla.org/satchel/form-autocomplete;1", &rv);
   1.629 +      NS_ENSURE_SUCCESS(rv, rv);
   1.630 +
   1.631 +      formAutoComplete->AutoCompleteSearchAsync(aSearchParam,
   1.632 +                                                aSearchString,
   1.633 +                                                mFocusedInput,
   1.634 +                                                aPreviousResult,
   1.635 +                                                this);
   1.636 +      mLastFormAutoComplete = formAutoComplete;
   1.637 +    } else {
   1.638 +      mLastSearchString = aSearchString;
   1.639 +
   1.640 +      // Even if autocomplete is disabled, handle the inputlist anyway as that was
   1.641 +      // specifically requested by the page. This is so a field can have the default
   1.642 +      // autocomplete disabled and replaced with a custom inputlist autocomplete.
   1.643 +      return PerformInputListAutoComplete(aPreviousResult);
   1.644 +    }
   1.645 +  }
   1.646 +
   1.647 +  return NS_OK;
   1.648 +}
   1.649 +
   1.650 +nsresult
   1.651 +nsFormFillController::PerformInputListAutoComplete(nsIAutoCompleteResult* aPreviousResult)
   1.652 +{
   1.653 +  // If an <input> is focused, check if it has a list="<datalist>" which can
   1.654 +  // provide the list of suggestions.
   1.655 +
   1.656 +  nsresult rv;
   1.657 +  nsCOMPtr<nsIAutoCompleteResult> result;
   1.658 +
   1.659 +  nsCOMPtr <nsIInputListAutoComplete> inputListAutoComplete =
   1.660 +    do_GetService("@mozilla.org/satchel/inputlist-autocomplete;1", &rv);
   1.661 +  NS_ENSURE_SUCCESS(rv, rv);
   1.662 +  rv = inputListAutoComplete->AutoCompleteSearch(aPreviousResult,
   1.663 +                                                 mLastSearchString,
   1.664 +                                                 mFocusedInput,
   1.665 +                                                 getter_AddRefs(result));
   1.666 +  NS_ENSURE_SUCCESS(rv, rv);
   1.667 +
   1.668 +  if (mFocusedInput) {
   1.669 +    nsCOMPtr<nsIDOMHTMLElement> list;
   1.670 +    mFocusedInput->GetList(getter_AddRefs(list));
   1.671 +
   1.672 +    // Add a mutation observer to check for changes to the items in the <datalist>
   1.673 +    // and update the suggestions accordingly.
   1.674 +    nsCOMPtr<nsINode> node = do_QueryInterface(list);
   1.675 +    if (mListNode != node) {
   1.676 +      if (mListNode) {
   1.677 +        mListNode->RemoveMutationObserver(this);
   1.678 +        mListNode = nullptr;
   1.679 +      }
   1.680 +      if (node) {
   1.681 +        node->AddMutationObserverUnlessExists(this);
   1.682 +        mListNode = node;
   1.683 +      }
   1.684 +    }
   1.685 +  }
   1.686 +
   1.687 +  if (mLastListener) {
   1.688 +    mLastListener->OnSearchResult(this, result);
   1.689 +  }
   1.690 +
   1.691 +  return NS_OK;
   1.692 +}
   1.693 +
   1.694 +class UpdateSearchResultRunnable : public nsRunnable
   1.695 +{
   1.696 +public:
   1.697 +  UpdateSearchResultRunnable(nsIAutoCompleteObserver* aObserver,
   1.698 +                             nsIAutoCompleteSearch* aSearch,
   1.699 +                             nsIAutoCompleteResult* aResult)
   1.700 +    : mObserver(aObserver)
   1.701 +    , mSearch(aSearch)
   1.702 +    , mResult(aResult)
   1.703 +  {}
   1.704 +
   1.705 +  NS_IMETHOD Run() {
   1.706 +    NS_ASSERTION(mObserver, "You shouldn't call this runnable with a null observer!");
   1.707 +
   1.708 +    mObserver->OnUpdateSearchResult(mSearch, mResult);
   1.709 +    return NS_OK;
   1.710 +  }
   1.711 +
   1.712 +private:
   1.713 +  nsCOMPtr<nsIAutoCompleteObserver> mObserver;
   1.714 +  nsCOMPtr<nsIAutoCompleteSearch> mSearch;
   1.715 +  nsCOMPtr<nsIAutoCompleteResult> mResult;
   1.716 +};
   1.717 +
   1.718 +void nsFormFillController::RevalidateDataList()
   1.719 +{
   1.720 +  if (!mLastListener) {
   1.721 +    return;
   1.722 +  }
   1.723 +  nsresult rv;
   1.724 +  nsCOMPtr <nsIInputListAutoComplete> inputListAutoComplete =
   1.725 +    do_GetService("@mozilla.org/satchel/inputlist-autocomplete;1", &rv);
   1.726 +
   1.727 +  nsCOMPtr<nsIAutoCompleteResult> result;
   1.728 +
   1.729 +  rv = inputListAutoComplete->AutoCompleteSearch(mLastSearchResult,
   1.730 +                                                 mLastSearchString,
   1.731 +                                                 mFocusedInput,
   1.732 +                                                 getter_AddRefs(result));
   1.733 +
   1.734 +  nsCOMPtr<nsIRunnable> event =
   1.735 +    new UpdateSearchResultRunnable(mLastListener, this, result);
   1.736 +  NS_DispatchToCurrentThread(event);
   1.737 +}
   1.738 +
   1.739 +NS_IMETHODIMP
   1.740 +nsFormFillController::StopSearch()
   1.741 +{
   1.742 +  // Make sure to stop and clear this, otherwise the controller will prevent
   1.743 +  // mLastFormAutoComplete from being deleted.
   1.744 +  if (mLastFormAutoComplete) {
   1.745 +    mLastFormAutoComplete->StopAutoCompleteSearch();
   1.746 +    mLastFormAutoComplete = nullptr;
   1.747 +  }
   1.748 +  return NS_OK;
   1.749 +}
   1.750 +
   1.751 +////////////////////////////////////////////////////////////////////////
   1.752 +//// nsIFormAutoCompleteObserver
   1.753 +
   1.754 +NS_IMETHODIMP
   1.755 +nsFormFillController::OnSearchCompletion(nsIAutoCompleteResult *aResult)
   1.756 +{
   1.757 +  nsCOMPtr<nsIAutoCompleteResult> resultParam = do_QueryInterface(aResult);
   1.758 +
   1.759 +  nsAutoString searchString;
   1.760 +  resultParam->GetSearchString(searchString);
   1.761 +  mLastSearchResult = aResult;
   1.762 +  mLastSearchString = searchString;
   1.763 +
   1.764 +  return PerformInputListAutoComplete(resultParam);
   1.765 +}
   1.766 +
   1.767 +////////////////////////////////////////////////////////////////////////
   1.768 +//// nsIDOMEventListener
   1.769 +
   1.770 +NS_IMETHODIMP
   1.771 +nsFormFillController::HandleEvent(nsIDOMEvent* aEvent)
   1.772 +{
   1.773 +  nsAutoString type;
   1.774 +  aEvent->GetType(type);
   1.775 +
   1.776 +  if (type.EqualsLiteral("focus")) {
   1.777 +    return Focus(aEvent);
   1.778 +  }
   1.779 +  if (type.EqualsLiteral("mousedown")) {
   1.780 +    return MouseDown(aEvent);
   1.781 +  }
   1.782 +  if (type.EqualsLiteral("keypress")) {
   1.783 +    return KeyPress(aEvent);
   1.784 +  }
   1.785 +  if (type.EqualsLiteral("input")) {
   1.786 +    return (!mSuppressOnInput && mController && mFocusedInput) ?
   1.787 +           mController->HandleText() : NS_OK;
   1.788 +  }
   1.789 +  if (type.EqualsLiteral("blur")) {
   1.790 +    if (mFocusedInput)
   1.791 +      StopControllingInput();
   1.792 +    return NS_OK;
   1.793 +  }
   1.794 +  if (type.EqualsLiteral("compositionstart")) {
   1.795 +    NS_ASSERTION(mController, "should have a controller!");
   1.796 +    if (mController && mFocusedInput)
   1.797 +      mController->HandleStartComposition();
   1.798 +    return NS_OK;
   1.799 +  }
   1.800 +  if (type.EqualsLiteral("compositionend")) {
   1.801 +    NS_ASSERTION(mController, "should have a controller!");
   1.802 +    if (mController && mFocusedInput)
   1.803 +      mController->HandleEndComposition();
   1.804 +    return NS_OK;
   1.805 +  }
   1.806 +  if (type.EqualsLiteral("contextmenu")) {
   1.807 +    if (mFocusedPopup)
   1.808 +      mFocusedPopup->ClosePopup();
   1.809 +    return NS_OK;
   1.810 +  }
   1.811 +  if (type.EqualsLiteral("pagehide")) {
   1.812 +
   1.813 +    nsCOMPtr<nsIDocument> doc = do_QueryInterface(
   1.814 +      aEvent->InternalDOMEvent()->GetTarget());
   1.815 +    if (!doc)
   1.816 +      return NS_OK;
   1.817 +
   1.818 +    if (mFocusedInput) {
   1.819 +      if (doc == mFocusedInputNode->OwnerDoc())
   1.820 +        StopControllingInput();
   1.821 +    }
   1.822 +
   1.823 +    PwmgrInputsEnumData ed(this, doc);
   1.824 +    mPwmgrInputs.Enumerate(RemoveForDocumentEnumerator, &ed);
   1.825 +  }
   1.826 +
   1.827 +  return NS_OK;
   1.828 +}
   1.829 +
   1.830 +
   1.831 +/* static */ PLDHashOperator
   1.832 +nsFormFillController::RemoveForDocumentEnumerator(const nsINode* aKey,
   1.833 +                                                  bool& aEntry,
   1.834 +                                                  void* aUserData)
   1.835 +{
   1.836 +  PwmgrInputsEnumData* ed = static_cast<PwmgrInputsEnumData*>(aUserData);
   1.837 +  if (aKey && (!ed->mDoc || aKey->OwnerDoc() == ed->mDoc)) {
   1.838 +    // mFocusedInputNode's observer is tracked separately, don't remove it here.
   1.839 +    if (aKey != ed->mFFC->mFocusedInputNode) {
   1.840 +      const_cast<nsINode*>(aKey)->RemoveMutationObserver(ed->mFFC);
   1.841 +    }
   1.842 +    return PL_DHASH_REMOVE;
   1.843 +  }
   1.844 +  return PL_DHASH_NEXT;
   1.845 +}
   1.846 +
   1.847 +nsresult
   1.848 +nsFormFillController::Focus(nsIDOMEvent* aEvent)
   1.849 +{
   1.850 +  nsCOMPtr<nsIDOMHTMLInputElement> input = do_QueryInterface(
   1.851 +    aEvent->InternalDOMEvent()->GetTarget());
   1.852 +  nsCOMPtr<nsINode> inputNode = do_QueryInterface(input);
   1.853 +  if (!inputNode)
   1.854 +    return NS_OK;
   1.855 +
   1.856 +  nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(input);
   1.857 +  if (!formControl || !formControl->IsSingleLineTextControl(true))
   1.858 +    return NS_OK;
   1.859 +
   1.860 +  bool isReadOnly = false;
   1.861 +  input->GetReadOnly(&isReadOnly);
   1.862 +  if (isReadOnly)
   1.863 +    return NS_OK;
   1.864 +
   1.865 +  bool autocomplete = nsContentUtils::IsAutocompleteEnabled(input);
   1.866 +
   1.867 +  nsCOMPtr<nsIDOMHTMLElement> datalist;
   1.868 +  input->GetList(getter_AddRefs(datalist));
   1.869 +  bool hasList = datalist != nullptr;
   1.870 +
   1.871 +  bool dummy;
   1.872 +  bool isPwmgrInput = false;
   1.873 +  if (mPwmgrInputs.Get(inputNode, &dummy))
   1.874 +      isPwmgrInput = true;
   1.875 +
   1.876 +  if (isPwmgrInput || hasList || autocomplete) {
   1.877 +    StartControllingInput(input);
   1.878 +  }
   1.879 +
   1.880 +  return NS_OK;
   1.881 +}
   1.882 +
   1.883 +nsresult
   1.884 +nsFormFillController::KeyPress(nsIDOMEvent* aEvent)
   1.885 +{
   1.886 +  NS_ASSERTION(mController, "should have a controller!");
   1.887 +  if (!mFocusedInput || !mController)
   1.888 +    return NS_OK;
   1.889 +
   1.890 +  nsCOMPtr<nsIDOMKeyEvent> keyEvent = do_QueryInterface(aEvent);
   1.891 +  if (!keyEvent)
   1.892 +    return NS_ERROR_FAILURE;
   1.893 +
   1.894 +  bool cancel = false;
   1.895 +
   1.896 +  uint32_t k;
   1.897 +  keyEvent->GetKeyCode(&k);
   1.898 +  switch (k) {
   1.899 +  case nsIDOMKeyEvent::DOM_VK_DELETE:
   1.900 +#ifndef XP_MACOSX
   1.901 +    mController->HandleDelete(&cancel);
   1.902 +    break;
   1.903 +  case nsIDOMKeyEvent::DOM_VK_BACK_SPACE:
   1.904 +    mController->HandleText();
   1.905 +    break;
   1.906 +#else
   1.907 +  case nsIDOMKeyEvent::DOM_VK_BACK_SPACE:
   1.908 +    {
   1.909 +      bool isShift = false;
   1.910 +      keyEvent->GetShiftKey(&isShift);
   1.911 +
   1.912 +      if (isShift)
   1.913 +        mController->HandleDelete(&cancel);
   1.914 +      else
   1.915 +        mController->HandleText();
   1.916 +
   1.917 +      break;
   1.918 +    }
   1.919 +#endif
   1.920 +  case nsIDOMKeyEvent::DOM_VK_PAGE_UP:
   1.921 +  case nsIDOMKeyEvent::DOM_VK_PAGE_DOWN:
   1.922 +    {
   1.923 +      bool isCtrl, isAlt, isMeta;
   1.924 +      keyEvent->GetCtrlKey(&isCtrl);
   1.925 +      keyEvent->GetAltKey(&isAlt);
   1.926 +      keyEvent->GetMetaKey(&isMeta);
   1.927 +      if (isCtrl || isAlt || isMeta)
   1.928 +        break;
   1.929 +    }
   1.930 +    /* fall through */
   1.931 +  case nsIDOMKeyEvent::DOM_VK_UP:
   1.932 +  case nsIDOMKeyEvent::DOM_VK_DOWN:
   1.933 +  case nsIDOMKeyEvent::DOM_VK_LEFT:
   1.934 +  case nsIDOMKeyEvent::DOM_VK_RIGHT:
   1.935 +    mController->HandleKeyNavigation(k, &cancel);
   1.936 +    break;
   1.937 +  case nsIDOMKeyEvent::DOM_VK_ESCAPE:
   1.938 +    mController->HandleEscape(&cancel);
   1.939 +    break;
   1.940 +  case nsIDOMKeyEvent::DOM_VK_TAB:
   1.941 +    mController->HandleTab();
   1.942 +    cancel = false;
   1.943 +    break;
   1.944 +  case nsIDOMKeyEvent::DOM_VK_RETURN:
   1.945 +    mController->HandleEnter(false, &cancel);
   1.946 +    break;
   1.947 +  }
   1.948 +
   1.949 +  if (cancel) {
   1.950 +    aEvent->PreventDefault();
   1.951 +  }
   1.952 +
   1.953 +  return NS_OK;
   1.954 +}
   1.955 +
   1.956 +nsresult
   1.957 +nsFormFillController::MouseDown(nsIDOMEvent* aEvent)
   1.958 +{
   1.959 +  nsCOMPtr<nsIDOMMouseEvent> mouseEvent(do_QueryInterface(aEvent));
   1.960 +  if (!mouseEvent)
   1.961 +    return NS_ERROR_FAILURE;
   1.962 +
   1.963 +  nsCOMPtr<nsIDOMHTMLInputElement> targetInput = do_QueryInterface(
   1.964 +    aEvent->InternalDOMEvent()->GetTarget());
   1.965 +  if (!targetInput)
   1.966 +    return NS_OK;
   1.967 +
   1.968 +  int16_t button;
   1.969 +  mouseEvent->GetButton(&button);
   1.970 +  if (button != 0)
   1.971 +    return NS_OK;
   1.972 +
   1.973 +  bool isOpen = false;
   1.974 +  GetPopupOpen(&isOpen);
   1.975 +  if (isOpen)
   1.976 +    return NS_OK;
   1.977 +
   1.978 +  nsCOMPtr<nsIAutoCompleteInput> input;
   1.979 +  mController->GetInput(getter_AddRefs(input));
   1.980 +  if (!input)
   1.981 +    return NS_OK;
   1.982 +
   1.983 +  nsAutoString value;
   1.984 +  input->GetTextValue(value);
   1.985 +  if (value.Length() > 0) {
   1.986 +    // Show the popup with a filtered result set
   1.987 +    mController->SetSearchString(EmptyString());
   1.988 +    mController->HandleText();
   1.989 +  } else {
   1.990 +    // Show the popup with the complete result set.  Can't use HandleText()
   1.991 +    // because it doesn't display the popup if the input is blank.
   1.992 +    bool cancel = false;
   1.993 +    mController->HandleKeyNavigation(nsIDOMKeyEvent::DOM_VK_DOWN, &cancel);
   1.994 +  }
   1.995 +
   1.996 +  return NS_OK;
   1.997 +}
   1.998 +
   1.999 +////////////////////////////////////////////////////////////////////////
  1.1000 +//// nsFormFillController
  1.1001 +
  1.1002 +void
  1.1003 +nsFormFillController::AddWindowListeners(nsIDOMWindow *aWindow)
  1.1004 +{
  1.1005 +  if (!aWindow)
  1.1006 +    return;
  1.1007 +
  1.1008 +  nsCOMPtr<nsPIDOMWindow> privateDOMWindow(do_QueryInterface(aWindow));
  1.1009 +  EventTarget* target = nullptr;
  1.1010 +  if (privateDOMWindow)
  1.1011 +    target = privateDOMWindow->GetChromeEventHandler();
  1.1012 +
  1.1013 +  if (!target)
  1.1014 +    return;
  1.1015 +
  1.1016 +  target->AddEventListener(NS_LITERAL_STRING("focus"), this,
  1.1017 +                           true, false);
  1.1018 +  target->AddEventListener(NS_LITERAL_STRING("blur"), this,
  1.1019 +                           true, false);
  1.1020 +  target->AddEventListener(NS_LITERAL_STRING("pagehide"), this,
  1.1021 +                           true, false);
  1.1022 +  target->AddEventListener(NS_LITERAL_STRING("mousedown"), this,
  1.1023 +                           true, false);
  1.1024 +  target->AddEventListener(NS_LITERAL_STRING("input"), this,
  1.1025 +                           true, false);
  1.1026 +  target->AddEventListener(NS_LITERAL_STRING("compositionstart"), this,
  1.1027 +                           true, false);
  1.1028 +  target->AddEventListener(NS_LITERAL_STRING("compositionend"), this,
  1.1029 +                           true, false);
  1.1030 +  target->AddEventListener(NS_LITERAL_STRING("contextmenu"), this,
  1.1031 +                           true, false);
  1.1032 +
  1.1033 +  // Note that any additional listeners added should ensure that they ignore
  1.1034 +  // untrusted events, which might be sent by content that's up to no good.
  1.1035 +}
  1.1036 +
  1.1037 +void
  1.1038 +nsFormFillController::RemoveWindowListeners(nsIDOMWindow *aWindow)
  1.1039 +{
  1.1040 +  if (!aWindow)
  1.1041 +    return;
  1.1042 +
  1.1043 +  StopControllingInput();
  1.1044 +
  1.1045 +  nsCOMPtr<nsIDOMDocument> domDoc;
  1.1046 +  aWindow->GetDocument(getter_AddRefs(domDoc));
  1.1047 +  nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
  1.1048 +  PwmgrInputsEnumData ed(this, doc);
  1.1049 +  mPwmgrInputs.Enumerate(RemoveForDocumentEnumerator, &ed);
  1.1050 +
  1.1051 +  nsCOMPtr<nsPIDOMWindow> privateDOMWindow(do_QueryInterface(aWindow));
  1.1052 +  EventTarget* target = nullptr;
  1.1053 +  if (privateDOMWindow)
  1.1054 +    target = privateDOMWindow->GetChromeEventHandler();
  1.1055 +
  1.1056 +  if (!target)
  1.1057 +    return;
  1.1058 +
  1.1059 +  target->RemoveEventListener(NS_LITERAL_STRING("focus"), this, true);
  1.1060 +  target->RemoveEventListener(NS_LITERAL_STRING("blur"), this, true);
  1.1061 +  target->RemoveEventListener(NS_LITERAL_STRING("pagehide"), this, true);
  1.1062 +  target->RemoveEventListener(NS_LITERAL_STRING("mousedown"), this, true);
  1.1063 +  target->RemoveEventListener(NS_LITERAL_STRING("input"), this, true);
  1.1064 +  target->RemoveEventListener(NS_LITERAL_STRING("compositionstart"), this,
  1.1065 +                              true);
  1.1066 +  target->RemoveEventListener(NS_LITERAL_STRING("compositionend"), this,
  1.1067 +                              true);
  1.1068 +  target->RemoveEventListener(NS_LITERAL_STRING("contextmenu"), this, true);
  1.1069 +}
  1.1070 +
  1.1071 +void
  1.1072 +nsFormFillController::AddKeyListener(nsINode* aInput)
  1.1073 +{
  1.1074 +  aInput->AddEventListener(NS_LITERAL_STRING("keypress"), this,
  1.1075 +                           true, false);
  1.1076 +}
  1.1077 +
  1.1078 +void
  1.1079 +nsFormFillController::RemoveKeyListener()
  1.1080 +{
  1.1081 +  if (!mFocusedInputNode)
  1.1082 +    return;
  1.1083 +
  1.1084 +  mFocusedInputNode->RemoveEventListener(NS_LITERAL_STRING("keypress"), this, true);
  1.1085 +}
  1.1086 +
  1.1087 +void
  1.1088 +nsFormFillController::StartControllingInput(nsIDOMHTMLInputElement *aInput)
  1.1089 +{
  1.1090 +  // Make sure we're not still attached to an input
  1.1091 +  StopControllingInput();
  1.1092 +
  1.1093 +  // Find the currently focused docShell
  1.1094 +  nsCOMPtr<nsIDocShell> docShell = GetDocShellForInput(aInput);
  1.1095 +  int32_t index = GetIndexOfDocShell(docShell);
  1.1096 +  if (index < 0)
  1.1097 +    return;
  1.1098 +
  1.1099 +  // Cache the popup for the focused docShell
  1.1100 +  mFocusedPopup = mPopups.SafeElementAt(index);
  1.1101 +
  1.1102 +  nsCOMPtr<nsINode> node = do_QueryInterface(aInput);
  1.1103 +  if (!node) {
  1.1104 +    return;
  1.1105 +  }
  1.1106 +
  1.1107 +  AddKeyListener(node);
  1.1108 +
  1.1109 +  node->AddMutationObserverUnlessExists(this);
  1.1110 +  mFocusedInputNode = node;
  1.1111 +  mFocusedInput = aInput;
  1.1112 +
  1.1113 +  nsCOMPtr<nsIDOMHTMLElement> list;
  1.1114 +  mFocusedInput->GetList(getter_AddRefs(list));
  1.1115 +  nsCOMPtr<nsINode> listNode = do_QueryInterface(list);
  1.1116 +  if (listNode) {
  1.1117 +    listNode->AddMutationObserverUnlessExists(this);
  1.1118 +    mListNode = listNode;
  1.1119 +  }
  1.1120 +
  1.1121 +  // Now we are the autocomplete controller's bitch
  1.1122 +  mController->SetInput(this);
  1.1123 +}
  1.1124 +
  1.1125 +void
  1.1126 +nsFormFillController::StopControllingInput()
  1.1127 +{
  1.1128 +  RemoveKeyListener();
  1.1129 +
  1.1130 +  if (mListNode) {
  1.1131 +    mListNode->RemoveMutationObserver(this);
  1.1132 +    mListNode = nullptr;
  1.1133 +  }
  1.1134 +
  1.1135 +  // Reset the controller's input, but not if it has been switched
  1.1136 +  // to another input already, which might happen if the user switches
  1.1137 +  // focus by clicking another autocomplete textbox
  1.1138 +  nsCOMPtr<nsIAutoCompleteInput> input;
  1.1139 +  mController->GetInput(getter_AddRefs(input));
  1.1140 +  if (input == this)
  1.1141 +    mController->SetInput(nullptr);
  1.1142 +
  1.1143 +  if (mFocusedInputNode) {
  1.1144 +    MaybeRemoveMutationObserver(mFocusedInputNode);
  1.1145 +    mFocusedInputNode = nullptr;
  1.1146 +    mFocusedInput = nullptr;
  1.1147 +  }
  1.1148 +  mFocusedPopup = nullptr;
  1.1149 +}
  1.1150 +
  1.1151 +nsIDocShell *
  1.1152 +nsFormFillController::GetDocShellForInput(nsIDOMHTMLInputElement *aInput)
  1.1153 +{
  1.1154 +  nsCOMPtr<nsIDOMDocument> domDoc;
  1.1155 +  nsCOMPtr<nsIDOMElement> element = do_QueryInterface(aInput);
  1.1156 +  element->GetOwnerDocument(getter_AddRefs(domDoc));
  1.1157 +  nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
  1.1158 +  NS_ENSURE_TRUE(doc, nullptr);
  1.1159 +  nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(doc->GetWindow());
  1.1160 +  nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(webNav);
  1.1161 +  return docShell;
  1.1162 +}
  1.1163 +
  1.1164 +nsIDOMWindow *
  1.1165 +nsFormFillController::GetWindowForDocShell(nsIDocShell *aDocShell)
  1.1166 +{
  1.1167 +  nsCOMPtr<nsIContentViewer> contentViewer;
  1.1168 +  aDocShell->GetContentViewer(getter_AddRefs(contentViewer));
  1.1169 +  NS_ENSURE_TRUE(contentViewer, nullptr);
  1.1170 +
  1.1171 +  nsCOMPtr<nsIDOMDocument> domDoc;
  1.1172 +  contentViewer->GetDOMDocument(getter_AddRefs(domDoc));
  1.1173 +  nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
  1.1174 +  NS_ENSURE_TRUE(doc, nullptr);
  1.1175 +
  1.1176 +  return doc->GetWindow();
  1.1177 +}
  1.1178 +
  1.1179 +int32_t
  1.1180 +nsFormFillController::GetIndexOfDocShell(nsIDocShell *aDocShell)
  1.1181 +{
  1.1182 +  if (!aDocShell)
  1.1183 +    return -1;
  1.1184 +
  1.1185 +  // Loop through our cached docShells looking for the given docShell
  1.1186 +  uint32_t count = mDocShells.Length();
  1.1187 +  for (uint32_t i = 0; i < count; ++i) {
  1.1188 +    if (mDocShells[i] == aDocShell)
  1.1189 +      return i;
  1.1190 +  }
  1.1191 +
  1.1192 +  // Recursively check the parent docShell of this one
  1.1193 +  nsCOMPtr<nsIDocShellTreeItem> treeItem = do_QueryInterface(aDocShell);
  1.1194 +  nsCOMPtr<nsIDocShellTreeItem> parentItem;
  1.1195 +  treeItem->GetParent(getter_AddRefs(parentItem));
  1.1196 +  if (parentItem) {
  1.1197 +    nsCOMPtr<nsIDocShell> parentShell = do_QueryInterface(parentItem);
  1.1198 +    return GetIndexOfDocShell(parentShell);
  1.1199 +  }
  1.1200 +
  1.1201 +  return -1;
  1.1202 +}
  1.1203 +
  1.1204 +NS_GENERIC_FACTORY_CONSTRUCTOR(nsFormFillController)
  1.1205 +
  1.1206 +NS_DEFINE_NAMED_CID(NS_FORMFILLCONTROLLER_CID);
  1.1207 +
  1.1208 +static const mozilla::Module::CIDEntry kSatchelCIDs[] = {
  1.1209 +  { &kNS_FORMFILLCONTROLLER_CID, false, nullptr, nsFormFillControllerConstructor },
  1.1210 +  { nullptr }
  1.1211 +};
  1.1212 +
  1.1213 +static const mozilla::Module::ContractIDEntry kSatchelContracts[] = {
  1.1214 +  { "@mozilla.org/satchel/form-fill-controller;1", &kNS_FORMFILLCONTROLLER_CID },
  1.1215 +  { NS_FORMHISTORYAUTOCOMPLETE_CONTRACTID, &kNS_FORMFILLCONTROLLER_CID },
  1.1216 +  { nullptr }
  1.1217 +};
  1.1218 +
  1.1219 +static const mozilla::Module kSatchelModule = {
  1.1220 +  mozilla::Module::kVersion,
  1.1221 +  kSatchelCIDs,
  1.1222 +  kSatchelContracts
  1.1223 +};
  1.1224 +
  1.1225 +NSMODULE_DEFN(satchel) = &kSatchelModule;

mercurial