atom_autofill_agent.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright (c) 2017 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/renderer/atom_autofill_agent.h"
  5. #include <vector>
  6. #include "atom/common/api/api_messages.h"
  7. #include "content/public/renderer/render_frame.h"
  8. #include "content/public/renderer/render_view.h"
  9. #include "third_party/WebKit/public/platform/WebKeyboardEvent.h"
  10. #include "third_party/WebKit/public/platform/WebString.h"
  11. #include "third_party/WebKit/public/web/WebDocument.h"
  12. #include "third_party/WebKit/public/web/WebLocalFrame.h"
  13. #include "third_party/WebKit/public/web/WebOptionElement.h"
  14. #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
  15. #include "ui/events/keycodes/keyboard_codes.h"
  16. #include "ui/gfx/geometry/rect_f.h"
  17. namespace atom {
  18. namespace {
  19. const size_t kMaxDataLength = 1024;
  20. const size_t kMaxListSize = 512;
  21. void GetDataListSuggestions(const blink::WebInputElement& element,
  22. std::vector<base::string16>* values,
  23. std::vector<base::string16>* labels) {
  24. for (const auto& option : element.FilteredDataListOptions()) {
  25. values->push_back(option.Value().Utf16());
  26. if (option.Value() != option.Label())
  27. labels->push_back(option.Label().Utf16());
  28. else
  29. labels->push_back(base::string16());
  30. }
  31. }
  32. void TrimStringVectorForIPC(std::vector<base::string16>* strings) {
  33. // Limit the size of the vector.
  34. if (strings->size() > kMaxListSize)
  35. strings->resize(kMaxListSize);
  36. // Limit the size of the strings in the vector.
  37. for (size_t i = 0; i < strings->size(); ++i) {
  38. if ((*strings)[i].length() > kMaxDataLength)
  39. (*strings)[i].resize(kMaxDataLength);
  40. }
  41. }
  42. } // namespace
  43. AutofillAgent::AutofillAgent(content::RenderFrame* frame)
  44. : content::RenderFrameObserver(frame), weak_ptr_factory_(this) {
  45. render_frame()->GetWebFrame()->SetAutofillClient(this);
  46. }
  47. AutofillAgent::~AutofillAgent() = default;
  48. void AutofillAgent::OnDestruct() {
  49. delete this;
  50. }
  51. void AutofillAgent::DidChangeScrollOffset() {
  52. HidePopup();
  53. }
  54. void AutofillAgent::FocusedNodeChanged(const blink::WebNode&) {
  55. focused_node_was_last_clicked_ = false;
  56. was_focused_before_now_ = false;
  57. HidePopup();
  58. }
  59. void AutofillAgent::TextFieldDidEndEditing(const blink::WebInputElement&) {
  60. HidePopup();
  61. }
  62. void AutofillAgent::TextFieldDidChange(
  63. const blink::WebFormControlElement& element) {
  64. if (!IsUserGesture() && !render_frame()->IsPasting())
  65. return;
  66. weak_ptr_factory_.InvalidateWeakPtrs();
  67. base::ThreadTaskRunnerHandle::Get()->PostTask(
  68. FROM_HERE, base::BindOnce(&AutofillAgent::TextFieldDidChangeImpl,
  69. weak_ptr_factory_.GetWeakPtr(), element));
  70. }
  71. void AutofillAgent::TextFieldDidChangeImpl(
  72. const blink::WebFormControlElement& element) {
  73. ShowSuggestionsOptions options;
  74. options.requires_caret_at_end = true;
  75. ShowSuggestions(element, options);
  76. }
  77. void AutofillAgent::TextFieldDidReceiveKeyDown(
  78. const blink::WebInputElement& element,
  79. const blink::WebKeyboardEvent& event) {
  80. if (event.windows_key_code == ui::VKEY_DOWN ||
  81. event.windows_key_code == ui::VKEY_UP) {
  82. ShowSuggestionsOptions options;
  83. options.autofill_on_empty_values = true;
  84. options.requires_caret_at_end = true;
  85. ShowSuggestions(element, options);
  86. }
  87. }
  88. void AutofillAgent::OpenTextDataListChooser(
  89. const blink::WebInputElement& element) {
  90. ShowSuggestionsOptions options;
  91. options.autofill_on_empty_values = true;
  92. ShowSuggestions(element, options);
  93. }
  94. void AutofillAgent::DataListOptionsChanged(
  95. const blink::WebInputElement& element) {
  96. if (!element.Focused())
  97. return;
  98. ShowSuggestionsOptions options;
  99. options.requires_caret_at_end = true;
  100. ShowSuggestions(element, options);
  101. }
  102. AutofillAgent::ShowSuggestionsOptions::ShowSuggestionsOptions()
  103. : autofill_on_empty_values(false), requires_caret_at_end(false) {}
  104. void AutofillAgent::ShowSuggestions(const blink::WebFormControlElement& element,
  105. const ShowSuggestionsOptions& options) {
  106. if (!element.IsEnabled() || element.IsReadOnly())
  107. return;
  108. const blink::WebInputElement* input_element = ToWebInputElement(&element);
  109. if (input_element) {
  110. if (!input_element->IsTextField())
  111. return;
  112. }
  113. blink::WebString value = element.EditingValue();
  114. if (value.length() > kMaxDataLength ||
  115. (!options.autofill_on_empty_values && value.IsEmpty()) ||
  116. (options.requires_caret_at_end &&
  117. (element.SelectionStart() != element.SelectionEnd() ||
  118. element.SelectionEnd() != static_cast<int>(value.length())))) {
  119. HidePopup();
  120. return;
  121. }
  122. std::vector<base::string16> data_list_values;
  123. std::vector<base::string16> data_list_labels;
  124. if (input_element) {
  125. GetDataListSuggestions(*input_element, &data_list_values,
  126. &data_list_labels);
  127. TrimStringVectorForIPC(&data_list_values);
  128. TrimStringVectorForIPC(&data_list_labels);
  129. }
  130. ShowPopup(element, data_list_values, data_list_labels);
  131. }
  132. void AutofillAgent::DidReceiveLeftMouseDownOrGestureTapInNode(
  133. const blink::WebNode& node) {
  134. focused_node_was_last_clicked_ = !node.IsNull() && node.Focused();
  135. }
  136. void AutofillAgent::DidCompleteFocusChangeInFrame() {
  137. DoFocusChangeComplete();
  138. }
  139. bool AutofillAgent::OnMessageReceived(const IPC::Message& message) {
  140. bool handled = true;
  141. IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message)
  142. IPC_MESSAGE_HANDLER(AtomAutofillFrameMsg_AcceptSuggestion,
  143. OnAcceptSuggestion)
  144. IPC_MESSAGE_UNHANDLED(handled = false)
  145. IPC_END_MESSAGE_MAP()
  146. return handled;
  147. }
  148. bool AutofillAgent::IsUserGesture() const {
  149. return blink::WebUserGestureIndicator::IsProcessingUserGesture();
  150. }
  151. void AutofillAgent::HidePopup() {
  152. Send(new AtomAutofillFrameHostMsg_HidePopup(render_frame()->GetRoutingID()));
  153. }
  154. void AutofillAgent::ShowPopup(const blink::WebFormControlElement& element,
  155. const std::vector<base::string16>& values,
  156. const std::vector<base::string16>& labels) {
  157. gfx::RectF bounds =
  158. render_frame()->GetRenderView()->ElementBoundsInWindow(element);
  159. Send(new AtomAutofillFrameHostMsg_ShowPopup(render_frame()->GetRoutingID(),
  160. bounds, values, labels));
  161. }
  162. void AutofillAgent::OnAcceptSuggestion(base::string16 suggestion) {
  163. auto element = render_frame()->GetWebFrame()->GetDocument().FocusedElement();
  164. if (element.IsFormControlElement()) {
  165. ToWebInputElement(&element)->SetAutofillValue(
  166. blink::WebString::FromUTF16(suggestion));
  167. }
  168. }
  169. void AutofillAgent::DoFocusChangeComplete() {
  170. auto element = render_frame()->GetWebFrame()->GetDocument().FocusedElement();
  171. if (element.IsNull() || !element.IsFormControlElement())
  172. return;
  173. if (focused_node_was_last_clicked_ && was_focused_before_now_) {
  174. ShowSuggestionsOptions options;
  175. options.autofill_on_empty_values = true;
  176. auto* input_element = ToWebInputElement(&element);
  177. if (input_element)
  178. ShowSuggestions(*input_element, options);
  179. }
  180. was_focused_before_now_ = true;
  181. focused_node_was_last_clicked_ = false;
  182. }
  183. } // namespace atom