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

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

Issue 10033017: More misc. cleanups to minimize future refactoring diffs. (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/template_url_service.h" 5 #include "chrome/browser/search_engines/template_url_service.h"
6 6
7 #include "base/auto_reset.h" 7 #include "base/auto_reset.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/i18n/case_conversion.h" 10 #include "base/i18n/case_conversion.h"
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 // We already have a TemplateURL for this keyword. Only allow it to be 247 // We already have a TemplateURL for this keyword. Only allow it to be
248 // replaced if the TemplateURL can be replaced. 248 // replaced if the TemplateURL can be replaced.
249 return CanReplace(existing_url); 249 return CanReplace(existing_url);
250 } 250 }
251 251
252 // We don't have a TemplateURL with keyword. Only allow a new one if there 252 // We don't have a TemplateURL with keyword. Only allow a new one if there
253 // isn't a TemplateURL for the specified host, or there is one but it can 253 // isn't a TemplateURL for the specified host, or there is one but it can
254 // be replaced. We do this to ensure that if the user assigns a different 254 // be replaced. We do this to ensure that if the user assigns a different
255 // keyword to a generated TemplateURL, we won't regenerate another keyword for 255 // keyword to a generated TemplateURL, we won't regenerate another keyword for
256 // the same host. 256 // the same host.
257 if (url.is_valid() && !url.host().empty()) 257 return !url.is_valid() || url.host().empty() ||
258 return CanReplaceKeywordForHost(url.host(), template_url_to_replace); 258 CanReplaceKeywordForHost(url.host(), template_url_to_replace);
259 return true;
260 } 259 }
261 260
262 void TemplateURLService::FindMatchingKeywords( 261 void TemplateURLService::FindMatchingKeywords(
263 const string16& prefix, 262 const string16& prefix,
264 bool support_replacement_only, 263 bool support_replacement_only,
265 std::vector<string16>* matches) const { 264 std::vector<string16>* matches) const {
266 // Sanity check args. 265 // Sanity check args.
267 if (prefix.empty()) 266 if (prefix.empty())
268 return; 267 return;
269 DCHECK(matches != NULL); 268 DCHECK(matches != NULL);
(...skipping 18 matching lines...) Expand all
288 DCHECK(!i->second->url().empty()); 287 DCHECK(!i->second->url().empty());
289 if (!support_replacement_only || i->second->url_ref().SupportsReplacement()) 288 if (!support_replacement_only || i->second->url_ref().SupportsReplacement())
290 matches->push_back(i->first); 289 matches->push_back(i->first);
291 } 290 }
292 } 291 }
293 292
294 const TemplateURL* TemplateURLService::GetTemplateURLForKeyword( 293 const TemplateURL* TemplateURLService::GetTemplateURLForKeyword(
295 const string16& keyword) const { 294 const string16& keyword) const {
296 KeywordToTemplateMap::const_iterator elem( 295 KeywordToTemplateMap::const_iterator elem(
297 keyword_to_template_map_.find(keyword)); 296 keyword_to_template_map_.find(keyword));
298 return (elem == keyword_to_template_map_.end()) ? NULL : elem->second; 297 if (elem != keyword_to_template_map_.end())
298 return elem->second;
299 return (initial_default_search_provider_.get() &&
300 (initial_default_search_provider_->keyword() == keyword)) ?
301 initial_default_search_provider_.get() : NULL;
299 } 302 }
300 303
301 const TemplateURL* TemplateURLService::GetTemplateURLForGUID( 304 const TemplateURL* TemplateURLService::GetTemplateURLForGUID(
302 const std::string& sync_guid) const { 305 const std::string& sync_guid) const {
303 GUIDToTemplateMap::const_iterator elem( 306 GUIDToTemplateMap::const_iterator elem(
304 guid_to_template_map_.find(sync_guid)); 307 guid_to_template_map_.find(sync_guid));
305 return (elem == guid_to_template_map_.end()) ? NULL : elem->second; 308 if (elem != guid_to_template_map_.end())
309 return elem->second;
310 return (initial_default_search_provider_.get() &&
311 (initial_default_search_provider_->sync_guid() == sync_guid)) ?
312 initial_default_search_provider_.get() : NULL;
306 } 313 }
307 314
308 const TemplateURL* TemplateURLService::GetTemplateURLForHost( 315 const TemplateURL* TemplateURLService::GetTemplateURLForHost(
309 const std::string& host) const { 316 const std::string& host) const {
310 return provider_map_.GetTemplateURLForHost(host); 317 const TemplateURL* t_url = provider_map_.GetTemplateURLForHost(host);
318 if (t_url)
319 return t_url;
320 return (initial_default_search_provider_.get() &&
321 (GenerateSearchURL(initial_default_search_provider_.get()).host() ==
322 host)) ? initial_default_search_provider_.get() : NULL;
311 } 323 }
312 324
313 void TemplateURLService::Add(TemplateURL* template_url) { 325 void TemplateURLService::Add(TemplateURL* template_url) {
314 AddNoNotify(template_url); 326 AddNoNotify(template_url, true);
315 NotifyObservers(); 327 NotifyObservers();
316 } 328 }
317 329
318 void TemplateURLService::AddWithOverrides(const TemplateURL* template_url, 330 void TemplateURLService::AddWithOverrides(const TemplateURL* template_url,
319 const string16& short_name, 331 const string16& short_name,
320 const string16& keyword, 332 const string16& keyword,
321 const std::string& url) { 333 const std::string& url) {
322 TemplateURL* modifiable_url = const_cast<TemplateURL*>(template_url); 334 TemplateURL* modifiable_url = const_cast<TemplateURL*>(template_url);
323 modifiable_url->data_.short_name = short_name; 335 modifiable_url->data_.short_name = short_name;
324 modifiable_url->data_.SetKeyword(keyword); 336 modifiable_url->data_.SetKeyword(keyword);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 // TODO(mpcomplete): disable the keyword when the extension is disabled. 380 // TODO(mpcomplete): disable the keyword when the extension is disabled.
369 if (extension->omnibox_keyword().empty()) 381 if (extension->omnibox_keyword().empty())
370 return; 382 return;
371 383
372 Load(); 384 Load();
373 if (!loaded_) { 385 if (!loaded_) {
374 pending_extension_ids_.push_back(extension->id()); 386 pending_extension_ids_.push_back(extension->id());
375 return; 387 return;
376 } 388 }
377 389
378 const TemplateURL* existing_url = GetTemplateURLForExtension(extension); 390 if (!GetTemplateURLForExtension(extension)) {
379 string16 keyword = UTF8ToUTF16(extension->omnibox_keyword()); 391 TemplateURLData data;
380 392 data.short_name = UTF8ToUTF16(extension->name());
381 TemplateURLData data; 393 data.SetKeyword(UTF8ToUTF16(extension->omnibox_keyword()));
382 data.short_name = UTF8ToUTF16(extension->name()); 394 // This URL is not actually used for navigation. It holds the extension's
383 data.SetKeyword(UTF8ToUTF16(extension->omnibox_keyword())); 395 // ID, as well as forcing the TemplateURL to be treated as a search keyword.
384 // This URL is not actually used for navigation. It holds the extension's 396 data.SetURL(std::string(chrome::kExtensionScheme) + "://" +
385 // ID, as well as forcing the TemplateURL to be treated as a search keyword. 397 extension->id() + "/?q={searchTerms}");
386 data.SetURL(std::string(chrome::kExtensionScheme) + "://" + extension->id() + 398 Add(new TemplateURL(data));
387 "/?q={searchTerms}");
388 scoped_ptr<TemplateURL> template_url(new TemplateURL(data));
389
390 if (existing_url) {
391 // TODO(mpcomplete): only replace if the user hasn't changed the keyword.
392 // (We don't have UI for that yet).
393 UpdateNoNotify(existing_url, *template_url);
394 } else {
395 AddNoNotify(template_url.release());
396 } 399 }
397 NotifyObservers();
398 } 400 }
399 401
400 void TemplateURLService::UnregisterExtensionKeyword( 402 void TemplateURLService::UnregisterExtensionKeyword(
401 const Extension* extension) { 403 const Extension* extension) {
402 const TemplateURL* url = GetTemplateURLForExtension(extension); 404 if (loaded_) {
403 if (url) 405 const TemplateURL* url = GetTemplateURLForExtension(extension);
404 Remove(url); 406 if (url)
407 Remove(url);
408 } else {
409 PendingExtensionIDs::iterator i = std::find(pending_extension_ids_.begin(),
410 pending_extension_ids_.end(), extension->id());
411 if (i != pending_extension_ids_.end())
412 pending_extension_ids_.erase(i);
413 }
405 } 414 }
406 415
407 const TemplateURL* TemplateURLService::GetTemplateURLForExtension( 416 const TemplateURL* TemplateURLService::GetTemplateURLForExtension(
408 const Extension* extension) const { 417 const Extension* extension) const {
409 for (TemplateURLVector::const_iterator i = template_urls_.begin(); 418 for (TemplateURLVector::const_iterator i = template_urls_.begin();
410 i != template_urls_.end(); ++i) { 419 i != template_urls_.end(); ++i) {
411 if ((*i)->IsExtensionKeyword() && 420 if ((*i)->IsExtensionKeyword() &&
412 ((*i)->url_ref().GetHost() == extension->id())) 421 ((*i)->url_ref().GetHost() == extension->id()))
413 return *i; 422 return *i;
414 } 423 }
415 424
416 return NULL; 425 return NULL;
417 } 426 }
418 427
419 std::vector<const TemplateURL*> TemplateURLService::GetTemplateURLs() const { 428 TemplateURLService::TemplateURLVector
429 TemplateURLService::GetTemplateURLs() const {
420 return template_urls_; 430 return template_urls_;
421 } 431 }
422 432
423 void TemplateURLService::IncrementUsageCount(const TemplateURL* url) { 433 void TemplateURLService::IncrementUsageCount(const TemplateURL* url) {
424 DCHECK(url && std::find(template_urls_.begin(), template_urls_.end(), url) != 434 DCHECK(url && std::find(template_urls_.begin(), template_urls_.end(), url) !=
425 template_urls_.end()); 435 template_urls_.end());
426 ++const_cast<TemplateURL*>(url)->data_.usage_count; 436 ++const_cast<TemplateURL*>(url)->data_.usage_count;
427 if (service_.get()) 437 // Extension keywords are not persisted.
428 service_->UpdateKeyword(*url); 438 // TODO(mpcomplete): If we allow editing extension keywords, then those should
439 // be persisted to disk and synced.
440 if (service_.get() && !url->IsExtensionKeyword())
441 service_.get()->UpdateKeyword(*url);
429 } 442 }
430 443
431 void TemplateURLService::ResetTemplateURL(const TemplateURL* url, 444 void TemplateURLService::ResetTemplateURL(const TemplateURL* url,
432 const string16& title, 445 const string16& title,
433 const string16& keyword, 446 const string16& keyword,
434 const std::string& search_url) { 447 const std::string& search_url) {
435 TemplateURLData data(url->data()); 448 TemplateURLData data(url->data());
436 data.short_name = title; 449 data.short_name = title;
437 data.SetKeyword(keyword); 450 data.SetKeyword(keyword);
438 if (search_url != data.url()) { 451 if (search_url != data.url()) {
439 data.SetURL(search_url); 452 data.SetURL(search_url);
440 // The urls have changed, reset the favicon url. 453 // The urls have changed, reset the favicon url.
441 data.favicon_url = GURL(); 454 data.favicon_url = GURL();
442 } 455 }
443 data.safe_for_autoreplace = false; 456 data.safe_for_autoreplace = false;
444 data.last_modified = time_provider_(); 457 data.last_modified = time_provider_();
445 TemplateURL new_url(data); 458 TemplateURL new_url(data);
446 UpdateNoNotify(url, new_url); 459 UpdateNoNotify(url, new_url);
447 NotifyObservers(); 460 NotifyObservers();
448 } 461 }
449 462
450 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { 463 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) {
451 return url != GetDefaultSearchProvider() && 464 return url != GetDefaultSearchProvider() &&
452 url->url_ref().SupportsReplacement() && !is_default_search_managed(); 465 url->url_ref().SupportsReplacement() && !is_default_search_managed();
453 } 466 }
454 467
455 void TemplateURLService::SetDefaultSearchProvider(const TemplateURL* url) { 468 void TemplateURLService::SetDefaultSearchProvider(const TemplateURL* url) {
456 if (is_default_search_managed_) { 469 DCHECK(!is_default_search_managed_);
457 NOTREACHED(); 470 // Extension keywords cannot be made default, as they are inherently async.
458 return; 471 DCHECK(!url || !url->IsExtensionKeyword());
459 } 472
460 // Always persist the setting in the database, that way if the backup 473 // Always persist the setting in the database, that way if the backup
461 // signature has changed out from under us it gets reset correctly. 474 // signature has changed out from under us it gets reset correctly.
462 SetDefaultSearchProviderNoNotify(url); 475 SetDefaultSearchProviderNoNotify(url);
463 NotifyObservers(); 476 NotifyObservers();
464 } 477 }
465 478
466 const TemplateURL* TemplateURLService::GetDefaultSearchProvider() { 479 const TemplateURL* TemplateURLService::GetDefaultSearchProvider() {
467 if (loaded_ && !load_failed_) 480 if (loaded_ && !load_failed_)
468 return default_search_provider_; 481 return default_search_provider_;
469 482
470 // We're not loaded, rely on the default search provider stored in prefs. 483 // We're not loaded, rely on the default search provider stored in prefs.
471 return initial_default_search_provider_.get(); 484 return initial_default_search_provider_.get();
472 } 485 }
473 486
474 const TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() { 487 const TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() {
475 // See if the prepoluated default still exists. 488 // See if the prepopulated default still exists.
476 scoped_ptr<TemplateURL> prepopulated_default( 489 scoped_ptr<TemplateURL> prepopulated_default(
477 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(GetPrefs())); 490 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(GetPrefs()));
478 for (TemplateURLVector::iterator i = template_urls_.begin(); 491 for (TemplateURLVector::iterator i = template_urls_.begin();
479 i != template_urls_.end(); ++i) { 492 i != template_urls_.end(); ++i) {
480 if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id()) 493 if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id())
481 return *i; 494 return *i;
482 } 495 }
483 // If not, use the first of the templates. 496 // If not, use the first non-extension keyword of the templates.
484 return template_urls_.empty() ? NULL : template_urls_[0]; 497 for (TemplateURLVector::const_iterator i(template_urls_.begin());
498 i != template_urls_.end(); ++i) {
499 if (!(*i)->IsExtensionKeyword())
500 return *i;
501 }
502 return NULL;
485 } 503 }
486 504
487 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) { 505 void TemplateURLService::AddObserver(TemplateURLServiceObserver* observer) {
488 model_observers_.AddObserver(observer); 506 model_observers_.AddObserver(observer);
489 } 507 }
490 508
491 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) { 509 void TemplateURLService::RemoveObserver(TemplateURLServiceObserver* observer) {
492 model_observers_.RemoveObserver(observer); 510 model_observers_.RemoveObserver(observer);
493 } 511 }
494 512
(...skipping 29 matching lines...) Expand all
524 } 542 }
525 543
526 // initial_default_search_provider_ is only needed before we've finished 544 // initial_default_search_provider_ is only needed before we've finished
527 // loading. Now that we've loaded we can nuke it. 545 // loading. Now that we've loaded we can nuke it.
528 initial_default_search_provider_.reset(); 546 initial_default_search_provider_.reset();
529 is_default_search_managed_ = false; 547 is_default_search_managed_ = false;
530 548
531 std::vector<TemplateURL*> template_urls; 549 std::vector<TemplateURL*> template_urls;
532 const TemplateURL* default_search_provider = NULL; 550 const TemplateURL* default_search_provider = NULL;
533 int new_resource_keyword_version = 0; 551 int new_resource_keyword_version = 0;
534 GetSearchProvidersUsingKeywordResult(*result, 552 GetSearchProvidersUsingKeywordResult(*result, service_.get(), GetPrefs(),
535 service_.get(), 553 &template_urls, &default_search_provider, &new_resource_keyword_version);
536 GetPrefs(),
537 &template_urls,
538 &default_search_provider,
539 &new_resource_keyword_version);
540 554
541 bool database_specified_a_default = (default_search_provider != NULL); 555 bool database_specified_a_default = (default_search_provider != NULL);
542 556
543 // Check if default search provider is now managed. 557 // Check if default search provider is now managed.
544 scoped_ptr<TemplateURL> default_from_prefs; 558 scoped_ptr<TemplateURL> default_from_prefs;
545 LoadDefaultSearchProviderFromPrefs(&default_from_prefs, 559 LoadDefaultSearchProviderFromPrefs(&default_from_prefs,
546 &is_default_search_managed_); 560 &is_default_search_managed_);
547 561
548 // Check if the default search provider has been changed in Web Data by 562 // Check if the default search provider has been changed in Web Data by
549 // another program. No immediate action is performed because the default 563 // another program. No immediate action is performed because the default
(...skipping 27 matching lines...) Expand all
577 // The value from the preferences was previously stored in the database. 591 // The value from the preferences was previously stored in the database.
578 // Reuse it. 592 // Reuse it.
579 } else { 593 } else {
580 // The value from the preferences takes over. 594 // The value from the preferences takes over.
581 default_search_provider = NULL; 595 default_search_provider = NULL;
582 if (default_from_prefs.get()) { 596 if (default_from_prefs.get()) {
583 TemplateURLData data(default_from_prefs->data()); 597 TemplateURLData data(default_from_prefs->data());
584 data.created_by_policy = true; 598 data.created_by_policy = true;
585 data.id = kInvalidTemplateURLID; 599 data.id = kInvalidTemplateURLID;
586 TemplateURL* managed_default = new TemplateURL(data); 600 TemplateURL* managed_default = new TemplateURL(data);
587 AddNoNotify(managed_default); 601 AddNoNotify(managed_default, true);
588 default_search_provider = managed_default; 602 default_search_provider = managed_default;
589 } 603 }
590 } 604 }
591 // Note that this saves the default search provider to prefs. 605 // Note that this saves the default search provider to prefs.
592 SetDefaultSearchProviderNoNotify(default_search_provider); 606 if (!default_search_provider ||
607 !default_search_provider->IsExtensionKeyword())
608 SetDefaultSearchProviderNoNotify(default_search_provider);
593 } else { 609 } else {
594 // If we had a managed default, replace it with the synced default if 610 // If we had a managed default, replace it with the synced default if
595 // applicable, or the first provider of the list. 611 // applicable, or the first provider of the list.
596 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); 612 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider();
597 if (synced_default) { 613 if (synced_default) {
598 default_search_provider = synced_default; 614 default_search_provider = synced_default;
599 pending_synced_default_search_ = false; 615 pending_synced_default_search_ = false;
600 } else if (database_specified_a_default && 616 } else if (database_specified_a_default &&
601 default_search_provider == NULL && 617 default_search_provider == NULL) {
602 !template_urls.empty()) { 618 for (std::vector<TemplateURL*>::const_iterator i = template_urls.begin();
603 default_search_provider = template_urls[0]; 619 i != template_urls.end(); ++i) {
620 if (!(*i)->IsExtensionKeyword()) {
621 default_search_provider = *i;
622 break;
623 }
624 }
604 } 625 }
605 626
606 // If the default search provider existed previously, then just 627 // If the default search provider existed previously, then just
607 // set the member variable. Otherwise, we'll set it using the method 628 // set the member variable. Otherwise, we'll set it using the method
608 // to ensure that it is saved properly after its id is set. 629 // to ensure that it is saved properly after its id is set.
609 if (default_search_provider && 630 if (default_search_provider &&
610 (default_search_provider->id() != kInvalidTemplateURLID)) { 631 (default_search_provider->id() != kInvalidTemplateURLID)) {
611 default_search_provider_ = default_search_provider; 632 default_search_provider_ = default_search_provider;
612 default_search_provider = NULL; 633 default_search_provider = NULL;
613 } 634 }
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 } 754 }
734 755
735 SyncDataList TemplateURLService::GetAllSyncData( 756 SyncDataList TemplateURLService::GetAllSyncData(
736 syncable::ModelType type) const { 757 syncable::ModelType type) const {
737 DCHECK_EQ(syncable::SEARCH_ENGINES, type); 758 DCHECK_EQ(syncable::SEARCH_ENGINES, type);
738 759
739 SyncDataList current_data; 760 SyncDataList current_data;
740 for (TemplateURLVector::const_iterator iter = template_urls_.begin(); 761 for (TemplateURLVector::const_iterator iter = template_urls_.begin();
741 iter != template_urls_.end(); ++iter) { 762 iter != template_urls_.end(); ++iter) {
742 // We don't sync extension keywords. 763 // We don't sync extension keywords.
764 // TODO(mpcomplete): If we allow editing extension keywords, then those
765 // should be persisted to disk and synced.
743 if ((*iter)->IsExtensionKeyword()) 766 if ((*iter)->IsExtensionKeyword())
744 continue; 767 continue;
745 // We don't sync keywords managed by policy. 768 // We don't sync keywords managed by policy.
746 if ((*iter)->created_by_policy()) 769 if ((*iter)->created_by_policy())
747 continue; 770 continue;
748 current_data.push_back(CreateSyncDataFromTemplateURL(**iter)); 771 current_data.push_back(CreateSyncDataFromTemplateURL(**iter));
749 } 772 }
750 773
751 return current_data; 774 return current_data;
752 } 775 }
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 local_data_map[local_turl->sync_guid()])); 920 local_data_map[local_turl->sync_guid()]));
898 } 921 }
899 local_data_map.erase(iter->first); 922 local_data_map.erase(iter->first);
900 } else { 923 } else {
901 // The search engine from the cloud has not been synced locally, but there 924 // The search engine from the cloud has not been synced locally, but there
902 // might be a local search engine that is a duplicate that needs to be 925 // might be a local search engine that is a duplicate that needs to be
903 // merged. 926 // merged.
904 const TemplateURL* dupe_turl = FindDuplicateOfSyncTemplateURL(*sync_turl); 927 const TemplateURL* dupe_turl = FindDuplicateOfSyncTemplateURL(*sync_turl);
905 if (dupe_turl) { 928 if (dupe_turl) {
906 // Merge duplicates and remove the processed local TURL from the map. 929 // Merge duplicates and remove the processed local TURL from the map.
907 TemplateURL* modifiable_dupe_turl =
908 const_cast<TemplateURL*>(dupe_turl);
909 std::string old_guid = dupe_turl->sync_guid(); 930 std::string old_guid = dupe_turl->sync_guid();
910 MergeSyncAndLocalURLDuplicates(sync_turl.release(), 931 MergeSyncAndLocalURLDuplicates(sync_turl.release(),
911 modifiable_dupe_turl, 932 const_cast<TemplateURL*>(dupe_turl),
912 &new_changes); 933 &new_changes);
913 local_data_map.erase(old_guid); 934 local_data_map.erase(old_guid);
914 } else { 935 } else {
915 std::string guid = sync_turl->sync_guid(); 936 std::string guid = sync_turl->sync_guid();
916 // Keyword conflict is possible in this case. Resolve it first before 937 // Keyword conflict is possible in this case. Resolve it first before
917 // adding the new TemplateURL. Note that we don't remove the local TURL 938 // adding the new TemplateURL. Note that we don't remove the local TURL
918 // from local_data_map in this case as it may still need to be pushed to 939 // from local_data_map in this case as it may still need to be pushed to
919 // the cloud. 940 // the cloud.
920 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes); 941 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes);
921 // Force the local ID to kInvalidTemplateURLID so we can add it. 942 // Force the local ID to kInvalidTemplateURLID so we can add it.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
956 DCHECK_NE(type, SyncChange::ACTION_INVALID); 977 DCHECK_NE(type, SyncChange::ACTION_INVALID);
957 DCHECK(turl); 978 DCHECK(turl);
958 979
959 if (!models_associated_) 980 if (!models_associated_)
960 return; // Not syncing. 981 return; // Not syncing.
961 982
962 if (processing_syncer_changes_) 983 if (processing_syncer_changes_)
963 return; // These are changes originating from us. Ignore. 984 return; // These are changes originating from us. Ignore.
964 985
965 // Avoid syncing Extension keywords. 986 // Avoid syncing Extension keywords.
987 // TODO(mpcomplete): If we allow editing extension keywords, then those should
988 // be persisted to disk and synced.
966 if (turl->IsExtensionKeyword()) 989 if (turl->IsExtensionKeyword())
967 return; 990 return;
968 991
969 // Avoid syncing keywords managed by policy. 992 // Avoid syncing keywords managed by policy.
970 if (turl->created_by_policy()) 993 if (turl->created_by_policy())
971 return; 994 return;
972 995
973 SyncChangeList changes; 996 SyncChangeList changes;
974 997
975 SyncData sync_data = CreateSyncDataFromTemplateURL(*turl); 998 SyncData sync_data = CreateSyncDataFromTemplateURL(*turl);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1025 base::SplitString(specifics.input_encodings(), ';', &data.input_encodings); 1048 base::SplitString(specifics.input_encodings(), ';', &data.input_encodings);
1026 data.date_created = base::Time::FromInternalValue(specifics.date_created()); 1049 data.date_created = base::Time::FromInternalValue(specifics.date_created());
1027 data.last_modified = base::Time::FromInternalValue(specifics.last_modified()); 1050 data.last_modified = base::Time::FromInternalValue(specifics.last_modified());
1028 data.prepopulate_id = specifics.prepopulate_id(); 1051 data.prepopulate_id = specifics.prepopulate_id();
1029 data.sync_guid = specifics.sync_guid(); 1052 data.sync_guid = specifics.sync_guid();
1030 if (existing_turl) { 1053 if (existing_turl) {
1031 data.id = existing_turl->id(); 1054 data.id = existing_turl->id();
1032 data.created_by_policy = existing_turl->created_by_policy(); 1055 data.created_by_policy = existing_turl->created_by_policy();
1033 data.usage_count = existing_turl->usage_count(); 1056 data.usage_count = existing_turl->usage_count();
1034 } 1057 }
1035 return new TemplateURL(data); 1058
1059 TemplateURL* turl = new TemplateURL(data);
1060 DCHECK(!turl->IsExtensionKeyword());
1061 return turl;
1036 } 1062 }
1037 1063
1038 // static 1064 // static
1039 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap( 1065 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap(
1040 const SyncDataList& sync_data) { 1066 const SyncDataList& sync_data) {
1041 SyncDataMap data_map; 1067 SyncDataMap data_map;
1042 SyncDataList::const_iterator iter; 1068 for (SyncDataList::const_iterator i(sync_data.begin()); i != sync_data.end();
1043 for (iter = sync_data.begin(); iter != sync_data.end(); ++iter) { 1069 ++i)
1044 data_map[iter->GetSpecifics().search_engine().sync_guid()] = *iter; 1070 data_map[i->GetSpecifics().search_engine().sync_guid()] = *i;
1045 }
1046 return data_map; 1071 return data_map;
1047 } 1072 }
1048 1073
1049 void TemplateURLService::SetKeywordSearchTermsForURL(const TemplateURL* t_url, 1074 void TemplateURLService::SetKeywordSearchTermsForURL(const TemplateURL* t_url,
1050 const GURL& url, 1075 const GURL& url,
1051 const string16& term) { 1076 const string16& term) {
1052 HistoryService* history = profile_ ? 1077 HistoryService* history = profile_ ?
1053 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL; 1078 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS) : NULL;
1054 if (!history) 1079 if (!history)
1055 return; 1080 return;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1090 std::string osd_url(initializers[i].url); 1115 std::string osd_url(initializers[i].url);
1091 osd_url.replace(template_position, arraysize(kTemplateParameter) - 1, 1116 osd_url.replace(template_position, arraysize(kTemplateParameter) - 1,
1092 kSearchTermParameter); 1117 kSearchTermParameter);
1093 1118
1094 // TemplateURLService ends up owning the TemplateURL, don't try and free 1119 // TemplateURLService ends up owning the TemplateURL, don't try and free
1095 // it. 1120 // it.
1096 TemplateURLData data; 1121 TemplateURLData data;
1097 data.short_name = UTF8ToUTF16(initializers[i].content); 1122 data.short_name = UTF8ToUTF16(initializers[i].content);
1098 data.SetKeyword(UTF8ToUTF16(initializers[i].keyword)); 1123 data.SetKeyword(UTF8ToUTF16(initializers[i].keyword));
1099 data.SetURL(osd_url); 1124 data.SetURL(osd_url);
1100 AddNoNotify(new TemplateURL(data)); 1125 AddNoNotify(new TemplateURL(data), true);
1101 } 1126 }
1102 } 1127 }
1103 1128
1104 // Initialize default search. 1129 // Initialize default search.
1105 UpdateDefaultSearch(); 1130 UpdateDefaultSearch();
1106 1131
1107 // Request a server check for the correct Google URL if Google is the 1132 // Request a server check for the correct Google URL if Google is the
1108 // default search engine, not in headless mode and not in Chrome Frame. 1133 // default search engine, not in headless mode and not in Chrome Frame.
1109 if (initial_default_search_provider_.get() && 1134 if (initial_default_search_provider_.get() &&
1110 initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) { 1135 initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) {
1111 scoped_ptr<base::Environment> env(base::Environment::Create()); 1136 scoped_ptr<base::Environment> env(base::Environment::Create());
1112 if (!env->HasVar(env_vars::kHeadless) && 1137 if (!env->HasVar(env_vars::kHeadless) &&
1113 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) 1138 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame))
1114 GoogleURLTracker::RequestServerCheck(); 1139 GoogleURLTracker::RequestServerCheck();
1115 } 1140 }
1116 } 1141 }
1117 1142
1118 void TemplateURLService::RemoveFromMaps(const TemplateURL* template_url) { 1143 void TemplateURLService::RemoveFromMaps(const TemplateURL* template_url) {
1119 if (!template_url->keyword().empty()) 1144 if (!template_url->keyword().empty())
1120 keyword_to_template_map_.erase(template_url->keyword()); 1145 keyword_to_template_map_.erase(template_url->keyword());
1146
1147 // If the keyword we're removing is from an extension, we're now done, since
1148 // it won't be synced or stored in the provider map.
1149 // TODO(mpcomplete): If we allow editing extension keywords, then those should
1150 // be synced.
1151 if (template_url->IsExtensionKeyword())
1152 return;
1153
1121 if (!template_url->sync_guid().empty()) 1154 if (!template_url->sync_guid().empty())
1122 guid_to_template_map_.erase(template_url->sync_guid()); 1155 guid_to_template_map_.erase(template_url->sync_guid());
1123 if (loaded_) 1156 if (loaded_)
1124 provider_map_.Remove(template_url); 1157 provider_map_.Remove(template_url);
1125 } 1158 }
1126 1159
1127 void TemplateURLService::RemoveFromKeywordMapByPointer( 1160 void TemplateURLService::RemoveFromKeywordMapByPointer(
1128 const TemplateURL* template_url) { 1161 const TemplateURL* template_url) {
1129 DCHECK(template_url); 1162 DCHECK(template_url);
1130 for (KeywordToTemplateMap::iterator i = keyword_to_template_map_.begin(); 1163 for (KeywordToTemplateMap::iterator i = keyword_to_template_map_.begin();
1131 i != keyword_to_template_map_.end(); ++i) { 1164 i != keyword_to_template_map_.end(); ++i) {
1132 if (i->second == template_url) { 1165 if (i->second == template_url) {
1133 keyword_to_template_map_.erase(i); 1166 keyword_to_template_map_.erase(i);
1134 // A given TemplateURL only occurs once in the map. As soon as we find the 1167 // A given TemplateURL only occurs once in the map. As soon as we find the
1135 // entry, stop. 1168 // entry, stop.
1136 break; 1169 break;
1137 } 1170 }
1138 } 1171 }
1139 } 1172 }
1140 1173
1141 void TemplateURLService::AddToMaps(const TemplateURL* template_url) { 1174 void TemplateURLService::AddToMaps(const TemplateURL* template_url) {
1142 if (!template_url->keyword().empty()) 1175 if (!template_url->keyword().empty())
1143 keyword_to_template_map_[template_url->keyword()] = template_url; 1176 keyword_to_template_map_[template_url->keyword()] = template_url;
1177
1178 // Extension keywords are not synced, so they don't go into the GUID map,
1179 // and do not use host-based search URLs, so they don't go into the provider
1180 // map, so at this point we're done.
1181 // TODO(mpcomplete): If we allow editing extension keywords, then those should
1182 // be persisted to disk and synced.
1183 if (template_url->IsExtensionKeyword())
1184 return;
1185
1144 if (!template_url->sync_guid().empty()) 1186 if (!template_url->sync_guid().empty())
1145 guid_to_template_map_[template_url->sync_guid()] = template_url; 1187 guid_to_template_map_[template_url->sync_guid()] = template_url;
1146 if (loaded_) { 1188 if (loaded_) {
1147 UIThreadSearchTermsData search_terms_data; 1189 UIThreadSearchTermsData search_terms_data;
1148 provider_map_.Add(template_url, search_terms_data); 1190 provider_map_.Add(template_url, search_terms_data);
1149 } 1191 }
1150 } 1192 }
1151 1193
1152 void TemplateURLService::SetTemplateURLs( 1194 void TemplateURLService::SetTemplateURLs(
1153 const std::vector<TemplateURL*>& urls) { 1195 const std::vector<TemplateURL*>& urls) {
1154 // Add mappings for the new items. 1196 // Add mappings for the new items.
1155 1197
1156 // First, add the items that already have id's, so that the next_id_ 1198 // First, add the items that already have id's, so that the next_id_
1157 // gets properly set. 1199 // gets properly set.
1158 for (std::vector<TemplateURL*>::const_iterator i = urls.begin(); 1200 for (std::vector<TemplateURL*>::const_iterator i = urls.begin();
1159 i != urls.end(); 1201 i != urls.end(); ++i) {
1160 ++i) {
1161 if ((*i)->id() != kInvalidTemplateURLID) { 1202 if ((*i)->id() != kInvalidTemplateURLID) {
1162 next_id_ = std::max(next_id_, (*i)->id()); 1203 next_id_ = std::max(next_id_, (*i)->id());
1163 AddToMaps(*i); 1204 AddNoNotify(*i, false);
Peter Kasting 2012/04/10 01:24:33 I changed this call because in the future I need t
1164 template_urls_.push_back(*i);
1165 } 1205 }
1166 } 1206 }
1167 1207
1168 // Next add the new items that don't have id's. 1208 // Next add the new items that don't have id's.
1169 for (std::vector<TemplateURL*>::const_iterator i = urls.begin(); 1209 for (std::vector<TemplateURL*>::const_iterator i = urls.begin();
1170 i != urls.end(); 1210 i != urls.end(); ++i) {
1171 ++i) {
1172 if ((*i)->id() == kInvalidTemplateURLID) 1211 if ((*i)->id() == kInvalidTemplateURLID)
1173 AddNoNotify(*i); 1212 AddNoNotify(*i, true);
1174 } 1213 }
1175 } 1214 }
1176 1215
1177 void TemplateURLService::ChangeToLoadedState() { 1216 void TemplateURLService::ChangeToLoadedState() {
1178 DCHECK(!loaded_); 1217 DCHECK(!loaded_);
1179 1218
1180 UIThreadSearchTermsData search_terms_data; 1219 UIThreadSearchTermsData search_terms_data;
1181 provider_map_.Init(template_urls_, search_terms_data); 1220 provider_map_.Init(template_urls_, search_terms_data);
1182 loaded_ = true; 1221 loaded_ = true;
1183 } 1222 }
(...skipping 24 matching lines...) Expand all
1208 std::string search_url; 1247 std::string search_url;
1209 std::string suggest_url; 1248 std::string suggest_url;
1210 std::string instant_url; 1249 std::string instant_url;
1211 std::string icon_url; 1250 std::string icon_url;
1212 std::string encodings; 1251 std::string encodings;
1213 std::string short_name; 1252 std::string short_name;
1214 std::string keyword; 1253 std::string keyword;
1215 std::string id_string; 1254 std::string id_string;
1216 std::string prepopulate_id; 1255 std::string prepopulate_id;
1217 if (t_url) { 1256 if (t_url) {
1257 DCHECK(!t_url->IsExtensionKeyword());
1218 enabled = true; 1258 enabled = true;
1219 search_url = t_url->url(); 1259 search_url = t_url->url();
1220 suggest_url = t_url->suggestions_url(); 1260 suggest_url = t_url->suggestions_url();
1221 instant_url = t_url->instant_url(); 1261 instant_url = t_url->instant_url();
1222 GURL icon_gurl = t_url->favicon_url(); 1262 GURL icon_gurl = t_url->favicon_url();
1223 if (!icon_gurl.is_empty()) 1263 if (!icon_gurl.is_empty())
1224 icon_url = icon_gurl.spec(); 1264 icon_url = icon_gurl.spec();
1225 encodings = JoinString(t_url->input_encodings(), ';'); 1265 encodings = JoinString(t_url->input_encodings(), ';');
1226 short_name = UTF16ToUTF8(t_url->short_name()); 1266 short_name = UTF16ToUTF8(t_url->short_name());
1227 keyword = UTF16ToUTF8(t_url->keyword()); 1267 keyword = UTF16ToUTF8(t_url->keyword());
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1291 int64 value; 1331 int64 value;
1292 base::StringToInt64(id_string, &value); 1332 base::StringToInt64(id_string, &value);
1293 data.id = value; 1333 data.id = value;
1294 } 1334 }
1295 if (!prepopulate_id.empty() && !*is_managed) { 1335 if (!prepopulate_id.empty() && !*is_managed) {
1296 int value; 1336 int value;
1297 base::StringToInt(prepopulate_id, &value); 1337 base::StringToInt(prepopulate_id, &value);
1298 data.prepopulate_id = value; 1338 data.prepopulate_id = value;
1299 } 1339 }
1300 default_provider->reset(new TemplateURL(data)); 1340 default_provider->reset(new TemplateURL(data));
1341 DCHECK(!(*default_provider)->IsExtensionKeyword());
1301 return true; 1342 return true;
1302 } 1343 }
1303 1344
1304 bool TemplateURLService::CanReplaceKeywordForHost( 1345 bool TemplateURLService::CanReplaceKeywordForHost(
1305 const std::string& host, 1346 const std::string& host,
1306 const TemplateURL** to_replace) { 1347 const TemplateURL** to_replace) {
1348 DCHECK(to_replace);
1349 DCHECK(!*to_replace);
1307 const TemplateURLSet* urls = provider_map_.GetURLsForHost(host); 1350 const TemplateURLSet* urls = provider_map_.GetURLsForHost(host);
1308 if (urls) { 1351 if (!urls)
1309 for (TemplateURLSet::const_iterator i = urls->begin(); 1352 return true;
1310 i != urls->end(); ++i) { 1353 for (TemplateURLSet::const_iterator i(urls->begin()); i != urls->end(); ++i) {
1311 const TemplateURL* url = *i; 1354 if (CanReplace(*i)) {
1312 if (CanReplace(url)) { 1355 if (to_replace)
1313 if (to_replace) 1356 *to_replace = *i;
1314 *to_replace = url; 1357 return true;
1315 return true;
1316 }
1317 } 1358 }
1318 } 1359 }
1319 1360 return false;
1320 if (to_replace)
1321 *to_replace = NULL;
1322 return !urls;
1323 } 1361 }
1324 1362
1325 bool TemplateURLService::CanReplace(const TemplateURL* t_url) { 1363 bool TemplateURLService::CanReplace(const TemplateURL* t_url) {
1326 return (t_url != default_search_provider_ && !t_url->show_in_default_list() && 1364 return (t_url != default_search_provider_ && !t_url->show_in_default_list() &&
1327 t_url->safe_for_autoreplace()); 1365 t_url->safe_for_autoreplace());
1328 } 1366 }
1329 1367
1330 void TemplateURLService::UpdateNoNotify(const TemplateURL* existing_turl, 1368 void TemplateURLService::UpdateNoNotify(const TemplateURL* existing_turl,
1331 const TemplateURL& new_values) { 1369 const TemplateURL& new_values) {
1332 DCHECK(loaded_); 1370 DCHECK(loaded_);
1333 DCHECK(existing_turl); 1371 DCHECK(existing_turl);
1334 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), 1372 DCHECK(std::find(template_urls_.begin(), template_urls_.end(),
1335 existing_turl) != template_urls_.end()); 1373 existing_turl) != template_urls_.end());
1336 1374
1337 if (!existing_turl->keyword().empty()) 1375 // TODO(mpcomplete): If we allow editing extension keywords, then those should
1338 keyword_to_template_map_.erase(existing_turl->keyword()); 1376 // be persisted to disk and synced. In this case this DCHECK should be
1377 // removed.
1378 DCHECK(!existing_turl->IsExtensionKeyword());
1379
1380 string16 old_keyword(existing_turl->keyword());
1381 if (!old_keyword.empty())
1382 keyword_to_template_map_.erase(old_keyword);
1339 if (!existing_turl->sync_guid().empty()) 1383 if (!existing_turl->sync_guid().empty())
1340 guid_to_template_map_.erase(existing_turl->sync_guid()); 1384 guid_to_template_map_.erase(existing_turl->sync_guid());
1341 1385
1342 provider_map_.Remove(existing_turl); 1386 provider_map_.Remove(existing_turl);
1343 TemplateURLID previous_id = existing_turl->id(); 1387 TemplateURLID previous_id = existing_turl->id();
1344 TemplateURL* modifiable_turl = const_cast<TemplateURL*>(existing_turl); 1388 TemplateURL* modifiable_turl = const_cast<TemplateURL*>(existing_turl);
1345 *modifiable_turl = new_values; 1389 *modifiable_turl = new_values;
1346 modifiable_turl->data_.id = previous_id; 1390 modifiable_turl->data_.id = previous_id;
1347 UIThreadSearchTermsData search_terms_data; 1391 UIThreadSearchTermsData search_terms_data;
1348 provider_map_.Add(existing_turl, search_terms_data); 1392 provider_map_.Add(existing_turl, search_terms_data);
1349 1393
1350 if (!existing_turl->keyword().empty()) 1394 const string16& keyword = existing_turl->keyword();
1351 keyword_to_template_map_[existing_turl->keyword()] = existing_turl; 1395 if (!keyword.empty())
1396 keyword_to_template_map_[keyword] = existing_turl;
1352 if (!existing_turl->sync_guid().empty()) 1397 if (!existing_turl->sync_guid().empty())
1353 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; 1398 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl;
1354 1399
1355 if (service_.get()) 1400 if (service_.get())
1356 service_->UpdateKeyword(*existing_turl); 1401 service_->UpdateKeyword(*existing_turl);
1357 1402
1358 // Inform sync of the update. 1403 // Inform sync of the update.
1359 ProcessTemplateURLChange(existing_turl, SyncChange::ACTION_UPDATE); 1404 ProcessTemplateURLChange(existing_turl, SyncChange::ACTION_UPDATE);
1360 1405
1361 if (default_search_provider_ == existing_turl) 1406 if (default_search_provider_ == existing_turl)
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1407 built_terms = true; 1452 built_terms = true;
1408 1453
1409 if (content::PageTransitionStripQualifier(details.transition) == 1454 if (content::PageTransitionStripQualifier(details.transition) ==
1410 content::PAGE_TRANSITION_KEYWORD) { 1455 content::PAGE_TRANSITION_KEYWORD) {
1411 // The visit is the result of the user entering a keyword, generate a 1456 // The visit is the result of the user entering a keyword, generate a
1412 // KEYWORD_GENERATED visit for the KEYWORD so that the keyword typed 1457 // KEYWORD_GENERATED visit for the KEYWORD so that the keyword typed
1413 // count is boosted. 1458 // count is boosted.
1414 AddTabToSearchVisit(**i); 1459 AddTabToSearchVisit(**i);
1415 } 1460 }
1416 1461
1417 QueryTerms::iterator terms_iterator = 1462 QueryTerms::iterator j(query_terms.find(search_ref.GetSearchTermKey()));
1418 query_terms.find(search_ref.GetSearchTermKey()); 1463 if (j != query_terms.end() && !j->second.empty()) {
1419 if (terms_iterator != query_terms.end() &&
1420 !terms_iterator->second.empty()) {
1421 SetKeywordSearchTermsForURL(*i, row.url(), 1464 SetKeywordSearchTermsForURL(*i, row.url(),
1422 search_ref.SearchTermToString16(terms_iterator->second)); 1465 search_ref.SearchTermToString16(j->second));
1423 } 1466 }
1424 } 1467 }
1425 } 1468 }
1426 } 1469 }
1427 1470
1428 void TemplateURLService::AddTabToSearchVisit(const TemplateURL& t_url) { 1471 void TemplateURLService::AddTabToSearchVisit(const TemplateURL& t_url) {
1429 // Only add visits for entries the user hasn't modified. If the user modified 1472 // Only add visits for entries the user hasn't modified. If the user modified
1430 // the entry the keyword may no longer correspond to the host name. It may be 1473 // the entry the keyword may no longer correspond to the host name. It may be
1431 // possible to do something more sophisticated here, but it's so rare as to 1474 // possible to do something more sophisticated here, but it's so rare as to
1432 // not be worth it. 1475 // not be worth it.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1480 valid_term_count++; 1523 valid_term_count++;
1481 (*query_terms)[key_string] = value_string; 1524 (*query_terms)[key_string] = value_string;
1482 } 1525 }
1483 } 1526 }
1484 } 1527 }
1485 return (valid_term_count > 0); 1528 return (valid_term_count > 0);
1486 } 1529 }
1487 1530
1488 void TemplateURLService::GoogleBaseURLChanged() { 1531 void TemplateURLService::GoogleBaseURLChanged() {
1489 bool something_changed = false; 1532 bool something_changed = false;
1490 for (size_t i = 0; i < template_urls_.size(); ++i) { 1533 for (TemplateURLVector::iterator i(template_urls_.begin());
1491 const TemplateURL* t_url = template_urls_[i]; 1534 i != template_urls_.end(); ++i) {
1535 TemplateURL* t_url = const_cast<TemplateURL*>(*i);
1492 if (t_url->url_ref().HasGoogleBaseURLs() || 1536 if (t_url->url_ref().HasGoogleBaseURLs() ||
1493 t_url->suggestions_url_ref().HasGoogleBaseURLs()) { 1537 t_url->suggestions_url_ref().HasGoogleBaseURLs()) {
1538 something_changed = true;
1539 string16 original_keyword(t_url->keyword());
1540 t_url->InvalidateCachedValues();
1541 const string16& new_keyword(t_url->keyword());
1494 RemoveFromKeywordMapByPointer(t_url); 1542 RemoveFromKeywordMapByPointer(t_url);
1495 t_url->InvalidateCachedValues(); 1543 if (!new_keyword.empty())
1496 if (!t_url->keyword().empty()) 1544 keyword_to_template_map_[new_keyword] = t_url;
1497 keyword_to_template_map_[t_url->keyword()] = t_url;
1498 something_changed = true;
1499 } 1545 }
1500 } 1546 }
1501 1547
1502 if (something_changed && loaded_) { 1548 if (something_changed && loaded_) {
1503 UIThreadSearchTermsData search_terms_data; 1549 UIThreadSearchTermsData search_terms_data;
1504 provider_map_.UpdateGoogleBaseURLs(search_terms_data); 1550 provider_map_.UpdateGoogleBaseURLs(search_terms_data);
1505 NotifyObservers(); 1551 NotifyObservers();
1506 } 1552 }
1507 } 1553 }
1508 1554
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1548 // invalid value. 1594 // invalid value.
1549 const TemplateURL* old_default = default_search_provider_; 1595 const TemplateURL* old_default = default_search_provider_;
1550 SetDefaultSearchProviderNoNotify(NULL); 1596 SetDefaultSearchProviderNoNotify(NULL);
1551 RemoveNoNotify(old_default); 1597 RemoveNoNotify(old_default);
1552 } else if (default_search_provider_) { 1598 } else if (default_search_provider_) {
1553 TemplateURLData data(new_default_from_prefs->data()); 1599 TemplateURLData data(new_default_from_prefs->data());
1554 data.created_by_policy = true; 1600 data.created_by_policy = true;
1555 TemplateURL new_values(data); 1601 TemplateURL new_values(data);
1556 UpdateNoNotify(default_search_provider_, new_values); 1602 UpdateNoNotify(default_search_provider_, new_values);
1557 } else { 1603 } else {
1558 // AddNoNotify will take ownership of new_template, so it's safe to
1559 // release.
1560 TemplateURL* new_template = NULL; 1604 TemplateURL* new_template = NULL;
1561 if (new_default_from_prefs.get()) { 1605 if (new_default_from_prefs.get()) {
1562 TemplateURLData data(new_default_from_prefs->data()); 1606 TemplateURLData data(new_default_from_prefs->data());
1563 data.created_by_policy = true; 1607 data.created_by_policy = true;
1564 new_template = new TemplateURL(data); 1608 new_template = new TemplateURL(data);
1565 AddNoNotify(new_template); 1609 AddNoNotify(new_template, true);
1566 } 1610 }
1567 SetDefaultSearchProviderNoNotify(new_template); 1611 SetDefaultSearchProviderNoNotify(new_template);
1568 } 1612 }
1569 } else if (!is_default_search_managed_ && new_is_default_managed) { 1613 } else if (!is_default_search_managed_ && new_is_default_managed) {
1570 // The default used to be unmanaged and is now managed. Add the new 1614 // The default used to be unmanaged and is now managed. Add the new
1571 // managed default to the list of URLs and set it as default. 1615 // managed default to the list of URLs and set it as default.
1572 is_default_search_managed_ = new_is_default_managed; 1616 is_default_search_managed_ = new_is_default_managed;
1573 // AddNoNotify will take ownership of new_template, so it's safe to
1574 // release.
1575 TemplateURL* new_template = NULL; 1617 TemplateURL* new_template = NULL;
1576 if (new_default_from_prefs.get()) { 1618 if (new_default_from_prefs.get()) {
1577 TemplateURLData data(new_default_from_prefs->data()); 1619 TemplateURLData data(new_default_from_prefs->data());
1578 data.created_by_policy = true; 1620 data.created_by_policy = true;
1579 new_template = new TemplateURL(data); 1621 new_template = new TemplateURL(data);
1580 AddNoNotify(new_template); 1622 AddNoNotify(new_template, true);
1581 } 1623 }
1582 SetDefaultSearchProviderNoNotify(new_template); 1624 SetDefaultSearchProviderNoNotify(new_template);
1583 } else { 1625 } else {
1584 // The default was managed and is no longer. 1626 // The default was managed and is no longer.
1585 DCHECK(is_default_search_managed_ && !new_is_default_managed); 1627 DCHECK(is_default_search_managed_ && !new_is_default_managed);
1586 is_default_search_managed_ = new_is_default_managed; 1628 is_default_search_managed_ = new_is_default_managed;
1587 // If we had a default, delete the previous default if created by policy 1629 // If we had a default, delete the previous default if created by policy
1588 // and set a likely default. 1630 // and set a likely default.
1589 if ((default_search_provider_ != NULL) && 1631 if ((default_search_provider_ != NULL) &&
1590 default_search_provider_->created_by_policy()) { 1632 default_search_provider_->created_by_policy()) {
1591 const TemplateURL* old_default = default_search_provider_; 1633 const TemplateURL* old_default = default_search_provider_;
1592 default_search_provider_ = NULL; 1634 default_search_provider_ = NULL;
1593 RemoveNoNotify(old_default); 1635 RemoveNoNotify(old_default);
1594 } 1636 }
1595 1637
1596 // The likely default should be from Sync if we were waiting on Sync. 1638 // The likely default should be from Sync if we were waiting on Sync.
1597 // Otherwise, it should be FindNewDefaultSearchProvider. 1639 // Otherwise, it should be FindNewDefaultSearchProvider.
1598 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); 1640 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider();
1599 if (synced_default) 1641 if (synced_default)
1600 pending_synced_default_search_ = false; 1642 pending_synced_default_search_ = false;
1601 SetDefaultSearchProviderNoNotify(synced_default ? synced_default : 1643 SetDefaultSearchProviderNoNotify(synced_default ? synced_default :
1602 FindNewDefaultSearchProvider()); 1644 FindNewDefaultSearchProvider());
1603 } 1645 }
1604 NotifyObservers(); 1646 NotifyObservers();
1605 } 1647 }
1606 1648
1607 void TemplateURLService::SetDefaultSearchProviderNoNotify( 1649 void TemplateURLService::SetDefaultSearchProviderNoNotify(
1608 const TemplateURL* url) { 1650 const TemplateURL* url) {
1609 DCHECK(!url || std::find(template_urls_.begin(), template_urls_.end(), url) != 1651 if (url) {
1610 template_urls_.end()); 1652 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), url) !=
1653 template_urls_.end());
1654 // Extension keywords cannot be made default, as they're inherently async.
1655 DCHECK(!url->IsExtensionKeyword());
1656 }
1657
1611 default_search_provider_ = url; 1658 default_search_provider_ = url;
1612 1659
1613 if (url) { 1660 if (url) {
1614 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url); 1661 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url);
1615 // Don't mark the url as edited, otherwise we won't be able to rev the 1662 // Don't mark the url as edited, otherwise we won't be able to rev the
1616 // template urls we ship with. 1663 // template urls we ship with.
1617 modifiable_url->data_.show_in_default_list = true; 1664 modifiable_url->data_.show_in_default_list = true;
1618 if (service_.get()) 1665 if (service_.get())
1619 service_->UpdateKeyword(*url); 1666 service_->UpdateKeyword(*url);
1620 1667
(...skipping 22 matching lines...) Expand all
1643 } 1690 }
1644 1691
1645 if (service_.get()) 1692 if (service_.get())
1646 service_->SetDefaultSearchProvider(url); 1693 service_->SetDefaultSearchProvider(url);
1647 1694
1648 // Inform sync the change to the show_in_default_list flag. 1695 // Inform sync the change to the show_in_default_list flag.
1649 if (url) 1696 if (url)
1650 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE); 1697 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE);
1651 } 1698 }
1652 1699
1653 void TemplateURLService::AddNoNotify(TemplateURL* template_url) { 1700 void TemplateURLService::AddNoNotify(TemplateURL* template_url,
1701 bool newly_adding) {
1654 DCHECK(template_url); 1702 DCHECK(template_url);
1655 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); 1703
1656 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), 1704 if (newly_adding) {
1657 template_url) == template_urls_.end()); 1705 DCHECK_EQ(kInvalidTemplateURLID, template_url->id());
1658 template_url->data_.id = ++next_id_; 1706 DCHECK(std::find(template_urls_.begin(), template_urls_.end(),
1707 template_url) == template_urls_.end());
1708 template_url->data_.id = ++next_id_;
1709 }
1710
1659 template_urls_.push_back(template_url); 1711 template_urls_.push_back(template_url);
1660 AddToMaps(template_url); 1712 AddToMaps(template_url);
1661 1713
1662 if (service_.get()) 1714 if (newly_adding) {
1663 service_->AddKeyword(*template_url); 1715 // Don't persist extension keywords to disk. They'll get re-added on each
1716 // launch as the extensions are loaded.
1717 // TODO(mpcomplete): If we allow editing extension keywords, then those
1718 // should be persisted to disk and synced.
1719 if (service_.get() && !template_url->IsExtensionKeyword())
1720 service_->AddKeyword(*template_url);
1664 1721
1665 // Inform sync of the addition. Note that this will assign a GUID to 1722 // Inform sync of the addition. Note that this will assign a GUID to
1666 // template_url and add it to the guid_to_template_map_. 1723 // template_url and add it to the guid_to_template_map_.
1667 ProcessTemplateURLChange(template_url, SyncChange::ACTION_ADD); 1724 ProcessTemplateURLChange(template_url, SyncChange::ACTION_ADD);
1725 }
1668 } 1726 }
1669 1727
1670 void TemplateURLService::RemoveNoNotify(const TemplateURL* template_url) { 1728 void TemplateURLService::RemoveNoNotify(const TemplateURL* template_url) {
1671 TemplateURLVector::iterator i = 1729 TemplateURLVector::iterator i =
1672 std::find(template_urls_.begin(), template_urls_.end(), template_url); 1730 std::find(template_urls_.begin(), template_urls_.end(), template_url);
1673 if (i == template_urls_.end()) 1731 if (i == template_urls_.end())
1674 return; 1732 return;
1675 1733
1676 if (template_url == default_search_provider_) { 1734 if (template_url == default_search_provider_) {
1677 // Should never delete the default search provider. 1735 // Should never delete the default search provider.
1678 NOTREACHED(); 1736 NOTREACHED();
1679 return; 1737 return;
1680 } 1738 }
1681 1739
1682 RemoveFromMaps(template_url); 1740 RemoveFromMaps(template_url);
1683 1741
1684 // Remove it from the vector containing all TemplateURLs. 1742 // Remove it from the vector containing all TemplateURLs.
1685 template_urls_.erase(i); 1743 template_urls_.erase(i);
1686 1744
1687 if (service_.get()) 1745 // Extension keywords are not persisted.
1746 // TODO(mpcomplete): If we allow editing extension keywords, then those should
1747 // be persisted to disk and synced.
1748 if (service_.get() && !template_url->IsExtensionKeyword())
1688 service_->RemoveKeyword(template_url->id()); 1749 service_->RemoveKeyword(template_url->id());
1689 1750
1690 // Inform sync of the deletion. 1751 // Inform sync of the deletion.
1691 ProcessTemplateURLChange(template_url, SyncChange::ACTION_DELETE); 1752 ProcessTemplateURLChange(template_url, SyncChange::ACTION_DELETE);
1692 1753
1693 if (profile_) { 1754 if (profile_) {
1694 content::Source<Profile> source(profile_); 1755 content::Source<Profile> source(profile_);
1695 TemplateURLID id = template_url->id(); 1756 TemplateURLID id = template_url->id();
1696 content::NotificationService::current()->Notify( 1757 content::NotificationService::current()->Notify(
1697 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, 1758 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED,
(...skipping 21 matching lines...) Expand all
1719 // This function removes from the vector and the database all the TemplateURLs 1780 // This function removes from the vector and the database all the TemplateURLs
1720 // that were set by policy, unless it is the current default search provider 1781 // that were set by policy, unless it is the current default search provider
1721 // and matches what is set by a managed preference. 1782 // and matches what is set by a managed preference.
1722 void TemplateURLService::RemoveProvidersCreatedByPolicy( 1783 void TemplateURLService::RemoveProvidersCreatedByPolicy(
1723 std::vector<TemplateURL*>* template_urls, 1784 std::vector<TemplateURL*>* template_urls,
1724 const TemplateURL** default_search_provider, 1785 const TemplateURL** default_search_provider,
1725 const TemplateURL* default_from_prefs) { 1786 const TemplateURL* default_from_prefs) {
1726 DCHECK(template_urls); 1787 DCHECK(template_urls);
1727 DCHECK(default_search_provider); 1788 DCHECK(default_search_provider);
1728 for (std::vector<TemplateURL*>::iterator i = template_urls->begin(); 1789 for (std::vector<TemplateURL*>::iterator i = template_urls->begin();
1729 i != template_urls->end(); ) { 1790 i != template_urls->end(); ) {
1730 TemplateURL* template_url = *i; 1791 TemplateURL* template_url = *i;
1731 if (template_url->created_by_policy()) { 1792 if (template_url->created_by_policy()) {
1732 if (template_url == *default_search_provider && 1793 if (template_url == *default_search_provider &&
1733 is_default_search_managed_ && 1794 is_default_search_managed_ &&
1734 TemplateURLsHaveSamePrefs(template_url, 1795 TemplateURLsHaveSamePrefs(template_url,
1735 default_from_prefs)) { 1796 default_from_prefs)) {
1736 // If the database specified a default search provider that was set 1797 // If the database specified a default search provider that was set
1737 // by policy, and the default search provider from the preferences 1798 // by policy, and the default search provider from the preferences
1738 // is also set by policy and they are the same, keep the entry in the 1799 // is also set by policy and they are the same, keep the entry in the
1739 // database and the |default_search_provider|. 1800 // database and the |default_search_provider|.
1740 ++i; 1801 ++i;
1741 continue; 1802 continue;
1742 } 1803 }
1743 1804
1744 // The database loaded a managed |default_search_provider|, but it has 1805 // The database loaded a managed |default_search_provider|, but it has
1745 // been updated in the prefs. Remove it from the database, and update the 1806 // been updated in the prefs. Remove it from the database, and update the
1746 // |default_search_provider| pointer here. 1807 // |default_search_provider| pointer here.
1747 if (*default_search_provider && 1808 if (*default_search_provider &&
1748 (*default_search_provider)->id() == template_url->id()) 1809 (*default_search_provider)->id() == template_url->id())
1749 *default_search_provider = NULL; 1810 *default_search_provider = NULL;
1750 1811
1751 i = template_urls->erase(i); 1812 i = template_urls->erase(i);
1752 if (service_.get()) 1813 // Extension keywords are not persisted.
1814 // TODO(mpcomplete): If we allow editing extension keywords, then those
1815 // should be persisted to disk and synced.
1816 if (service_.get() && !template_url->IsExtensionKeyword())
1753 service_->RemoveKeyword(template_url->id()); 1817 service_->RemoveKeyword(template_url->id());
1754 delete template_url; 1818 delete template_url;
1755 } else { 1819 } else {
1756 ++i; 1820 ++i;
1757 } 1821 }
1758 } 1822 }
1759 } 1823 }
1760 1824
1761 void TemplateURLService::ResetTemplateURLGUID(const TemplateURL* url, 1825 void TemplateURLService::ResetTemplateURLGUID(const TemplateURL* url,
1762 const std::string& guid) { 1826 const std::string& guid) {
1763 DCHECK(!guid.empty()); 1827 DCHECK(!guid.empty());
1764 1828
1765 TemplateURLData data(url->data()); 1829 TemplateURLData data(url->data());
1766 data.sync_guid = guid; 1830 data.sync_guid = guid;
1767 TemplateURL new_url(data); 1831 TemplateURL new_url(data);
1768 UpdateNoNotify(url, new_url); 1832 UpdateNoNotify(url, new_url);
1769 } 1833 }
1770 1834
1771 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl) const { 1835 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl) const {
1772 // Already unique. 1836 // Already unique.
1773 if (!GetTemplateURLForKeyword(turl.keyword())) 1837 if (!GetTemplateURLForKeyword(turl.keyword()))
1774 return turl.keyword(); 1838 return turl.keyword();
1775 1839
1776 // First, try to return the generated keyword for the TemplateURL. 1840 // First, try to return the generated keyword for the TemplateURL.
1777 GURL gurl(turl.url()); 1841 GURL gurl(turl.url());
1778 string16 keyword_candidate = GenerateKeyword(gurl, true); 1842 if (gurl.is_valid()) {
1779 if (!GetTemplateURLForKeyword(keyword_candidate) && 1843 string16 keyword_candidate = GenerateKeyword(gurl, true);
1780 !keyword_candidate.empty()) { 1844 if (!GetTemplateURLForKeyword(keyword_candidate) &&
1781 return keyword_candidate; 1845 !keyword_candidate.empty())
1846 return keyword_candidate;
1782 } 1847 }
1783 1848
1784 // We try to uniquify the keyword by appending a special character to the end. 1849 // We try to uniquify the keyword by appending a special character to the end.
1785 // This is a best-effort approach where we try to preserve the original 1850 // This is a best-effort approach where we try to preserve the original
1786 // keyword and let the user do what they will after our attempt. 1851 // keyword and let the user do what they will after our attempt.
1787 keyword_candidate = turl.keyword(); 1852 string16 keyword_candidate(turl.keyword());
1788 do { 1853 do {
1789 keyword_candidate.append(ASCIIToUTF16("_")); 1854 keyword_candidate.append(ASCIIToUTF16("_"));
1790 } while (GetTemplateURLForKeyword(keyword_candidate)); 1855 } while (GetTemplateURLForKeyword(keyword_candidate));
1791 1856
1792 return keyword_candidate; 1857 return keyword_candidate;
1793 } 1858 }
1794 1859
1795 bool TemplateURLService::ResolveSyncKeywordConflict( 1860 bool TemplateURLService::ResolveSyncKeywordConflict(
1796 TemplateURL* sync_turl, 1861 TemplateURL* sync_turl,
1797 SyncChangeList* change_list) { 1862 SyncChangeList* change_list) {
(...skipping 23 matching lines...) Expand all
1821 UpdateNoNotify(existing_turl, new_turl); 1886 UpdateNoNotify(existing_turl, new_turl);
1822 NotifyObservers(); 1887 NotifyObservers();
1823 } 1888 }
1824 return true; 1889 return true;
1825 } 1890 }
1826 1891
1827 const TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( 1892 const TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL(
1828 const TemplateURL& sync_turl) { 1893 const TemplateURL& sync_turl) {
1829 const TemplateURL* existing_turl = 1894 const TemplateURL* existing_turl =
1830 GetTemplateURLForKeyword(sync_turl.keyword()); 1895 GetTemplateURLForKeyword(sync_turl.keyword());
1831 if (!existing_turl) 1896 return existing_turl && !existing_turl->url().empty() &&
1832 return NULL; 1897 (existing_turl->url() == sync_turl.url()) ? existing_turl : NULL;
1833
1834 if (!existing_turl->url().empty() &&
1835 existing_turl->url() == sync_turl.url()) {
1836 return existing_turl;
1837 }
1838 return NULL;
1839 } 1898 }
1840 1899
1841 void TemplateURLService::MergeSyncAndLocalURLDuplicates( 1900 void TemplateURLService::MergeSyncAndLocalURLDuplicates(
1842 TemplateURL* sync_turl, 1901 TemplateURL* sync_turl,
1843 TemplateURL* local_turl, 1902 TemplateURL* local_turl,
1844 SyncChangeList* change_list) { 1903 SyncChangeList* change_list) {
1845 DCHECK(sync_turl); 1904 DCHECK(sync_turl);
1846 DCHECK(local_turl); 1905 DCHECK(local_turl);
1847 DCHECK(change_list); 1906 DCHECK(change_list);
1848 scoped_ptr<TemplateURL> scoped_sync_turl(sync_turl); 1907 scoped_ptr<TemplateURL> scoped_sync_turl(sync_turl);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1902 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID)); 1961 prefs->GetString(prefs::kSyncedDefaultSearchProviderGUID));
1903 } 1962 }
1904 1963
1905 void TemplateURLService::PatchMissingSyncGUIDs( 1964 void TemplateURLService::PatchMissingSyncGUIDs(
1906 std::vector<TemplateURL*>* template_urls) { 1965 std::vector<TemplateURL*>* template_urls) {
1907 DCHECK(template_urls); 1966 DCHECK(template_urls);
1908 for (std::vector<TemplateURL*>::iterator i = template_urls->begin(); 1967 for (std::vector<TemplateURL*>::iterator i = template_urls->begin();
1909 i != template_urls->end(); ++i) { 1968 i != template_urls->end(); ++i) {
1910 TemplateURL* template_url = *i; 1969 TemplateURL* template_url = *i;
1911 DCHECK(template_url); 1970 DCHECK(template_url);
1912 if (template_url->sync_guid().empty()) { 1971 // Extension keywords are never synced.
1972 // TODO(mpcomplete): If we allow editing extension keywords, then those
1973 // should be persisted to disk and synced.
1974 if (template_url->sync_guid().empty() &&
1975 !template_url->IsExtensionKeyword()) {
1913 template_url->data_.sync_guid = guid::GenerateGUID(); 1976 template_url->data_.sync_guid = guid::GenerateGUID();
1914 if (service_.get()) 1977 if (service_.get())
1915 service_->UpdateKeyword(*template_url); 1978 service_->UpdateKeyword(*template_url);
1916 } 1979 }
1917 } 1980 }
1918 } 1981 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698