Chromium Code Reviews
[email protected] (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: chrome/browser/search_engines/util.cc

Issue 3110038: Extract the processing of the web data service results processing in a standalone function. (Closed)
Patch Set: Addressed feedback. Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/search_engines/util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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>
8 #include <vector>
9
10 #include "base/logging.h"
7 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "chrome/browser/chrome_thread.h"
13 #include "chrome/browser/search_engines/template_url.h"
8 #include "chrome/browser/search_engines/template_url_model.h" 14 #include "chrome/browser/search_engines/template_url_model.h"
15 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
16 #include "chrome/browser/prefs/pref_service.h"
9 #include "chrome/browser/profile.h" 17 #include "chrome/browser/profile.h"
10 18
11 string16 GetDefaultSearchEngineName(Profile* profile) { 19 string16 GetDefaultSearchEngineName(Profile* profile) {
12 if (!profile) { 20 if (!profile) {
13 NOTREACHED(); 21 NOTREACHED();
14 return string16(); 22 return string16();
15 } 23 }
16 const TemplateURL* const default_provider = 24 const TemplateURL* const default_provider =
17 profile->GetTemplateURLModel()->GetDefaultSearchProvider(); 25 profile->GetTemplateURLModel()->GetDefaultSearchProvider();
18 if (!default_provider) { 26 if (!default_provider) {
19 // TODO(cpu): bug 1187517. It is possible to have no default provider. 27 // TODO(cpu): bug 1187517. It is possible to have no default provider.
20 // returning an empty string is a stopgap measure for the crash 28 // returning an empty string is a stopgap measure for the crash
21 // http://code.google.com/p/chromium/issues/detail?id=2573 29 // http://code.google.com/p/chromium/issues/detail?id=2573
22 return string16(); 30 return string16();
23 } 31 }
24 return WideToUTF16(default_provider->short_name()); 32 return WideToUTF16(default_provider->short_name());
25 } 33 }
34
35 // Removes (and deletes) TemplateURLs from |urls| that have duplicate
36 // prepopulate ids. Duplicate prepopulate ids are not allowed, but due to a
37 // bug it was possible get dups. This step is only called when the version
38 // number changes. Only pass in a non-NULL value for |service| if the removed
39 // items should be removed from the DB.
40 static void RemoveDuplicatePrepopulateIDs(
41 std::vector<TemplateURL*>* template_urls,
42 WebDataService* service) {
43 DCHECK(template_urls);
44 DCHECK(service == NULL || ChromeThread::CurrentlyOn(ChromeThread::UI));
45
46 std::set<int> ids;
47 for (std::vector<TemplateURL*>::iterator i = template_urls->begin();
48 i != template_urls->end(); ) {
49 int prepopulate_id = (*i)->prepopulate_id();
50 if (prepopulate_id) {
51 if (ids.find(prepopulate_id) != ids.end()) {
52 if (service)
53 service->RemoveKeyword(**i);
54 delete *i;
55 i = template_urls->erase(i);
56 } else {
57 ids.insert(prepopulate_id);
58 ++i;
59 }
60 } else {
61 ++i;
62 }
63 }
64 }
65
66 // Loads engines from prepopulate data and merges them in with the existing
67 // engines. This is invoked when the version of the prepopulate data changes.
68 void MergeEnginesFromPrepopulateData(
69 PrefService* prefs,
70 WebDataService* service,
71 std::vector<TemplateURL*>* template_urls,
72 const TemplateURL** default_search_provider) {
73 DCHECK(service == NULL || ChromeThread::CurrentlyOn(ChromeThread::UI));
74 DCHECK(template_urls);
75 DCHECK(default_search_provider);
76
77 // Build a map from prepopulate id to TemplateURL of existing urls.
78 typedef std::map<int, TemplateURL*> IDMap;
79 IDMap id_to_turl;
80 for (std::vector<TemplateURL*>::iterator i(template_urls->begin());
81 i != template_urls->end(); ++i) {
82 int prepopulate_id = (*i)->prepopulate_id();
83 if (prepopulate_id > 0)
84 id_to_turl[prepopulate_id] = *i;
85 }
86
87 std::vector<TemplateURL*> loaded_urls;
88 size_t default_search_index;
89 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs,
90 &loaded_urls,
91 &default_search_index);
92
93 std::set<int> updated_ids;
94 for (size_t i = 0; i < loaded_urls.size(); ++i) {
95 // We take ownership of |t_url|.
96 scoped_ptr<TemplateURL> t_url(loaded_urls[i]);
97 const int t_url_id = t_url->prepopulate_id();
98 if (!t_url_id || updated_ids.count(t_url_id)) {
99 // Prepopulate engines need a unique id.
100 NOTREACHED();
101 continue;
102 }
103
104 TemplateURL* current_t_url = t_url.get();
105 IDMap::iterator existing_url_iter(id_to_turl.find(t_url_id));
106 if (existing_url_iter != id_to_turl.end()) {
107 current_t_url = existing_url_iter->second;
108 if (!current_t_url->safe_for_autoreplace()) {
109 // User edited the entry, preserve the keyword and description.
110 t_url->set_safe_for_autoreplace(false);
111 t_url->set_keyword(current_t_url->keyword());
112 t_url->set_autogenerate_keyword(
113 current_t_url->autogenerate_keyword());
114 t_url->set_short_name(current_t_url->short_name());
115 }
116 t_url->set_id(current_t_url->id());
117
118 *current_t_url = *t_url;
119 if (service)
120 service->UpdateKeyword(*current_t_url);
121 id_to_turl.erase(existing_url_iter);
122 } else {
123 template_urls->push_back(t_url.release());
124 }
125 if (i == default_search_index && !*default_search_provider)
126 *default_search_provider = current_t_url;
127
128 updated_ids.insert(t_url_id);
129 }
130
131 // Remove any prepopulated engines which are no longer in the master list, as
132 // long as the user hasn't modified them or made them the default engine.
133 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) {
134 const TemplateURL* template_url = i->second;
135 if ((template_url->safe_for_autoreplace()) &&
136 (template_url != *default_search_provider)) {
137 std::vector<TemplateURL*>::iterator i = find(template_urls->begin(),
138 template_urls->end(),
139 template_url);
140 DCHECK(i != template_urls->end());
141 template_urls->erase(i);
142 if (service)
143 service->RemoveKeyword(*template_url);
144 delete template_url;
145 }
146 }
147 }
148
149 void GetSearchProvidersUsingKeywordResult(
150 const WDTypedResult& result,
151 WebDataService* service,
152 PrefService* prefs,
153 std::vector<TemplateURL*>* template_urls,
154 const TemplateURL** default_search_provider,
155 int* new_resource_keyword_version) {
156 DCHECK(service == NULL || ChromeThread::CurrentlyOn(ChromeThread::UI));
157 DCHECK(template_urls);
158 DCHECK(template_urls->empty());
159 DCHECK(default_search_provider);
160 DCHECK(*default_search_provider == NULL);
161 DCHECK(result.GetType() == KEYWORDS_RESULT);
162 DCHECK(new_resource_keyword_version);
163
164 *new_resource_keyword_version = 0;
165 WDKeywordsResult keyword_result = reinterpret_cast<
166 const WDResult<WDKeywordsResult>*>(&result)->GetValue();
167
168 template_urls->swap(keyword_result.keywords);
169
170 const int resource_keyword_version =
171 TemplateURLPrepopulateData::GetDataVersion(prefs);
172 if (keyword_result.builtin_keyword_version != resource_keyword_version) {
173 // There should never be duplicate TemplateURLs. We had a bug such that
174 // duplicate TemplateURLs existed for one locale. As such we invoke
175 // RemoveDuplicatePrepopulateIDs to nuke the duplicates.
176 RemoveDuplicatePrepopulateIDs(template_urls, service);
177 }
178
179 if (keyword_result.default_search_provider_id) {
180 // See if we can find the default search provider.
181 for (std::vector<TemplateURL*>::iterator i = template_urls->begin();
182 i != template_urls->end(); ++i) {
183 if ((*i)->id() == keyword_result.default_search_provider_id) {
184 *default_search_provider = *i;
185 break;
186 }
187 }
188 }
189
190 if (keyword_result.builtin_keyword_version != resource_keyword_version) {
191 MergeEnginesFromPrepopulateData(prefs, service, template_urls,
192 default_search_provider);
193 *new_resource_keyword_version = resource_keyword_version;
194 }
195 }
OLDNEW
« no previous file with comments | « chrome/browser/search_engines/util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698