start_suggest_service.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SEARCH_START_SUGGEST_SERVICE_H_
  5. #define COMPONENTS_SEARCH_START_SUGGEST_SERVICE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "base/callback_forward.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/weak_ptr.h"
  13. #include "base/observer_list.h"
  14. #include "components/keyed_service/core/keyed_service.h"
  15. #include "components/search_engines/template_url.h"
  16. #include "services/data_decoder/public/cpp/data_decoder.h"
  17. #include "url/gurl.h"
  18. class AutocompleteSchemeClassifier;
  19. class SearchProviderObserver;
  20. class TemplateURLService;
  21. namespace network {
  22. class SimpleURLLoader;
  23. class SharedURLLoaderFactory;
  24. } // namespace network
  25. class QuerySuggestion {
  26. public:
  27. bool operator==(const QuerySuggestion& other) const {
  28. return query == other.query && destination_url == other.destination_url;
  29. }
  30. bool operator!=(const QuerySuggestion& other) const {
  31. return !(this == &other);
  32. }
  33. // Query suggestion.
  34. std::u16string query;
  35. // Destination url of query.
  36. GURL destination_url;
  37. };
  38. using QuerySuggestions = std::vector<QuerySuggestion>;
  39. // Service that retrieves query suggestions for the Start/NTP surface when
  40. // Google is the default search provider.
  41. class StartSuggestService : public KeyedService {
  42. public:
  43. StartSuggestService(
  44. TemplateURLService* template_url_service,
  45. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
  46. std::unique_ptr<AutocompleteSchemeClassifier> scheme_classifier,
  47. const GURL& request_initiator_url);
  48. ~StartSuggestService() override;
  49. StartSuggestService(const StartSuggestService&) = delete;
  50. StartSuggestService& operator=(const StartSuggestService&) = delete;
  51. using SuggestResultCallback = base::OnceCallback<void(QuerySuggestions)>;
  52. // If Google is the default search provider, asynchronously requests
  53. // query suggestions from the server and returns them to the caller in
  54. // `callback`. If `fetch_from_server` is true, any saved suggestions from a
  55. // previous fetch are bypassed and a new request will be sent.
  56. void FetchSuggestions(const TemplateURLRef::SearchTermsArgs& args,
  57. SuggestResultCallback callback,
  58. bool fetch_from_server = false);
  59. protected:
  60. // Called when the default search provider changes.
  61. void SearchProviderChanged();
  62. // Returns the server request URL.
  63. GURL GetRequestURL(const TemplateURLRef::SearchTermsArgs& search_terms_args);
  64. // Returns the destination url for `query` for `search_provider`.
  65. GURL GetQueryDestinationURL(const std::u16string& query,
  66. const TemplateURL* search_provider);
  67. virtual SearchProviderObserver* search_provider_observer();
  68. private:
  69. // Handles request response from the server.
  70. void SuggestResponseLoaded(network::SimpleURLLoader* loader,
  71. SuggestResultCallback callback,
  72. std::unique_ptr<std::string> response);
  73. void SuggestionsParsed(SuggestResultCallback callback,
  74. data_decoder::DataDecoder::ValueOrError result);
  75. // Cannot be null. Must outlive `this`.
  76. raw_ptr<TemplateURLService> template_url_service_;
  77. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  78. std::unique_ptr<AutocompleteSchemeClassifier> scheme_classifier_;
  79. // Indicates what page is initiating the requests by this service.
  80. const GURL request_initiator_url_;
  81. // Used to ensure requests are only made if Google is the default search
  82. // engine.
  83. std::unique_ptr<SearchProviderObserver> search_provider_observer_;
  84. // Holds all SimpleURLLoader instances started by this service that have not
  85. // received responses yet.
  86. std::vector<std::unique_ptr<network::SimpleURLLoader>> loaders_;
  87. // Stores last fetched suggestions.
  88. std::map<std::string, QuerySuggestions> suggestions_cache_;
  89. base::WeakPtrFactory<StartSuggestService> weak_ptr_factory_{this};
  90. };
  91. #endif // COMPONENTS_SEARCH_START_SUGGEST_SERVICE_H_