Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / search_engines / search_terms_data.cc
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.
4
5 #include "components/search_engines/search_terms_data.h"
6 #include <memory>
7
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"
14 #include "url/gurl.h"
15
16 namespace {
17
18 // -----------------------------------------------------------------
19 // SearchTermsDataSnapshot
20
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 {
27  public:
28   explicit SearchTermsDataSnapshot(const SearchTermsData* search_terms_data);
29   ~SearchTermsDataSnapshot() override;
30   SearchTermsDataSnapshot(const SearchTermsDataSnapshot&) = delete;
31   SearchTermsDataSnapshot& operator=(const SearchTermsDataSnapshot&) = delete;
32
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;
38
39   // Estimates dynamic memory usage.
40   // See base/trace_event/memory_usage_estimator.h for more info.
41   size_t EstimateMemoryUsage() const override;
42
43  private:
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_;
49 };
50
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();
59   }
60 }
61
62 SearchTermsDataSnapshot::~SearchTermsDataSnapshot() = default;
63
64 std::string SearchTermsDataSnapshot::GoogleBaseURLValue() const {
65   return google_base_url_value_;
66 }
67
68 std::string SearchTermsDataSnapshot::GetApplicationLocale() const {
69   return application_locale_;
70 }
71
72 std::u16string SearchTermsDataSnapshot::GetRlzParameterValue(
73     bool from_app_list) const {
74   return rlz_parameter_value_;
75 }
76
77 std::string SearchTermsDataSnapshot::GetSearchClient() const {
78   return search_client_;
79 }
80
81 std::string SearchTermsDataSnapshot::GoogleImageSearchSource() const {
82   return google_image_search_source_;
83 }
84
85 size_t SearchTermsDataSnapshot::EstimateMemoryUsage() const {
86   size_t res = 0;
87
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_);
93
94   return res;
95 }
96
97 }  // namespace
98
99 // static
100 std::unique_ptr<SearchTermsData> SearchTermsData::MakeSnapshot(
101     const SearchTermsData* original_data) {
102   return std::make_unique<SearchTermsDataSnapshot>(original_data);
103 }
104
105 SearchTermsData::SearchTermsData() = default;
106
107 SearchTermsData::~SearchTermsData() = default;
108
109 std::string SearchTermsData::GoogleBaseURLValue() const {
110   return google_util::kGoogleHomepageURL;
111 }
112
113 std::string SearchTermsData::GoogleBaseSearchByImageURLValue() const {
114   const std::string kGoogleHomepageURLPath = std::string("searchbyimage/");
115
116 #if !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
117   if (base::FeatureList::IsEnabled(lens::features::kLensStandalone)) {
118     return lens::features::GetHomepageURLForLens();
119   }
120 #endif  // !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
121
122   return google_util::kGoogleHomepageURL + kGoogleHomepageURLPath;
123 }
124
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());
129
130   GURL::Replacements repl;
131
132   // Replace any existing path with "/complete/".
133   repl.SetPathStr("/complete/");
134
135   // Clear the query and ref.
136   repl.ClearQuery();
137   repl.ClearRef();
138   return base_url.ReplaceComponents(repl).spec();
139 }
140
141 std::string SearchTermsData::GetApplicationLocale() const {
142   return "en";
143 }
144
145 std::u16string SearchTermsData::GetRlzParameterValue(bool from_app_list) const {
146   return std::u16string();
147 }
148
149 std::string SearchTermsData::GetSearchClient() const {
150   return std::string();
151 }
152
153 std::string SearchTermsData::GetSuggestClient(
154     RequestSource request_source) const {
155   return std::string();
156 }
157
158 std::string SearchTermsData::GetSuggestRequestIdentifier(
159     RequestSource request_source) const {
160   return std::string();
161 }
162
163 std::string SearchTermsData::GoogleImageSearchSource() const {
164   return std::string();
165 }
166
167 std::string SearchTermsData::GetYandexReferralID() const {
168   return std::string();
169 }
170
171 std::string SearchTermsData::GetMailRUReferralID() const {
172   return std::string();
173 }
174
175 size_t SearchTermsData::EstimateMemoryUsage() const {
176   return 0;
177 }