1 // Copyright 2014 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.
5 #include "chrome/browser/search_engines/default_search_manager.h"
10 #include "base/compiler_specific.h"
11 #include "base/i18n/case_conversion.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/prefs/pref_value_map.h"
14 #include "base/stl_util.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_split.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/search_engines/template_url.h"
22 #include "chrome/browser/search_engines/template_url_service.h"
23 #include "chrome/browser/search_engines/util.h"
24 #include "chrome/common/pref_names.h"
25 #include "components/user_prefs/pref_registry_syncable.h"
29 // A dictionary to hold all data related to the Default Search Engine.
30 // Eventually, this should replace all the data stored in the
31 // default_search_provider.* prefs.
32 const char kDefaultSearchProviderData
[] =
33 "default_search_provider_data.template_url_data";
37 const char DefaultSearchManager::kID
[] = "id";
38 const char DefaultSearchManager::kShortName
[] = "short_name";
39 const char DefaultSearchManager::kKeyword
[] = "keyword";
40 const char DefaultSearchManager::kPrepopulateID
[] = "prepopulate_id";
41 const char DefaultSearchManager::kSyncGUID
[] = "synced_guid";
43 const char DefaultSearchManager::kURL
[] = "url";
44 const char DefaultSearchManager::kSuggestionsURL
[] = "suggestions_url";
45 const char DefaultSearchManager::kInstantURL
[] = "instant_url";
46 const char DefaultSearchManager::kImageURL
[] = "image_url";
47 const char DefaultSearchManager::kNewTabURL
[] = "new_tab_url";
48 const char DefaultSearchManager::kFaviconURL
[] = "favicon_url";
49 const char DefaultSearchManager::kOriginatingURL
[] = "originating_url";
51 const char DefaultSearchManager::kSearchURLPostParams
[] =
52 "search_url_post_params";
53 const char DefaultSearchManager::kSuggestionsURLPostParams
[] =
54 "suggestions_url_post_params";
55 const char DefaultSearchManager::kInstantURLPostParams
[] =
56 "instant_url_post_params";
57 const char DefaultSearchManager::kImageURLPostParams
[] =
58 "image_url_post_params";
60 const char DefaultSearchManager::kSafeForAutoReplace
[] = "safe_for_autoreplace";
61 const char DefaultSearchManager::kInputEncodings
[] = "input_encodings";
63 const char DefaultSearchManager::kDateCreated
[] = "date_created";
64 const char DefaultSearchManager::kLastModified
[] = "last_modified";
66 const char DefaultSearchManager::kUsageCount
[] = "usage_count";
67 const char DefaultSearchManager::kAlternateURLs
[] = "alternate_urls";
68 const char DefaultSearchManager::kSearchTermsReplacementKey
[] =
69 "search_terms_replacement_key";
70 const char DefaultSearchManager::kCreatedByPolicy
[] = "created_by_policy";
71 const char DefaultSearchManager::kDisabledByPolicy
[] = "disabled_by_policy";
73 DefaultSearchManager::DefaultSearchManager(PrefService
* pref_service
)
74 : pref_service_(pref_service
) {
75 DCHECK(pref_service_
);
78 DefaultSearchManager::~DefaultSearchManager() {
82 void DefaultSearchManager::RegisterProfilePrefs(
83 user_prefs::PrefRegistrySyncable
* registry
) {
84 registry
->RegisterDictionaryPref(
85 kDefaultSearchProviderData
,
86 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
90 void DefaultSearchManager::AddPrefValueToMap(base::DictionaryValue
* value
,
91 PrefValueMap
* pref_value_map
) {
92 pref_value_map
->SetValue(kDefaultSearchProviderData
, value
);
95 bool DefaultSearchManager::GetDefaultSearchEngine(TemplateURLData
* data
) {
96 const base::DictionaryValue
* url_dict
=
97 pref_service_
->GetDictionary(kDefaultSearchProviderData
);
99 if (url_dict
->empty())
102 std::string search_url
;
103 base::string16 keyword
;
104 url_dict
->GetString(kURL
, &search_url
);
105 url_dict
->GetString(kKeyword
, &keyword
);
106 if (search_url
.empty())
109 keyword
= TemplateURLService::GenerateKeyword(GURL(search_url
));
110 data
->SetKeyword(keyword
);
111 data
->SetURL(search_url
);
114 url_dict
->GetString(kID
, &id
);
115 base::StringToInt64(id
, &data
->id
);
116 url_dict
->GetString(kShortName
, &data
->short_name
);
117 url_dict
->GetInteger(kPrepopulateID
, &data
->prepopulate_id
);
118 url_dict
->GetString(kSyncGUID
, &data
->sync_guid
);
120 url_dict
->GetString(kSuggestionsURL
, &data
->suggestions_url
);
121 url_dict
->GetString(kInstantURL
, &data
->instant_url
);
122 url_dict
->GetString(kImageURL
, &data
->image_url
);
123 url_dict
->GetString(kNewTabURL
, &data
->new_tab_url
);
125 std::string favicon_url
;
126 std::string originating_url
;
127 url_dict
->GetString(kFaviconURL
, &favicon_url
);
128 url_dict
->GetString(kOriginatingURL
, &originating_url
);
129 data
->favicon_url
= GURL(favicon_url
);
130 data
->originating_url
= GURL(originating_url
);
132 url_dict
->GetString(kSearchURLPostParams
, &data
->search_url_post_params
);
133 url_dict
->GetString(kSuggestionsURLPostParams
,
134 &data
->suggestions_url_post_params
);
135 url_dict
->GetString(kInstantURLPostParams
, &data
->instant_url_post_params
);
136 url_dict
->GetString(kImageURLPostParams
, &data
->image_url_post_params
);
138 url_dict
->GetBoolean(kSafeForAutoReplace
, &data
->safe_for_autoreplace
);
140 double date_created
= 0.0;
141 double last_modified
= 0.0;
142 url_dict
->GetDouble(kDateCreated
, &date_created
);
143 url_dict
->GetDouble(kLastModified
, &last_modified
);
144 data
->date_created
= base::Time::FromInternalValue(date_created
);
145 data
->last_modified
= base::Time::FromInternalValue(last_modified
);
147 url_dict
->GetInteger(kUsageCount
, &data
->usage_count
);
149 const base::ListValue
* alternate_urls
;
150 url_dict
->GetList(kAlternateURLs
, &alternate_urls
);
151 data
->alternate_urls
.clear();
152 for (base::ListValue::const_iterator it
= alternate_urls
->begin();
153 it
!= alternate_urls
->end(); ++it
) {
154 std::string alternate_url
;
155 if ((*it
)->GetAsString(&alternate_url
))
156 data
->alternate_urls
.push_back(alternate_url
);
159 const base::ListValue
* encodings
;
160 url_dict
->GetList(kInputEncodings
, &encodings
);
161 data
->input_encodings
.clear();
162 for (base::ListValue::const_iterator it
= encodings
->begin();
163 it
!= encodings
->end(); ++it
) {
164 std::string encoding
;
165 if ((*it
)->GetAsString(&encoding
))
166 data
->input_encodings
.push_back(encoding
);
169 url_dict
->GetString(kSearchTermsReplacementKey
,
170 &data
->search_terms_replacement_key
);
172 url_dict
->GetBoolean(kCreatedByPolicy
, &data
->created_by_policy
);
174 data
->show_in_default_list
= true;
179 void DefaultSearchManager::SetUserSelectedDefaultSearchEngine(
180 const TemplateURLData
& data
) {
181 base::DictionaryValue url_dict
;
182 url_dict
.SetString(kID
, base::Int64ToString(data
.id
));
183 url_dict
.SetString(kShortName
, data
.short_name
);
184 url_dict
.SetString(kKeyword
, data
.keyword());
185 url_dict
.SetInteger(kPrepopulateID
, data
.prepopulate_id
);
186 url_dict
.SetString(kSyncGUID
, data
.sync_guid
);
188 url_dict
.SetString(kURL
, data
.url());
189 url_dict
.SetString(kSuggestionsURL
, data
.suggestions_url
);
190 url_dict
.SetString(kInstantURL
, data
.instant_url
);
191 url_dict
.SetString(kImageURL
, data
.image_url
);
192 url_dict
.SetString(kNewTabURL
, data
.new_tab_url
);
193 url_dict
.SetString(kFaviconURL
, data
.favicon_url
.spec());
194 url_dict
.SetString(kOriginatingURL
, data
.originating_url
.spec());
196 url_dict
.SetString(kSearchURLPostParams
, data
.search_url_post_params
);
197 url_dict
.SetString(kSuggestionsURLPostParams
,
198 data
.suggestions_url_post_params
);
199 url_dict
.SetString(kInstantURLPostParams
, data
.instant_url_post_params
);
200 url_dict
.SetString(kImageURLPostParams
, data
.image_url_post_params
);
202 url_dict
.SetBoolean(kSafeForAutoReplace
, data
.safe_for_autoreplace
);
204 url_dict
.SetDouble(kDateCreated
, data
.date_created
.ToInternalValue());
205 url_dict
.SetDouble(kLastModified
, data
.last_modified
.ToInternalValue());
206 url_dict
.SetInteger(kUsageCount
, data
.usage_count
);
208 scoped_ptr
<base::ListValue
> alternate_urls(new base::ListValue
);
209 for (std::vector
<std::string
>::const_iterator it
=
210 data
.alternate_urls
.begin();
211 it
!= data
.alternate_urls
.end(); ++it
) {
212 alternate_urls
->AppendString(*it
);
214 url_dict
.Set(kAlternateURLs
, alternate_urls
.release());
216 scoped_ptr
<base::ListValue
> encodings(new base::ListValue
);
217 for (std::vector
<std::string
>::const_iterator it
=
218 data
.input_encodings
.begin();
219 it
!= data
.input_encodings
.end(); ++it
) {
220 encodings
->AppendString(*it
);
222 url_dict
.Set(kInputEncodings
, encodings
.release());
224 url_dict
.SetString(kSearchTermsReplacementKey
,
225 data
.search_terms_replacement_key
);
227 url_dict
.SetBoolean(kCreatedByPolicy
, data
.created_by_policy
);
229 pref_service_
->Set(kDefaultSearchProviderData
, url_dict
);
232 void DefaultSearchManager::ClearUserSelectedDefaultSearchEngine() {
233 pref_service_
->ClearPref(kDefaultSearchProviderData
);