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

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

Issue 6322001: Remove wstring from TemplateURL and friends.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/template_url_model.h" 5 #include "chrome/browser/search_engines/template_url_model.h"
6 6
7 #include "app/l10n_util.h" 7 #include "app/l10n_util.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/environment.h" 9 #include "base/environment.h"
10 #include "base/stl_util-inl.h" 10 #include "base/stl_util-inl.h"
11 #include "base/string_number_conversions.h" 11 #include "base/string_number_conversions.h"
(...skipping 27 matching lines...) Expand all
39 39
40 // String in the URL that is replaced by the search term. 40 // String in the URL that is replaced by the search term.
41 static const char kSearchTermParameter[] = "{searchTerms}"; 41 static const char kSearchTermParameter[] = "{searchTerms}";
42 42
43 // String in Initializer that is replaced with kSearchTermParameter. 43 // String in Initializer that is replaced with kSearchTermParameter.
44 static const char kTemplateParameter[] = "%s"; 44 static const char kTemplateParameter[] = "%s";
45 45
46 // Term used when generating a search url. Use something obscure so that on 46 // Term used when generating a search url. Use something obscure so that on
47 // the rare case the term replaces the URL it's unlikely another keyword would 47 // the rare case the term replaces the URL it's unlikely another keyword would
48 // have the same url. 48 // have the same url.
49 static const wchar_t kReplacementTerm[] = L"blah.blah.blah.blah.blah"; 49 static const char kReplacementTerm[] = "blah.blah.blah.blah.blah";
50 50
51 51
52 // Removes from the vector any template URL that was created because of 52 // Removes from the vector any template URL that was created because of
53 // policy. These TemplateURLs are freed. 53 // policy. These TemplateURLs are freed.
54 // Sets default_search_provider to NULL if it was one of them. 54 // Sets default_search_provider to NULL if it was one of them.
55 static void RemoveProvidersCreatedByPolicy( 55 static void RemoveProvidersCreatedByPolicy(
56 std::vector<TemplateURL*>* template_urls, 56 std::vector<TemplateURL*>* template_urls,
57 const TemplateURL** default_search_provider) { 57 const TemplateURL** default_search_provider) {
58 DCHECK(template_urls); 58 DCHECK(template_urls);
59 DCHECK(default_search_provider); 59 DCHECK(default_search_provider);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 TemplateURLModel::~TemplateURLModel() { 122 TemplateURLModel::~TemplateURLModel() {
123 if (load_handle_) { 123 if (load_handle_) {
124 DCHECK(service_.get()); 124 DCHECK(service_.get());
125 service_->CancelRequest(load_handle_); 125 service_->CancelRequest(load_handle_);
126 } 126 }
127 127
128 STLDeleteElements(&template_urls_); 128 STLDeleteElements(&template_urls_);
129 } 129 }
130 130
131 // static 131 // static
132 std::wstring TemplateURLModel::GenerateKeyword(const GURL& url, 132 string16 TemplateURLModel::GenerateKeyword(const GURL& url,
133 bool autodetected) { 133 bool autodetected) {
134 // Don't autogenerate keywords for referrers that are the result of a form 134 // Don't autogenerate keywords for referrers that are the result of a form
135 // submission (TODO: right now we approximate this by checking for the URL 135 // submission (TODO: right now we approximate this by checking for the URL
136 // having a query, but we should replace this with a call to WebCore to see if 136 // having a query, but we should replace this with a call to WebCore to see if
137 // the originating page was actually a form submission), anything other than 137 // the originating page was actually a form submission), anything other than
138 // http, or referrers with a path. 138 // http, or referrers with a path.
139 // 139 //
140 // If we relax the path constraint, we need to be sure to sanitize the path 140 // If we relax the path constraint, we need to be sure to sanitize the path
141 // elements and update AutocompletePopup to look for keywords using the path. 141 // elements and update AutocompletePopup to look for keywords using the path.
142 // See http://b/issue?id=863583. 142 // See http://b/issue?id=863583.
143 if (!url.is_valid() || 143 if (!url.is_valid() ||
144 (autodetected && (url.has_query() || !url.SchemeIs(chrome::kHttpScheme) || 144 (autodetected && (url.has_query() || !url.SchemeIs(chrome::kHttpScheme) ||
145 ((url.path() != "") && (url.path() != "/"))))) 145 ((url.path() != "") && (url.path() != "/")))))
146 return std::wstring(); 146 return string16();
147 147
148 // Strip "www." off the front of the keyword; otherwise the keyword won't work 148 // Strip "www." off the front of the keyword; otherwise the keyword won't work
149 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 . 149 // properly. See http://code.google.com/p/chromium/issues/detail?id=6984 .
150 return UTF16ToWideHack(net::StripWWW(UTF8ToUTF16(url.host()))); 150 return net::StripWWW(UTF8ToUTF16(url.host()));
151 } 151 }
152 152
153 // static 153 // static
154 std::wstring TemplateURLModel::CleanUserInputKeyword( 154 string16 TemplateURLModel::CleanUserInputKeyword(const string16& keyword) {
155 const std::wstring& keyword) {
156 // Remove the scheme. 155 // Remove the scheme.
157 std::wstring result(UTF16ToWide(l10n_util::ToLower(WideToUTF16(keyword)))); 156 string16 result(l10n_util::ToLower(keyword));
158 url_parse::Component scheme_component; 157 url_parse::Component scheme_component;
159 if (url_parse::ExtractScheme(WideToUTF8(keyword).c_str(), 158 if (url_parse::ExtractScheme(UTF16ToUTF8(keyword).c_str(),
160 static_cast<int>(keyword.length()), 159 static_cast<int>(keyword.length()),
161 &scheme_component)) { 160 &scheme_component)) {
162 // If the scheme isn't "http" or "https", bail. The user isn't trying to 161 // If the scheme isn't "http" or "https", bail. The user isn't trying to
163 // type a web address, but rather an FTP, file:, or other scheme URL, or a 162 // type a web address, but rather an FTP, file:, or other scheme URL, or a
164 // search query with some sort of initial operator (e.g. "site:"). 163 // search query with some sort of initial operator (e.g. "site:").
165 if (result.compare(0, scheme_component.end(), 164 if (result.compare(0, scheme_component.end(),
166 ASCIIToWide(chrome::kHttpScheme)) && 165 ASCIIToUTF16(chrome::kHttpScheme)) &&
167 result.compare(0, scheme_component.end(), 166 result.compare(0, scheme_component.end(),
168 ASCIIToWide(chrome::kHttpsScheme))) 167 ASCIIToUTF16(chrome::kHttpsScheme)))
169 return std::wstring(); 168 return string16();
170 169
171 // Include trailing ':'. 170 // Include trailing ':'.
172 result.erase(0, scheme_component.end() + 1); 171 result.erase(0, scheme_component.end() + 1);
173 // Many schemes usually have "//" after them, so strip it too. 172 // Many schemes usually have "//" after them, so strip it too.
174 const std::wstring after_scheme(L"//"); 173 const string16 after_scheme(ASCIIToUTF16("//"));
175 if (result.compare(0, after_scheme.length(), after_scheme) == 0) 174 if (result.compare(0, after_scheme.length(), after_scheme) == 0)
176 result.erase(0, after_scheme.length()); 175 result.erase(0, after_scheme.length());
177 } 176 }
178 177
179 // Remove leading "www.". 178 // Remove leading "www.".
180 result = UTF16ToWideHack(net::StripWWW(WideToUTF16(result))); 179 result = net::StripWWW(result);
181 180
182 // Remove trailing "/". 181 // Remove trailing "/".
183 return (result.length() > 0 && result[result.length() - 1] == L'/') ? 182 return (result.length() > 0 && result[result.length() - 1] == '/') ?
184 result.substr(0, result.length() - 1) : result; 183 result.substr(0, result.length() - 1) : result;
185 } 184 }
186 185
187 // static 186 // static
188 GURL TemplateURLModel::GenerateSearchURL(const TemplateURL* t_url) { 187 GURL TemplateURLModel::GenerateSearchURL(const TemplateURL* t_url) {
189 DCHECK(t_url); 188 DCHECK(t_url);
190 UIThreadSearchTermsData search_terms_data; 189 UIThreadSearchTermsData search_terms_data;
191 return GenerateSearchURLUsingTermsData(t_url, search_terms_data); 190 return GenerateSearchURLUsingTermsData(t_url, search_terms_data);
192 } 191 }
193 192
194 // static 193 // static
195 GURL TemplateURLModel::GenerateSearchURLUsingTermsData( 194 GURL TemplateURLModel::GenerateSearchURLUsingTermsData(
196 const TemplateURL* t_url, 195 const TemplateURL* t_url,
197 const SearchTermsData& search_terms_data) { 196 const SearchTermsData& search_terms_data) {
198 DCHECK(t_url); 197 DCHECK(t_url);
199 const TemplateURLRef* search_ref = t_url->url(); 198 const TemplateURLRef* search_ref = t_url->url();
200 // Extension keywords don't have host-based search URLs. 199 // Extension keywords don't have host-based search URLs.
201 if (!search_ref || !search_ref->IsValidUsingTermsData(search_terms_data) || 200 if (!search_ref || !search_ref->IsValidUsingTermsData(search_terms_data) ||
202 t_url->IsExtensionKeyword()) 201 t_url->IsExtensionKeyword())
203 return GURL(); 202 return GURL();
204 203
205 if (!search_ref->SupportsReplacementUsingTermsData(search_terms_data)) 204 if (!search_ref->SupportsReplacementUsingTermsData(search_terms_data))
206 return GURL(search_ref->url()); 205 return GURL(search_ref->url());
207 206
208 return GURL(search_ref->ReplaceSearchTermsUsingTermsData( 207 return GURL(search_ref->ReplaceSearchTermsUsingTermsData(
209 *t_url, kReplacementTerm, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, 208 *t_url, ASCIIToUTF16(kReplacementTerm),
210 std::wstring(), search_terms_data)); 209 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE,
210 string16(), search_terms_data));
211 } 211 }
212 212
213 bool TemplateURLModel::CanReplaceKeyword( 213 bool TemplateURLModel::CanReplaceKeyword(
214 const std::wstring& keyword, 214 const string16& keyword,
215 const GURL& url, 215 const GURL& url,
216 const TemplateURL** template_url_to_replace) { 216 const TemplateURL** template_url_to_replace) {
217 DCHECK(!keyword.empty()); // This should only be called for non-empty 217 DCHECK(!keyword.empty()); // This should only be called for non-empty
218 // keywords. If we need to support empty kewords 218 // keywords. If we need to support empty kewords
219 // the code needs to change slightly. 219 // the code needs to change slightly.
220 const TemplateURL* existing_url = GetTemplateURLForKeyword(keyword); 220 const TemplateURL* existing_url = GetTemplateURLForKeyword(keyword);
221 if (existing_url) { 221 if (existing_url) {
222 // We already have a TemplateURL for this keyword. Only allow it to be 222 // We already have a TemplateURL for this keyword. Only allow it to be
223 // replaced if the TemplateURL can be replaced. 223 // replaced if the TemplateURL can be replaced.
224 if (template_url_to_replace) 224 if (template_url_to_replace)
225 *template_url_to_replace = existing_url; 225 *template_url_to_replace = existing_url;
226 return CanReplace(existing_url); 226 return CanReplace(existing_url);
227 } 227 }
228 228
229 // We don't have a TemplateURL with keyword. Only allow a new one if there 229 // We don't have a TemplateURL with keyword. Only allow a new one if there
230 // isn't a TemplateURL for the specified host, or there is one but it can 230 // isn't a TemplateURL for the specified host, or there is one but it can
231 // be replaced. We do this to ensure that if the user assigns a different 231 // be replaced. We do this to ensure that if the user assigns a different
232 // keyword to a generated TemplateURL, we won't regenerate another keyword for 232 // keyword to a generated TemplateURL, we won't regenerate another keyword for
233 // the same host. 233 // the same host.
234 if (url.is_valid() && !url.host().empty()) 234 if (url.is_valid() && !url.host().empty())
235 return CanReplaceKeywordForHost(url.host(), template_url_to_replace); 235 return CanReplaceKeywordForHost(url.host(), template_url_to_replace);
236 return true; 236 return true;
237 } 237 }
238 238
239 void TemplateURLModel::FindMatchingKeywords( 239 void TemplateURLModel::FindMatchingKeywords(
240 const std::wstring& prefix, 240 const string16& prefix,
241 bool support_replacement_only, 241 bool support_replacement_only,
242 std::vector<std::wstring>* matches) const { 242 std::vector<string16>* matches) const {
243 // Sanity check args. 243 // Sanity check args.
244 if (prefix.empty()) 244 if (prefix.empty())
245 return; 245 return;
246 DCHECK(matches != NULL); 246 DCHECK(matches != NULL);
247 DCHECK(matches->empty()); // The code for exact matches assumes this. 247 DCHECK(matches->empty()); // The code for exact matches assumes this.
248 248
249 // Find matching keyword range. Searches the element map for keywords 249 // Find matching keyword range. Searches the element map for keywords
250 // beginning with |prefix| and stores the endpoints of the resulting set in 250 // beginning with |prefix| and stores the endpoints of the resulting set in
251 // |match_range|. 251 // |match_range|.
252 const std::pair<KeywordToTemplateMap::const_iterator, 252 const std::pair<KeywordToTemplateMap::const_iterator,
253 KeywordToTemplateMap::const_iterator> match_range( 253 KeywordToTemplateMap::const_iterator> match_range(
254 std::equal_range( 254 std::equal_range(
255 keyword_to_template_map_.begin(), keyword_to_template_map_.end(), 255 keyword_to_template_map_.begin(), keyword_to_template_map_.end(),
256 KeywordToTemplateMap::value_type(prefix, NULL), LessWithPrefix())); 256 KeywordToTemplateMap::value_type(prefix, NULL), LessWithPrefix()));
257 257
258 // Return vector of matching keywords. 258 // Return vector of matching keywords.
259 for (KeywordToTemplateMap::const_iterator i(match_range.first); 259 for (KeywordToTemplateMap::const_iterator i(match_range.first);
260 i != match_range.second; ++i) { 260 i != match_range.second; ++i) {
261 DCHECK(i->second->url()); 261 DCHECK(i->second->url());
262 if (!support_replacement_only || i->second->url()->SupportsReplacement()) 262 if (!support_replacement_only || i->second->url()->SupportsReplacement())
263 matches->push_back(i->first); 263 matches->push_back(i->first);
264 } 264 }
265 } 265 }
266 266
267 const TemplateURL* TemplateURLModel::GetTemplateURLForKeyword( 267 const TemplateURL* TemplateURLModel::GetTemplateURLForKeyword(
268 const std::wstring& keyword) const { 268 const string16& keyword) const {
269 KeywordToTemplateMap::const_iterator elem( 269 KeywordToTemplateMap::const_iterator elem(
270 keyword_to_template_map_.find(keyword)); 270 keyword_to_template_map_.find(keyword));
271 return (elem == keyword_to_template_map_.end()) ? NULL : elem->second; 271 return (elem == keyword_to_template_map_.end()) ? NULL : elem->second;
272 } 272 }
273 273
274 const TemplateURL* TemplateURLModel::GetTemplateURLForHost( 274 const TemplateURL* TemplateURLModel::GetTemplateURLForHost(
275 const std::string& host) const { 275 const std::string& host) const {
276 return provider_map_.GetTemplateURLForHost(host); 276 return provider_map_.GetTemplateURLForHost(host);
277 } 277 }
278 278
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 if (extension->omnibox_keyword().empty()) 313 if (extension->omnibox_keyword().empty())
314 return; 314 return;
315 315
316 Load(); 316 Load();
317 if (!loaded_) { 317 if (!loaded_) {
318 pending_extension_ids_.push_back(extension->id()); 318 pending_extension_ids_.push_back(extension->id());
319 return; 319 return;
320 } 320 }
321 321
322 const TemplateURL* existing_url = GetTemplateURLForExtension(extension); 322 const TemplateURL* existing_url = GetTemplateURLForExtension(extension);
323 std::wstring keyword = UTF8ToWide(extension->omnibox_keyword()); 323 string16 keyword = UTF8ToUTF16(extension->omnibox_keyword());
324 324
325 scoped_ptr<TemplateURL> template_url(new TemplateURL); 325 scoped_ptr<TemplateURL> template_url(new TemplateURL);
326 template_url->set_short_name(UTF8ToWide(extension->name())); 326 template_url->set_short_name(UTF8ToUTF16(extension->name()));
327 template_url->set_keyword(keyword); 327 template_url->set_keyword(keyword);
328 // This URL is not actually used for navigation. It holds the extension's 328 // This URL is not actually used for navigation. It holds the extension's
329 // ID, as well as forcing the TemplateURL to be treated as a search keyword. 329 // ID, as well as forcing the TemplateURL to be treated as a search keyword.
330 template_url->SetURL( 330 template_url->SetURL(
331 std::string(chrome::kExtensionScheme) + "://" + 331 std::string(chrome::kExtensionScheme) + "://" +
332 extension->id() + "/?q={searchTerms}", 0, 0); 332 extension->id() + "/?q={searchTerms}", 0, 0);
333 template_url->set_safe_for_autoreplace(false); 333 template_url->set_safe_for_autoreplace(false);
334 334
335 if (existing_url) { 335 if (existing_url) {
336 // TODO(mpcomplete): only replace if the user hasn't changed the keyword. 336 // TODO(mpcomplete): only replace if the user hasn't changed the keyword.
(...skipping 28 matching lines...) Expand all
365 365
366 void TemplateURLModel::IncrementUsageCount(const TemplateURL* url) { 366 void TemplateURLModel::IncrementUsageCount(const TemplateURL* url) {
367 DCHECK(url && find(template_urls_.begin(), template_urls_.end(), url) != 367 DCHECK(url && find(template_urls_.begin(), template_urls_.end(), url) !=
368 template_urls_.end()); 368 template_urls_.end());
369 const_cast<TemplateURL*>(url)->set_usage_count(url->usage_count() + 1); 369 const_cast<TemplateURL*>(url)->set_usage_count(url->usage_count() + 1);
370 if (service_.get()) 370 if (service_.get())
371 service_.get()->UpdateKeyword(*url); 371 service_.get()->UpdateKeyword(*url);
372 } 372 }
373 373
374 void TemplateURLModel::ResetTemplateURL(const TemplateURL* url, 374 void TemplateURLModel::ResetTemplateURL(const TemplateURL* url,
375 const std::wstring& title, 375 const string16& title,
376 const std::wstring& keyword, 376 const string16& keyword,
377 const std::string& search_url) { 377 const std::string& search_url) {
378 TemplateURL new_url(*url); 378 TemplateURL new_url(*url);
379 new_url.set_short_name(title); 379 new_url.set_short_name(title);
380 new_url.set_keyword(keyword); 380 new_url.set_keyword(keyword);
381 if ((new_url.url() && search_url.empty()) || 381 if ((new_url.url() && search_url.empty()) ||
382 (!new_url.url() && !search_url.empty()) || 382 (!new_url.url() && !search_url.empty()) ||
383 (new_url.url() && new_url.url()->url() != search_url)) { 383 (new_url.url() && new_url.url()->url() != search_url)) {
384 // The urls have changed, reset the favicon url. 384 // The urls have changed, reset the favicon url.
385 new_url.SetFavIconURL(GURL()); 385 new_url.SetFavIconURL(GURL());
386 new_url.SetURL(search_url, 0, 0); 386 new_url.SetURL(search_url, 0, 0);
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 UpdateKeywordSearchTermsForURL(visits_to_add_[i]); 531 UpdateKeywordSearchTermsForURL(visits_to_add_[i]);
532 visits_to_add_.clear(); 532 visits_to_add_.clear();
533 533
534 if (new_resource_keyword_version && service_.get()) 534 if (new_resource_keyword_version && service_.get())
535 service_->SetBuiltinKeywordVersion(new_resource_keyword_version); 535 service_->SetBuiltinKeywordVersion(new_resource_keyword_version);
536 536
537 NotifyObservers(); 537 NotifyObservers();
538 NotifyLoaded(); 538 NotifyLoaded();
539 } 539 }
540 540
541 std::wstring TemplateURLModel::GetKeywordShortName(const std::wstring& keyword, 541 string16 TemplateURLModel::GetKeywordShortName(const string16& keyword,
542 bool* is_extension_keyword) { 542 bool* is_extension_keyword) {
543 const TemplateURL* template_url = GetTemplateURLForKeyword(keyword); 543 const TemplateURL* template_url = GetTemplateURLForKeyword(keyword);
544 544
545 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLModel 545 // TODO(sky): Once LocationBarView adds a listener to the TemplateURLModel
546 // to track changes to the model, this should become a DCHECK. 546 // to track changes to the model, this should become a DCHECK.
547 if (template_url) { 547 if (template_url) {
548 *is_extension_keyword = template_url->IsExtensionKeyword(); 548 *is_extension_keyword = template_url->IsExtensionKeyword();
549 return template_url->AdjustedShortNameForLocaleDirection(); 549 return template_url->AdjustedShortNameForLocaleDirection();
550 } 550 }
551 *is_extension_keyword = false; 551 *is_extension_keyword = false;
552 return std::wstring(); 552 return string16();
553 } 553 }
554 554
555 void TemplateURLModel::Observe(NotificationType type, 555 void TemplateURLModel::Observe(NotificationType type,
556 const NotificationSource& source, 556 const NotificationSource& source,
557 const NotificationDetails& details) { 557 const NotificationDetails& details) {
558 if (type == NotificationType::HISTORY_URL_VISITED) { 558 if (type == NotificationType::HISTORY_URL_VISITED) {
559 Details<history::URLVisitedDetails> visit_details(details); 559 Details<history::URLVisitedDetails> visit_details(details);
560 if (!loaded()) 560 if (!loaded())
561 visits_to_add_.push_back(*visit_details.ptr()); 561 visits_to_add_.push_back(*visit_details.ptr());
562 else 562 else
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 prefs->RegisterStringPref( 595 prefs->RegisterStringPref(
596 prefs::kDefaultSearchProviderKeyword, std::string()); 596 prefs::kDefaultSearchProviderKeyword, std::string());
597 prefs->RegisterStringPref( 597 prefs->RegisterStringPref(
598 prefs::kDefaultSearchProviderIconURL, std::string()); 598 prefs::kDefaultSearchProviderIconURL, std::string());
599 prefs->RegisterStringPref( 599 prefs->RegisterStringPref(
600 prefs::kDefaultSearchProviderEncodings, std::string()); 600 prefs::kDefaultSearchProviderEncodings, std::string());
601 } 601 }
602 602
603 void TemplateURLModel::SetKeywordSearchTermsForURL(const TemplateURL* t_url, 603 void TemplateURLModel::SetKeywordSearchTermsForURL(const TemplateURL* t_url,
604 const GURL& url, 604 const GURL& url,
605 const std::wstring& term) { 605 const string16& term) {
606 HistoryService* history = profile_ ? 606 HistoryService* history = profile_ ?
607 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL; 607 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL;
608 if (!history) 608 if (!history)
609 return; 609 return;
610 history->SetKeywordSearchTermsForURL(url, t_url->id(), WideToUTF16Hack(term)); 610 history->SetKeywordSearchTermsForURL(url, t_url->id(), term);
611 } 611 }
612 612
613 void TemplateURLModel::Init(const Initializer* initializers, 613 void TemplateURLModel::Init(const Initializer* initializers,
614 int num_initializers) { 614 int num_initializers) {
615 // Register for notifications. 615 // Register for notifications.
616 if (profile_) { 616 if (profile_) {
617 // TODO(sky): bug 1166191. The keywords should be moved into the history 617 // TODO(sky): bug 1166191. The keywords should be moved into the history
618 // db, which will mean we no longer need this notification and the history 618 // db, which will mean we no longer need this notification and the history
619 // backend can handle automatically adding the search terms as the user 619 // backend can handle automatically adding the search terms as the user
620 // navigates. 620 // navigates.
(...skipping 12 matching lines...) Expand all
633 ChangeToLoadedState(); 633 ChangeToLoadedState();
634 634
635 // Add specific initializers, if any. 635 // Add specific initializers, if any.
636 for (int i(0); i < num_initializers; ++i) { 636 for (int i(0); i < num_initializers; ++i) {
637 DCHECK(initializers[i].keyword); 637 DCHECK(initializers[i].keyword);
638 DCHECK(initializers[i].url); 638 DCHECK(initializers[i].url);
639 DCHECK(initializers[i].content); 639 DCHECK(initializers[i].content);
640 640
641 size_t template_position = 641 size_t template_position =
642 std::string(initializers[i].url).find(kTemplateParameter); 642 std::string(initializers[i].url).find(kTemplateParameter);
643 DCHECK(template_position != std::wstring::npos); 643 DCHECK(template_position != std::string::npos);
644 std::string osd_url(initializers[i].url); 644 std::string osd_url(initializers[i].url);
645 osd_url.replace(template_position, arraysize(kTemplateParameter) - 1, 645 osd_url.replace(template_position, arraysize(kTemplateParameter) - 1,
646 kSearchTermParameter); 646 kSearchTermParameter);
647 647
648 // TemplateURLModel ends up owning the TemplateURL, don't try and free it. 648 // TemplateURLModel ends up owning the TemplateURL, don't try and free it.
649 TemplateURL* template_url = new TemplateURL(); 649 TemplateURL* template_url = new TemplateURL();
650 template_url->set_keyword(initializers[i].keyword); 650 template_url->set_keyword(UTF8ToUTF16(initializers[i].keyword));
651 template_url->set_short_name(initializers[i].content); 651 template_url->set_short_name(UTF8ToUTF16(initializers[i].content));
652 template_url->SetURL(osd_url, 0, 0); 652 template_url->SetURL(osd_url, 0, 0);
653 AddNoNotify(template_url); 653 AddNoNotify(template_url);
654 } 654 }
655 } 655 }
656 656
657 // Initialize default search. 657 // Initialize default search.
658 UpdateDefaultSearch(); 658 UpdateDefaultSearch();
659 659
660 // Request a server check for the correct Google URL if Google is the 660 // Request a server check for the correct Google URL if Google is the
661 // default search engine, not in headless mode and not in Chrome Frame. 661 // default search engine, not in headless mode and not in Chrome Frame.
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
770 if (t_url->url()) 770 if (t_url->url())
771 search_url = t_url->url()->url(); 771 search_url = t_url->url()->url();
772 if (t_url->suggestions_url()) 772 if (t_url->suggestions_url())
773 suggest_url = t_url->suggestions_url()->url(); 773 suggest_url = t_url->suggestions_url()->url();
774 if (t_url->instant_url()) 774 if (t_url->instant_url())
775 instant_url = t_url->instant_url()->url(); 775 instant_url = t_url->instant_url()->url();
776 GURL icon_gurl = t_url->GetFavIconURL(); 776 GURL icon_gurl = t_url->GetFavIconURL();
777 if (!icon_gurl.is_empty()) 777 if (!icon_gurl.is_empty())
778 icon_url = icon_gurl.spec(); 778 icon_url = icon_gurl.spec();
779 encodings = JoinString(t_url->input_encodings(), ';'); 779 encodings = JoinString(t_url->input_encodings(), ';');
780 short_name = WideToUTF8(t_url->short_name()); 780 short_name = UTF16ToUTF8(t_url->short_name());
781 keyword = WideToUTF8(t_url->keyword()); 781 keyword = UTF16ToUTF8(t_url->keyword());
782 id_string = base::Int64ToString(t_url->id()); 782 id_string = base::Int64ToString(t_url->id());
783 prepopulate_id = base::Int64ToString(t_url->prepopulate_id()); 783 prepopulate_id = base::Int64ToString(t_url->prepopulate_id());
784 } 784 }
785 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled); 785 prefs->SetBoolean(prefs::kDefaultSearchProviderEnabled, enabled);
786 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url); 786 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url);
787 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url); 787 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url);
788 prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url); 788 prefs->SetString(prefs::kDefaultSearchProviderInstantURL, instant_url);
789 prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url); 789 prefs->SetString(prefs::kDefaultSearchProviderIconURL, icon_url);
790 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings); 790 prefs->SetString(prefs::kDefaultSearchProviderEncodings, encodings);
791 prefs->SetString(prefs::kDefaultSearchProviderName, short_name); 791 prefs->SetString(prefs::kDefaultSearchProviderName, short_name);
(...skipping 23 matching lines...) Expand all
815 prefs->GetString(prefs::kDefaultSearchProviderSearchURL); 815 prefs->GetString(prefs::kDefaultSearchProviderSearchURL);
816 std::string instant_url = 816 std::string instant_url =
817 prefs->GetString(prefs::kDefaultSearchProviderInstantURL); 817 prefs->GetString(prefs::kDefaultSearchProviderInstantURL);
818 818
819 if (!enabled || (suggest_url.empty() && search_url.empty())) { 819 if (!enabled || (suggest_url.empty() && search_url.empty())) {
820 // The user doesn't want a default search provider. 820 // The user doesn't want a default search provider.
821 default_provider->reset(NULL); 821 default_provider->reset(NULL);
822 return true; 822 return true;
823 } 823 }
824 824
825 std::wstring name = 825 string16 name =
826 UTF8ToWide(prefs->GetString(prefs::kDefaultSearchProviderName)); 826 UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderName));
827 std::wstring keyword = 827 string16 keyword =
828 UTF8ToWide(prefs->GetString(prefs::kDefaultSearchProviderKeyword)); 828 UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderKeyword));
829 std::string icon_url = 829 std::string icon_url =
830 prefs->GetString(prefs::kDefaultSearchProviderIconURL); 830 prefs->GetString(prefs::kDefaultSearchProviderIconURL);
831 std::string encodings = 831 std::string encodings =
832 prefs->GetString(prefs::kDefaultSearchProviderEncodings); 832 prefs->GetString(prefs::kDefaultSearchProviderEncodings);
833 std::string id_string = prefs->GetString(prefs::kDefaultSearchProviderID); 833 std::string id_string = prefs->GetString(prefs::kDefaultSearchProviderID);
834 std::string prepopulate_id = 834 std::string prepopulate_id =
835 prefs->GetString(prefs::kDefaultSearchProviderPrepopulateID); 835 prefs->GetString(prefs::kDefaultSearchProviderPrepopulateID);
836 836
837 default_provider->reset(new TemplateURL()); 837 default_provider->reset(new TemplateURL());
838 (*default_provider)->set_short_name(name); 838 (*default_provider)->set_short_name(name);
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
976 // KEYWORD_GENERATED visit for the KEYWORD so that the keyword typed 976 // KEYWORD_GENERATED visit for the KEYWORD so that the keyword typed
977 // count is boosted. 977 // count is boosted.
978 AddTabToSearchVisit(**i); 978 AddTabToSearchVisit(**i);
979 } 979 }
980 980
981 QueryTerms::iterator terms_iterator = 981 QueryTerms::iterator terms_iterator =
982 query_terms.find(search_ref->GetSearchTermKey()); 982 query_terms.find(search_ref->GetSearchTermKey());
983 if (terms_iterator != query_terms.end() && 983 if (terms_iterator != query_terms.end() &&
984 !terms_iterator->second.empty()) { 984 !terms_iterator->second.empty()) {
985 SetKeywordSearchTermsForURL( 985 SetKeywordSearchTermsForURL(
986 *i, row.url(), search_ref->SearchTermToWide(*(*i), 986 *i, row.url(), search_ref->SearchTermToString16(*(*i),
987 terms_iterator->second)); 987 terms_iterator->second));
988 } 988 }
989 } 989 }
990 } 990 }
991 } 991 }
992 992
993 void TemplateURLModel::AddTabToSearchVisit(const TemplateURL& t_url) { 993 void TemplateURLModel::AddTabToSearchVisit(const TemplateURL& t_url) {
994 // Only add visits for entries the user hasn't modified. If the user modified 994 // Only add visits for entries the user hasn't modified. If the user modified
995 // the entry the keyword may no longer correspond to the host name. It may be 995 // the entry the keyword may no longer correspond to the host name. It may be
996 // possible to do something more sophisticated here, but it's so rare as to 996 // possible to do something more sophisticated here, but it's so rare as to
997 // not be worth it. 997 // not be worth it.
998 if (!t_url.safe_for_autoreplace()) 998 if (!t_url.safe_for_autoreplace())
999 return; 999 return;
1000 1000
1001 if (!profile_) 1001 if (!profile_)
1002 return; 1002 return;
1003 1003
1004 HistoryService* history = 1004 HistoryService* history =
1005 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); 1005 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
1006 if (!history) 1006 if (!history)
1007 return; 1007 return;
1008 1008
1009 GURL url(URLFixerUpper::FixupURL(WideToUTF8(t_url.keyword()), std::string())); 1009 GURL url(URLFixerUpper::FixupURL(UTF16ToUTF8(t_url.keyword()),
1010 std::string()));
1010 if (!url.is_valid()) 1011 if (!url.is_valid())
1011 return; 1012 return;
1012 1013
1013 // Synthesize a visit for the keyword. This ensures the url for the keyword is 1014 // Synthesize a visit for the keyword. This ensures the url for the keyword is
1014 // autocompleted even if the user doesn't type the url in directly. 1015 // autocompleted even if the user doesn't type the url in directly.
1015 history->AddPage(url, NULL, 0, GURL(), 1016 history->AddPage(url, NULL, 0, GURL(),
1016 PageTransition::KEYWORD_GENERATED, 1017 PageTransition::KEYWORD_GENERATED,
1017 history::RedirectList(), history::SOURCE_BROWSED, false); 1018 history::RedirectList(), history::SOURCE_BROWSED, false);
1018 } 1019 }
1019 1020
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
1251 delete template_url; 1252 delete template_url;
1252 } 1253 }
1253 1254
1254 void TemplateURLModel::NotifyObservers() { 1255 void TemplateURLModel::NotifyObservers() {
1255 if (!loaded_) 1256 if (!loaded_)
1256 return; 1257 return;
1257 1258
1258 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, 1259 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_,
1259 OnTemplateURLModelChanged()); 1260 OnTemplateURLModelChanged());
1260 } 1261 }
OLDNEW
« no previous file with comments | « chrome/browser/search_engines/template_url_model.h ('k') | chrome/browser/search_engines/template_url_model_test_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698