| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/template_url_model.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/string_util.h" | |
| 11 #include "chrome/app/locales/locale_settings.h" | |
| 12 #include "chrome/browser/browser_process.h" | |
| 13 #include "chrome/browser/google_url_tracker.h" | |
| 14 #include "chrome/browser/history/history.h" | |
| 15 #include "chrome/browser/profile.h" | |
| 16 #include "chrome/browser/rlz/rlz.h" | |
| 17 #include "chrome/browser/template_url.h" | |
| 18 #include "chrome/browser/template_url_prepopulate_data.h" | |
| 19 #include "chrome/common/l10n_util.h" | |
| 20 #include "chrome/common/pref_names.h" | |
| 21 #include "chrome/common/pref_service.h" | |
| 22 #include "chrome/common/stl_util-inl.h" | |
| 23 #include "googleurl/src/gurl.h" | |
| 24 #include "googleurl/src/url_parse.h" | |
| 25 #include "net/base/net_util.h" | |
| 26 #include "unicode/rbbi.h" | |
| 27 #include "unicode/uchar.h" | |
| 28 | |
| 29 using base::Time; | |
| 30 | |
| 31 // String in the URL that is replaced by the search term. | |
| 32 static const wchar_t kSearchTermParameter[] = L"{searchTerms}"; | |
| 33 | |
| 34 // String in Initializer that is replaced with kSearchTermParameter. | |
| 35 static const wchar_t kTemplateParameter[] = L"%s"; | |
| 36 | |
| 37 // Term used when generating a search url. Use something obscure so that on | |
| 38 // the rare case the term replaces the URL it's unlikely another keyword would | |
| 39 // have the same url. | |
| 40 static const wchar_t kReplacementTerm[] = L"blah.blah.blah.blah.blah"; | |
| 41 | |
| 42 class TemplateURLModel::LessWithPrefix { | |
| 43 public: | |
| 44 // We want to find the set of keywords that begin with a prefix. The STL | |
| 45 // algorithms will return the set of elements that are "equal to" the | |
| 46 // prefix, where "equal(x, y)" means "!(cmp(x, y) || cmp(y, x))". When | |
| 47 // cmp() is the typical std::less<>, this results in lexicographic equality; | |
| 48 // we need to extend this to mark a prefix as "not less than" a keyword it | |
| 49 // begins, which will cause the desired elements to be considered "equal to" | |
| 50 // the prefix. Note: this is still a strict weak ordering, as required by | |
| 51 // equal_range() (though I will not prove that here). | |
| 52 // | |
| 53 // Unfortunately the calling convention is not "prefix and element" but | |
| 54 // rather "two elements", so we pass the prefix as a fake "element" which has | |
| 55 // a NULL KeywordDataElement pointer. | |
| 56 bool operator()(const KeywordToTemplateMap::value_type& elem1, | |
| 57 const KeywordToTemplateMap::value_type& elem2) const { | |
| 58 return (elem1.second == NULL) ? | |
| 59 (elem2.first.compare(0, elem1.first.length(), elem1.first) > 0) : | |
| 60 (elem1.first < elem2.first); | |
| 61 } | |
| 62 }; | |
| 63 | |
| 64 TemplateURLModel::TemplateURLModel(Profile* profile) | |
| 65 : profile_(profile), | |
| 66 loaded_(false), | |
| 67 load_handle_(0), | |
| 68 default_search_provider_(NULL), | |
| 69 next_id_(1) { | |
| 70 DCHECK(profile_); | |
| 71 Init(NULL, 0); | |
| 72 } | |
| 73 | |
| 74 TemplateURLModel::TemplateURLModel(const Initializer* initializers, | |
| 75 const int count) | |
| 76 : profile_(NULL), | |
| 77 loaded_(true), | |
| 78 load_handle_(0), | |
| 79 service_(NULL), | |
| 80 default_search_provider_(NULL), | |
| 81 next_id_(1) { | |
| 82 Init(initializers, count); | |
| 83 } | |
| 84 | |
| 85 TemplateURLModel::~TemplateURLModel() { | |
| 86 if (load_handle_) { | |
| 87 DCHECK(service_.get()); | |
| 88 service_->CancelRequest(load_handle_); | |
| 89 } | |
| 90 | |
| 91 STLDeleteElements(&template_urls_); | |
| 92 | |
| 93 NotificationService* ns = NotificationService::current(); | |
| 94 if (profile_) { | |
| 95 ns->RemoveObserver(this, NOTIFY_HISTORY_URL_VISITED, | |
| 96 Source<Profile>(profile_->GetOriginalProfile())); | |
| 97 } | |
| 98 ns->RemoveObserver(this, NOTIFY_GOOGLE_URL_UPDATED, | |
| 99 NotificationService::AllSources()); | |
| 100 } | |
| 101 | |
| 102 void TemplateURLModel::Init(const Initializer* initializers, | |
| 103 int num_initializers) { | |
| 104 // Register for notifications. | |
| 105 NotificationService* ns = NotificationService::current(); | |
| 106 if (profile_) { | |
| 107 // TODO(sky): bug 1166191. The keywords should be moved into the history | |
| 108 // db, which will mean we no longer need this notification and the history | |
| 109 // backend can handle automatically adding the search terms as the user | |
| 110 // navigates. | |
| 111 ns->AddObserver(this, NOTIFY_HISTORY_URL_VISITED, | |
| 112 Source<Profile>(profile_->GetOriginalProfile())); | |
| 113 } | |
| 114 ns->AddObserver(this, NOTIFY_GOOGLE_URL_UPDATED, | |
| 115 NotificationService::AllSources()); | |
| 116 | |
| 117 // Add specific initializers, if any. | |
| 118 for (int i(0); i < num_initializers; ++i) { | |
| 119 DCHECK(initializers[i].keyword); | |
| 120 DCHECK(initializers[i].url); | |
| 121 DCHECK(initializers[i].content); | |
| 122 | |
| 123 size_t template_position = | |
| 124 std::wstring(initializers[i].url).find(kTemplateParameter); | |
| 125 DCHECK(template_position != std::wstring::npos); | |
| 126 std::wstring osd_url(initializers[i].url); | |
| 127 osd_url.replace(template_position, arraysize(kTemplateParameter) - 1, | |
| 128 kSearchTermParameter); | |
| 129 | |
| 130 // TemplateURLModel ends up owning the TemplateURL, don't try and free it. | |
| 131 TemplateURL* template_url = new TemplateURL(); | |
| 132 template_url->set_keyword(initializers[i].keyword); | |
| 133 template_url->set_short_name(initializers[i].content); | |
| 134 template_url->SetURL(osd_url, 0, 0); | |
| 135 Add(template_url); | |
| 136 } | |
| 137 | |
| 138 // Request a server check for the correct Google URL if Google is the default | |
| 139 // search engine. | |
| 140 const TemplateURL* default_provider = GetDefaultSearchProvider(); | |
| 141 if (default_provider) { | |
| 142 const TemplateURLRef* default_provider_ref = default_provider->url(); | |
| 143 if (default_provider_ref && default_provider_ref->HasGoogleBaseURLs()) | |
| 144 GoogleURLTracker::RequestServerCheck(); | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 // static | |
| 149 std::wstring TemplateURLModel::GenerateKeyword(const GURL& url, | |
| 150 bool autodetected) { | |
| 151 // Don't autogenerate keywords for referrers that are the result of a form | |
| 152 // submission (TODO: right now we approximate this by checking for the URL | |
| 153 // having a query, but we should replace this with a call to WebCore to see if | |
| 154 // the originating page was actually a form submission), anything other than | |
| 155 // http, or referrers with a path. | |
| 156 // | |
| 157 // If we relax the path constraint, we need to be sure to sanitize the path | |
| 158 // elements and update AutocompletePopup to look for keywords using the path. | |
| 159 // See http://b/issue?id=863583. | |
| 160 if (!url.is_valid() || | |
| 161 (autodetected && (url.has_query() || (url.scheme() != "http") || | |
| 162 ((url.path() != "") && (url.path() != "/"))))) | |
| 163 return std::wstring(); | |
| 164 | |
| 165 // Strip "www." off the front of the keyword; otherwise the keyword won't work | |
| 166 // properly. See http://b/issue?id=1205573. | |
| 167 return net::StripWWW(UTF8ToWide(url.host())); | |
| 168 } | |
| 169 | |
| 170 // static | |
| 171 std::wstring TemplateURLModel::CleanUserInputKeyword( | |
| 172 const std::wstring& keyword) { | |
| 173 // Remove the scheme. | |
| 174 std::wstring result(l10n_util::ToLower(keyword)); | |
| 175 url_parse::Component scheme_component; | |
| 176 if (url_parse::ExtractScheme(WideToUTF8(keyword).c_str(), | |
| 177 static_cast<int>(keyword.length()), | |
| 178 &scheme_component)) { | |
| 179 // Include trailing ':'. | |
| 180 result.erase(0, scheme_component.end() + 1); | |
| 181 // Many schemes usually have "//" after them, so strip it too. | |
| 182 const std::wstring after_scheme(L"//"); | |
| 183 if (result.compare(0, after_scheme.length(), after_scheme) == 0) | |
| 184 result.erase(0, after_scheme.length()); | |
| 185 } | |
| 186 | |
| 187 // Remove leading "www.". | |
| 188 result = net::StripWWW(result); | |
| 189 | |
| 190 // Remove trailing "/". | |
| 191 return (result.length() > 0 && result[result.length() - 1] == L'/') ? | |
| 192 result.substr(0, result.length() - 1) : result; | |
| 193 } | |
| 194 | |
| 195 // static | |
| 196 GURL TemplateURLModel::GenerateSearchURL(const TemplateURL* t_url) { | |
| 197 DCHECK(t_url); | |
| 198 const TemplateURLRef* search_ref = t_url->url(); | |
| 199 if (!search_ref || !search_ref->IsValid()) | |
| 200 return GURL(); | |
| 201 | |
| 202 if (!search_ref->SupportsReplacement()) | |
| 203 return GURL(WideToUTF8(search_ref->url())); | |
| 204 | |
| 205 return search_ref->ReplaceSearchTerms( | |
| 206 *t_url, | |
| 207 kReplacementTerm, | |
| 208 TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, std::wstring()); | |
| 209 } | |
| 210 | |
| 211 bool TemplateURLModel::CanReplaceKeyword( | |
| 212 const std::wstring& keyword, | |
| 213 const std::wstring& url, | |
| 214 const TemplateURL** template_url_to_replace) { | |
| 215 DCHECK(!keyword.empty()); // This should only be called for non-empty | |
| 216 // keywords. If we need to support empty kewords | |
| 217 // the code needs to change slightly. | |
| 218 const TemplateURL* existing_url = GetTemplateURLForKeyword(keyword); | |
| 219 if (existing_url) { | |
| 220 // We already have a TemplateURL for this keyword. Only allow it to be | |
| 221 // replaced if the TemplateURL can be replaced. | |
| 222 if (template_url_to_replace) | |
| 223 *template_url_to_replace = existing_url; | |
| 224 return CanReplace(existing_url); | |
| 225 } | |
| 226 | |
| 227 // We don't have a TemplateURL with keyword. Only allow a new one if there | |
| 228 // isn't a TemplateURL for the specified host, or there is one but it can | |
| 229 // be replaced. We do this to ensure that if the user assigns a different | |
| 230 // keyword to a generated TemplateURL, we won't regenerate another keyword for | |
| 231 // the same host. | |
| 232 GURL gurl(WideToUTF8(url)); | |
| 233 if (gurl.is_valid() && !gurl.host().empty()) | |
| 234 return CanReplaceKeywordForHost(gurl.host(), template_url_to_replace); | |
| 235 return true; | |
| 236 } | |
| 237 | |
| 238 void TemplateURLModel::FindMatchingKeywords( | |
| 239 const std::wstring& prefix, | |
| 240 bool support_replacement_only, | |
| 241 std::vector<std::wstring>* matches) const { | |
| 242 // Sanity check args. | |
| 243 if (prefix.empty()) | |
| 244 return; | |
| 245 DCHECK(matches != NULL); | |
| 246 DCHECK(matches->empty()); // The code for exact matches assumes this. | |
| 247 | |
| 248 // Find matching keyword range. Searches the element map for keywords | |
| 249 // beginning with |prefix| and stores the endpoints of the resulting set in | |
| 250 // |match_range|. | |
| 251 const std::pair<KeywordToTemplateMap::const_iterator, | |
| 252 KeywordToTemplateMap::const_iterator> match_range( | |
| 253 std::equal_range( | |
| 254 keyword_to_template_map_.begin(), keyword_to_template_map_.end(), | |
| 255 KeywordToTemplateMap::value_type(prefix, NULL), LessWithPrefix())); | |
| 256 | |
| 257 // Return vector of matching keywords. | |
| 258 for (KeywordToTemplateMap::const_iterator i(match_range.first); | |
| 259 i != match_range.second; ++i) { | |
| 260 DCHECK(i->second->url()); | |
| 261 if (!support_replacement_only || i->second->url()->SupportsReplacement()) | |
| 262 matches->push_back(i->first); | |
| 263 } | |
| 264 } | |
| 265 | |
| 266 const TemplateURL* TemplateURLModel::GetTemplateURLForKeyword( | |
| 267 const std::wstring& keyword) const { | |
| 268 KeywordToTemplateMap::const_iterator elem( | |
| 269 keyword_to_template_map_.find(keyword)); | |
| 270 return (elem == keyword_to_template_map_.end()) ? NULL : elem->second; | |
| 271 } | |
| 272 | |
| 273 const TemplateURL* TemplateURLModel::GetTemplateURLForHost( | |
| 274 const std::string& host) const { | |
| 275 HostToURLsMap::const_iterator iter = host_to_urls_map_.find(host); | |
| 276 if (iter == host_to_urls_map_.end() || iter->second.empty()) | |
| 277 return NULL; | |
| 278 return *(iter->second.begin()); // Return the 1st element. | |
| 279 } | |
| 280 | |
| 281 void TemplateURLModel::Add(TemplateURL* template_url) { | |
| 282 DCHECK(template_url); | |
| 283 DCHECK(template_url->id() == 0); | |
| 284 DCHECK(find(template_urls_.begin(), template_urls_.end(), template_url) == | |
| 285 template_urls_.end()); | |
| 286 template_url->set_id(++next_id_); | |
| 287 template_urls_.push_back(template_url); | |
| 288 AddToMaps(template_url); | |
| 289 | |
| 290 if (service_.get()) | |
| 291 service_->AddKeyword(*template_url); | |
| 292 | |
| 293 if (loaded_) { | |
| 294 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, | |
| 295 OnTemplateURLModelChanged()); | |
| 296 } | |
| 297 } | |
| 298 | |
| 299 void TemplateURLModel::AddToMaps(const TemplateURL* template_url) { | |
| 300 if (!template_url->keyword().empty()) | |
| 301 keyword_to_template_map_[template_url->keyword()] = template_url; | |
| 302 | |
| 303 const GURL url(GenerateSearchURL(template_url)); | |
| 304 if (url.is_valid() && url.has_host()) | |
| 305 host_to_urls_map_[url.host()].insert(template_url); | |
| 306 } | |
| 307 | |
| 308 void TemplateURLModel::Remove(const TemplateURL* template_url) { | |
| 309 TemplateURLVector::iterator i = find(template_urls_.begin(), | |
| 310 template_urls_.end(), | |
| 311 template_url); | |
| 312 if (i == template_urls_.end()) | |
| 313 return; | |
| 314 | |
| 315 if (template_url == default_search_provider_) { | |
| 316 // Should never delete the default search provider. | |
| 317 NOTREACHED(); | |
| 318 return; | |
| 319 } | |
| 320 | |
| 321 RemoveFromMaps(template_url); | |
| 322 | |
| 323 // Remove it from the vector containing all TemplateURLs. | |
| 324 template_urls_.erase(i); | |
| 325 | |
| 326 if (loaded_) { | |
| 327 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, | |
| 328 OnTemplateURLModelChanged()); | |
| 329 } | |
| 330 | |
| 331 if (service_.get()) | |
| 332 service_->RemoveKeyword(*template_url); | |
| 333 | |
| 334 if (profile_) { | |
| 335 HistoryService* history = | |
| 336 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | |
| 337 if (history) | |
| 338 history->DeleteAllSearchTermsForKeyword(template_url->id()); | |
| 339 } | |
| 340 | |
| 341 // We own the TemplateURL and need to delete it. | |
| 342 delete template_url; | |
| 343 } | |
| 344 | |
| 345 void TemplateURLModel::Replace(const TemplateURL* existing_turl, | |
| 346 TemplateURL* new_turl) { | |
| 347 DCHECK(existing_turl && new_turl); | |
| 348 | |
| 349 TemplateURLVector::iterator i = find(template_urls_.begin(), | |
| 350 template_urls_.end(), | |
| 351 existing_turl); | |
| 352 DCHECK(i != template_urls_.end()); | |
| 353 RemoveFromMaps(existing_turl); | |
| 354 template_urls_.erase(i); | |
| 355 | |
| 356 new_turl->set_id(existing_turl->id()); | |
| 357 | |
| 358 template_urls_.push_back(new_turl); | |
| 359 AddToMaps(new_turl); | |
| 360 | |
| 361 if (service_.get()) | |
| 362 service_->UpdateKeyword(*new_turl); | |
| 363 | |
| 364 if (default_search_provider_ == existing_turl) | |
| 365 SetDefaultSearchProvider(new_turl); | |
| 366 | |
| 367 if (loaded_) { | |
| 368 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, | |
| 369 OnTemplateURLModelChanged()); | |
| 370 } | |
| 371 | |
| 372 delete existing_turl; | |
| 373 } | |
| 374 | |
| 375 void TemplateURLModel::RemoveAutoGeneratedBetween(Time created_after, | |
| 376 Time created_before) { | |
| 377 for (size_t i = 0; i < template_urls_.size();) { | |
| 378 if (template_urls_[i]->date_created() >= created_after && | |
| 379 (created_before.is_null() || | |
| 380 template_urls_[i]->date_created() < created_before) && | |
| 381 CanReplace(template_urls_[i])) { | |
| 382 Remove(template_urls_[i]); | |
| 383 } else { | |
| 384 ++i; | |
| 385 } | |
| 386 } | |
| 387 } | |
| 388 | |
| 389 void TemplateURLModel::RemoveAutoGeneratedSince(Time created_after) { | |
| 390 RemoveAutoGeneratedBetween(created_after, Time()); | |
| 391 } | |
| 392 | |
| 393 void TemplateURLModel::SetKeywordSearchTermsForURL(const TemplateURL* t_url, | |
| 394 const GURL& url, | |
| 395 const std::wstring& term) { | |
| 396 HistoryService* history = profile_ ? | |
| 397 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL; | |
| 398 if (!history) | |
| 399 return; | |
| 400 history->SetKeywordSearchTermsForURL(url, t_url->id(), term); | |
| 401 } | |
| 402 | |
| 403 void TemplateURLModel::RemoveFromMaps(const TemplateURL* template_url) { | |
| 404 if (!template_url->keyword().empty()) { | |
| 405 keyword_to_template_map_.erase(template_url->keyword()); | |
| 406 } | |
| 407 | |
| 408 const GURL url(GenerateSearchURL(template_url)); | |
| 409 if (url.is_valid() && url.has_host()) { | |
| 410 const std::string host(url.host()); | |
| 411 DCHECK(host_to_urls_map_.find(host) != host_to_urls_map_.end()); | |
| 412 TemplateURLSet& urls = host_to_urls_map_[host]; | |
| 413 DCHECK(urls.find(template_url) != urls.end()); | |
| 414 urls.erase(urls.find(template_url)); | |
| 415 if (urls.empty()) | |
| 416 host_to_urls_map_.erase(host_to_urls_map_.find(host)); | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 void TemplateURLModel::RemoveFromMapsByPointer( | |
| 421 const TemplateURL* template_url) { | |
| 422 DCHECK(template_url); | |
| 423 for (KeywordToTemplateMap::iterator i = keyword_to_template_map_.begin(); | |
| 424 i != keyword_to_template_map_.end(); ++i) { | |
| 425 if (i->second == template_url) { | |
| 426 keyword_to_template_map_.erase(i); | |
| 427 // A given TemplateURL only occurs once in the map. As soon as we find the | |
| 428 // entry, stop. | |
| 429 break; | |
| 430 } | |
| 431 } | |
| 432 | |
| 433 for (HostToURLsMap::iterator i = host_to_urls_map_.begin(); | |
| 434 i != host_to_urls_map_.end(); ++i) { | |
| 435 TemplateURLSet::iterator url_set_iterator = i->second.find(template_url); | |
| 436 if (url_set_iterator != i->second.end()) { | |
| 437 i->second.erase(url_set_iterator); | |
| 438 if (i->second.empty()) | |
| 439 host_to_urls_map_.erase(i); | |
| 440 // A given TemplateURL only occurs once in the map. As soon as we find the | |
| 441 // entry, stop. | |
| 442 return; | |
| 443 } | |
| 444 } | |
| 445 } | |
| 446 | |
| 447 void TemplateURLModel::SetTemplateURLs( | |
| 448 const std::vector<const TemplateURL*>& urls) { | |
| 449 DCHECK(template_urls_.empty()); // This should only be called on load, | |
| 450 // when we have no TemplateURLs. | |
| 451 | |
| 452 // Add mappings for the new items. | |
| 453 for (TemplateURLVector::const_iterator i = urls.begin(); i != urls.end(); | |
| 454 ++i) { | |
| 455 next_id_ = std::max(next_id_, (*i)->id()); | |
| 456 AddToMaps(*i); | |
| 457 } | |
| 458 | |
| 459 template_urls_ = urls; | |
| 460 } | |
| 461 | |
| 462 std::vector<const TemplateURL*> TemplateURLModel::GetTemplateURLs() const { | |
| 463 return template_urls_; | |
| 464 } | |
| 465 | |
| 466 void TemplateURLModel::IncrementUsageCount(const TemplateURL* url) { | |
| 467 DCHECK(url && find(template_urls_.begin(), template_urls_.end(), url) != | |
| 468 template_urls_.end()); | |
| 469 const_cast<TemplateURL*>(url)->set_usage_count(url->usage_count() + 1); | |
| 470 if (service_.get()) | |
| 471 service_.get()->UpdateKeyword(*url); | |
| 472 } | |
| 473 | |
| 474 void TemplateURLModel::ResetTemplateURL(const TemplateURL* url, | |
| 475 const std::wstring& title, | |
| 476 const std::wstring& keyword, | |
| 477 const std::wstring& search_url) { | |
| 478 DCHECK(url && find(template_urls_.begin(), template_urls_.end(), url) != | |
| 479 template_urls_.end()); | |
| 480 RemoveFromMaps(url); | |
| 481 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url); | |
| 482 modifiable_url->set_short_name(title); | |
| 483 modifiable_url->set_keyword(keyword); | |
| 484 if ((modifiable_url->url() && search_url.empty()) || | |
| 485 (!modifiable_url->url() && !search_url.empty()) || | |
| 486 (modifiable_url->url() && modifiable_url->url()->url() != search_url)) { | |
| 487 // The urls have changed, reset the favicon url. | |
| 488 modifiable_url->SetFavIconURL(GURL()); | |
| 489 modifiable_url->SetURL(search_url, 0, 0); | |
| 490 } | |
| 491 modifiable_url->set_safe_for_autoreplace(false); | |
| 492 AddToMaps(url); | |
| 493 if (service_.get()) | |
| 494 service_.get()->UpdateKeyword(*url); | |
| 495 | |
| 496 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, | |
| 497 OnTemplateURLModelChanged()); | |
| 498 } | |
| 499 | |
| 500 void TemplateURLModel::SetDefaultSearchProvider(const TemplateURL* url) { | |
| 501 if (default_search_provider_ == url) | |
| 502 return; | |
| 503 | |
| 504 DCHECK(!url || find(template_urls_.begin(), template_urls_.end(), url) != | |
| 505 template_urls_.end()); | |
| 506 default_search_provider_ = url; | |
| 507 | |
| 508 if (url) { | |
| 509 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url); | |
| 510 // Don't mark the url as edited, otherwise we won't be able to rev the | |
| 511 // templateurls we ship with. | |
| 512 modifiable_url->set_show_in_default_list(true); | |
| 513 if (service_.get()) | |
| 514 service_.get()->UpdateKeyword(*url); | |
| 515 | |
| 516 const TemplateURLRef* url_ref = url->url(); | |
| 517 if (url_ref && url_ref->HasGoogleBaseURLs()) { | |
| 518 GoogleURLTracker::RequestServerCheck(); | |
| 519 RLZTracker::RecordProductEvent(RLZTracker::CHROME, | |
| 520 RLZTracker::CHROME_OMNIBOX, | |
| 521 RLZTracker::SET_TO_GOOGLE); | |
| 522 } | |
| 523 } | |
| 524 | |
| 525 SaveDefaultSearchProviderToPrefs(url); | |
| 526 | |
| 527 if (service_.get()) | |
| 528 service_->SetDefaultSearchProvider(url); | |
| 529 | |
| 530 if (loaded_) { | |
| 531 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, | |
| 532 OnTemplateURLModelChanged()); | |
| 533 } | |
| 534 } | |
| 535 | |
| 536 const TemplateURL* TemplateURLModel::GetDefaultSearchProvider() { | |
| 537 if (loaded_) | |
| 538 return default_search_provider_; | |
| 539 | |
| 540 if (!prefs_default_search_provider_.get()) { | |
| 541 TemplateURL* default_from_prefs; | |
| 542 if (LoadDefaultSearchProviderFromPrefs(&default_from_prefs)) { | |
| 543 prefs_default_search_provider_.reset(default_from_prefs); | |
| 544 } else { | |
| 545 std::vector<TemplateURL*> loaded_urls; | |
| 546 size_t default_search_index; | |
| 547 TemplateURLPrepopulateData::GetPrepopulatedEngines(GetPrefs(), | |
| 548 &loaded_urls, | |
| 549 &default_search_index); | |
| 550 if (default_search_index < loaded_urls.size()) { | |
| 551 prefs_default_search_provider_.reset(loaded_urls[default_search_index]); | |
| 552 loaded_urls.erase(loaded_urls.begin() + default_search_index); | |
| 553 } | |
| 554 STLDeleteElements(&loaded_urls); | |
| 555 } | |
| 556 } | |
| 557 | |
| 558 return prefs_default_search_provider_.get(); | |
| 559 } | |
| 560 | |
| 561 void TemplateURLModel::AddObserver(TemplateURLModelObserver* observer) { | |
| 562 model_observers_.AddObserver(observer); | |
| 563 } | |
| 564 | |
| 565 void TemplateURLModel::RemoveObserver(TemplateURLModelObserver* observer) { | |
| 566 model_observers_.RemoveObserver(observer); | |
| 567 } | |
| 568 | |
| 569 void TemplateURLModel::Load() { | |
| 570 if (loaded_ || load_handle_) | |
| 571 return; | |
| 572 | |
| 573 if (!service_.get()) | |
| 574 service_ = profile_->GetWebDataService(Profile::EXPLICIT_ACCESS); | |
| 575 | |
| 576 if (service_.get()) { | |
| 577 load_handle_ = service_->GetKeywords(this); | |
| 578 } else { | |
| 579 loaded_ = true; | |
| 580 NotifyLoaded(); | |
| 581 } | |
| 582 } | |
| 583 | |
| 584 void TemplateURLModel::OnWebDataServiceRequestDone( | |
| 585 WebDataService::Handle h, | |
| 586 const WDTypedResult* result) { | |
| 587 // Reset the load_handle so that we don't try and cancel the load in | |
| 588 // the destructor. | |
| 589 load_handle_ = 0; | |
| 590 | |
| 591 if (!result) { | |
| 592 // Results are null if the database went away. | |
| 593 loaded_ = true; | |
| 594 NotifyLoaded(); | |
| 595 return; | |
| 596 } | |
| 597 | |
| 598 DCHECK(result->GetType() == KEYWORDS_RESULT); | |
| 599 | |
| 600 WDKeywordsResult keyword_result = reinterpret_cast< | |
| 601 const WDResult<WDKeywordsResult>*>(result)->GetValue(); | |
| 602 | |
| 603 // prefs_default_search_provider_ is only needed before we've finished | |
| 604 // loading. Now that we've loaded we can nuke it. | |
| 605 prefs_default_search_provider_.reset(); | |
| 606 | |
| 607 // Compiler won't implicitly convert std::vector<TemplateURL*> to | |
| 608 // std::vector<const TemplateURL*>, and reinterpret_cast is unsafe, | |
| 609 // so we just copy it. | |
| 610 std::vector<const TemplateURL*> template_urls(keyword_result.keywords.begin(), | |
| 611 keyword_result.keywords.end()); | |
| 612 | |
| 613 const int resource_keyword_version = | |
| 614 TemplateURLPrepopulateData::GetDataVersion(); | |
| 615 if (keyword_result.builtin_keyword_version != resource_keyword_version) { | |
| 616 // There should never be duplicate TemplateURLs. We had a bug such that | |
| 617 // duplicate TemplateURLs existed for one locale. As such we invoke | |
| 618 // RemoveDuplicatePrepopulateIDs to nuke the duplicates. | |
| 619 RemoveDuplicatePrepopulateIDs(&template_urls); | |
| 620 } | |
| 621 SetTemplateURLs(template_urls); | |
| 622 | |
| 623 if (keyword_result.default_search_provider_id) { | |
| 624 // See if we can find the default search provider. | |
| 625 for (TemplateURLVector::iterator i = template_urls_.begin(); | |
| 626 i != template_urls_.end(); ++i) { | |
| 627 if ((*i)->id() == keyword_result.default_search_provider_id) { | |
| 628 default_search_provider_ = *i; | |
| 629 break; | |
| 630 } | |
| 631 } | |
| 632 } | |
| 633 | |
| 634 if (keyword_result.builtin_keyword_version != resource_keyword_version) { | |
| 635 MergeEnginesFromPrepopulateData(); | |
| 636 service_->SetBuiltinKeywordVersion(resource_keyword_version); | |
| 637 } | |
| 638 | |
| 639 // Always save the default search provider to prefs. That way we don't have to | |
| 640 // worry about it being out of sync. | |
| 641 if (default_search_provider_) | |
| 642 SaveDefaultSearchProviderToPrefs(default_search_provider_); | |
| 643 | |
| 644 // Delete any hosts that were deleted before we finished loading. | |
| 645 for (std::vector<std::wstring>::iterator i = hosts_to_delete_.begin(); | |
| 646 i != hosts_to_delete_.end(); ++i) { | |
| 647 DeleteGeneratedKeywordsMatchingHost(*i); | |
| 648 } | |
| 649 hosts_to_delete_.clear(); | |
| 650 | |
| 651 // Index any visits that occurred before we finished loading. | |
| 652 for (size_t i = 0; i < visits_to_add_.size(); ++i) | |
| 653 UpdateKeywordSearchTermsForURL(visits_to_add_[i]); | |
| 654 visits_to_add_.clear(); | |
| 655 | |
| 656 loaded_ = true; | |
| 657 | |
| 658 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, | |
| 659 OnTemplateURLModelChanged()); | |
| 660 | |
| 661 NotifyLoaded(); | |
| 662 } | |
| 663 | |
| 664 void TemplateURLModel::RemoveDuplicatePrepopulateIDs( | |
| 665 std::vector<const TemplateURL*>* urls) { | |
| 666 std::set<int> ids; | |
| 667 for (std::vector<const TemplateURL*>::iterator i = urls->begin(); | |
| 668 i != urls->end(); ) { | |
| 669 int prepopulate_id = (*i)->prepopulate_id(); | |
| 670 if (prepopulate_id) { | |
| 671 if (ids.find(prepopulate_id) != ids.end()) { | |
| 672 if (service_.get()) | |
| 673 service_->RemoveKeyword(**i); | |
| 674 delete *i; | |
| 675 i = urls->erase(i); | |
| 676 } else { | |
| 677 ids.insert(prepopulate_id); | |
| 678 ++i; | |
| 679 } | |
| 680 } else { | |
| 681 ++i; | |
| 682 } | |
| 683 } | |
| 684 } | |
| 685 | |
| 686 void TemplateURLModel::Observe(NotificationType type, | |
| 687 const NotificationSource& source, | |
| 688 const NotificationDetails& details) { | |
| 689 if (type == NOTIFY_HISTORY_URL_VISITED) { | |
| 690 Details<history::URLVisitedDetails> visit_details(details); | |
| 691 | |
| 692 if (!loaded()) | |
| 693 visits_to_add_.push_back(visit_details->row); | |
| 694 else | |
| 695 UpdateKeywordSearchTermsForURL(visit_details->row); | |
| 696 } else if (type == NOTIFY_GOOGLE_URL_UPDATED) { | |
| 697 if (loaded_) | |
| 698 GoogleBaseURLChanged(); | |
| 699 } else { | |
| 700 NOTREACHED(); | |
| 701 } | |
| 702 } | |
| 703 | |
| 704 void TemplateURLModel::DeleteGeneratedKeywordsMatchingHost( | |
| 705 const std::wstring& host) { | |
| 706 const std::wstring host_slash = host + L"/"; | |
| 707 // Iterate backwards as we may end up removing multiple entries. | |
| 708 for (int i = static_cast<int>(template_urls_.size()) - 1; i >= 0; --i) { | |
| 709 if (CanReplace(template_urls_[i]) && | |
| 710 (template_urls_[i]->keyword() == host || | |
| 711 template_urls_[i]->keyword().compare(0, host_slash.length(), | |
| 712 host_slash) == 0)) { | |
| 713 Remove(template_urls_[i]); | |
| 714 } | |
| 715 } | |
| 716 } | |
| 717 | |
| 718 void TemplateURLModel::NotifyLoaded() { | |
| 719 NotificationService::current()-> | |
| 720 Notify(TEMPLATE_URL_MODEL_LOADED, Source<TemplateURLModel>(this), | |
| 721 NotificationService::NoDetails()); | |
| 722 } | |
| 723 | |
| 724 void TemplateURLModel::MergeEnginesFromPrepopulateData() { | |
| 725 // Build a map from prepopulate id to TemplateURL of existing urls. | |
| 726 std::map<int, const TemplateURL*> id_to_turl; | |
| 727 for (size_t i = 0; i < template_urls_.size(); ++i) { | |
| 728 if (template_urls_[i]->prepopulate_id() > 0) | |
| 729 id_to_turl[template_urls_[i]->prepopulate_id()] = template_urls_[i]; | |
| 730 } | |
| 731 | |
| 732 std::vector<TemplateURL*> loaded_urls; | |
| 733 size_t default_search_index; | |
| 734 TemplateURLPrepopulateData::GetPrepopulatedEngines(GetPrefs(), | |
| 735 &loaded_urls, | |
| 736 &default_search_index); | |
| 737 | |
| 738 for (size_t i = 0; i < loaded_urls.size(); ++i) { | |
| 739 scoped_ptr<TemplateURL> t_url(loaded_urls[i]); | |
| 740 | |
| 741 if (!t_url->prepopulate_id()) { | |
| 742 // Prepopulate engines need an id. | |
| 743 NOTREACHED(); | |
| 744 continue; | |
| 745 } | |
| 746 | |
| 747 const TemplateURL* existing_url = id_to_turl[t_url->prepopulate_id()]; | |
| 748 if (existing_url) { | |
| 749 if (!existing_url->safe_for_autoreplace()) { | |
| 750 // User edited the entry, preserve the keyword and description. | |
| 751 loaded_urls[i]->set_safe_for_autoreplace(false); | |
| 752 loaded_urls[i]->set_keyword(existing_url->keyword()); | |
| 753 loaded_urls[i]->set_autogenerate_keyword( | |
| 754 existing_url->autogenerate_keyword()); | |
| 755 loaded_urls[i]->set_short_name(existing_url->short_name()); | |
| 756 } | |
| 757 Replace(existing_url, loaded_urls[i]); | |
| 758 id_to_turl[t_url->prepopulate_id()] = loaded_urls[i]; | |
| 759 } else { | |
| 760 Add(loaded_urls[i]); | |
| 761 } | |
| 762 if (i == default_search_index && !default_search_provider_) | |
| 763 SetDefaultSearchProvider(loaded_urls[i]); | |
| 764 | |
| 765 t_url.release(); | |
| 766 } | |
| 767 } | |
| 768 | |
| 769 void TemplateURLModel::SaveDefaultSearchProviderToPrefs( | |
| 770 const TemplateURL* t_url) { | |
| 771 PrefService* prefs = GetPrefs(); | |
| 772 if (!prefs) | |
| 773 return; | |
| 774 | |
| 775 RegisterPrefs(prefs); | |
| 776 | |
| 777 const std::wstring search_url = | |
| 778 (t_url && t_url->url()) ? t_url->url()->url() : std::wstring(); | |
| 779 prefs->SetString(prefs::kDefaultSearchProviderSearchURL, search_url); | |
| 780 | |
| 781 const std::wstring suggest_url = | |
| 782 (t_url && t_url->suggestions_url()) ? t_url->suggestions_url()->url() : | |
| 783 std::wstring(); | |
| 784 prefs->SetString(prefs::kDefaultSearchProviderSuggestURL, suggest_url); | |
| 785 | |
| 786 const std::wstring name = | |
| 787 t_url ? t_url->short_name() : std::wstring(); | |
| 788 prefs->SetString(prefs::kDefaultSearchProviderName, name); | |
| 789 | |
| 790 const std::wstring id_string = | |
| 791 t_url ? Int64ToWString(t_url->id()) : std::wstring(); | |
| 792 prefs->SetString(prefs::kDefaultSearchProviderID, id_string); | |
| 793 | |
| 794 prefs->ScheduleSavePersistentPrefs(g_browser_process->file_thread()); | |
| 795 } | |
| 796 | |
| 797 bool TemplateURLModel::LoadDefaultSearchProviderFromPrefs( | |
| 798 TemplateURL** default_provider) { | |
| 799 PrefService* prefs = GetPrefs(); | |
| 800 if (!prefs || !prefs->HasPrefPath(prefs::kDefaultSearchProviderSearchURL) || | |
| 801 !prefs->HasPrefPath(prefs::kDefaultSearchProviderSuggestURL) || | |
| 802 !prefs->HasPrefPath(prefs::kDefaultSearchProviderName) || | |
| 803 !prefs->HasPrefPath(prefs::kDefaultSearchProviderID)) { | |
| 804 return false; | |
| 805 } | |
| 806 RegisterPrefs(prefs); | |
| 807 | |
| 808 std::wstring suggest_url = | |
| 809 prefs->GetString(prefs::kDefaultSearchProviderSuggestURL); | |
| 810 std::wstring search_url = | |
| 811 prefs->GetString(prefs::kDefaultSearchProviderSearchURL); | |
| 812 | |
| 813 if (suggest_url.empty() && search_url.empty()) { | |
| 814 // The user doesn't want a default search provider. | |
| 815 *default_provider = NULL; | |
| 816 return true; | |
| 817 } | |
| 818 | |
| 819 std::wstring name = prefs->GetString(prefs::kDefaultSearchProviderName); | |
| 820 | |
| 821 std::wstring id_string = prefs->GetString(prefs::kDefaultSearchProviderID); | |
| 822 | |
| 823 *default_provider = new TemplateURL(); | |
| 824 (*default_provider)->set_short_name(name); | |
| 825 (*default_provider)->SetURL(search_url, 0, 0); | |
| 826 (*default_provider)->SetSuggestionsURL(suggest_url, 0, 0); | |
| 827 if (!id_string.empty()) | |
| 828 (*default_provider)->set_id(StringToInt64(id_string)); | |
| 829 return true; | |
| 830 } | |
| 831 | |
| 832 void TemplateURLModel::RegisterPrefs(PrefService* prefs) { | |
| 833 if (prefs->IsPrefRegistered(prefs::kDefaultSearchProviderName)) | |
| 834 return; | |
| 835 prefs->RegisterStringPref( | |
| 836 prefs::kDefaultSearchProviderName, std::wstring()); | |
| 837 prefs->RegisterStringPref( | |
| 838 prefs::kDefaultSearchProviderID, std::wstring()); | |
| 839 prefs->RegisterStringPref( | |
| 840 prefs::kDefaultSearchProviderSuggestURL, std::wstring()); | |
| 841 prefs->RegisterStringPref( | |
| 842 prefs::kDefaultSearchProviderSearchURL, std::wstring()); | |
| 843 } | |
| 844 | |
| 845 bool TemplateURLModel::CanReplaceKeywordForHost( | |
| 846 const std::string& host, | |
| 847 const TemplateURL** to_replace) { | |
| 848 const HostToURLsMap::iterator matching_urls = host_to_urls_map_.find(host); | |
| 849 const bool have_matching_urls = (matching_urls != host_to_urls_map_.end()); | |
| 850 if (have_matching_urls) { | |
| 851 TemplateURLSet& urls = matching_urls->second; | |
| 852 for (TemplateURLSet::iterator i = urls.begin(); i != urls.end(); ++i) { | |
| 853 const TemplateURL* url = *i; | |
| 854 if (CanReplace(url)) { | |
| 855 if (to_replace) | |
| 856 *to_replace = url; | |
| 857 return true; | |
| 858 } | |
| 859 } | |
| 860 } | |
| 861 | |
| 862 if (to_replace) | |
| 863 *to_replace = NULL; | |
| 864 return !have_matching_urls; | |
| 865 } | |
| 866 | |
| 867 bool TemplateURLModel::CanReplace(const TemplateURL* t_url) { | |
| 868 return (t_url != default_search_provider_ && !t_url->show_in_default_list() && | |
| 869 t_url->safe_for_autoreplace()); | |
| 870 } | |
| 871 | |
| 872 PrefService* TemplateURLModel::GetPrefs() { | |
| 873 return profile_ ? profile_->GetPrefs() : NULL; | |
| 874 } | |
| 875 | |
| 876 void TemplateURLModel::UpdateKeywordSearchTermsForURL( | |
| 877 const history::URLRow& row) { | |
| 878 if (!row.url().is_valid() || | |
| 879 !row.url().parsed_for_possibly_invalid_spec().query.is_nonempty()) { | |
| 880 return; | |
| 881 } | |
| 882 | |
| 883 HostToURLsMap::const_iterator t_urls_for_host_iterator = | |
| 884 host_to_urls_map_.find(row.url().host()); | |
| 885 if (t_urls_for_host_iterator == host_to_urls_map_.end() || | |
| 886 t_urls_for_host_iterator->second.empty()) { | |
| 887 return; | |
| 888 } | |
| 889 | |
| 890 const TemplateURLSet& urls_for_host = t_urls_for_host_iterator->second; | |
| 891 QueryTerms query_terms; | |
| 892 bool built_terms = false; // Most URLs won't match a TemplateURLs host; | |
| 893 // so we lazily build the query_terms. | |
| 894 const std::string path = row.url().path(); | |
| 895 | |
| 896 for (TemplateURLSet::const_iterator i = urls_for_host.begin(); | |
| 897 i != urls_for_host.end(); ++i) { | |
| 898 const TemplateURLRef* search_ref = (*i)->url(); | |
| 899 | |
| 900 // Count the URL against a TemplateURL if the host and path of the | |
| 901 // visited URL match that of the TemplateURL as well as the search term's | |
| 902 // key of the TemplateURL occurring in the visited url. | |
| 903 // | |
| 904 // NOTE: Even though we're iterating over TemplateURLs indexed by the host | |
| 905 // of the URL we still need to call GetHost on the search_ref. In | |
| 906 // particular, GetHost returns an empty string if search_ref doesn't support | |
| 907 // replacement or isn't valid for use in keyword search terms. | |
| 908 | |
| 909 if (search_ref && search_ref->GetHost() == row.url().host() && | |
| 910 search_ref->GetPath() == path) { | |
| 911 if (!built_terms && !BuildQueryTerms(row.url(), &query_terms)) { | |
| 912 // No query terms. No need to continue with the rest of the | |
| 913 // TemplateURLs. | |
| 914 return; | |
| 915 } | |
| 916 built_terms = true; | |
| 917 | |
| 918 QueryTerms::iterator terms_iterator = | |
| 919 query_terms.find(search_ref->GetSearchTermKey()); | |
| 920 if (terms_iterator != query_terms.end() && | |
| 921 !terms_iterator->second.empty()) { | |
| 922 SetKeywordSearchTermsForURL( | |
| 923 *i, row.url(), search_ref->SearchTermToWide(*(*i), | |
| 924 terms_iterator->second)); | |
| 925 } | |
| 926 } | |
| 927 } | |
| 928 } | |
| 929 | |
| 930 // static | |
| 931 bool TemplateURLModel::BuildQueryTerms(const GURL& url, | |
| 932 QueryTerms* query_terms) { | |
| 933 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; | |
| 934 url_parse::Component key, value; | |
| 935 size_t valid_term_count = 0; | |
| 936 while (url_parse::ExtractQueryKeyValue(url.spec().c_str(), &query, &key, | |
| 937 &value)) { | |
| 938 if (key.is_nonempty() && value.is_nonempty()) { | |
| 939 std::string key_string = url.spec().substr(key.begin, key.len); | |
| 940 std::string value_string = url.spec().substr(value.begin, value.len); | |
| 941 QueryTerms::iterator query_terms_iterator = | |
| 942 query_terms->find(key_string); | |
| 943 if (query_terms_iterator != query_terms->end()) { | |
| 944 if (!query_terms_iterator->second.empty() && | |
| 945 query_terms_iterator->second != value_string) { | |
| 946 // The term occurs in multiple places with different values. Treat | |
| 947 // this as if the term doesn't occur by setting the value to an empty | |
| 948 // string. | |
| 949 (*query_terms)[key_string] = std::string(); | |
| 950 DCHECK (valid_term_count > 0); | |
| 951 valid_term_count--; | |
| 952 } | |
| 953 } else { | |
| 954 valid_term_count++; | |
| 955 (*query_terms)[key_string] = value_string; | |
| 956 } | |
| 957 } | |
| 958 } | |
| 959 return (valid_term_count > 0); | |
| 960 } | |
| 961 | |
| 962 void TemplateURLModel::GoogleBaseURLChanged() { | |
| 963 bool something_changed = false; | |
| 964 for (size_t i = 0; i < template_urls_.size(); ++i) { | |
| 965 const TemplateURL* t_url = template_urls_[i]; | |
| 966 if ((t_url->url() && t_url->url()->HasGoogleBaseURLs()) || | |
| 967 (t_url->suggestions_url() && | |
| 968 t_url->suggestions_url()->HasGoogleBaseURLs())) { | |
| 969 RemoveFromMapsByPointer(t_url); | |
| 970 t_url->InvalidateCachedValues(); | |
| 971 AddToMaps(t_url); | |
| 972 something_changed = true; | |
| 973 } | |
| 974 } | |
| 975 | |
| 976 if (something_changed && loaded_) { | |
| 977 FOR_EACH_OBSERVER(TemplateURLModelObserver, model_observers_, | |
| 978 OnTemplateURLModelChanged()); | |
| 979 } | |
| 980 } | |
| OLD | NEW |