Add flag for single click autofill.

This currently defaults to off and if enabled will show suggestions when the mouse is first clicked in an edit box.

Converts ShowSuggestions function call which took 6 bool parameters to take a struct for clarity and future enhancement.

Review URL: https://codereview.chromium.org/753393004

Cr-Commit-Position: refs/heads/master@{#305889}
diff --git a/components/autofill/content/renderer/autofill_agent.cc b/components/autofill/content/renderer/autofill_agent.cc
index 6efe69d..1b4daba 100644
--- a/components/autofill/content/renderer/autofill_agent.cc
+++ b/components/autofill/content/renderer/autofill_agent.cc
@@ -119,6 +119,15 @@
 
 }  // namespace
 
+AutofillAgent::ShowSuggestionsOptions::ShowSuggestionsOptions()
+    : autofill_on_empty_values(false),
+      requires_caret_at_end(false),
+      display_warning_if_disabled(false),
+      datalist_only(false),
+      show_full_suggestion_list(false),
+      show_password_suggestions_only(false) {
+}
+
 AutofillAgent::AutofillAgent(content::RenderView* render_view,
                              PasswordAutofillAgent* password_autofill_agent,
                              PasswordGenerationAgent* password_generation_agent)
@@ -264,7 +273,7 @@
       render_view()->GetSSLStatusOfFrame(form.document().frame());
   bool is_safe = url.SchemeIs(url::kHttpsScheme) &&
       !net::IsCertStatusError(ssl_status.cert_status);
-  bool allow_unsafe = CommandLine::ForCurrentProcess()->HasSwitch(
+  bool allow_unsafe = base::CommandLine::ForCurrentProcess()->HasSwitch(
       ::switches::kReduceSecurityForTesting);
 
   FormData form_data;
@@ -316,24 +325,26 @@
   if (!input_element && !IsTextAreaElement(element))
     return;
 
-  bool show_full_suggestion_list = element.isAutofilled() || was_focused;
-  bool show_password_suggestions_only = !was_focused;
+  ShowSuggestionsOptions options;
+  options.autofill_on_empty_values = true;
+  options.display_warning_if_disabled = true;
+  options.show_full_suggestion_list = element.isAutofilled();
 
-  // TODO(gcasto): Remove after crbug.com/430318 has been fixed.
-  bool show_suggestions = true;
+  if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
+          switches::kEnableSingleClickAutofill)) {
+    // Show full suggestions when clicking on an already-focused form field. On
+    // the initial click (not focused yet), only show password suggestions.
 #if defined(OS_ANDROID)
-  show_suggestions = was_focused;
+    // TODO(gcasto): Remove after crbug.com/430318 has been fixed.
+    if (!was_focused)
+      return;
 #endif
 
-  if (show_suggestions) {
-    ShowSuggestions(element,
-                    true,
-                    false,
-                    true,
-                    false,
-                    show_full_suggestion_list,
-                    show_password_suggestions_only);
+    options.show_full_suggestion_list =
+        options.show_full_suggestion_list || was_focused;
+    options.show_password_suggestions_only = !was_focused;
   }
+  ShowSuggestions(element, options);
 }
 
 void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) {
@@ -385,7 +396,9 @@
     }
   }
 
-  ShowSuggestions(element, false, true, false, false, false, false);
+  ShowSuggestionsOptions options;
+  options.requires_caret_at_end = true;
+  ShowSuggestions(element, options);
 
   FormData form;
   FormFieldData field;
@@ -406,12 +419,20 @@
   }
 
   if (event.windowsKeyCode == ui::VKEY_DOWN ||
-      event.windowsKeyCode == ui::VKEY_UP)
-    ShowSuggestions(element, true, true, true, false, false, false);
+      event.windowsKeyCode == ui::VKEY_UP) {
+    ShowSuggestionsOptions options;
+    options.autofill_on_empty_values = true;
+    options.requires_caret_at_end = true;
+    options.display_warning_if_disabled = true;
+    ShowSuggestions(element, options);
+  }
 }
 
 void AutofillAgent::openTextDataListChooser(const WebInputElement& element) {
-  ShowSuggestions(element, true, false, false, true, false, false);
+  ShowSuggestionsOptions options;
+  options.autofill_on_empty_values = true;
+  options.datalist_only = true;
+  ShowSuggestions(element, options);
 }
 
 void AutofillAgent::firstUserGestureObserved() {
@@ -562,22 +583,17 @@
 }
 
 void AutofillAgent::ShowSuggestions(const WebFormControlElement& element,
-                                    bool autofill_on_empty_values,
-                                    bool requires_caret_at_end,
-                                    bool display_warning_if_disabled,
-                                    bool datalist_only,
-                                    bool show_full_suggestion_list,
-                                    bool show_password_suggestions_only) {
+                                    const ShowSuggestionsOptions& options) {
   if (!element.isEnabled() || element.isReadOnly())
     return;
-  if (!datalist_only && !element.suggestedValue().isEmpty())
+  if (!options.datalist_only && !element.suggestedValue().isEmpty())
     return;
 
   const WebInputElement* input_element = toWebInputElement(&element);
   if (input_element) {
     if (!input_element->isTextField() || input_element->isPasswordField())
       return;
-    if (!datalist_only && !input_element->suggestedValue().isEmpty())
+    if (!options.datalist_only && !input_element->suggestedValue().isEmpty())
       return;
   } else {
     DCHECK(IsTextAreaElement(element));
@@ -588,10 +604,10 @@
   // Don't attempt to autofill with values that are too large or if filling
   // criteria are not met.
   WebString value = element.editingValue();
-  if (!datalist_only &&
+  if (!options.datalist_only &&
       (value.length() > kMaxDataLength ||
-       (!autofill_on_empty_values && value.isEmpty()) ||
-       (requires_caret_at_end &&
+       (!options.autofill_on_empty_values && value.isEmpty()) ||
+       (options.requires_caret_at_end &&
         (element.selectionStart() != element.selectionEnd() ||
          element.selectionEnd() != static_cast<int>(value.length()))))) {
     // Any popup currently showing is obsolete.
@@ -601,9 +617,9 @@
 
   element_ = element;
   if (IsAutofillableInputElement(input_element) &&
-      (password_autofill_agent_->ShowSuggestions(*input_element,
-                                                 show_full_suggestion_list) ||
-       show_password_suggestions_only)) {
+      (password_autofill_agent_->ShowSuggestions(
+           *input_element, options.show_full_suggestion_list) ||
+       options.show_password_suggestions_only)) {
     is_popup_possibly_visible_ = true;
     return;
   }
@@ -613,14 +629,15 @@
   // popup. Note that we cannot use the WebKit method element.autoComplete()
   // as it does not allow us to distinguish the case where autocomplete is
   // disabled for *both* the element and for the form.
-  const base::string16 autocomplete_attribute =
-      element.getAttribute("autocomplete");
-  if (LowerCaseEqualsASCII(autocomplete_attribute, "off"))
-    display_warning_if_disabled = false;
+  bool display_warning = options.display_warning_if_disabled;
+  if (display_warning) {
+    const base::string16 autocomplete_attribute =
+        element.getAttribute("autocomplete");
+    if (LowerCaseEqualsASCII(autocomplete_attribute, "off"))
+      display_warning = false;
+  }
 
-  QueryAutofillSuggestions(element,
-                           display_warning_if_disabled,
-                           datalist_only);
+  QueryAutofillSuggestions(element, display_warning, options.datalist_only);
 }
 
 void AutofillAgent::QueryAutofillSuggestions(