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

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

Issue 10021008: Reland r131019: Move most TemplateURL data members to a new struct, TemplateURLData. This allows us… (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); 146 if (*default_search_provider == existing_url.get())
147 *default_search_provider = url_in_vector;
140 } else { 148 } else {
141 existing_url = prepopulated_url.get();
142 template_urls->push_back(prepopulated_url.release()); 149 template_urls->push_back(prepopulated_url.release());
150 url_in_vector = template_urls->back();
143 } 151 }
144 DCHECK(existing_url); 152 DCHECK(url_in_vector);
145 if (i == default_search_index && !*default_search_provider) 153 if (i == default_search_index && !*default_search_provider)
146 *default_search_provider = existing_url; 154 *default_search_provider = url_in_vector;
147
148 updated_ids.insert(prepopulated_id);
149 } 155 }
150 156
151 // Remove any prepopulated engines which are no longer in the master list, as 157 // 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. 158 // found in the prepopulate data. Any remaining URLs that haven't been
159 // 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) { 160 for (IDMap::iterator i(id_to_turl.begin()); i != id_to_turl.end(); ++i) {
154 const TemplateURL* template_url = i->second; 161 const TemplateURL* template_url = i->second;
155 if ((template_url->safe_for_autoreplace()) && 162 if ((template_url->safe_for_autoreplace()) &&
156 (template_url != *default_search_provider)) { 163 (template_url != *default_search_provider)) {
157 std::vector<TemplateURL*>::iterator i = std::find(template_urls->begin(), 164 std::vector<TemplateURL*>::iterator j =
158 template_urls->end(), 165 std::find(template_urls->begin(), template_urls->end(), template_url);
159 template_url); 166 DCHECK(j != template_urls->end());
160 DCHECK(i != template_urls->end()); 167 template_urls->erase(j);
161 template_urls->erase(i);
162 if (service) 168 if (service)
163 service->RemoveKeyword(*template_url); 169 service->RemoveKeyword(template_url->id());
164 delete template_url; 170 delete template_url;
165 } 171 }
166 } 172 }
167 } 173 }
168 174
169 void GetSearchProvidersUsingKeywordResult( 175 void GetSearchProvidersUsingKeywordResult(
170 const WDTypedResult& result, 176 const WDTypedResult& result,
171 WebDataService* service, 177 WebDataService* service,
172 PrefService* prefs, 178 PrefService* prefs,
173 std::vector<TemplateURL*>* template_urls, 179 std::vector<TemplateURL*>* template_urls,
174 const TemplateURL** default_search_provider, 180 const TemplateURL** default_search_provider,
175 int* new_resource_keyword_version) { 181 int* new_resource_keyword_version) {
176 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI)); 182 DCHECK(service == NULL || BrowserThread::CurrentlyOn(BrowserThread::UI));
177 DCHECK(template_urls); 183 DCHECK(template_urls);
178 DCHECK(template_urls->empty()); 184 DCHECK(template_urls->empty());
179 DCHECK(default_search_provider); 185 DCHECK(default_search_provider);
180 DCHECK(*default_search_provider == NULL); 186 DCHECK(*default_search_provider == NULL);
181 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); 187 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT);
182 DCHECK(new_resource_keyword_version); 188 DCHECK(new_resource_keyword_version);
183 189
184 *new_resource_keyword_version = 0; 190 *new_resource_keyword_version = 0;
185 WDKeywordsResult keyword_result = reinterpret_cast< 191 WDKeywordsResult keyword_result = reinterpret_cast<
186 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); 192 const WDResult<WDKeywordsResult>*>(&result)->GetValue();
187 193
188 template_urls->swap(keyword_result.keywords); 194 for (KeywordTable::Keywords::const_iterator i(
195 keyword_result.keywords.begin()); i != keyword_result.keywords.end();
196 ++i)
197 template_urls->push_back(new TemplateURL(*i));
189 198
190 const int resource_keyword_version = 199 const int resource_keyword_version =
191 TemplateURLPrepopulateData::GetDataVersion(prefs); 200 TemplateURLPrepopulateData::GetDataVersion(prefs);
192 if (keyword_result.builtin_keyword_version != resource_keyword_version) { 201 if (keyword_result.builtin_keyword_version != resource_keyword_version) {
193 // 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
194 // duplicate TemplateURLs existed for one locale. As such we invoke 203 // duplicate TemplateURLs existed for one locale. As such we invoke
195 // RemoveDuplicatePrepopulateIDs to nuke the duplicates. 204 // RemoveDuplicatePrepopulateIDs to nuke the duplicates.
196 RemoveDuplicatePrepopulateIDs(template_urls, service); 205 RemoveDuplicatePrepopulateIDs(template_urls, service);
197 } 206 }
198 207
(...skipping 16 matching lines...) Expand all
215 DCHECK(backup_default_search_provider); 224 DCHECK(backup_default_search_provider);
216 DCHECK(!backup_default_search_provider->get()); 225 DCHECK(!backup_default_search_provider->get());
217 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT); 226 DCHECK_EQ(result.GetType(), KEYWORDS_RESULT);
218 227
219 WDKeywordsResult keyword_result = reinterpret_cast< 228 WDKeywordsResult keyword_result = reinterpret_cast<
220 const WDResult<WDKeywordsResult>*>(&result)->GetValue(); 229 const WDResult<WDKeywordsResult>*>(&result)->GetValue();
221 230
222 if (!keyword_result.did_default_search_provider_change) 231 if (!keyword_result.did_default_search_provider_change)
223 return false; 232 return false;
224 233
225 backup_default_search_provider->reset( 234 if (keyword_result.backup_valid) {
226 keyword_result.default_search_provider_backup); 235 backup_default_search_provider->reset(new TemplateURL(
236 keyword_result.default_search_provider_backup));
237 }
227 return true; 238 return true;
228 } 239 }
229 240
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