[omnibox] Add TemplateURLRef::SearchTermsArgs::omnibox_focus_type

Add an omnibox_focus_type member to TemplateURLRef::SearchTermsArgs.

In a followup CL, this enum value will be used to build up the proper
suggest URL.

This also refactors RemoteSuggestionsService to take a SearchTermsArgs
parameter, so that ZeroSuggestProvider can set the ON_FOCUS value.

We don't want RemoteSuggestionsService to set ON_FOCUS, as it would
seems surprising for all requests coming from RemoteSuggestionsService
to be presumed to be on-focus. (Even though that's true today.)

Bug: 963173
Change-Id: I6202b68876398b9fc2a2258898a1c975225db13b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1670123
Reviewed-by: Mark Pearson <[email protected]>
Commit-Queue: Tommy Li <[email protected]>
Cr-Commit-Position: refs/heads/master@{#673038}
diff --git a/components/omnibox/browser/remote_suggestions_service.cc b/components/omnibox/browser/remote_suggestions_service.cc
index 8c5ec1e..10f9ecd 100644
--- a/components/omnibox/browser/remote_suggestions_service.cc
+++ b/components/omnibox/browser/remote_suggestions_service.cc
@@ -98,20 +98,20 @@
 RemoteSuggestionsService::~RemoteSuggestionsService() {}
 
 void RemoteSuggestionsService::CreateSuggestionsRequest(
-    const std::string& current_url,
+    const TemplateURLRef::SearchTermsArgs& search_terms_args,
     const base::Time& visit_time,
-    const AutocompleteInput& input,
     const TemplateURLService* template_url_service,
     StartCallback start_callback,
     CompletionCallback completion_callback) {
-  const GURL experimental_suggest_url =
-      ExperimentalEndpointUrl(current_url, template_url_service);
+  const GURL experimental_suggest_url = ExperimentalEndpointUrl(
+      search_terms_args.current_page_url, template_url_service);
   if (experimental_suggest_url.is_valid())
-    CreateExperimentalRequest(current_url, visit_time, experimental_suggest_url,
+    CreateExperimentalRequest(search_terms_args.current_page_url, visit_time,
+                              experimental_suggest_url,
                               std::move(start_callback),
                               std::move(completion_callback));
   else
-    CreateDefaultRequest(current_url, input, template_url_service,
+    CreateDefaultRequest(search_terms_args, template_url_service,
                          std::move(start_callback),
                          std::move(completion_callback));
 }
@@ -123,8 +123,7 @@
 
 // static
 GURL RemoteSuggestionsService::EndpointUrl(
-    const std::string& current_url,
-    const AutocompleteInput& input,
+    TemplateURLRef::SearchTermsArgs search_terms_args,
     const TemplateURLService* template_url_service) {
   if (template_url_service == nullptr) {
     return GURL();
@@ -140,18 +139,12 @@
       search_engine->suggestions_url_ref();
   const SearchTermsData& search_terms_data =
       template_url_service->search_terms_data();
-  TemplateURLRef::SearchTermsArgs search_term_args;
-  if (!current_url.empty()) {
-    search_term_args.current_page_url = current_url;
-  }
-
-  search_term_args.page_classification = input.current_page_classification();
 
   // Append a specific suggest client in ChromeOS app_list launcher contexts.
   BaseSearchProvider::AppendSuggestClientToAdditionalQueryParams(
-      search_engine, search_terms_data, input.current_page_classification(),
-      &search_term_args);
-  return GURL(suggestion_url_ref.ReplaceSearchTerms(search_term_args,
+      search_engine, search_terms_data, search_terms_args.page_classification,
+      &search_terms_args);
+  return GURL(suggestion_url_ref.ReplaceSearchTerms(search_terms_args,
                                                     search_terms_data));
 }
 
@@ -194,13 +187,11 @@
 }
 
 void RemoteSuggestionsService::CreateDefaultRequest(
-    const std::string& current_url,
-    const AutocompleteInput& input,
+    const TemplateURLRef::SearchTermsArgs& search_terms_args,
     const TemplateURLService* template_url_service,
     StartCallback start_callback,
     CompletionCallback completion_callback) {
-  const GURL suggest_url =
-      EndpointUrl(current_url, input, template_url_service);
+  const GURL suggest_url = EndpointUrl(search_terms_args, template_url_service);
   DCHECK(suggest_url.is_valid());
 
   net::NetworkTrafficAnnotationTag traffic_annotation =
diff --git a/components/omnibox/browser/remote_suggestions_service.h b/components/omnibox/browser/remote_suggestions_service.h
index e669a426..4034dba 100644
--- a/components/omnibox/browser/remote_suggestions_service.h
+++ b/components/omnibox/browser/remote_suggestions_service.h
@@ -11,6 +11,7 @@
 #include "base/memory/scoped_refptr.h"
 #include "components/keyed_service/core/keyed_service.h"
 #include "components/omnibox/browser/autocomplete_input.h"
+#include "components/search_engines/template_url.h"
 #include "net/traffic_annotation/network_traffic_annotation.h"
 #include "services/identity/public/cpp/access_token_info.h"
 #include "url/gurl.h"
@@ -55,21 +56,19 @@
       base::OnceCallback<void(const network::SimpleURLLoader* source,
                               std::unique_ptr<std::string> response_body)>;
 
-  // Creates a loader for remote suggestions for |current_url| and passes
+  // Creates a loader for remote suggestions for |search_terms_args| and passes
   // the loader to |start_callback|. It uses a number of signals to create the
   // loader, including field trial / experimental parameters, and it may
   // pass a nullptr to |start_callback| (see below for details).
   //
-  // |current_url| may be empty, in which case the system will never use the
-  // experimental suggestions service. It's possible the non-experimental
+  // |search_terms_args| encapsulates the arguments sent to the remote service.
+  // If |search_terms_args.current_page_url| is empty, the system will never use
+  // the experimental suggestions service. It's possible the non-experimental
   // service may decide to offer general-purpose suggestions.
   //
   // |visit_time| is the time of the visit for the URL for which suggestions
   // should be fetched.
   //
-  // |input| is the current user input of the autocomplete query, whose
-  // |current_page_classification| will be used to build the suggestion url.
-  //
   // |template_url_service| may be null, but some services may be disabled.
   //
   // |start_callback| is called to transfer ownership of the created loader to
@@ -82,12 +81,12 @@
   //
   // This method sends a request for an OAuth2 token and temporarily
   // instantiates |token_fetcher_|.
-  void CreateSuggestionsRequest(const std::string& current_url,
-                                const base::Time& visit_time,
-                                const AutocompleteInput& input,
-                                const TemplateURLService* template_url_service,
-                                StartCallback start_callback,
-                                CompletionCallback completion_callback);
+  void CreateSuggestionsRequest(
+      const TemplateURLRef::SearchTermsArgs& search_terms_args,
+      const base::Time& visit_time,
+      const TemplateURLService* template_url_service,
+      StartCallback start_callback,
+      CompletionCallback completion_callback);
 
   // Advises the service to stop any process that creates a suggestion request.
   void StopCreatingSuggestionsRequest();
@@ -98,15 +97,14 @@
   // Returns an invalid URL (i.e.: GURL::is_valid() == false) in case of an
   // error.
   //
-  // |current_url| is the page the user is currently browsing and may be empty.
-  //
-  // |input| is the current user input of the autocomplete query, whose
-  // |current_page_classification| will be used to build the suggestion url.
+  // |search_terms_args| encapsulates the arguments sent to the suggest service.
+  // Various parts of it (including the current page URL and classification) are
+  // used to build the final endpoint URL. Note that the current page URL can
+  // be empty.
   //
   // Note that this method is public and is also used by ZeroSuggestProvider for
-  // suggestions that do not take |current_url| into consideration.
-  static GURL EndpointUrl(const std::string& current_url,
-                          const AutocompleteInput& input,
+  // suggestions that do not take the current page URL into consideration.
+  static GURL EndpointUrl(TemplateURLRef::SearchTermsArgs search_terms_args,
                           const TemplateURLService* template_url_service);
 
  private:
@@ -133,11 +131,11 @@
   //
   // This function is called by CreateSuggestionsRequest. See its
   // function definition for details on the parameters.
-  void CreateDefaultRequest(const std::string& current_url,
-                            const AutocompleteInput& input,
-                            const TemplateURLService* template_url_service,
-                            StartCallback start_callback,
-                            CompletionCallback completion_callback);
+  void CreateDefaultRequest(
+      const TemplateURLRef::SearchTermsArgs& search_terms_args,
+      const TemplateURLService* template_url_service,
+      StartCallback start_callback,
+      CompletionCallback completion_callback);
 
   // Upon succesful creation of an HTTP POST request for experimental remote
   // suggestions, the |callback| function is run with the HTTP POST request as a
diff --git a/components/omnibox/browser/remote_suggestions_service_unittest.cc b/components/omnibox/browser/remote_suggestions_service_unittest.cc
index ecc6e33..969a7e7 100644
--- a/components/omnibox/browser/remote_suggestions_service_unittest.cc
+++ b/components/omnibox/browser/remote_suggestions_service_unittest.cc
@@ -54,11 +54,12 @@
 
   RemoteSuggestionsService service(nullptr /* identity_manager */,
                                    GetUrlLoaderFactory());
-  AutocompleteInput input;
   base::Time visit_time;
   TemplateURLService template_url_service(nullptr, 0);
+  TemplateURLRef::SearchTermsArgs search_terms_args;
+  search_terms_args.current_page_url = "https://www.google.com/";
   service.CreateSuggestionsRequest(
-      "https://www.google.com/", visit_time, input, &template_url_service,
+      search_terms_args, visit_time, &template_url_service,
       base::BindOnce(&RemoteSuggestionsServiceTest::OnRequestStart,
                      base::Unretained(this)),
       base::BindOnce(&RemoteSuggestionsServiceTest::OnRequestComplete,
diff --git a/components/omnibox/browser/zero_suggest_provider.cc b/components/omnibox/browser/zero_suggest_provider.cc
index 6d8d4d3..23d25bfa 100644
--- a/components/omnibox/browser/zero_suggest_provider.cc
+++ b/components/omnibox/browser/zero_suggest_provider.cc
@@ -163,8 +163,12 @@
   current_page_classification_ = input.current_page_classification();
   current_url_match_ = MatchForCurrentURL();
 
+  TemplateURLRef::SearchTermsArgs search_terms_args;
+  search_terms_args.page_classification = current_page_classification_;
+  search_terms_args.omnibox_focus_type =
+      TemplateURLRef::SearchTermsArgs::OmniboxFocusType::ON_FOCUS;
   GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
-      /*current_url=*/"", input, client()->GetTemplateURLService());
+      search_terms_args, client()->GetTemplateURLService());
   if (!suggest_url.is_valid())
     return;
 
@@ -191,14 +195,14 @@
     return;
   }
 
-  const std::string current_url =
+  search_terms_args.current_page_url =
       result_type_running_ == REMOTE_SEND_URL ? current_query_ : std::string();
   // Create a request for suggestions, routing completion to
   // OnRemoteSuggestionsLoaderAvailable.
   client()
       ->GetRemoteSuggestionsService(/*create_if_necessary=*/true)
       ->CreateSuggestionsRequest(
-          current_url, client()->GetCurrentVisitTimestamp(), input,
+          search_terms_args, client()->GetCurrentVisitTimestamp(),
           client()->GetTemplateURLService(),
           base::BindOnce(
               &ZeroSuggestProvider::OnRemoteSuggestionsLoaderAvailable,
@@ -278,9 +282,8 @@
       client->GetTemplateURLService();
   // Template URL service can be null in tests.
   if (template_url_service != nullptr) {
-    AutocompleteInput empty_input;
     GURL suggest_url = RemoteSuggestionsService::EndpointUrl(
-        /*current_url=*/"", /*empty input*/ empty_input, template_url_service);
+        TemplateURLRef::SearchTermsArgs(), template_url_service);
     // To check whether this is allowed, use an arbitrary insecure (http) URL
     // as the URL we'd want suggestions for.  The value of OTHER as the current
     // page classification is to correspond with that URL.