Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

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

Issue 9982018: Move most TemplateURL data members to a new struct, TemplateURLData. This allows us to eliminate t… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 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 | Annotate | Revision Log
OLDNEW
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"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 DCHECK(template_urls); 46 DCHECK(template_urls);
47 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 47 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI));
48 48
49 std::set<int> ids; 49 std::set<int> ids;
50 for (std::vector<TemplateURL*>::iterator i = template_urls->begin(); 50 for (std::vector<TemplateURL*>::iterator i = template_urls->begin();
51 i != template_urls->end(); ) { 51 i != template_urls->end(); ) {
52 int prepopulate_id = (*i)->prepopulate_id(); 52 int prepopulate_id = (*i)->prepopulate_id();
53 if (prepopulate_id) { 53 if (prepopulate_id) {
54 if (ids.find(prepopulate_id) != ids.end()) { 54 if (ids.find(prepopulate_id) != ids.end()) {
55 if (service) 55 if (service)
56 service->RemoveKeyword(**i); 56 service->RemoveKeyword((*i)->id());
57 delete *i; 57 delete *i;
58 i = template_urls->erase(i); 58 i = template_urls->erase(i);
59 } else { 59 } else {
60 ids.insert(prepopulate_id); 60 ids.insert(prepopulate_id);
61 ++i; 61 ++i;
62 } 62 }
63 } else { 63 } else {
64 ++i; 64 ++i;
65 } 65 }
66 } 66 }
(...skipping 17 matching lines...) Expand all
84 // engines. This is invoked when the version of the prepopulate data changes. 84 // engines. This is invoked when the version of the prepopulate data changes.
85 void MergeEnginesFromPrepopulateData( 85 void MergeEnginesFromPrepopulateData(
86 PrefService* prefs, 86 PrefService* prefs,
87 WebDataService* service, 87 WebDataService* service,
88 std::vector<TemplateURL*>* template_urls, 88 std::vector<TemplateURL*>* template_urls,
89 const TemplateURL** default_search_provider) { 89 const TemplateURL** default_search_provider) {
90 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 90 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI));
91 DCHECK(template_urls); 91 DCHECK(template_urls);
92 DCHECK(default_search_provider); 92 DCHECK(default_search_provider);
93 93
94 // Build a map from prepopulate id to TemplateURL of existing urls. 94 // Create a map to hold all provided |template_urls| that originally came from
95 // prepopulate data (i.e. have a non-zero prepopulate_id()).
95 typedef std::map<int, TemplateURL*> IDMap; 96 typedef std::map<int, TemplateURL*> IDMap;
96 IDMap id_to_turl; 97 IDMap id_to_turl;
97 for (std::vector<TemplateURL*>::iterator i(template_urls->begin()); 98 for (std::vector<TemplateURL*>::iterator i(template_urls->begin());
98 i != template_urls->end(); ++i) { 99 i != template_urls->end(); ++i) {
99 int prepopulate_id = (*i)->prepopulate_id(); 100 int prepopulate_id = (*i)->prepopulate_id();
100 if (prepopulate_id > 0) 101 if (prepopulate_id > 0)
101 id_to_turl[prepopulate_id] = *i; 102 id_to_turl[prepopulate_id] = *i;
102 } 103 }
103 104
105 // Get the current set of prepopulatd URLs.
104 std::vector<TemplateURL*> prepopulated_urls; 106 std::vector<TemplateURL*> prepopulated_urls;
105 size_t default_search_index; 107 size_t default_search_index;
106 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs, 108 TemplateURLPrepopulateData::GetPrepopulatedEngines(prefs,
107 &prepopulated_urls, 109 &prepopulated_urls, &default_search_index);
108 &default_search_index);
109 110
110 std::set<int> updated_ids; 111 // 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
113 // 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|.
115 // Along the way, point |default_search_provider| at the default prepopulated
116 // URL, if the user hasn't already set another URL as default.
111 for (size_t i = 0; i < prepopulated_urls.size(); ++i) { 117 for (size_t i = 0; i < prepopulated_urls.size(); ++i) {
112 // We take ownership of |prepopulated_urls[i]|. 118 // We take ownership of |prepopulated_urls[i]|.
113 scoped_ptr<TemplateURL> prepopulated_url(prepopulated_urls[i]); 119 scoped_ptr<TemplateURL> prepopulated_url(prepopulated_urls[i]);
114 const int prepopulated_id = prepopulated_url->prepopulate_id(); 120 const int prepopulated_id = prepopulated_url->prepopulate_id();
115 if (!prepopulated_id || updated_ids.count(prepopulated_id)) { 121 DCHECK_NE(0, prepopulated_id);
116 // Prepopulate engines need a unique id.
117 NOTREACHED();
118 continue;
119 }
120 122
121 TemplateURL* existing_url = NULL; 123 TemplateURL* url_in_vector = NULL;
122 IDMap::iterator existing_url_iter(id_to_turl.find(prepopulated_id)); 124 IDMap::iterator existing_url_iter(id_to_turl.find(prepopulated_id));
123 if (existing_url_iter != id_to_turl.end()) { 125 if (existing_url_iter != id_to_turl.end()) {
124 existing_url = existing_url_iter->second; 126 // Update the data store with the new prepopulated data. Preserve user
127 // edits to the name and keyword.
128 TemplateURLData data(prepopulated_url->data());
129 scoped_ptr<TemplateURL> existing_url(existing_url_iter->second);
130 id_to_turl.erase(existing_url_iter);
125 if (!existing_url->safe_for_autoreplace()) { 131 if (!existing_url->safe_for_autoreplace()) {
126 // User edited the entry, preserve the keyword and description. 132 data.safe_for_autoreplace = false;
127 prepopulated_url->set_safe_for_autoreplace(false); 133 data.SetKeyword(existing_url->keyword());
128 prepopulated_url->set_keyword(existing_url->keyword()); 134 data.SetAutogenerateKeyword(existing_url->autogenerate_keyword());
129 prepopulated_url->set_autogenerate_keyword( 135 data.short_name = existing_url->short_name();
130 existing_url->autogenerate_keyword());
131 prepopulated_url->set_short_name(existing_url->short_name());
132 } 136 }
133 prepopulated_url->set_id(existing_url->id()); 137 data.id = existing_url->id();
138 url_in_vector = new TemplateURL(data);
139 if (service)
140 service->UpdateKeyword(*url_in_vector);
134 141
135 *existing_url = *prepopulated_url; 142 // Replace the entry in |template_urls| with the updated one.
136 if (service) { 143 std::vector<TemplateURL*>::iterator j = std::find(template_urls->begin(),
137 service->UpdateKeyword(*existing_url); 144 template_urls->end(), existing_url.get());
138 } 145 *j = url_in_vector;
139 id_to_turl.erase(existing_url_iter);
140 } else { 146 } else {
141 existing_url = prepopulated_url.get();
142 template_urls->push_back(prepopulated_url.release()); 147 template_urls->push_back(prepopulated_url.release());
148 url_in_vector = template_urls->back();
143 } 149 }
144 DCHECK(existing_url); 150 DCHECK(url_in_vector);
145 if (i == default_search_index && !*default_search_provider) 151 if (i == default_search_index && !*default_search_provider)
146 *default_search_provider = existing_url; 152 *default_search_provider = url_in_vector;
147
148 updated_ids.insert(prepopulated_id);
149 } 153 }
150 154
151 // Remove any prepopulated engines which are no longer in the master list, as 155 // The block above removed all the URLs from the |id_to_turl| map that were
152 // long as the user hasn't modified them or made them the default engine. 156 // found in the prepopulate data. Any remaining URLs that haven't been
157 // user-edited or made default can be removed from the data store.
153 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) { 158 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) {
154 const TemplateURL* template_url = i->second; 159 const TemplateURL* template_url = i->second;
155 if ((template_url->safe_for_autoreplace()) && 160 if ((template_url->safe_for_autoreplace()) &&
156 (template_url != *default_search_provider)) { 161 (template_url != *default_search_provider)) {
157 std::vector<TemplateURL*>::iterator i = std::find(template_urls->begin(), 162 std::vector<TemplateURL*>::iterator j =
158 template_urls->end(), 163 std::find(template_urls->begin(), template_urls->end(), template_url);
159 template_url); 164 DCHECK(j != template_urls->end());
160 DCHECK(i != template_urls->end()); 165 template_urls->erase(j);
161 template_urls->erase(i);
162 if (service) 166 if (service)
163 service->RemoveKeyword(*template_url); 167 service->RemoveKeyword(template_url->id());
164 delete template_url; 168 delete template_url;
165 } 169 }
166 } 170 }
167 } 171 }
168 172
169 void GetSearchProvidersUsingKeywordResult( 173 void GetSearchProvidersUsingKeywordResult(
170 const WDTypedResult& result, 174 const WDTypedResult& result,
171 WebDataService* service, 175 WebDataService* service,
172 PrefService* prefs, 176 PrefService* prefs,
173 std::vector<TemplateURL*>* template_urls, 177 std::vector<TemplateURL*>* template_urls,
174 const TemplateURL** default_search_provider, 178 const TemplateURL** default_search_provider,
175 int* new_resource_keyword_version) { 179 int* new_resource_keyword_version) {
176 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 180 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI));
177 DCHECK(template_urls); 181 DCHECK(template_urls);
178 DCHECK(template_urls->empty()); 182 DCHECK(template_urls->empty());
179 DCHECK(default_search_provider); 183 DCHECK(default_search_provider);
180 DCHECK(*default_search_provider == NULL); 184 DCHECK(*default_search_provider == NULL);
181 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); 185 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT);
182 DCHECK(new_resource_keyword_version); 186 DCHECK(new_resource_keyword_version);
183 187
184 *new_resource_keyword_version = 0; 188 *new_resource_keyword_version = 0;
185 WDKeywordsResult keyword_result = reinterpret_cast< 189 WDKeywordsResult keyword_result = reinterpret_cast<
186 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); 190 const WDResult<WDKeywordsResult>*>(&result)->GetValue();
187 191
188 template_urls->swap(keyword_result.keywords); 192 for (KeywordTable::Keywords::const_iterator i(
193 keyword_result.keywords.begin()); i != keyword_result.keywords.end();
194 ++i)
195 template_urls->push_back(new TemplateURL(*i));
189 196
190 const int resource_keyword_version = 197 const int resource_keyword_version =
191 TemplateURLPrepopulateData::GetDataVersion(prefs); 198 TemplateURLPrepopulateData::GetDataVersion(prefs);
192 if (keyword_result.builtin_keyword_version != resource_keyword_version) { 199 if (keyword_result.builtin_keyword_version != resource_keyword_version) {
193 // There should never be duplicate TemplateURLs. We had a bug such that 200 // There should never be duplicate TemplateURLs. We had a bug such that
194 // duplicate TemplateURLs existed for one locale. As such we invoke 201 // duplicate TemplateURLs existed for one locale. As such we invoke
195 // RemoveDuplicatePrepopulateIDs to nuke the duplicates. 202 // RemoveDuplicatePrepopulateIDs to nuke the duplicates.
196 RemoveDuplicatePrepopulateIDs(template_urls, service); 203 RemoveDuplicatePrepopulateIDs(template_urls, service);
197 } 204 }
198 205
(...skipping 16 matching lines...) Expand all
215 DCHECK(backup_default_search_provider); 222 DCHECK(backup_default_search_provider);
216 DCHECK(!backup_default_search_provider->get()); 223 DCHECK(!backup_default_search_provider->get());
217 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); 224 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT);
218 225
219 WDKeywordsResult keyword_result = reinterpret_cast< 226 WDKeywordsResult keyword_result = reinterpret_cast<
220 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); 227 const WDResult<WDKeywordsResult>*>(&result)->GetValue();
221 228
222 if (!keyword_result.did_default_search_provider_change) 229 if (!keyword_result.did_default_search_provider_change)
223 return false; 230 return false;
224 231
225 backup_default_search_provider->reset( 232 if (keyword_result.backup_valid) {
226 keyword_result.default_search_provider_backup); 233 backup_default_search_provider->reset(new TemplateURL(
234 keyword_result.default_search_provider_backup));
235 }
227 return true; 236 return true;
228 } 237 }
229 238
OLDNEW
« no previous file with comments | « chrome/browser/search_engines/template_url_unittest.cc ('k') | chrome/browser/sync/test/integration/search_engines_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698