InjectedBundlePageFormClient.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "InjectedBundlePageFormClient.h"
  27. #include "ImmutableArray.h"
  28. #include "ImmutableDictionary.h"
  29. #include "InjectedBundleNodeHandle.h"
  30. #include "WKAPICast.h"
  31. #include "WKBundleAPICast.h"
  32. #include <WebCore/HTMLFormElement.h>
  33. #include <WebCore/HTMLInputElement.h>
  34. #include <WebCore/HTMLTextAreaElement.h>
  35. using namespace WebCore;
  36. namespace WebKit {
  37. void InjectedBundlePageFormClient::didFocusTextField(WebPage* page, HTMLInputElement* inputElement, WebFrame* frame)
  38. {
  39. if (!m_client.didFocusTextField)
  40. return;
  41. RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(inputElement);
  42. m_client.didFocusTextField(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), m_client.clientInfo);
  43. }
  44. void InjectedBundlePageFormClient::textFieldDidBeginEditing(WebPage* page, HTMLInputElement* inputElement, WebFrame* frame)
  45. {
  46. if (!m_client.textFieldDidBeginEditing)
  47. return;
  48. RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(inputElement);
  49. m_client.textFieldDidBeginEditing(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), m_client.clientInfo);
  50. }
  51. void InjectedBundlePageFormClient::textFieldDidEndEditing(WebPage* page, HTMLInputElement* inputElement, WebFrame* frame)
  52. {
  53. if (!m_client.textFieldDidEndEditing)
  54. return;
  55. RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(inputElement);
  56. m_client.textFieldDidEndEditing(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), m_client.clientInfo);
  57. }
  58. void InjectedBundlePageFormClient::textDidChangeInTextField(WebPage* page, HTMLInputElement* inputElement, WebFrame* frame)
  59. {
  60. if (!m_client.textDidChangeInTextField)
  61. return;
  62. RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(inputElement);
  63. m_client.textDidChangeInTextField(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), m_client.clientInfo);
  64. }
  65. void InjectedBundlePageFormClient::textDidChangeInTextArea(WebPage* page, HTMLTextAreaElement* textAreaElement, WebFrame* frame)
  66. {
  67. if (!m_client.textDidChangeInTextArea)
  68. return;
  69. RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(textAreaElement);
  70. m_client.textDidChangeInTextArea(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), m_client.clientInfo);
  71. }
  72. bool InjectedBundlePageFormClient::shouldPerformActionInTextField(WebPage* page, HTMLInputElement* inputElement, WKInputFieldActionType actionType, WebFrame* frame)
  73. {
  74. if (!m_client.shouldPerformActionInTextField)
  75. return false;
  76. RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(inputElement);
  77. return m_client.shouldPerformActionInTextField(toAPI(page), toAPI(nodeHandle.get()), actionType, toAPI(frame), m_client.clientInfo);
  78. }
  79. void InjectedBundlePageFormClient::willSendSubmitEvent(WebPage* page, HTMLFormElement* formElement, WebFrame* frame, WebFrame* sourceFrame, const Vector<std::pair<String, String>>& values)
  80. {
  81. if (!m_client.willSendSubmitEvent)
  82. return;
  83. RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(formElement);
  84. ImmutableDictionary::MapType map;
  85. for (size_t i = 0; i < values.size(); ++i)
  86. map.set(values[i].first, WebString::create(values[i].second));
  87. RefPtr<ImmutableDictionary> textFieldsMap = ImmutableDictionary::adopt(map);
  88. m_client.willSendSubmitEvent(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), toAPI(sourceFrame), toAPI(textFieldsMap.get()), m_client.clientInfo);
  89. }
  90. void InjectedBundlePageFormClient::willSubmitForm(WebPage* page, HTMLFormElement* formElement, WebFrame* frame, WebFrame* sourceFrame, const Vector<std::pair<String, String>>& values, RefPtr<APIObject>& userData)
  91. {
  92. if (!m_client.willSubmitForm)
  93. return;
  94. RefPtr<InjectedBundleNodeHandle> nodeHandle = InjectedBundleNodeHandle::getOrCreate(formElement);
  95. ImmutableDictionary::MapType map;
  96. for (size_t i = 0; i < values.size(); ++i)
  97. map.set(values[i].first, WebString::create(values[i].second));
  98. RefPtr<ImmutableDictionary> textFieldsMap = ImmutableDictionary::adopt(map);
  99. WKTypeRef userDataToPass = 0;
  100. m_client.willSubmitForm(toAPI(page), toAPI(nodeHandle.get()), toAPI(frame), toAPI(sourceFrame), toAPI(textFieldsMap.get()), &userDataToPass, m_client.clientInfo);
  101. userData = adoptRef(toImpl(userDataToPass));
  102. }
  103. void InjectedBundlePageFormClient::didAssociateFormControls(WebPage* page, const Vector<RefPtr<WebCore::Element>>& elements)
  104. {
  105. if (!m_client.didAssociateFormControls)
  106. return;
  107. size_t size = elements.size();
  108. Vector<RefPtr<APIObject>> elementHandles;
  109. elementHandles.reserveCapacity(size);
  110. for (size_t i = 0; i < size; ++i)
  111. elementHandles.uncheckedAppend(InjectedBundleNodeHandle::getOrCreate(elements[i].get()).get());
  112. m_client.didAssociateFormControls(toAPI(page), toAPI(ImmutableArray::adopt(elementHandles).get()), m_client.clientInfo);
  113. }
  114. bool InjectedBundlePageFormClient::shouldNotifyOnFormChanges(WebPage* page)
  115. {
  116. if (!m_client.shouldNotifyOnFormChanges)
  117. return false;
  118. return m_client.shouldNotifyOnFormChanges(toAPI(page), m_client.clientInfo);
  119. }
  120. } // namespace WebKit