OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/search_engines/util.h" | 5 #include "chrome/browser/search_engines/util.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "chrome/browser/prefs/pref_service.h" | |
12 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/search_engines/template_url.h" | 12 #include "chrome/browser/search_engines/template_url.h" |
14 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 13 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
15 #include "chrome/browser/search_engines/template_url_service.h" | 14 #include "chrome/browser/search_engines/template_url_service.h" |
16 #include "chrome/browser/search_engines/template_url_service_factory.h" | 15 #include "chrome/browser/search_engines/template_url_service_factory.h" |
17 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
18 | 17 |
19 using content::BrowserThread; | 18 using content::BrowserThread; |
20 | 19 |
21 string16 GetDefaultSearchEngineName(Profile* profile) { | 20 string16 GetDefaultSearchEngineName(Profile* profile) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 if ((*i)->id() == id) { | 75 if ((*i)->id() == id) { |
77 return *i; | 76 return *i; |
78 } | 77 } |
79 } | 78 } |
80 return NULL; | 79 return NULL; |
81 } | 80 } |
82 | 81 |
83 // Loads engines from prepopulate data and merges them in with the existing | 82 // Loads engines from prepopulate data and merges them in with the existing |
84 // engines. This is invoked when the version of the prepopulate data changes. | 83 // engines. This is invoked when the version of the prepopulate data changes. |
85 void MergeEnginesFromPrepopulateData( | 84 void MergeEnginesFromPrepopulateData( |
86 PrefService* prefs, | 85 Profile* profile, |
87 WebDataService* service, | 86 WebDataService* service, |
88 std::vector<TemplateURL*>* template_urls, | 87 std::vector<TemplateURL*>* template_urls, |
89 const TemplateURL** default_search_provider) { | 88 const TemplateURL** default_search_provider) { |
90 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); | 89 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); |
91 DCHECK(template_urls); | 90 DCHECK(template_urls); |
92 DCHECK(default_search_provider); | 91 DCHECK(default_search_provider); |
93 | 92 |
94 // Create a map to hold all provided |template_urls| that originally came from | 93 // Create a map to hold all provided |template_urls| that originally came from |
95 // prepopulate data (i.e. have a non-zero prepopulate_id()). | 94 // prepopulate data (i.e. have a non-zero prepopulate_id()). |
96 typedef std::map<int, TemplateURL*> IDMap; | 95 typedef std::map<int, TemplateURL*> IDMap; |
97 IDMap id_to_turl; | 96 IDMap id_to_turl; |
98 for (std::vector<TemplateURL*>::iterator i(template_urls->begin()); | 97 for (std::vector<TemplateURL*>::iterator i(template_urls->begin()); |
99 i != template_urls->end(); ++i) { | 98 i != template_urls->end(); ++i) { |
100 int prepopulate_id = (*i)->prepopulate_id(); | 99 int prepopulate_id = (*i)->prepopulate_id(); |
101 if (prepopulate_id > 0) | 100 if (prepopulate_id > 0) |
102 id_to_turl[prepopulate_id] = *i; | 101 id_to_turl[prepopulate_id] = *i; |
103 } | 102 } |
104 | 103 |
105 // Get the current set of prepopulatd URLs. | 104 // Get the current set of prepopulatd URLs. |
106 std::vector<TemplateURL*> prepopulated_urls; | 105 std::vector<TemplateURL*> prepopulated_urls; |
107 size_t default_search_index; | 106 size_t default_search_index; |
108 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs, | 107 TemplateURLPrepopulateData::GetPrepopulatedEngines(profile, |
109 &prepopulated_urls, &default_search_index); | 108 &prepopulated_urls, &default_search_index); |
110 | 109 |
111 // For each current prepopulated URL, check whether |template_urls| contained | 110 // For each current prepopulated URL, check whether |template_urls| contained |
112 // a matching prepopulated URL. If so, update the passed-in URL to match the | 111 // a matching prepopulated URL. If so, update the passed-in URL to match the |
113 // current data. (If the passed-in URL was user-edited, we persist the user's | 112 // current data. (If the passed-in URL was user-edited, we persist the user's |
114 // name and keyword.) If not, add the prepopulated URL to |template_urls|. | 113 // name and keyword.) If not, add the prepopulated URL to |template_urls|. |
115 // Along the way, point |default_search_provider| at the default prepopulated | 114 // Along the way, point |default_search_provider| at the default prepopulated |
116 // URL, if the user hasn't already set another URL as default. | 115 // URL, if the user hasn't already set another URL as default. |
117 for (size_t i = 0; i < prepopulated_urls.size(); ++i) { | 116 for (size_t i = 0; i < prepopulated_urls.size(); ++i) { |
118 // We take ownership of |prepopulated_urls[i]|. | 117 // We take ownership of |prepopulated_urls[i]|. |
119 scoped_ptr<TemplateURL> prepopulated_url(prepopulated_urls[i]); | 118 scoped_ptr<TemplateURL> prepopulated_url(prepopulated_urls[i]); |
120 const int prepopulated_id = prepopulated_url->prepopulate_id(); | 119 const int prepopulated_id = prepopulated_url->prepopulate_id(); |
121 DCHECK_NE(0, prepopulated_id); | 120 DCHECK_NE(0, prepopulated_id); |
122 | 121 |
123 TemplateURL* url_in_vector = NULL; | 122 TemplateURL* url_in_vector = NULL; |
124 IDMap::iterator existing_url_iter(id_to_turl.find(prepopulated_id)); | 123 IDMap::iterator existing_url_iter(id_to_turl.find(prepopulated_id)); |
125 if (existing_url_iter != id_to_turl.end()) { | 124 if (existing_url_iter != id_to_turl.end()) { |
126 // Update the data store with the new prepopulated data. Preserve user | 125 // Update the data store with the new prepopulated data. Preserve user |
127 // edits to the name and keyword. | 126 // edits to the name and keyword. |
128 TemplateURLData data(prepopulated_url->data()); | 127 TemplateURLData data(prepopulated_url->data()); |
129 scoped_ptr<TemplateURL> existing_url(existing_url_iter->second); | 128 scoped_ptr<TemplateURL> existing_url(existing_url_iter->second); |
130 id_to_turl.erase(existing_url_iter); | 129 id_to_turl.erase(existing_url_iter); |
131 if (!existing_url->safe_for_autoreplace()) { | 130 if (!existing_url->safe_for_autoreplace()) { |
132 data.safe_for_autoreplace = false; | 131 data.safe_for_autoreplace = false; |
133 data.SetKeyword(existing_url->keyword()); | 132 data.SetKeyword(existing_url->keyword()); |
134 data.SetAutogenerateKeyword(existing_url->autogenerate_keyword()); | 133 data.SetAutogenerateKeyword(existing_url->autogenerate_keyword()); |
135 data.short_name = existing_url->short_name(); | 134 data.short_name = existing_url->short_name(); |
136 } | 135 } |
137 data.id = existing_url->id(); | 136 data.id = existing_url->id(); |
138 url_in_vector = new TemplateURL(data); | 137 url_in_vector = new TemplateURL(profile, data); |
139 if (service) | 138 if (service) |
140 service->UpdateKeyword(*url_in_vector); | 139 service->UpdateKeyword(*url_in_vector); |
141 | 140 |
142 // Replace the entry in |template_urls| with the updated one. | 141 // Replace the entry in |template_urls| with the updated one. |
143 std::vector<TemplateURL*>::iterator j = std::find(template_urls->begin(), | 142 std::vector<TemplateURL*>::iterator j = std::find(template_urls->begin(), |
144 template_urls->end(), existing_url.get()); | 143 template_urls->end(), existing_url.get()); |
145 *j = url_in_vector; | 144 *j = url_in_vector; |
146 if (*default_search_provider == existing_url.get()) | 145 if (*default_search_provider == existing_url.get()) |
147 *default_search_provider = url_in_vector; | 146 *default_search_provider = url_in_vector; |
148 } else { | 147 } else { |
(...skipping 19 matching lines...) Expand all Loading... |
168 if (service) | 167 if (service) |
169 service->RemoveKeyword(template_url->id()); | 168 service->RemoveKeyword(template_url->id()); |
170 delete template_url; | 169 delete template_url; |
171 } | 170 } |
172 } | 171 } |
173 } | 172 } |
174 | 173 |
175 void GetSearchProvidersUsingKeywordResult( | 174 void GetSearchProvidersUsingKeywordResult( |
176 const WDTypedResult& result, | 175 const WDTypedResult& result, |
177 WebDataService* service, | 176 WebDataService* service, |
178 PrefService* prefs, | 177 Profile* profile, |
179 std::vector<TemplateURL*>* template_urls, | 178 std::vector<TemplateURL*>* template_urls, |
180 const TemplateURL** default_search_provider, | 179 const TemplateURL** default_search_provider, |
181 int* new_resource_keyword_version) { | 180 int* new_resource_keyword_version) { |
182 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); | 181 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); |
183 DCHECK(template_urls); | 182 DCHECK(template_urls); |
184 DCHECK(template_urls->empty()); | 183 DCHECK(template_urls->empty()); |
185 DCHECK(default_search_provider); | 184 DCHECK(default_search_provider); |
186 DCHECK(*default_search_provider == NULL); | 185 DCHECK(*default_search_provider == NULL); |
187 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); | 186 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); |
188 DCHECK(new_resource_keyword_version); | 187 DCHECK(new_resource_keyword_version); |
189 | 188 |
190 *new_resource_keyword_version = 0; | 189 *new_resource_keyword_version = 0; |
191 WDKeywordsResult keyword_result = reinterpret_cast< | 190 WDKeywordsResult keyword_result = reinterpret_cast< |
192 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); | 191 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); |
193 | 192 |
194 for (KeywordTable::Keywords::const_iterator i( | 193 for (KeywordTable::Keywords::const_iterator i( |
195 keyword_result.keywords.begin()); i != keyword_result.keywords.end(); | 194 keyword_result.keywords.begin()); i != keyword_result.keywords.end(); |
196 ++i) | 195 ++i) |
197 template_urls->push_back(new TemplateURL(*i)); | 196 template_urls->push_back(new TemplateURL(profile, *i)); |
198 | 197 |
199 const int resource_keyword_version = | 198 const int resource_keyword_version = |
200 TemplateURLPrepopulateData::GetDataVersion(prefs); | 199 TemplateURLPrepopulateData::GetDataVersion( |
| 200 profile ? profile->GetPrefs() : NULL); |
201 if (keyword_result.builtin_keyword_version != resource_keyword_version) { | 201 if (keyword_result.builtin_keyword_version != resource_keyword_version) { |
202 // There should never be duplicate TemplateURLs. We had a bug such that | 202 // There should never be duplicate TemplateURLs. We had a bug such that |
203 // duplicate TemplateURLs existed for one locale. As such we invoke | 203 // duplicate TemplateURLs existed for one locale. As such we invoke |
204 // RemoveDuplicatePrepopulateIDs to nuke the duplicates. | 204 // RemoveDuplicatePrepopulateIDs to nuke the duplicates. |
205 RemoveDuplicatePrepopulateIDs(template_urls, service); | 205 RemoveDuplicatePrepopulateIDs(template_urls, service); |
206 } | 206 } |
207 | 207 |
208 int64 default_search_provider_id = keyword_result.default_search_provider_id; | 208 int64 default_search_provider_id = keyword_result.default_search_provider_id; |
209 if (default_search_provider_id) { | 209 if (default_search_provider_id) { |
210 *default_search_provider = | 210 *default_search_provider = |
211 GetTemplateURLByID(*template_urls, default_search_provider_id); | 211 GetTemplateURLByID(*template_urls, default_search_provider_id); |
212 } | 212 } |
213 | 213 |
214 if (keyword_result.builtin_keyword_version != resource_keyword_version) { | 214 if (keyword_result.builtin_keyword_version != resource_keyword_version) { |
215 MergeEnginesFromPrepopulateData(prefs, service, template_urls, | 215 MergeEnginesFromPrepopulateData(profile, service, template_urls, |
216 default_search_provider); | 216 default_search_provider); |
217 *new_resource_keyword_version = resource_keyword_version; | 217 *new_resource_keyword_version = resource_keyword_version; |
218 } | 218 } |
219 } | 219 } |
220 | 220 |
221 bool DidDefaultSearchProviderChange( | 221 bool DidDefaultSearchProviderChange( |
222 const WDTypedResult& result, | 222 const WDTypedResult& result, |
| 223 Profile* profile, |
223 scoped_ptr<TemplateURL>* backup_default_search_provider) { | 224 scoped_ptr<TemplateURL>* backup_default_search_provider) { |
224 DCHECK(backup_default_search_provider); | 225 DCHECK(backup_default_search_provider); |
225 DCHECK(!backup_default_search_provider->get()); | 226 DCHECK(!backup_default_search_provider->get()); |
226 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); | 227 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); |
227 | 228 |
228 WDKeywordsResult keyword_result = reinterpret_cast< | 229 WDKeywordsResult keyword_result = reinterpret_cast< |
229 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); | 230 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); |
230 | 231 |
231 if (!keyword_result.did_default_search_provider_change) | 232 if (!keyword_result.did_default_search_provider_change) |
232 return false; | 233 return false; |
233 | 234 |
234 if (keyword_result.backup_valid) { | 235 if (keyword_result.backup_valid) { |
235 backup_default_search_provider->reset(new TemplateURL( | 236 backup_default_search_provider->reset(new TemplateURL(profile, |
236 keyword_result.default_search_provider_backup)); | 237 keyword_result.default_search_provider_backup)); |
237 } | 238 } |
238 return true; | 239 return true; |
239 } | 240 } |
240 | 241 |
OLD | NEW |