Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / search_engines / template_url_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/template_url_data.h"
6
7 #include "base/check.h"
8 #include "base/i18n/case_conversion.h"
9 #include "base/strings/string_piece.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/trace_event/memory_usage_estimator.h"
14 #include "base/uuid.h"
15 #include "base/values.h"
16
17 namespace {
18
19 // Returns a GUID used for sync, which is random except for built-in search
20 // engines. The latter benefit from using a deterministic GUID, to make sure
21 // sync doesn't incur in duplicates for prepopulated engines.
22 std::string GenerateGUID(int prepopulate_id, int starter_pack_id) {
23   // We compute a GUID deterministically given |prepopulate_id| or
24   // |starter_pack_id|, using an arbitrary base GUID.
25   std::string guid;
26   // IDs above 1000 are reserved for distribution custom engines.
27   if (prepopulate_id > 0 && prepopulate_id <= 1000) {
28     guid = base::StringPrintf("485bf7d3-0215-45af-87dc-538868%06d",
29                               prepopulate_id);
30   } else if (starter_pack_id > 0) {
31     guid = base::StringPrintf("ec205736-edd7-4022-a9a3-b431fc%06d",
32                               starter_pack_id);
33   } else {
34     guid = base::Uuid::GenerateRandomV4().AsLowercaseString();
35   }
36
37   DCHECK(base::Uuid::ParseCaseInsensitive(guid).is_valid());
38   return guid;
39 }
40
41 }  // namespace
42
43 TemplateURLData::TemplateURLData()
44     : safe_for_autoreplace(false),
45       id(0),
46       date_created(base::Time::Now()),
47       last_modified(base::Time::Now()),
48       last_visited(base::Time()),
49       created_by_policy(false),
50       enforced_by_policy(false),
51       created_from_play_api(false),
52       usage_count(0),
53       prepopulate_id(0),
54       sync_guid(base::Uuid::GenerateRandomV4().AsLowercaseString()),
55       keyword_(u"dummy"),
56       url_("x") {}
57
58 TemplateURLData::TemplateURLData(const TemplateURLData& other) = default;
59
60 TemplateURLData& TemplateURLData::operator=(const TemplateURLData& other) =
61     default;
62
63 TemplateURLData::TemplateURLData(
64     const std::u16string& name,
65     const std::u16string& keyword,
66     base::StringPiece search_url,
67     base::StringPiece suggest_url,
68     base::StringPiece image_url,
69     base::StringPiece image_translate_url,
70     base::StringPiece new_tab_url,
71     base::StringPiece contextual_search_url,
72     base::StringPiece logo_url,
73     base::StringPiece doodle_url,
74     base::StringPiece search_url_post_params,
75     base::StringPiece suggest_url_post_params,
76     base::StringPiece image_url_post_params,
77     base::StringPiece side_search_param,
78     base::StringPiece side_image_search_param,
79     base::StringPiece image_translate_source_language_param_key,
80     base::StringPiece image_translate_target_language_param_key,
81     std::vector<std::string> search_intent_params,
82     base::StringPiece favicon_url,
83     base::StringPiece encoding,
84     base::StringPiece16 image_search_branding_label,
85     const base::Value::List& alternate_urls_list,
86     bool preconnect_to_search_url,
87     bool prefetch_likely_navigations,
88     int prepopulate_id)
89     : suggestions_url(suggest_url),
90       image_url(image_url),
91       image_translate_url(image_translate_url),
92       new_tab_url(new_tab_url),
93       contextual_search_url(contextual_search_url),
94       logo_url(logo_url),
95       doodle_url(doodle_url),
96       search_url_post_params(search_url_post_params),
97       suggestions_url_post_params(suggest_url_post_params),
98       image_url_post_params(image_url_post_params),
99       side_search_param(side_search_param),
100       side_image_search_param(side_image_search_param),
101       image_translate_source_language_param_key(
102           image_translate_source_language_param_key),
103       image_translate_target_language_param_key(
104           image_translate_target_language_param_key),
105       image_search_branding_label(image_search_branding_label),
106       search_intent_params(search_intent_params),
107       favicon_url(favicon_url),
108       safe_for_autoreplace(true),
109       id(0),
110       date_created(base::Time()),
111       last_modified(base::Time()),
112       created_by_policy(false),
113       enforced_by_policy(false),
114       created_from_play_api(false),
115       usage_count(0),
116       prepopulate_id(prepopulate_id),
117       sync_guid(GenerateGUID(prepopulate_id, 0)),
118       preconnect_to_search_url(preconnect_to_search_url),
119       prefetch_likely_navigations(prefetch_likely_navigations) {
120   SetShortName(name);
121   SetKeyword(keyword);
122   SetURL(std::string(search_url));
123   input_encodings.push_back(std::string(encoding));
124   for (const auto& entry : alternate_urls_list) {
125     const std::string* alternate_url = entry.GetIfString();
126     DCHECK(alternate_url && !alternate_url->empty());
127     if (alternate_url) {
128       alternate_urls.push_back(*alternate_url);
129     }
130   }
131 }
132
133 TemplateURLData::~TemplateURLData() = default;
134
135 void TemplateURLData::SetShortName(const std::u16string& short_name) {
136   // Remove tabs, carriage returns, and the like, as they can corrupt
137   // how the short name is displayed.
138   short_name_ = base::CollapseWhitespace(short_name, true);
139 }
140
141 void TemplateURLData::SetKeyword(const std::u16string& keyword) {
142   DCHECK(!keyword.empty());
143
144   // Case sensitive keyword matching is confusing. As such, we force all
145   // keywords to be lower case.
146   keyword_ = base::i18n::ToLower(keyword);
147
148   base::TrimWhitespace(keyword_, base::TRIM_ALL, &keyword_);
149 }
150
151 void TemplateURLData::SetURL(const std::string& url) {
152   DCHECK(!url.empty());
153   url_ = url;
154 }
155
156 void TemplateURLData::GenerateSyncGUID() {
157   sync_guid = GenerateGUID(prepopulate_id, starter_pack_id);
158 }
159
160 size_t TemplateURLData::EstimateMemoryUsage() const {
161   size_t res = 0;
162
163   res += base::trace_event::EstimateMemoryUsage(suggestions_url);
164   res += base::trace_event::EstimateMemoryUsage(image_url);
165   res += base::trace_event::EstimateMemoryUsage(new_tab_url);
166   res += base::trace_event::EstimateMemoryUsage(contextual_search_url);
167   res += base::trace_event::EstimateMemoryUsage(logo_url);
168   res += base::trace_event::EstimateMemoryUsage(doodle_url);
169   res += base::trace_event::EstimateMemoryUsage(search_url_post_params);
170   res += base::trace_event::EstimateMemoryUsage(suggestions_url_post_params);
171   res += base::trace_event::EstimateMemoryUsage(image_url_post_params);
172   res += base::trace_event::EstimateMemoryUsage(side_search_param);
173   res += base::trace_event::EstimateMemoryUsage(side_image_search_param);
174   res += base::trace_event::EstimateMemoryUsage(favicon_url);
175   res += base::trace_event::EstimateMemoryUsage(image_search_branding_label);
176   res += base::trace_event::EstimateMemoryUsage(originating_url);
177   res += base::trace_event::EstimateMemoryUsage(input_encodings);
178   res += base::trace_event::EstimateMemoryUsage(sync_guid);
179   res += base::trace_event::EstimateMemoryUsage(alternate_urls);
180   res += base::trace_event::EstimateMemoryUsage(short_name_);
181   res += base::trace_event::EstimateMemoryUsage(keyword_);
182   res += base::trace_event::EstimateMemoryUsage(url_);
183
184   return res;
185 }