OLD | NEW |
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 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 return (initial_default_search_provider_.get() && | 318 return (initial_default_search_provider_.get() && |
319 (GenerateSearchURL(initial_default_search_provider_.get()).host() == | 319 (GenerateSearchURL(initial_default_search_provider_.get()).host() == |
320 host)) ? initial_default_search_provider_.get() : NULL; | 320 host)) ? initial_default_search_provider_.get() : NULL; |
321 } | 321 } |
322 | 322 |
323 void TemplateURLService::Add(TemplateURL* template_url) { | 323 void TemplateURLService::Add(TemplateURL* template_url) { |
324 AddNoNotify(template_url, true); | 324 AddNoNotify(template_url, true); |
325 NotifyObservers(); | 325 NotifyObservers(); |
326 } | 326 } |
327 | 327 |
| 328 void TemplateURLService::AddAndSetProfile(TemplateURL* template_url, |
| 329 Profile* profile) { |
| 330 template_url->profile_ = profile; |
| 331 Add(template_url); |
| 332 } |
| 333 |
328 void TemplateURLService::AddWithOverrides(const TemplateURL* template_url, | 334 void TemplateURLService::AddWithOverrides(const TemplateURL* template_url, |
329 const string16& short_name, | 335 const string16& short_name, |
330 const string16& keyword, | 336 const string16& keyword, |
331 const std::string& url) { | 337 const std::string& url) { |
332 DCHECK(!url.empty()); | 338 DCHECK(!url.empty()); |
333 TemplateURL* modifiable_url = const_cast<TemplateURL*>(template_url); | 339 TemplateURL* modifiable_url = const_cast<TemplateURL*>(template_url); |
334 modifiable_url->data_.short_name = short_name; | 340 modifiable_url->data_.short_name = short_name; |
335 modifiable_url->data_.SetKeyword(keyword); | 341 modifiable_url->data_.SetKeyword(keyword); |
336 modifiable_url->SetURL(url); | 342 modifiable_url->SetURL(url); |
337 Add(modifiable_url); | 343 Add(modifiable_url); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 } | 393 } |
388 | 394 |
389 if (!GetTemplateURLForExtension(extension)) { | 395 if (!GetTemplateURLForExtension(extension)) { |
390 TemplateURLData data; | 396 TemplateURLData data; |
391 data.short_name = UTF8ToUTF16(extension->name()); | 397 data.short_name = UTF8ToUTF16(extension->name()); |
392 data.SetKeyword(UTF8ToUTF16(extension->omnibox_keyword())); | 398 data.SetKeyword(UTF8ToUTF16(extension->omnibox_keyword())); |
393 // This URL is not actually used for navigation. It holds the extension's | 399 // This URL is not actually used for navigation. It holds the extension's |
394 // ID, as well as forcing the TemplateURL to be treated as a search keyword. | 400 // ID, as well as forcing the TemplateURL to be treated as a search keyword. |
395 data.SetURL(std::string(chrome::kExtensionScheme) + "://" + | 401 data.SetURL(std::string(chrome::kExtensionScheme) + "://" + |
396 extension->id() + "/?q={searchTerms}"); | 402 extension->id() + "/?q={searchTerms}"); |
397 Add(new TemplateURL(data)); | 403 Add(new TemplateURL(profile_, data)); |
398 } | 404 } |
399 } | 405 } |
400 | 406 |
401 void TemplateURLService::UnregisterExtensionKeyword( | 407 void TemplateURLService::UnregisterExtensionKeyword( |
402 const Extension* extension) { | 408 const Extension* extension) { |
403 if (loaded_) { | 409 if (loaded_) { |
404 const TemplateURL* url = GetTemplateURLForExtension(extension); | 410 const TemplateURL* url = GetTemplateURLForExtension(extension); |
405 if (url) | 411 if (url) |
406 Remove(url); | 412 Remove(url); |
407 } else { | 413 } else { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 TemplateURLData data(url->data()); | 454 TemplateURLData data(url->data()); |
449 data.short_name = title; | 455 data.short_name = title; |
450 data.SetKeyword(keyword); | 456 data.SetKeyword(keyword); |
451 if (search_url != data.url()) { | 457 if (search_url != data.url()) { |
452 data.SetURL(search_url); | 458 data.SetURL(search_url); |
453 // The urls have changed, reset the favicon url. | 459 // The urls have changed, reset the favicon url. |
454 data.favicon_url = GURL(); | 460 data.favicon_url = GURL(); |
455 } | 461 } |
456 data.safe_for_autoreplace = false; | 462 data.safe_for_autoreplace = false; |
457 data.last_modified = time_provider_(); | 463 data.last_modified = time_provider_(); |
458 TemplateURL new_url(data); | 464 TemplateURL new_url(const_cast<TemplateURL*>(url)->profile(), data); |
459 UpdateNoNotify(url, new_url); | 465 UpdateNoNotify(url, new_url); |
460 NotifyObservers(); | 466 NotifyObservers(); |
461 } | 467 } |
462 | 468 |
463 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { | 469 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { |
464 return url != GetDefaultSearchProvider() && | 470 return url != GetDefaultSearchProvider() && |
465 url->url_ref().SupportsReplacement() && !is_default_search_managed(); | 471 url->url_ref().SupportsReplacement() && !is_default_search_managed(); |
466 } | 472 } |
467 | 473 |
468 void TemplateURLService::SetDefaultSearchProvider(const TemplateURL* url) { | 474 void TemplateURLService::SetDefaultSearchProvider(const TemplateURL* url) { |
(...skipping 11 matching lines...) Expand all Loading... |
480 if (loaded_ && !load_failed_) | 486 if (loaded_ && !load_failed_) |
481 return default_search_provider_; | 487 return default_search_provider_; |
482 | 488 |
483 // We're not loaded, rely on the default search provider stored in prefs. | 489 // We're not loaded, rely on the default search provider stored in prefs. |
484 return initial_default_search_provider_.get(); | 490 return initial_default_search_provider_.get(); |
485 } | 491 } |
486 | 492 |
487 const TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() { | 493 const TemplateURL* TemplateURLService::FindNewDefaultSearchProvider() { |
488 // See if the prepopulated default still exists. | 494 // See if the prepopulated default still exists. |
489 scoped_ptr<TemplateURL> prepopulated_default( | 495 scoped_ptr<TemplateURL> prepopulated_default( |
490 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(GetPrefs())); | 496 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(profile_)); |
491 for (TemplateURLVector::iterator i = template_urls_.begin(); | 497 for (TemplateURLVector::iterator i = template_urls_.begin(); |
492 i != template_urls_.end(); ++i) { | 498 i != template_urls_.end(); ++i) { |
493 if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id()) | 499 if ((*i)->prepopulate_id() == prepopulated_default->prepopulate_id()) |
494 return *i; | 500 return *i; |
495 } | 501 } |
496 // If not, use the first non-extension keyword of the templates. | 502 // If not, use the first non-extension keyword of the templates. |
497 for (TemplateURLVector::const_iterator i(template_urls_.begin()); | 503 for (TemplateURLVector::const_iterator i(template_urls_.begin()); |
498 i != template_urls_.end(); ++i) { | 504 i != template_urls_.end(); ++i) { |
499 if (!(*i)->IsExtensionKeyword()) | 505 if (!(*i)->IsExtensionKeyword()) |
500 return *i; | 506 return *i; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
542 } | 548 } |
543 | 549 |
544 // initial_default_search_provider_ is only needed before we've finished | 550 // initial_default_search_provider_ is only needed before we've finished |
545 // loading. Now that we've loaded we can nuke it. | 551 // loading. Now that we've loaded we can nuke it. |
546 initial_default_search_provider_.reset(); | 552 initial_default_search_provider_.reset(); |
547 is_default_search_managed_ = false; | 553 is_default_search_managed_ = false; |
548 | 554 |
549 std::vector<TemplateURL*> template_urls; | 555 std::vector<TemplateURL*> template_urls; |
550 const TemplateURL* default_search_provider = NULL; | 556 const TemplateURL* default_search_provider = NULL; |
551 int new_resource_keyword_version = 0; | 557 int new_resource_keyword_version = 0; |
552 GetSearchProvidersUsingKeywordResult(*result, service_.get(), GetPrefs(), | 558 GetSearchProvidersUsingKeywordResult(*result, service_.get(), profile_, |
553 &template_urls, &default_search_provider, &new_resource_keyword_version); | 559 &template_urls, &default_search_provider, &new_resource_keyword_version); |
554 | 560 |
555 bool database_specified_a_default = (default_search_provider != NULL); | 561 bool database_specified_a_default = (default_search_provider != NULL); |
556 | 562 |
557 // Check if default search provider is now managed. | 563 // Check if default search provider is now managed. |
558 scoped_ptr<TemplateURL> default_from_prefs; | 564 scoped_ptr<TemplateURL> default_from_prefs; |
559 LoadDefaultSearchProviderFromPrefs(&default_from_prefs, | 565 LoadDefaultSearchProviderFromPrefs(&default_from_prefs, |
560 &is_default_search_managed_); | 566 &is_default_search_managed_); |
561 | 567 |
562 // Check if the default search provider has been changed in Web Data by | 568 // Check if the default search provider has been changed in Web Data by |
563 // another program. No immediate action is performed because the default | 569 // another program. No immediate action is performed because the default |
564 // search may be changed below by Sync which effectively undoes the hijacking. | 570 // search may be changed below by Sync which effectively undoes the hijacking. |
565 bool is_default_search_hijacked = false; | 571 bool is_default_search_hijacked = false; |
566 const TemplateURL* hijacked_default_search_provider = NULL; | 572 const TemplateURL* hijacked_default_search_provider = NULL; |
567 scoped_ptr<TemplateURL> backup_default_search_provider; | 573 scoped_ptr<TemplateURL> backup_default_search_provider; |
568 // No check is required if the default search is managed. | 574 // No check is required if the default search is managed. |
569 // |DidDefaultSearchProviderChange| must always be called because it will | 575 // |DidDefaultSearchProviderChange| must always be called because it will |
570 // take care of the unowned backup default search provider instance. | 576 // take care of the unowned backup default search provider instance. |
571 if (DidDefaultSearchProviderChange(*result, | 577 if (DidDefaultSearchProviderChange(*result, profile_, |
572 &backup_default_search_provider) && | 578 &backup_default_search_provider) && |
573 !is_default_search_managed_) { | 579 !is_default_search_managed_) { |
574 hijacked_default_search_provider = default_search_provider; | 580 hijacked_default_search_provider = default_search_provider; |
575 is_default_search_hijacked = true; | 581 is_default_search_hijacked = true; |
576 } | 582 } |
577 | 583 |
578 // Remove entries that were created because of policy as they may have | 584 // Remove entries that were created because of policy as they may have |
579 // changed since the database was saved. | 585 // changed since the database was saved. |
580 RemoveProvidersCreatedByPolicy(&template_urls, | 586 RemoveProvidersCreatedByPolicy(&template_urls, |
581 &default_search_provider, | 587 &default_search_provider, |
582 default_from_prefs.get()); | 588 default_from_prefs.get()); |
583 | 589 |
584 PatchMissingSyncGUIDs(&template_urls); | 590 PatchMissingSyncGUIDs(&template_urls); |
585 | 591 |
586 if (is_default_search_managed_) { | 592 if (is_default_search_managed_) { |
587 SetTemplateURLs(template_urls); | 593 SetTemplateURLs(template_urls); |
588 | 594 |
589 if (TemplateURLsHaveSamePrefs(default_search_provider, | 595 if (TemplateURLsHaveSamePrefs(default_search_provider, |
590 default_from_prefs.get())) { | 596 default_from_prefs.get())) { |
591 // The value from the preferences was previously stored in the database. | 597 // The value from the preferences was previously stored in the database. |
592 // Reuse it. | 598 // Reuse it. |
593 } else { | 599 } else { |
594 // The value from the preferences takes over. | 600 // The value from the preferences takes over. |
595 default_search_provider = NULL; | 601 default_search_provider = NULL; |
596 if (default_from_prefs.get()) { | 602 if (default_from_prefs.get()) { |
597 TemplateURLData data(default_from_prefs->data()); | 603 TemplateURLData data(default_from_prefs->data()); |
598 data.created_by_policy = true; | 604 data.created_by_policy = true; |
599 data.id = kInvalidTemplateURLID; | 605 data.id = kInvalidTemplateURLID; |
600 TemplateURL* managed_default = new TemplateURL(data); | 606 TemplateURL* managed_default = new TemplateURL(profile_, data); |
601 AddNoNotify(managed_default, true); | 607 AddNoNotify(managed_default, true); |
602 default_search_provider = managed_default; | 608 default_search_provider = managed_default; |
603 } | 609 } |
604 } | 610 } |
605 // Note that this saves the default search provider to prefs. | 611 // Note that this saves the default search provider to prefs. |
606 if (!default_search_provider || | 612 if (!default_search_provider || |
607 !default_search_provider->IsExtensionKeyword()) | 613 !default_search_provider->IsExtensionKeyword()) |
608 SetDefaultSearchProviderNoNotify(default_search_provider); | 614 SetDefaultSearchProviderNoNotify(default_search_provider); |
609 } else { | 615 } else { |
610 // If we had a managed default, replace it with the synced default if | 616 // If we had a managed default, replace it with the synced default if |
611 // applicable, or the first provider of the list. | 617 // applicable, or the first provider of the list. |
612 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); | 618 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); |
613 if (synced_default) { | 619 if (synced_default) { |
614 default_search_provider = synced_default; | 620 default_search_provider = synced_default; |
615 pending_synced_default_search_ = false; | 621 pending_synced_default_search_ = false; |
616 } else if (database_specified_a_default && | 622 } else if (database_specified_a_default && |
617 default_search_provider == NULL) { | 623 default_search_provider == NULL) { |
618 for (std::vector<TemplateURL*>::const_iterator i = template_urls.begin(); | 624 for (std::vector<TemplateURL*>::const_iterator i = template_urls.begin(); |
619 i != template_urls.end(); ++i) { | 625 i != template_urls.end(); ++i) { |
620 if (!(*i)->IsExtensionKeyword()) { | 626 if (!(*i)->IsExtensionKeyword() && (*i)->SupportsReplacement()) { |
621 default_search_provider = *i; | 627 default_search_provider = *i; |
622 break; | 628 break; |
623 } | 629 } |
624 } | 630 } |
625 } | 631 } |
626 | 632 |
627 // If the default search provider existed previously, then just | 633 // If the default search provider existed previously, then just |
628 // set the member variable. Otherwise, we'll set it using the method | 634 // set the member variable. Otherwise, we'll set it using the method |
629 // to ensure that it is saved properly after its id is set. | 635 // to ensure that it is saved properly after its id is set. |
630 if (default_search_provider && | 636 if (default_search_provider && |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
784 SyncChangeList new_changes; | 790 SyncChangeList new_changes; |
785 SyncError error; | 791 SyncError error; |
786 for (SyncChangeList::const_iterator iter = change_list.begin(); | 792 for (SyncChangeList::const_iterator iter = change_list.begin(); |
787 iter != change_list.end(); ++iter) { | 793 iter != change_list.end(); ++iter) { |
788 DCHECK_EQ(syncable::SEARCH_ENGINES, iter->sync_data().GetDataType()); | 794 DCHECK_EQ(syncable::SEARCH_ENGINES, iter->sync_data().GetDataType()); |
789 | 795 |
790 std::string guid = | 796 std::string guid = |
791 iter->sync_data().GetSpecifics().search_engine().sync_guid(); | 797 iter->sync_data().GetSpecifics().search_engine().sync_guid(); |
792 const TemplateURL* existing_turl = GetTemplateURLForGUID(guid); | 798 const TemplateURL* existing_turl = GetTemplateURLForGUID(guid); |
793 scoped_ptr<TemplateURL> turl(CreateTemplateURLFromTemplateURLAndSyncData( | 799 scoped_ptr<TemplateURL> turl(CreateTemplateURLFromTemplateURLAndSyncData( |
794 existing_turl, iter->sync_data(), &new_changes)); | 800 profile_, existing_turl, iter->sync_data(), &new_changes)); |
795 if (!turl.get()) | 801 if (!turl.get()) |
796 continue; | 802 continue; |
797 | 803 |
798 const TemplateURL* existing_keyword_turl = | 804 const TemplateURL* existing_keyword_turl = |
799 GetTemplateURLForKeyword(turl->keyword()); | 805 GetTemplateURLForKeyword(turl->keyword()); |
800 | 806 |
801 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) { | 807 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) { |
802 bool delete_default = (existing_turl == GetDefaultSearchProvider()); | 808 bool delete_default = (existing_turl == GetDefaultSearchProvider()); |
803 | 809 |
804 if (delete_default && is_default_search_managed_) { | 810 if (delete_default && is_default_search_managed_) { |
805 NOTREACHED() << "Tried to delete managed default search provider"; | 811 NOTREACHED() << "Tried to delete managed default search provider"; |
806 } else { | 812 } else { |
807 if (delete_default) | 813 if (delete_default) |
808 default_search_provider_ = NULL; | 814 default_search_provider_ = NULL; |
809 | 815 |
810 Remove(existing_turl); | 816 Remove(existing_turl); |
811 | 817 |
812 if (delete_default) | 818 if (delete_default) |
813 SetDefaultSearchProvider(FindNewDefaultSearchProvider()); | 819 SetDefaultSearchProvider(FindNewDefaultSearchProvider()); |
814 } | 820 } |
815 } else if (iter->change_type() == SyncChange::ACTION_ADD && | 821 } else if (iter->change_type() == SyncChange::ACTION_ADD && |
816 !existing_turl) { | 822 !existing_turl) { |
817 std::string guid = turl->sync_guid(); | 823 std::string guid = turl->sync_guid(); |
818 if (existing_keyword_turl) | 824 if (existing_keyword_turl) |
819 ResolveSyncKeywordConflict(turl.get(), &new_changes); | 825 ResolveSyncKeywordConflict(turl.get(), &new_changes); |
820 // Force the local ID to kInvalidTemplateURLID so we can add it. | 826 // Force the local ID to kInvalidTemplateURLID so we can add it. |
821 TemplateURLData data(turl->data()); | 827 TemplateURLData data(turl->data()); |
822 data.id = kInvalidTemplateURLID; | 828 data.id = kInvalidTemplateURLID; |
823 Add(new TemplateURL(data)); | 829 Add(new TemplateURL(profile_, data)); |
824 | 830 |
825 // Possibly set the newly added |turl| as the default search provider. | 831 // Possibly set the newly added |turl| as the default search provider. |
826 SetDefaultSearchProviderIfNewlySynced(guid); | 832 SetDefaultSearchProviderIfNewlySynced(guid); |
827 } else if (iter->change_type() == SyncChange::ACTION_UPDATE && | 833 } else if (iter->change_type() == SyncChange::ACTION_UPDATE && |
828 existing_turl) { | 834 existing_turl) { |
829 // Possibly resolve a keyword conflict if they have the same keywords but | 835 // Possibly resolve a keyword conflict if they have the same keywords but |
830 // are not the same entry. | 836 // are not the same entry. |
831 if (existing_keyword_turl && existing_keyword_turl != existing_turl) | 837 if (existing_keyword_turl && existing_keyword_turl != existing_turl) |
832 ResolveSyncKeywordConflict(turl.get(), &new_changes); | 838 ResolveSyncKeywordConflict(turl.get(), &new_changes); |
833 UpdateNoNotify(existing_turl, *turl); | 839 UpdateNoNotify(existing_turl, *turl); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 | 890 |
885 // Build maps of our sync GUIDs to SyncData. | 891 // Build maps of our sync GUIDs to SyncData. |
886 SyncDataMap local_data_map = CreateGUIDToSyncDataMap( | 892 SyncDataMap local_data_map = CreateGUIDToSyncDataMap( |
887 GetAllSyncData(syncable::SEARCH_ENGINES)); | 893 GetAllSyncData(syncable::SEARCH_ENGINES)); |
888 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data); | 894 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data); |
889 | 895 |
890 for (SyncDataMap::const_iterator iter = sync_data_map.begin(); | 896 for (SyncDataMap::const_iterator iter = sync_data_map.begin(); |
891 iter != sync_data_map.end(); ++iter) { | 897 iter != sync_data_map.end(); ++iter) { |
892 const TemplateURL* local_turl = GetTemplateURLForGUID(iter->first); | 898 const TemplateURL* local_turl = GetTemplateURLForGUID(iter->first); |
893 scoped_ptr<TemplateURL> sync_turl( | 899 scoped_ptr<TemplateURL> sync_turl( |
894 CreateTemplateURLFromTemplateURLAndSyncData(local_turl, iter->second, | 900 CreateTemplateURLFromTemplateURLAndSyncData(profile_, local_turl, |
895 &new_changes)); | 901 iter->second, &new_changes)); |
896 if (!sync_turl.get()) | 902 if (!sync_turl.get()) |
897 continue; | 903 continue; |
898 | 904 |
899 if (local_turl) { | 905 if (local_turl) { |
900 // This local search engine is already synced. If the timestamp differs | 906 // This local search engine is already synced. If the timestamp differs |
901 // from Sync, we need to update locally or to the cloud. Note that if the | 907 // from Sync, we need to update locally or to the cloud. Note that if the |
902 // timestamps are equal, we touch neither. | 908 // timestamps are equal, we touch neither. |
903 if (sync_turl->last_modified() > local_turl->last_modified()) { | 909 if (sync_turl->last_modified() > local_turl->last_modified()) { |
904 // We've received an update from Sync. We should replace all synced | 910 // We've received an update from Sync. We should replace all synced |
905 // fields in the local TemplateURL. Note that this includes the | 911 // fields in the local TemplateURL. Note that this includes the |
(...skipping 24 matching lines...) Expand all Loading... |
930 } else { | 936 } else { |
931 std::string guid = sync_turl->sync_guid(); | 937 std::string guid = sync_turl->sync_guid(); |
932 // Keyword conflict is possible in this case. Resolve it first before | 938 // Keyword conflict is possible in this case. Resolve it first before |
933 // adding the new TemplateURL. Note that we don't remove the local TURL | 939 // adding the new TemplateURL. Note that we don't remove the local TURL |
934 // from local_data_map in this case as it may still need to be pushed to | 940 // from local_data_map in this case as it may still need to be pushed to |
935 // the cloud. | 941 // the cloud. |
936 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes); | 942 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes); |
937 // Force the local ID to kInvalidTemplateURLID so we can add it. | 943 // Force the local ID to kInvalidTemplateURLID so we can add it. |
938 TemplateURLData data(sync_turl->data()); | 944 TemplateURLData data(sync_turl->data()); |
939 data.id = kInvalidTemplateURLID; | 945 data.id = kInvalidTemplateURLID; |
940 Add(new TemplateURL(data)); | 946 Add(new TemplateURL(profile_, data)); |
941 | 947 |
942 // Possibly set the newly added |turl| as the default search provider. | 948 // Possibly set the newly added |turl| as the default search provider. |
943 SetDefaultSearchProviderIfNewlySynced(guid); | 949 SetDefaultSearchProviderIfNewlySynced(guid); |
944 } | 950 } |
945 } | 951 } |
946 } | 952 } |
947 | 953 |
948 // The remaining SyncData in local_data_map should be everything that needs to | 954 // The remaining SyncData in local_data_map should be everything that needs to |
949 // be pushed as ADDs to sync. | 955 // be pushed as ADDs to sync. |
950 for (SyncDataMap::const_iterator iter = local_data_map.begin(); | 956 for (SyncDataMap::const_iterator iter = local_data_map.begin(); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1017 se_specifics->set_instant_url(turl.instant_url()); | 1023 se_specifics->set_instant_url(turl.instant_url()); |
1018 se_specifics->set_last_modified(turl.last_modified().ToInternalValue()); | 1024 se_specifics->set_last_modified(turl.last_modified().ToInternalValue()); |
1019 se_specifics->set_sync_guid(turl.sync_guid()); | 1025 se_specifics->set_sync_guid(turl.sync_guid()); |
1020 return SyncData::CreateLocalData(se_specifics->sync_guid(), | 1026 return SyncData::CreateLocalData(se_specifics->sync_guid(), |
1021 se_specifics->keyword(), | 1027 se_specifics->keyword(), |
1022 specifics); | 1028 specifics); |
1023 } | 1029 } |
1024 | 1030 |
1025 // static | 1031 // static |
1026 TemplateURL* TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( | 1032 TemplateURL* TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( |
| 1033 Profile* profile, |
1027 const TemplateURL* existing_turl, | 1034 const TemplateURL* existing_turl, |
1028 const SyncData& sync_data, | 1035 const SyncData& sync_data, |
1029 SyncChangeList* change_list) { | 1036 SyncChangeList* change_list) { |
| 1037 DCHECK(change_list); |
| 1038 |
1030 sync_pb::SearchEngineSpecifics specifics = | 1039 sync_pb::SearchEngineSpecifics specifics = |
1031 sync_data.GetSpecifics().search_engine(); | 1040 sync_data.GetSpecifics().search_engine(); |
1032 | 1041 |
1033 // Past bugs might have caused either of these fields to be empty. Just | 1042 // Past bugs might have caused either of these fields to be empty. Just |
1034 // delete this data off the server. | 1043 // delete this data off the server. |
1035 if (specifics.url().empty() || specifics.sync_guid().empty()) { | 1044 if (specifics.url().empty() || specifics.sync_guid().empty()) { |
1036 change_list->push_back(SyncChange(SyncChange::ACTION_DELETE, sync_data)); | 1045 change_list->push_back(SyncChange(SyncChange::ACTION_DELETE, sync_data)); |
1037 return NULL; | 1046 return NULL; |
1038 } | 1047 } |
1039 | 1048 |
(...skipping 12 matching lines...) Expand all Loading... |
1052 data.date_created = base::Time::FromInternalValue(specifics.date_created()); | 1061 data.date_created = base::Time::FromInternalValue(specifics.date_created()); |
1053 data.last_modified = base::Time::FromInternalValue(specifics.last_modified()); | 1062 data.last_modified = base::Time::FromInternalValue(specifics.last_modified()); |
1054 data.prepopulate_id = specifics.prepopulate_id(); | 1063 data.prepopulate_id = specifics.prepopulate_id(); |
1055 data.sync_guid = specifics.sync_guid(); | 1064 data.sync_guid = specifics.sync_guid(); |
1056 if (existing_turl) { | 1065 if (existing_turl) { |
1057 data.id = existing_turl->id(); | 1066 data.id = existing_turl->id(); |
1058 data.created_by_policy = existing_turl->created_by_policy(); | 1067 data.created_by_policy = existing_turl->created_by_policy(); |
1059 data.usage_count = existing_turl->usage_count(); | 1068 data.usage_count = existing_turl->usage_count(); |
1060 } | 1069 } |
1061 | 1070 |
1062 TemplateURL* turl = new TemplateURL(data); | 1071 TemplateURL* turl = new TemplateURL(profile, data); |
1063 DCHECK(!turl->IsExtensionKeyword()); | 1072 DCHECK(!turl->IsExtensionKeyword()); |
1064 return turl; | 1073 return turl; |
1065 } | 1074 } |
1066 | 1075 |
1067 // static | 1076 // static |
1068 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap( | 1077 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap( |
1069 const SyncDataList& sync_data) { | 1078 const SyncDataList& sync_data) { |
1070 SyncDataMap data_map; | 1079 SyncDataMap data_map; |
1071 for (SyncDataList::const_iterator i(sync_data.begin()); i != sync_data.end(); | 1080 for (SyncDataList::const_iterator i(sync_data.begin()); i != sync_data.end(); |
1072 ++i) | 1081 ++i) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1120 std::string osd_url(initializers[i].url); | 1129 std::string osd_url(initializers[i].url); |
1121 osd_url.replace(template_position, arraysize(kTemplateParameter) - 1, | 1130 osd_url.replace(template_position, arraysize(kTemplateParameter) - 1, |
1122 kSearchTermParameter); | 1131 kSearchTermParameter); |
1123 | 1132 |
1124 // TemplateURLService ends up owning the TemplateURL, don't try and free | 1133 // TemplateURLService ends up owning the TemplateURL, don't try and free |
1125 // it. | 1134 // it. |
1126 TemplateURLData data; | 1135 TemplateURLData data; |
1127 data.short_name = UTF8ToUTF16(initializers[i].content); | 1136 data.short_name = UTF8ToUTF16(initializers[i].content); |
1128 data.SetKeyword(UTF8ToUTF16(initializers[i].keyword)); | 1137 data.SetKeyword(UTF8ToUTF16(initializers[i].keyword)); |
1129 data.SetURL(osd_url); | 1138 data.SetURL(osd_url); |
1130 AddNoNotify(new TemplateURL(data), true); | 1139 AddNoNotify(new TemplateURL(profile_, data), true); |
1131 } | 1140 } |
1132 } | 1141 } |
1133 | 1142 |
1134 // Initialize default search. | 1143 // Initialize default search. |
1135 UpdateDefaultSearch(); | 1144 UpdateDefaultSearch(); |
1136 | 1145 |
1137 // Request a server check for the correct Google URL if Google is the | 1146 // Request a server check for the correct Google URL if Google is the |
1138 // default search engine, not in headless mode and not in Chrome Frame. | 1147 // default search engine, not in headless mode and not in Chrome Frame. |
1139 if (initial_default_search_provider_.get() && | 1148 if (initial_default_search_provider_.get() && |
1140 initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) { | 1149 initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) { |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1337 if (!id_string.empty() && !*is_managed) { | 1346 if (!id_string.empty() && !*is_managed) { |
1338 int64 value; | 1347 int64 value; |
1339 base::StringToInt64(id_string, &value); | 1348 base::StringToInt64(id_string, &value); |
1340 data.id = value; | 1349 data.id = value; |
1341 } | 1350 } |
1342 if (!prepopulate_id.empty() && !*is_managed) { | 1351 if (!prepopulate_id.empty() && !*is_managed) { |
1343 int value; | 1352 int value; |
1344 base::StringToInt(prepopulate_id, &value); | 1353 base::StringToInt(prepopulate_id, &value); |
1345 data.prepopulate_id = value; | 1354 data.prepopulate_id = value; |
1346 } | 1355 } |
1347 default_provider->reset(new TemplateURL(data)); | 1356 default_provider->reset(new TemplateURL(profile_, data)); |
1348 DCHECK(!(*default_provider)->IsExtensionKeyword()); | 1357 DCHECK(!(*default_provider)->IsExtensionKeyword()); |
1349 return true; | 1358 return true; |
1350 } | 1359 } |
1351 | 1360 |
1352 bool TemplateURLService::CanReplaceKeywordForHost( | 1361 bool TemplateURLService::CanReplaceKeywordForHost( |
1353 const std::string& host, | 1362 const std::string& host, |
1354 const TemplateURL** to_replace) { | 1363 const TemplateURL** to_replace) { |
1355 DCHECK(!to_replace || !*to_replace); | 1364 DCHECK(!to_replace || !*to_replace); |
1356 const TemplateURLSet* urls = provider_map_.GetURLsForHost(host); | 1365 const TemplateURLSet* urls = provider_map_.GetURLsForHost(host); |
1357 if (!urls) | 1366 if (!urls) |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1560 | 1569 |
1561 void TemplateURLService::UpdateDefaultSearch() { | 1570 void TemplateURLService::UpdateDefaultSearch() { |
1562 if (!loaded_) { | 1571 if (!loaded_) { |
1563 // Set |initial_default_search_provider_| from the preferences. We use this | 1572 // Set |initial_default_search_provider_| from the preferences. We use this |
1564 // value for default search provider until the database has been loaded. | 1573 // value for default search provider until the database has been loaded. |
1565 if (!LoadDefaultSearchProviderFromPrefs(&initial_default_search_provider_, | 1574 if (!LoadDefaultSearchProviderFromPrefs(&initial_default_search_provider_, |
1566 &is_default_search_managed_)) { | 1575 &is_default_search_managed_)) { |
1567 // Prefs does not specify, so rely on the prepopulated engines. This | 1576 // Prefs does not specify, so rely on the prepopulated engines. This |
1568 // should happen only the first time Chrome is started. | 1577 // should happen only the first time Chrome is started. |
1569 initial_default_search_provider_.reset( | 1578 initial_default_search_provider_.reset( |
1570 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(GetPrefs())); | 1579 TemplateURLPrepopulateData::GetPrepopulatedDefaultSearch(profile_)); |
1571 is_default_search_managed_ = false; | 1580 is_default_search_managed_ = false; |
1572 } | 1581 } |
1573 return; | 1582 return; |
1574 } | 1583 } |
1575 // Load the default search specified in prefs. | 1584 // Load the default search specified in prefs. |
1576 scoped_ptr<TemplateURL> new_default_from_prefs; | 1585 scoped_ptr<TemplateURL> new_default_from_prefs; |
1577 bool new_is_default_managed = false; | 1586 bool new_is_default_managed = false; |
1578 // Load the default from prefs. It's possible that it won't succeed | 1587 // Load the default from prefs. It's possible that it won't succeed |
1579 // because we are in the middle of doing SaveDefaultSearchProviderToPrefs() | 1588 // because we are in the middle of doing SaveDefaultSearchProviderToPrefs() |
1580 // and all the preference items have not been saved. In that case, we | 1589 // and all the preference items have not been saved. In that case, we |
(...skipping 16 matching lines...) Expand all Loading... |
1597 if (new_default_from_prefs.get() == NULL) { | 1606 if (new_default_from_prefs.get() == NULL) { |
1598 // default_search_provider_ can't be NULL otherwise | 1607 // default_search_provider_ can't be NULL otherwise |
1599 // TemplateURLsHaveSamePrefs would have returned true. Remove this now | 1608 // TemplateURLsHaveSamePrefs would have returned true. Remove this now |
1600 // invalid value. | 1609 // invalid value. |
1601 const TemplateURL* old_default = default_search_provider_; | 1610 const TemplateURL* old_default = default_search_provider_; |
1602 SetDefaultSearchProviderNoNotify(NULL); | 1611 SetDefaultSearchProviderNoNotify(NULL); |
1603 RemoveNoNotify(old_default); | 1612 RemoveNoNotify(old_default); |
1604 } else if (default_search_provider_) { | 1613 } else if (default_search_provider_) { |
1605 TemplateURLData data(new_default_from_prefs->data()); | 1614 TemplateURLData data(new_default_from_prefs->data()); |
1606 data.created_by_policy = true; | 1615 data.created_by_policy = true; |
1607 TemplateURL new_values(data); | 1616 TemplateURL new_values(profile_, data); |
1608 UpdateNoNotify(default_search_provider_, new_values); | 1617 UpdateNoNotify(default_search_provider_, new_values); |
1609 } else { | 1618 } else { |
1610 TemplateURL* new_template = NULL; | 1619 TemplateURL* new_template = NULL; |
1611 if (new_default_from_prefs.get()) { | 1620 if (new_default_from_prefs.get()) { |
1612 TemplateURLData data(new_default_from_prefs->data()); | 1621 TemplateURLData data(new_default_from_prefs->data()); |
1613 data.created_by_policy = true; | 1622 data.created_by_policy = true; |
1614 new_template = new TemplateURL(data); | 1623 new_template = new TemplateURL(profile_, data); |
1615 AddNoNotify(new_template, true); | 1624 AddNoNotify(new_template, true); |
1616 } | 1625 } |
1617 SetDefaultSearchProviderNoNotify(new_template); | 1626 SetDefaultSearchProviderNoNotify(new_template); |
1618 } | 1627 } |
1619 } else if (!is_default_search_managed_ && new_is_default_managed) { | 1628 } else if (!is_default_search_managed_ && new_is_default_managed) { |
1620 // The default used to be unmanaged and is now managed. Add the new | 1629 // The default used to be unmanaged and is now managed. Add the new |
1621 // managed default to the list of URLs and set it as default. | 1630 // managed default to the list of URLs and set it as default. |
1622 is_default_search_managed_ = new_is_default_managed; | 1631 is_default_search_managed_ = new_is_default_managed; |
1623 TemplateURL* new_template = NULL; | 1632 TemplateURL* new_template = NULL; |
1624 if (new_default_from_prefs.get()) { | 1633 if (new_default_from_prefs.get()) { |
1625 TemplateURLData data(new_default_from_prefs->data()); | 1634 TemplateURLData data(new_default_from_prefs->data()); |
1626 data.created_by_policy = true; | 1635 data.created_by_policy = true; |
1627 new_template = new TemplateURL(data); | 1636 new_template = new TemplateURL(profile_, data); |
1628 AddNoNotify(new_template, true); | 1637 AddNoNotify(new_template, true); |
1629 } | 1638 } |
1630 SetDefaultSearchProviderNoNotify(new_template); | 1639 SetDefaultSearchProviderNoNotify(new_template); |
1631 } else { | 1640 } else { |
1632 // The default was managed and is no longer. | 1641 // The default was managed and is no longer. |
1633 DCHECK(is_default_search_managed_ && !new_is_default_managed); | 1642 DCHECK(is_default_search_managed_ && !new_is_default_managed); |
1634 is_default_search_managed_ = new_is_default_managed; | 1643 is_default_search_managed_ = new_is_default_managed; |
1635 // If we had a default, delete the previous default if created by policy | 1644 // If we had a default, delete the previous default if created by policy |
1636 // and set a likely default. | 1645 // and set a likely default. |
1637 if ((default_search_provider_ != NULL) && | 1646 if ((default_search_provider_ != NULL) && |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1827 } | 1836 } |
1828 } | 1837 } |
1829 } | 1838 } |
1830 | 1839 |
1831 void TemplateURLService::ResetTemplateURLGUID(const TemplateURL* url, | 1840 void TemplateURLService::ResetTemplateURLGUID(const TemplateURL* url, |
1832 const std::string& guid) { | 1841 const std::string& guid) { |
1833 DCHECK(!guid.empty()); | 1842 DCHECK(!guid.empty()); |
1834 | 1843 |
1835 TemplateURLData data(url->data()); | 1844 TemplateURLData data(url->data()); |
1836 data.sync_guid = guid; | 1845 data.sync_guid = guid; |
1837 TemplateURL new_url(data); | 1846 TemplateURL new_url(const_cast<TemplateURL*>(url)->profile(), data); |
1838 UpdateNoNotify(url, new_url); | 1847 UpdateNoNotify(url, new_url); |
1839 } | 1848 } |
1840 | 1849 |
1841 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl) const { | 1850 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl) const { |
1842 // Already unique. | 1851 // Already unique. |
1843 if (!GetTemplateURLForKeyword(turl.keyword())) | 1852 if (!GetTemplateURLForKeyword(turl.keyword())) |
1844 return turl.keyword(); | 1853 return turl.keyword(); |
1845 | 1854 |
1846 // First, try to return the generated keyword for the TemplateURL. | 1855 // First, try to return the generated keyword for the TemplateURL. |
1847 GURL gurl(turl.url()); | 1856 GURL gurl(turl.url()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1881 DCHECK(!GetTemplateURLForKeyword(new_keyword)); | 1890 DCHECK(!GetTemplateURLForKeyword(new_keyword)); |
1882 sync_turl->data_.SetKeyword(new_keyword); | 1891 sync_turl->data_.SetKeyword(new_keyword); |
1883 // If we update the cloud TURL, we need to push an update back to sync | 1892 // If we update the cloud TURL, we need to push an update back to sync |
1884 // informing it that something has changed. | 1893 // informing it that something has changed. |
1885 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); | 1894 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); |
1886 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 1895 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); |
1887 } else { | 1896 } else { |
1888 string16 new_keyword = UniquifyKeyword(*existing_turl); | 1897 string16 new_keyword = UniquifyKeyword(*existing_turl); |
1889 TemplateURLData data(existing_turl->data()); | 1898 TemplateURLData data(existing_turl->data()); |
1890 data.SetKeyword(new_keyword); | 1899 data.SetKeyword(new_keyword); |
1891 TemplateURL new_turl(data); | 1900 TemplateURL new_turl(const_cast<TemplateURL*>(existing_turl)->profile(), |
| 1901 data); |
1892 UpdateNoNotify(existing_turl, new_turl); | 1902 UpdateNoNotify(existing_turl, new_turl); |
1893 NotifyObservers(); | 1903 NotifyObservers(); |
1894 } | 1904 } |
1895 return true; | 1905 return true; |
1896 } | 1906 } |
1897 | 1907 |
1898 const TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( | 1908 const TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( |
1899 const TemplateURL& sync_turl) { | 1909 const TemplateURL& sync_turl) { |
1900 const TemplateURL* existing_turl = | 1910 const TemplateURL* existing_turl = |
1901 GetTemplateURLForKeyword(sync_turl.keyword()); | 1911 GetTemplateURLForKeyword(sync_turl.keyword()); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1978 // TODO(mpcomplete): If we allow editing extension keywords, then those | 1988 // TODO(mpcomplete): If we allow editing extension keywords, then those |
1979 // should be persisted to disk and synced. | 1989 // should be persisted to disk and synced. |
1980 if (template_url->sync_guid().empty() && | 1990 if (template_url->sync_guid().empty() && |
1981 !template_url->IsExtensionKeyword()) { | 1991 !template_url->IsExtensionKeyword()) { |
1982 template_url->data_.sync_guid = guid::GenerateGUID(); | 1992 template_url->data_.sync_guid = guid::GenerateGUID(); |
1983 if (service_.get()) | 1993 if (service_.get()) |
1984 service_->UpdateKeyword(*template_url); | 1994 service_->UpdateKeyword(*template_url); |
1985 } | 1995 } |
1986 } | 1996 } |
1987 } | 1997 } |
OLD | NEW |