1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SEARCH_START_SUGGEST_SERVICE_H_
6 #define COMPONENTS_SEARCH_START_SUGGEST_SERVICE_H_
12 #include "base/functional/callback.h"
13 #include "base/functional/callback_forward.h"
14 #include "base/memory/raw_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "components/search_engines/template_url.h"
19 #include "services/data_decoder/public/cpp/data_decoder.h"
22 class AutocompleteSchemeClassifier;
24 class SearchProviderObserver;
26 class TemplateURLService;
29 class SimpleURLLoader;
30 class SharedURLLoaderFactory;
31 } // namespace network
33 class QuerySuggestion {
35 bool operator==(const QuerySuggestion& other) const {
36 return query == other.query && destination_url == other.destination_url;
38 bool operator!=(const QuerySuggestion& other) const {
39 return !(this == &other);
44 // Destination url of query.
48 using QuerySuggestions = std::vector<QuerySuggestion>;
50 // Service that retrieves query suggestions for the Start/NTP surface when
51 // Google is the default search provider.
52 class StartSuggestService : public KeyedService {
55 TemplateURLService* template_url_service,
56 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
57 std::unique_ptr<AutocompleteSchemeClassifier> scheme_classifier,
58 const std::string& application_country,
59 const std::string& application_locale,
60 const GURL& request_initiator_url);
61 ~StartSuggestService() override;
62 StartSuggestService(const StartSuggestService&) = delete;
63 StartSuggestService& operator=(const StartSuggestService&) = delete;
65 using SuggestResultCallback = base::OnceCallback<void(QuerySuggestions)>;
66 // If Google is the default search provider, asynchronously requests
67 // query suggestions from the server and returns them to the caller in
68 // `callback`. If `fetch_from_server` is true, any saved suggestions from a
69 // previous fetch are bypassed and a new request will be sent.
70 void FetchSuggestions(const TemplateURLRef::SearchTermsArgs& args,
71 SuggestResultCallback callback,
72 bool fetch_from_server = false);
75 // Called when the default search provider changes.
76 void SearchProviderChanged();
78 // Returns the server request URL.
79 GURL GetRequestURL(const TemplateURLRef::SearchTermsArgs& search_terms_args);
80 // Returns the destination url for `query` for `search_provider`.
81 GURL GetQueryDestinationURL(const std::u16string& query,
82 const TemplateURL* search_provider);
84 virtual SearchProviderObserver* search_provider_observer();
87 // Handles request response from the server.
88 void SuggestResponseLoaded(network::SimpleURLLoader* loader,
89 SuggestResultCallback callback,
90 std::unique_ptr<std::string> response);
91 void SuggestionsParsed(SuggestResultCallback callback,
92 data_decoder::DataDecoder::ValueOrError result);
94 // Cannot be null. Must outlive `this`.
95 raw_ptr<TemplateURLService> template_url_service_;
96 scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
97 std::unique_ptr<AutocompleteSchemeClassifier> scheme_classifier_;
99 // The country locale used by this client.
100 const std::string application_country_;
102 // The language locale used by this client.
103 const std::string application_locale_;
105 // Indicates what page is initiating the requests by this service.
106 const GURL request_initiator_url_;
108 // Used to ensure requests are only made if Google is the default search
110 std::unique_ptr<SearchProviderObserver> search_provider_observer_;
112 // Holds all SimpleURLLoader instances started by this service that have not
113 // received responses yet.
114 std::vector<std::unique_ptr<network::SimpleURLLoader>> loaders_;
116 // Stores last fetched suggestions.
117 std::map<std::string, QuerySuggestions> suggestions_cache_;
119 base::WeakPtrFactory<StartSuggestService> weak_ptr_factory_{this};
122 #endif // COMPONENTS_SEARCH_START_SUGGEST_SERVICE_H_