AutofillManager.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2012 Research In Motion Limited. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "config.h"
  19. #include "AutofillManager.h"
  20. #include "AutofillBackingStore.h"
  21. #include "HTMLCollection.h"
  22. #include "HTMLFormElement.h"
  23. #include "HTMLInputElement.h"
  24. #include "WebPage_p.h"
  25. namespace WebCore {
  26. static bool isAutofillable(HTMLInputElement* element)
  27. {
  28. return element && element->isTextField() && !element->isPasswordField()
  29. && !element->isAutofilled() && element->shouldAutocomplete();
  30. }
  31. PassRefPtr<AutofillManager> AutofillManager::create(BlackBerry::WebKit::WebPagePrivate* page)
  32. {
  33. return adoptRef(new AutofillManager(page));
  34. }
  35. void AutofillManager::didChangeInTextField(HTMLInputElement* element)
  36. {
  37. if (!isAutofillable(element))
  38. return;
  39. if (m_element != element)
  40. m_element = element;
  41. Vector<String> candidates = autofillBackingStore().get(element->getAttribute(HTMLNames::nameAttr).string(), element->value());
  42. m_webPagePrivate->notifyPopupAutofillDialog(candidates);
  43. }
  44. void AutofillManager::textFieldDidEndEditing(HTMLInputElement*)
  45. {
  46. m_webPagePrivate->notifyDismissAutofillDialog();
  47. }
  48. void AutofillManager::autofillTextField(const String& value)
  49. {
  50. if (!m_element)
  51. return;
  52. m_element->setValue(value);
  53. m_element->setAutofilled();
  54. }
  55. void AutofillManager::saveTextFields(HTMLFormElement* form)
  56. {
  57. RefPtr<HTMLCollection> elements = form->elements();
  58. size_t itemCount = elements->length();
  59. for (size_t i = 0; i < itemCount; ++i) {
  60. HTMLInputElement* element = form->item(i)->toInputElement();
  61. if (!isAutofillable(element))
  62. continue;
  63. autofillBackingStore().add(element->getAttribute(HTMLNames::nameAttr).string(), element->value());
  64. }
  65. }
  66. void AutofillManager::clear()
  67. {
  68. autofillBackingStore().clear();
  69. }
  70. } // namespace WebCore