electron_autofill_agent.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "shell/renderer/electron_autofill_agent.h"
  5. #include <string>
  6. #include <utility>
  7. #include <vector>
  8. #include "content/public/renderer/render_frame.h"
  9. #include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
  10. #include "third_party/blink/public/common/input/web_keyboard_event.h"
  11. #include "third_party/blink/public/platform/web_string.h"
  12. #include "third_party/blink/public/web/web_document.h"
  13. #include "third_party/blink/public/web/web_local_frame.h"
  14. #include "third_party/blink/public/web/web_option_element.h"
  15. #include "ui/events/keycodes/keyboard_codes.h"
  16. #include "ui/gfx/geometry/rect_f.h"
  17. namespace electron {
  18. namespace {
  19. const size_t kMaxStringLength = 1024;
  20. const size_t kMaxListSize = 512;
  21. // Copied from components/autofill/content/renderer/form_autofill_util.cc
  22. void TrimStringVectorForIPC(std::vector<std::u16string>* strings) {
  23. // Limit the size of the vector.
  24. if (strings->size() > kMaxListSize)
  25. strings->resize(kMaxListSize);
  26. // Limit the size of the strings in the vector.
  27. for (auto& string : *strings) {
  28. if (string.length() > kMaxStringLength)
  29. string.resize(kMaxStringLength);
  30. }
  31. }
  32. // Copied from components/autofill/content/renderer/form_autofill_util.cc.
  33. void GetDataListSuggestions(const blink::WebInputElement& element,
  34. std::vector<std::u16string>* values,
  35. std::vector<std::u16string>* labels) {
  36. for (const auto& option : element.FilteredDataListOptions()) {
  37. values->push_back(option.Value().Utf16());
  38. if (option.Value() != option.Label())
  39. labels->push_back(option.Label().Utf16());
  40. else
  41. labels->emplace_back();
  42. }
  43. TrimStringVectorForIPC(values);
  44. TrimStringVectorForIPC(labels);
  45. }
  46. } // namespace
  47. AutofillAgent::AutofillAgent(content::RenderFrame* frame,
  48. blink::AssociatedInterfaceRegistry* registry)
  49. : content::RenderFrameObserver(frame) {
  50. render_frame()->GetWebFrame()->SetAutofillClient(this);
  51. registry->AddInterface<mojom::ElectronAutofillAgent>(base::BindRepeating(
  52. &AutofillAgent::BindPendingReceiver, base::Unretained(this)));
  53. }
  54. AutofillAgent::~AutofillAgent() = default;
  55. void AutofillAgent::BindPendingReceiver(
  56. mojo::PendingAssociatedReceiver<mojom::ElectronAutofillAgent>
  57. pending_receiver) {
  58. receiver_.Bind(std::move(pending_receiver));
  59. }
  60. void AutofillAgent::OnDestruct() {
  61. Shutdown();
  62. base::SingleThreadTaskRunner::GetCurrentDefault()->DeleteSoon(FROM_HERE,
  63. this);
  64. }
  65. void AutofillAgent::Shutdown() {
  66. receiver_.reset();
  67. weak_ptr_factory_.InvalidateWeakPtrs();
  68. }
  69. void AutofillAgent::DidChangeScrollOffset() {
  70. HidePopup();
  71. }
  72. void AutofillAgent::FocusedElementChanged(const blink::WebElement&) {
  73. focused_node_was_last_clicked_ = false;
  74. was_focused_before_now_ = false;
  75. HidePopup();
  76. }
  77. void AutofillAgent::TextFieldDidEndEditing(const blink::WebInputElement&) {
  78. HidePopup();
  79. }
  80. void AutofillAgent::TextFieldDidChange(
  81. const blink::WebFormControlElement& element) {
  82. if (!IsUserGesture() && !render_frame()->IsPasting())
  83. return;
  84. weak_ptr_factory_.InvalidateWeakPtrs();
  85. base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
  86. FROM_HERE, base::BindOnce(&AutofillAgent::TextFieldDidChangeImpl,
  87. weak_ptr_factory_.GetWeakPtr(), element));
  88. }
  89. void AutofillAgent::TextFieldDidChangeImpl(
  90. const blink::WebFormControlElement& element) {
  91. ShowSuggestions(element, {.requires_caret_at_end = true});
  92. }
  93. void AutofillAgent::TextFieldDidReceiveKeyDown(
  94. const blink::WebInputElement& element,
  95. const blink::WebKeyboardEvent& event) {
  96. if (event.windows_key_code == ui::VKEY_DOWN ||
  97. event.windows_key_code == ui::VKEY_UP) {
  98. ShowSuggestions(element, {.autofill_on_empty_values = true,
  99. .requires_caret_at_end = true});
  100. }
  101. }
  102. void AutofillAgent::OpenTextDataListChooser(
  103. const blink::WebInputElement& element) {
  104. ShowSuggestions(element, {.autofill_on_empty_values = true});
  105. }
  106. void AutofillAgent::DataListOptionsChanged(
  107. const blink::WebInputElement& element) {
  108. if (!element.Focused())
  109. return;
  110. ShowSuggestions(element, {.requires_caret_at_end = true});
  111. }
  112. void AutofillAgent::ShowSuggestions(const blink::WebFormControlElement& element,
  113. const ShowSuggestionsOptions& options) {
  114. if (!element.IsEnabled() || element.IsReadOnly())
  115. return;
  116. if (!element.SuggestedValue().IsEmpty())
  117. return;
  118. const blink::WebInputElement input_element =
  119. element.DynamicTo<blink::WebInputElement>();
  120. if (!input_element.IsNull()) {
  121. if (!input_element.IsTextField())
  122. return;
  123. if (!input_element.SuggestedValue().IsEmpty())
  124. return;
  125. }
  126. // Don't attempt to autofill with values that are too large or if filling
  127. // criteria are not met. Keyboard Accessory may still be shown when the
  128. // |value| is empty, do not attempt to hide it.
  129. blink::WebString value = element.EditingValue();
  130. if (value.length() > kMaxStringLength ||
  131. (!options.autofill_on_empty_values && value.IsEmpty()) ||
  132. (options.requires_caret_at_end &&
  133. (element.SelectionStart() != element.SelectionEnd() ||
  134. element.SelectionEnd() != static_cast<int>(value.length())))) {
  135. // Any popup currently showing is obsolete.
  136. HidePopup();
  137. return;
  138. }
  139. std::vector<std::u16string> data_list_values;
  140. std::vector<std::u16string> data_list_labels;
  141. if (!input_element.IsNull()) {
  142. GetDataListSuggestions(input_element, &data_list_values, &data_list_labels);
  143. }
  144. ShowPopup(element, data_list_values, data_list_labels);
  145. }
  146. void AutofillAgent::DidReceiveLeftMouseDownOrGestureTapInNode(
  147. const blink::WebNode& node) {
  148. DCHECK(!node.IsNull());
  149. focused_node_was_last_clicked_ = node.Focused();
  150. }
  151. void AutofillAgent::DidCompleteFocusChangeInFrame() {
  152. HandleFocusChangeComplete();
  153. }
  154. bool AutofillAgent::IsUserGesture() const {
  155. return render_frame()->GetWebFrame()->HasTransientUserActivation();
  156. }
  157. void AutofillAgent::HidePopup() {
  158. GetAutofillDriver()->HideAutofillPopup();
  159. }
  160. void AutofillAgent::ShowPopup(const blink::WebFormControlElement& element,
  161. const std::vector<std::u16string>& values,
  162. const std::vector<std::u16string>& labels) {
  163. gfx::RectF bounds = render_frame()->ElementBoundsInWindow(element);
  164. GetAutofillDriver()->ShowAutofillPopup(bounds, values, labels);
  165. }
  166. void AutofillAgent::AcceptDataListSuggestion(const std::u16string& suggestion) {
  167. auto element = render_frame()->GetWebFrame()->GetDocument().FocusedElement();
  168. if (element.IsFormControlElement()) {
  169. blink::WebInputElement input_element =
  170. element.DynamicTo<blink::WebInputElement>();
  171. if (!input_element.IsNull())
  172. input_element.SetAutofillValue(blink::WebString::FromUTF16(suggestion));
  173. }
  174. }
  175. void AutofillAgent::HandleFocusChangeComplete() {
  176. auto element = render_frame()->GetWebFrame()->GetDocument().FocusedElement();
  177. if (element.IsNull() || !element.IsFormControlElement())
  178. return;
  179. if (focused_node_was_last_clicked_ && was_focused_before_now_) {
  180. blink::WebInputElement input_element =
  181. element.DynamicTo<blink::WebInputElement>();
  182. if (!input_element.IsNull())
  183. ShowSuggestions(input_element, {.autofill_on_empty_values = true});
  184. }
  185. was_focused_before_now_ = true;
  186. focused_node_was_last_clicked_ = false;
  187. }
  188. const mojo::AssociatedRemote<mojom::ElectronAutofillDriver>&
  189. AutofillAgent::GetAutofillDriver() {
  190. if (!autofill_driver_) {
  191. render_frame()->GetRemoteAssociatedInterfaces()->GetInterface(
  192. &autofill_driver_);
  193. }
  194. return autofill_driver_;
  195. }
  196. } // namespace electron