1 // Copyright 2014 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 #include "components/search_engines/search_terms_data.h"
8 #include "base/check.h"
9 #include "base/trace_event/memory_usage_estimator.h"
10 #include "base/trace_event/trace_event.h"
11 #include "build/build_config.h"
12 #include "components/google/core/common/google_util.h"
13 #include "components/lens/lens_features.h"
18 // -----------------------------------------------------------------
19 // SearchTermsDataSnapshot
21 // Implementation of SearchTermsData that takes a snapshot of another
22 // SearchTermsData by copying all the responses to the different getters into
23 // member strings, then returning those strings when its own getters are called.
24 // This will typically be constructed on the UI thread from
25 // UIThreadSearchTermsData but is subsequently safe to use on any thread.
26 class SearchTermsDataSnapshot : public SearchTermsData {
28 explicit SearchTermsDataSnapshot(const SearchTermsData* search_terms_data);
29 ~SearchTermsDataSnapshot() override;
30 SearchTermsDataSnapshot(const SearchTermsDataSnapshot&) = delete;
31 SearchTermsDataSnapshot& operator=(const SearchTermsDataSnapshot&) = delete;
33 std::string GoogleBaseURLValue() const override;
34 std::string GetApplicationLocale() const override;
35 std::u16string GetRlzParameterValue(bool from_app_list) const override;
36 std::string GetSearchClient() const override;
37 std::string GoogleImageSearchSource() const override;
39 // Estimates dynamic memory usage.
40 // See base/trace_event/memory_usage_estimator.h for more info.
41 size_t EstimateMemoryUsage() const override;
44 std::string google_base_url_value_;
45 std::string application_locale_;
46 std::u16string rlz_parameter_value_;
47 std::string search_client_;
48 std::string google_image_search_source_;
51 SearchTermsDataSnapshot::SearchTermsDataSnapshot(
52 const SearchTermsData* search_terms_data) {
53 if (search_terms_data) {
54 google_base_url_value_ = search_terms_data->GoogleBaseURLValue();
55 application_locale_ = search_terms_data->GetApplicationLocale();
56 rlz_parameter_value_ = search_terms_data->GetRlzParameterValue(false);
57 search_client_ = search_terms_data->GetSearchClient();
58 google_image_search_source_ = search_terms_data->GoogleImageSearchSource();
62 SearchTermsDataSnapshot::~SearchTermsDataSnapshot() = default;
64 std::string SearchTermsDataSnapshot::GoogleBaseURLValue() const {
65 return google_base_url_value_;
68 std::string SearchTermsDataSnapshot::GetApplicationLocale() const {
69 return application_locale_;
72 std::u16string SearchTermsDataSnapshot::GetRlzParameterValue(
73 bool from_app_list) const {
74 return rlz_parameter_value_;
77 std::string SearchTermsDataSnapshot::GetSearchClient() const {
78 return search_client_;
81 std::string SearchTermsDataSnapshot::GoogleImageSearchSource() const {
82 return google_image_search_source_;
85 size_t SearchTermsDataSnapshot::EstimateMemoryUsage() const {
88 res += base::trace_event::EstimateMemoryUsage(google_base_url_value_);
89 res += base::trace_event::EstimateMemoryUsage(application_locale_);
90 res += base::trace_event::EstimateMemoryUsage(rlz_parameter_value_);
91 res += base::trace_event::EstimateMemoryUsage(search_client_);
92 res += base::trace_event::EstimateMemoryUsage(google_image_search_source_);
100 std::unique_ptr<SearchTermsData> SearchTermsData::MakeSnapshot(
101 const SearchTermsData* original_data) {
102 return std::make_unique<SearchTermsDataSnapshot>(original_data);
105 SearchTermsData::SearchTermsData() = default;
107 SearchTermsData::~SearchTermsData() = default;
109 std::string SearchTermsData::GoogleBaseURLValue() const {
110 return google_util::kGoogleHomepageURL;
113 std::string SearchTermsData::GoogleBaseSearchByImageURLValue() const {
114 const std::string kGoogleHomepageURLPath = std::string("searchbyimage/");
116 #if !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
117 if (base::FeatureList::IsEnabled(lens::features::kLensStandalone)) {
118 return lens::features::GetHomepageURLForLens();
120 #endif // !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
122 return google_util::kGoogleHomepageURL + kGoogleHomepageURLPath;
125 std::string SearchTermsData::GoogleBaseSuggestURLValue() const {
126 // Start with the Google base URL.
127 const GURL base_url(GoogleBaseURLValue());
128 DCHECK(base_url.is_valid());
130 GURL::Replacements repl;
132 // Replace any existing path with "/complete/".
133 repl.SetPathStr("/complete/");
135 // Clear the query and ref.
138 return base_url.ReplaceComponents(repl).spec();
141 std::string SearchTermsData::GetApplicationLocale() const {
145 std::u16string SearchTermsData::GetRlzParameterValue(bool from_app_list) const {
146 return std::u16string();
149 std::string SearchTermsData::GetSearchClient() const {
150 return std::string();
153 std::string SearchTermsData::GetSuggestClient(
154 RequestSource request_source) const {
155 return std::string();
158 std::string SearchTermsData::GetSuggestRequestIdentifier(
159 RequestSource request_source) const {
160 return std::string();
163 std::string SearchTermsData::GoogleImageSearchSource() const {
164 return std::string();
167 std::string SearchTermsData::GetYandexReferralID() const {
168 return std::string();
171 std::string SearchTermsData::GetMailRUReferralID() const {
172 return std::string();
175 size_t SearchTermsData::EstimateMemoryUsage() const {