OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
erikwright (departed)
2014/04/23 20:48:53
fix date, remove (c)
Cait (Slow)
2014/04/23 23:26:55
Done.
| |
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 "chrome/browser/search_engines/default_search_manager.h" | |
6 | |
7 #include <algorithm> | |
8 #include <utility> | |
9 | |
10 #include "base/compiler_specific.h" | |
11 #include "base/i18n/case_conversion.h" | |
12 #include "base/prefs/pref_service.h" | |
13 #include "base/stl_util.h" | |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "base/strings/string_split.h" | |
16 #include "base/strings/string_util.h" | |
17 #include "base/strings/utf_string_conversions.h" | |
18 #include "base/time/time.h" | |
19 #include "chrome/browser/profiles/profile.h" | |
20 #include "chrome/browser/search_engines/template_url.h" | |
21 #include "chrome/browser/search_engines/template_url_keys.h" | |
22 #include "chrome/browser/search_engines/util.h" | |
23 #include "chrome/common/pref_names.h" | |
24 #include "components/user_prefs/pref_registry_syncable.h" | |
25 | |
26 namespace { | |
27 // A dictionary to hold all data related to the Default Search Engine. | |
erikwright (departed)
2014/04/23 20:48:53
blank line before
Cait (Slow)
2014/04/23 23:26:55
Done.
| |
28 // Eventually, this should replace all the data stored in the | |
29 // default_search_provider.* prefs. | |
30 const char kDefaultSearchProviderData[] = | |
31 "default_search_provider.template_url_data"; | |
32 } // namespace | |
33 | |
34 DefaultSearchManager::DefaultSearchManager(PrefService* pref_service) | |
35 : pref_service_(pref_service) { | |
36 } | |
37 | |
38 DefaultSearchManager::~DefaultSearchManager() { | |
39 } | |
40 | |
41 // static | |
42 void DefaultSearchManager::RegisterProfilePrefs( | |
43 user_prefs::PrefRegistrySyncable* registry) { | |
44 registry->RegisterDictionaryPref( | |
45 kDefaultSearchProviderData, | |
46 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
47 } | |
48 | |
49 bool DefaultSearchManager::ReadFromPrefService(TemplateURLData* data) { | |
50 if (!pref_service_) { | |
51 return false; | |
gab
2014/04/23 20:00:18
Should this even be allowed by the constructor?
Peter Kasting
2014/04/23 20:41:06
I agree; the constructor should DCHECK this and th
erikwright (departed)
2014/04/23 20:48:53
Agreed. DCHECK in ctor.
Cait (Slow)
2014/04/23 23:26:55
Done.
| |
52 } | |
gab
2014/04/23 20:00:18
rm {}
Cait (Slow)
2014/04/23 23:26:55
Done.
| |
53 const base::DictionaryValue* url_dict = | |
54 pref_service_->GetDictionary(kDefaultSearchProviderData); | |
55 if (!url_dict) { | |
56 return false; | |
gab
2014/04/23 20:00:18
This is not possible; PrefService always returns a
erikwright (departed)
2014/04/23 20:48:53
Right. I guess you will have to treat an empty dic
Cait (Slow)
2014/04/23 23:26:55
Done.
| |
57 } | |
58 | |
59 std::string search_url; | |
60 base::string16 keyword; | |
Peter Kasting
2014/04/23 20:41:06
Nit: Reverse declaration order to match usage orde
Cait (Slow)
2014/04/23 23:26:55
Done.
| |
61 | |
Peter Kasting
2014/04/23 20:41:06
Nit: Eliminate this blank line and the next two
Cait (Slow)
2014/04/23 23:26:55
Done.
| |
62 url_dict->GetString(default_search::kKeyword, &keyword); | |
63 url_dict->GetString(default_search::kURL, &search_url); | |
64 | |
65 DCHECK(!keyword.empty()); | |
66 DCHECK(!search_url.empty()); | |
Peter Kasting
2014/04/23 20:41:06
Couldn't these DCHECKs fail if the user has mucked
Cait (Slow)
2014/04/23 23:26:55
Settings hardening should catch this, and reset th
| |
67 | |
68 data->SetKeyword(keyword); | |
69 data->SetURL(search_url); | |
70 | |
71 std::string id; | |
72 url_dict->GetString(default_search::kID, &id); | |
73 base::StringToInt64(id, &data->id); | |
gab
2014/04/23 20:00:18
see PrefService::GetInt64
erikwright (departed)
2014/04/23 20:48:53
She's working with a dict, not a PrefService, here
gab
2014/04/23 20:56:21
Duh, of course, this is annoying. DictionaryValue
Peter Kasting
2014/04/23 20:57:04
Then how about someone writes that change now, and
gab
2014/04/23 21:01:17
Sounds good. I'm on it.
gab
2014/04/23 23:20:34
CL up @ https://codereview.chromium.org/254473002/
gab
2014/04/24 14:50:03
My CL isn't as simple as I originally thought and
| |
74 url_dict->GetString(default_search::kShortName, &data->short_name); | |
75 url_dict->GetInteger(default_search::kPrepopulateID, &data->prepopulate_id); | |
76 url_dict->GetString(default_search::kSyncGUID, &data->sync_guid); | |
77 | |
78 url_dict->GetString(default_search::kSuggestionsURL, &data->suggestions_url); | |
79 url_dict->GetString(default_search::kInstantURL, &data->instant_url); | |
80 url_dict->GetString(default_search::kImageURL, &data->image_url); | |
81 url_dict->GetString(default_search::kNewTabURL, &data->new_tab_url); | |
82 | |
83 std::string favicon_url; | |
84 std::string originating_url; | |
85 url_dict->GetString(default_search::kFaviconURL, &favicon_url); | |
86 url_dict->GetString(default_search::kOriginatingURL, &originating_url); | |
87 data->favicon_url = GURL(favicon_url); | |
88 data->originating_url = GURL(originating_url); | |
89 | |
90 url_dict->GetString(default_search::kSearchURLPostParams, | |
91 &data->search_url_post_params); | |
92 url_dict->GetString(default_search::kSuggestionsURLPostParams, | |
93 &data->suggestions_url_post_params); | |
94 url_dict->GetString(default_search::kInstantURLPostParams, | |
95 &data->instant_url_post_params); | |
96 url_dict->GetString(default_search::kImageURLPostParams, | |
97 &data->image_url_post_params); | |
98 | |
99 url_dict->GetBoolean(default_search::kSafeForAutoReplace, | |
100 &data->safe_for_autoreplace); | |
101 | |
102 double date_created = 0.0; | |
103 double last_modified = 0.0; | |
104 url_dict->GetDouble(default_search::kDateCreated, &date_created); | |
105 url_dict->GetDouble(default_search::kLastModified, &last_modified); | |
106 data->date_created = base::Time::FromInternalValue(date_created); | |
107 data->last_modified = base::Time::FromInternalValue(last_modified); | |
108 | |
109 url_dict->GetInteger(default_search::kUsageCount, &data->usage_count); | |
110 | |
111 const base::ListValue* alternate_urls = new base::ListValue; | |
112 url_dict->GetList(default_search::kAlternateURLs, &alternate_urls); | |
113 data->alternate_urls.clear(); | |
114 for (size_t i = 0; i < alternate_urls->GetSize(); ++i) { | |
gab
2014/04/23 20:00:18
Use ListValue::const_iterator here.
Cait (Slow)
2014/04/23 23:26:55
Done.
| |
115 std::string alternate_url; | |
116 if (alternate_urls->GetString(i, &alternate_url)) | |
117 data->alternate_urls.push_back(alternate_url); | |
118 } | |
119 | |
120 const base::ListValue* encodings = new base::ListValue; | |
121 url_dict->GetList(default_search::kInputEncodings, &encodings); | |
122 data->input_encodings.clear(); | |
123 for (size_t i = 0; i < encodings->GetSize(); ++i) { | |
gab
2014/04/23 20:00:18
ListValue::const_iterator here as well.
Cait (Slow)
2014/04/23 23:26:55
Done.
| |
124 std::string encoding; | |
125 if (encodings->GetString(i, &encoding)) | |
126 data->input_encodings.push_back(encoding); | |
127 } | |
128 | |
129 url_dict->GetString(default_search::kSearchTermsReplacementKey, | |
130 &data->search_terms_replacement_key); | |
131 | |
132 url_dict->GetBoolean(default_search::kCreatedByPolicy, | |
133 &data->created_by_policy); | |
134 | |
135 data->show_in_default_list = true; | |
136 | |
137 return true; | |
138 } | |
139 | |
140 void DefaultSearchManager::SaveToPrefService(const TemplateURLData& data) { | |
141 if (!pref_service_) | |
142 return; | |
143 | |
144 base::DictionaryValue* url_dict = new base::DictionaryValue; | |
145 url_dict->SetString(default_search::kID, base::Int64ToString(data.id)); | |
gab
2014/04/23 20:00:18
USe PrefService::SetInt64()
erikwright (departed)
2014/04/23 20:48:53
not a PrefService..
| |
146 url_dict->SetString(default_search::kShortName, data.short_name); | |
147 url_dict->SetString(default_search::kKeyword, data.keyword()); | |
148 url_dict->SetInteger(default_search::kPrepopulateID, data.prepopulate_id); | |
149 url_dict->SetString(default_search::kSyncGUID, data.sync_guid); | |
150 | |
151 url_dict->SetString(default_search::kURL, data.url()); | |
152 url_dict->SetString(default_search::kSuggestionsURL, data.suggestions_url); | |
153 url_dict->SetString(default_search::kInstantURL, data.instant_url); | |
154 url_dict->SetString(default_search::kImageURL, data.image_url); | |
155 url_dict->SetString(default_search::kNewTabURL, data.new_tab_url); | |
156 url_dict->SetString(default_search::kFaviconURL, data.favicon_url.spec()); | |
157 url_dict->SetString(default_search::kOriginatingURL, | |
158 data.originating_url.spec()); | |
159 | |
160 url_dict->SetString(default_search::kSearchURLPostParams, | |
161 data.search_url_post_params); | |
162 url_dict->SetString(default_search::kSuggestionsURLPostParams, | |
163 data.suggestions_url_post_params); | |
164 url_dict->SetString(default_search::kInstantURLPostParams, | |
165 data.instant_url_post_params); | |
166 url_dict->SetString(default_search::kImageURLPostParams, | |
167 data.image_url_post_params); | |
168 | |
169 url_dict->SetBoolean(default_search::kSafeForAutoReplace, | |
170 data.safe_for_autoreplace); | |
171 | |
172 url_dict->SetDouble(default_search::kDateCreated, | |
173 data.date_created.ToInternalValue()); | |
174 url_dict->SetDouble(default_search::kLastModified, | |
175 data.last_modified.ToInternalValue()); | |
176 url_dict->SetInteger(default_search::kUsageCount, data.usage_count); | |
177 | |
178 base::ListValue* alternate_urls = new base::ListValue; | |
179 for (size_t i = 0; i < data.alternate_urls.size(); ++i) | |
gab
2014/04/23 20:00:18
std::vector<std::string>::const_iterator
Cait (Slow)
2014/04/23 23:26:55
Done.
| |
180 alternate_urls->AppendString(data.alternate_urls[i]); | |
181 url_dict->Set(default_search::kAlternateURLs, alternate_urls); | |
182 | |
183 base::ListValue* encodings = new base::ListValue; | |
184 for (size_t i = 0; i < data.input_encodings.size(); ++i) | |
gab
2014/04/23 20:00:18
std::vector<std::string>::const_iterator
Cait (Slow)
2014/04/23 23:26:55
Done.
| |
185 encodings->AppendString(data.input_encodings[i]); | |
186 url_dict->Set(default_search::kInputEncodings, encodings); | |
187 | |
188 url_dict->SetString(default_search::kSearchTermsReplacementKey, | |
189 data.search_terms_replacement_key); | |
190 | |
191 url_dict->SetBoolean(default_search::kCreatedByPolicy, | |
192 data.created_by_policy); | |
193 | |
194 pref_service_->Set(kDefaultSearchProviderData, *url_dict); | |
195 } | |
OLD | NEW |