Fix emulator build error
[platform/framework/web/chromium-efl.git] / components / search / start_suggest_service.h
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.
4
5 #ifndef COMPONENTS_SEARCH_START_SUGGEST_SERVICE_H_
6 #define COMPONENTS_SEARCH_START_SUGGEST_SERVICE_H_
7
8 #include <memory>
9 #include <string>
10 #include <vector>
11
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"
20 #include "url/gurl.h"
21
22 class AutocompleteSchemeClassifier;
23
24 class SearchProviderObserver;
25
26 class TemplateURLService;
27
28 namespace network {
29 class SimpleURLLoader;
30 class SharedURLLoaderFactory;
31 }  // namespace network
32
33 class QuerySuggestion {
34  public:
35   bool operator==(const QuerySuggestion& other) const {
36     return query == other.query && destination_url == other.destination_url;
37   }
38   bool operator!=(const QuerySuggestion& other) const {
39     return !(this == &other);
40   }
41
42   // Query suggestion.
43   std::u16string query;
44   // Destination url of query.
45   GURL destination_url;
46 };
47
48 using QuerySuggestions = std::vector<QuerySuggestion>;
49
50 // Service that retrieves query suggestions for the Start/NTP surface when
51 // Google is the default search provider.
52 class StartSuggestService : public KeyedService {
53  public:
54   StartSuggestService(
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;
64
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);
73
74  protected:
75   // Called when the default search provider changes.
76   void SearchProviderChanged();
77
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);
83
84   virtual SearchProviderObserver* search_provider_observer();
85
86  private:
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);
93
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_;
98
99   // The country locale used by this client.
100   const std::string application_country_;
101
102   // The language locale used by this client.
103   const std::string application_locale_;
104
105   // Indicates what page is initiating the requests by this service.
106   const GURL request_initiator_url_;
107
108   // Used to ensure requests are only made if Google is the default search
109   // engine.
110   std::unique_ptr<SearchProviderObserver> search_provider_observer_;
111
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_;
115
116   // Stores last fetched suggestions.
117   std::map<std::string, QuerySuggestions> suggestions_cache_;
118
119   base::WeakPtrFactory<StartSuggestService> weak_ptr_factory_{this};
120 };
121
122 #endif  // COMPONENTS_SEARCH_START_SUGGEST_SERVICE_H_