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 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 const TemplateURL* TemplateURLService::GetTemplateURLForHost( | 308 const TemplateURL* TemplateURLService::GetTemplateURLForHost( |
309 const std::string& host) const { | 309 const std::string& host) const { |
310 return provider_map_.GetTemplateURLForHost(host); | 310 return provider_map_.GetTemplateURLForHost(host); |
311 } | 311 } |
312 | 312 |
313 void TemplateURLService::Add(TemplateURL* template_url) { | 313 void TemplateURLService::Add(TemplateURL* template_url) { |
314 AddNoNotify(template_url); | 314 AddNoNotify(template_url); |
315 NotifyObservers(); | 315 NotifyObservers(); |
316 } | 316 } |
317 | 317 |
| 318 void TemplateURLService::AddWithOverrides(const TemplateURL* template_url, |
| 319 const string16& short_name, |
| 320 const string16& keyword, |
| 321 const std::string& url) { |
| 322 TemplateURL* modifiable_url = const_cast<TemplateURL*>(template_url); |
| 323 modifiable_url->data_.short_name = short_name; |
| 324 modifiable_url->data_.SetKeyword(keyword); |
| 325 modifiable_url->SetURL(url); |
| 326 Add(modifiable_url); |
| 327 } |
| 328 |
318 void TemplateURLService::Remove(const TemplateURL* template_url) { | 329 void TemplateURLService::Remove(const TemplateURL* template_url) { |
319 RemoveNoNotify(template_url); | 330 RemoveNoNotify(template_url); |
320 NotifyObservers(); | 331 NotifyObservers(); |
321 } | 332 } |
322 | 333 |
323 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { | 334 void TemplateURLService::RemoveAutoGeneratedSince(base::Time created_after) { |
324 RemoveAutoGeneratedBetween(created_after, base::Time()); | 335 RemoveAutoGeneratedBetween(created_after, base::Time()); |
325 } | 336 } |
326 | 337 |
327 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, | 338 void TemplateURLService::RemoveAutoGeneratedBetween(base::Time created_after, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 | 371 |
361 Load(); | 372 Load(); |
362 if (!loaded_) { | 373 if (!loaded_) { |
363 pending_extension_ids_.push_back(extension->id()); | 374 pending_extension_ids_.push_back(extension->id()); |
364 return; | 375 return; |
365 } | 376 } |
366 | 377 |
367 const TemplateURL* existing_url = GetTemplateURLForExtension(extension); | 378 const TemplateURL* existing_url = GetTemplateURLForExtension(extension); |
368 string16 keyword = UTF8ToUTF16(extension->omnibox_keyword()); | 379 string16 keyword = UTF8ToUTF16(extension->omnibox_keyword()); |
369 | 380 |
370 scoped_ptr<TemplateURL> template_url(new TemplateURL); | 381 TemplateURLData data; |
371 template_url->set_short_name(UTF8ToUTF16(extension->name())); | 382 data.short_name = UTF8ToUTF16(extension->name()); |
372 template_url->set_keyword(keyword); | 383 data.SetKeyword(UTF8ToUTF16(extension->omnibox_keyword())); |
373 // This URL is not actually used for navigation. It holds the extension's | 384 // This URL is not actually used for navigation. It holds the extension's |
374 // ID, as well as forcing the TemplateURL to be treated as a search keyword. | 385 // ID, as well as forcing the TemplateURL to be treated as a search keyword. |
375 template_url->SetURL( | 386 data.SetURL(std::string(chrome::kExtensionScheme) + "://" + extension->id() + |
376 std::string(chrome::kExtensionScheme) + "://" + | 387 "/?q={searchTerms}"); |
377 extension->id() + "/?q={searchTerms}"); | 388 scoped_ptr<TemplateURL> template_url(new TemplateURL(data)); |
378 template_url->set_safe_for_autoreplace(false); | |
379 | 389 |
380 if (existing_url) { | 390 if (existing_url) { |
381 // TODO(mpcomplete): only replace if the user hasn't changed the keyword. | 391 // TODO(mpcomplete): only replace if the user hasn't changed the keyword. |
382 // (We don't have UI for that yet). | 392 // (We don't have UI for that yet). |
383 UpdateNoNotify(existing_url, *template_url); | 393 UpdateNoNotify(existing_url, *template_url); |
384 } else { | 394 } else { |
385 AddNoNotify(template_url.release()); | 395 AddNoNotify(template_url.release()); |
386 } | 396 } |
387 NotifyObservers(); | 397 NotifyObservers(); |
388 } | 398 } |
(...skipping 17 matching lines...) Expand all Loading... |
406 return NULL; | 416 return NULL; |
407 } | 417 } |
408 | 418 |
409 std::vector<const TemplateURL*> TemplateURLService::GetTemplateURLs() const { | 419 std::vector<const TemplateURL*> TemplateURLService::GetTemplateURLs() const { |
410 return template_urls_; | 420 return template_urls_; |
411 } | 421 } |
412 | 422 |
413 void TemplateURLService::IncrementUsageCount(const TemplateURL* url) { | 423 void TemplateURLService::IncrementUsageCount(const TemplateURL* url) { |
414 DCHECK(url && std::find(template_urls_.begin(), template_urls_.end(), url) != | 424 DCHECK(url && std::find(template_urls_.begin(), template_urls_.end(), url) != |
415 template_urls_.end()); | 425 template_urls_.end()); |
416 const_cast<TemplateURL*>(url)->set_usage_count(url->usage_count() + 1); | 426 ++const_cast<TemplateURL*>(url)->data_.usage_count; |
417 if (service_.get()) | 427 if (service_.get()) |
418 service_.get()->UpdateKeyword(*url); | 428 service_->UpdateKeyword(*url); |
419 } | 429 } |
420 | 430 |
421 void TemplateURLService::ResetTemplateURL(const TemplateURL* url, | 431 void TemplateURLService::ResetTemplateURL(const TemplateURL* url, |
422 const string16& title, | 432 const string16& title, |
423 const string16& keyword, | 433 const string16& keyword, |
424 const std::string& search_url) { | 434 const std::string& search_url) { |
425 TemplateURL new_url(*url); | 435 TemplateURLData data(url->data()); |
426 new_url.set_short_name(title); | 436 data.short_name = title; |
427 new_url.set_keyword(keyword); | 437 data.SetKeyword(keyword); |
428 if (new_url.url() != search_url) { | 438 if (search_url != data.url()) { |
429 new_url.SetURL(search_url); | 439 data.SetURL(search_url); |
430 // The urls have changed, reset the favicon url. | 440 // The urls have changed, reset the favicon url. |
431 new_url.set_favicon_url(GURL()); | 441 data.favicon_url = GURL(); |
432 } | 442 } |
433 new_url.set_safe_for_autoreplace(false); | 443 data.safe_for_autoreplace = false; |
434 new_url.set_last_modified(time_provider_()); | 444 data.last_modified = time_provider_(); |
| 445 TemplateURL new_url(data); |
435 UpdateNoNotify(url, new_url); | 446 UpdateNoNotify(url, new_url); |
436 NotifyObservers(); | 447 NotifyObservers(); |
437 } | 448 } |
438 | 449 |
439 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { | 450 bool TemplateURLService::CanMakeDefault(const TemplateURL* url) { |
440 return url != GetDefaultSearchProvider() && | 451 return url != GetDefaultSearchProvider() && |
441 url->url_ref().SupportsReplacement() && !is_default_search_managed(); | 452 url->url_ref().SupportsReplacement() && !is_default_search_managed(); |
442 } | 453 } |
443 | 454 |
444 void TemplateURLService::SetDefaultSearchProvider(const TemplateURL* url) { | 455 void TemplateURLService::SetDefaultSearchProvider(const TemplateURL* url) { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 | 571 |
561 if (is_default_search_managed_) { | 572 if (is_default_search_managed_) { |
562 SetTemplateURLs(template_urls); | 573 SetTemplateURLs(template_urls); |
563 | 574 |
564 if (TemplateURLsHaveSamePrefs(default_search_provider, | 575 if (TemplateURLsHaveSamePrefs(default_search_provider, |
565 default_from_prefs.get())) { | 576 default_from_prefs.get())) { |
566 // The value from the preferences was previously stored in the database. | 577 // The value from the preferences was previously stored in the database. |
567 // Reuse it. | 578 // Reuse it. |
568 } else { | 579 } else { |
569 // The value from the preferences takes over. | 580 // The value from the preferences takes over. |
570 // | 581 default_search_provider = NULL; |
571 // AddNoNotify will take ownership of default_from_prefs so it is safe to | 582 if (default_from_prefs.get()) { |
572 // release. If it's null, there's no ownership to worry about :-) | 583 TemplateURLData data(default_from_prefs->data()); |
573 TemplateURL* managed_default = default_from_prefs.release(); | 584 data.created_by_policy = true; |
574 if (managed_default) { | 585 data.id = kInvalidTemplateURLID; |
575 managed_default->set_created_by_policy(true); | 586 TemplateURL* managed_default = new TemplateURL(data); |
576 managed_default->set_id(kInvalidTemplateURLID); | |
577 AddNoNotify(managed_default); | 587 AddNoNotify(managed_default); |
| 588 default_search_provider = managed_default; |
578 } | 589 } |
579 default_search_provider = managed_default; | |
580 } | 590 } |
581 // Note that this saves the default search provider to prefs. | 591 // Note that this saves the default search provider to prefs. |
582 SetDefaultSearchProviderNoNotify(default_search_provider); | 592 SetDefaultSearchProviderNoNotify(default_search_provider); |
583 } else { | 593 } else { |
584 // If we had a managed default, replace it with the synced default if | 594 // If we had a managed default, replace it with the synced default if |
585 // applicable, or the first provider of the list. | 595 // applicable, or the first provider of the list. |
586 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); | 596 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); |
587 if (synced_default) { | 597 if (synced_default) { |
588 default_search_provider = synced_default; | 598 default_search_provider = synced_default; |
589 pending_synced_default_search_ = false; | 599 pending_synced_default_search_ = false; |
590 } else if (database_specified_a_default && | 600 } else if (database_specified_a_default && |
591 NULL == default_search_provider && | 601 default_search_provider == NULL && |
592 !template_urls.empty()) { | 602 !template_urls.empty()) { |
593 default_search_provider = template_urls[0]; | 603 default_search_provider = template_urls[0]; |
594 } | 604 } |
595 | 605 |
596 // If the default search provider existed previously, then just | 606 // If the default search provider existed previously, then just |
597 // set the member variable. Otherwise, we'll set it using the method | 607 // set the member variable. Otherwise, we'll set it using the method |
598 // to ensure that it is saved properly after its id is set. | 608 // to ensure that it is saved properly after its id is set. |
599 if (default_search_provider && | 609 if (default_search_provider && |
600 (default_search_provider->id() != kInvalidTemplateURLID)) { | 610 (default_search_provider->id() != kInvalidTemplateURLID)) { |
601 default_search_provider_ = default_search_provider; | 611 default_search_provider_ = default_search_provider; |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 } | 753 } |
744 | 754 |
745 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); | 755 AutoReset<bool> processing_changes(&processing_syncer_changes_, true); |
746 | 756 |
747 SyncChangeList new_changes; | 757 SyncChangeList new_changes; |
748 SyncError error; | 758 SyncError error; |
749 for (SyncChangeList::const_iterator iter = change_list.begin(); | 759 for (SyncChangeList::const_iterator iter = change_list.begin(); |
750 iter != change_list.end(); ++iter) { | 760 iter != change_list.end(); ++iter) { |
751 DCHECK_EQ(syncable::SEARCH_ENGINES, iter->sync_data().GetDataType()); | 761 DCHECK_EQ(syncable::SEARCH_ENGINES, iter->sync_data().GetDataType()); |
752 | 762 |
753 scoped_ptr<TemplateURL> turl( | 763 std::string guid = |
754 CreateTemplateURLFromSyncData(iter->sync_data())); | 764 iter->sync_data().GetSpecifics().search_engine().sync_guid(); |
755 if (!turl.get()) { | 765 const TemplateURL* existing_turl = GetTemplateURLForGUID(guid); |
756 NOTREACHED() << "Failed to read search engine."; | 766 scoped_ptr<TemplateURL> turl(CreateTemplateURLFromTemplateURLAndSyncData( |
757 continue; | 767 existing_turl, iter->sync_data())); |
758 } | |
759 | 768 |
760 const TemplateURL* existing_turl = GetTemplateURLForGUID(turl->sync_guid()); | |
761 const TemplateURL* existing_keyword_turl = | 769 const TemplateURL* existing_keyword_turl = |
762 GetTemplateURLForKeyword(turl->keyword()); | 770 GetTemplateURLForKeyword(turl->keyword()); |
763 | 771 |
764 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) { | 772 if (iter->change_type() == SyncChange::ACTION_DELETE && existing_turl) { |
765 bool delete_default = (existing_turl == GetDefaultSearchProvider()); | 773 bool delete_default = (existing_turl == GetDefaultSearchProvider()); |
766 | 774 |
767 if (delete_default && is_default_search_managed_) { | 775 if (delete_default && is_default_search_managed_) { |
768 NOTREACHED() << "Tried to delete managed default search provider"; | 776 NOTREACHED() << "Tried to delete managed default search provider"; |
769 } else { | 777 } else { |
770 if (delete_default) | 778 if (delete_default) |
771 default_search_provider_ = NULL; | 779 default_search_provider_ = NULL; |
772 | 780 |
773 Remove(existing_turl); | 781 Remove(existing_turl); |
774 | 782 |
775 if (delete_default) | 783 if (delete_default) |
776 SetDefaultSearchProvider(FindNewDefaultSearchProvider()); | 784 SetDefaultSearchProvider(FindNewDefaultSearchProvider()); |
777 } | 785 } |
778 } else if (iter->change_type() == SyncChange::ACTION_ADD && | 786 } else if (iter->change_type() == SyncChange::ACTION_ADD && |
779 !existing_turl) { | 787 !existing_turl) { |
780 std::string guid = turl->sync_guid(); | 788 std::string guid = turl->sync_guid(); |
781 if (existing_keyword_turl) | 789 if (existing_keyword_turl) |
782 ResolveSyncKeywordConflict(turl.get(), &new_changes); | 790 ResolveSyncKeywordConflict(turl.get(), &new_changes); |
783 // Force the local ID to kInvalidTemplateURLID so we can add it. | 791 // Force the local ID to kInvalidTemplateURLID so we can add it. |
784 turl->set_id(kInvalidTemplateURLID); | 792 TemplateURLData data(turl->data()); |
785 Add(turl.release()); | 793 data.id = kInvalidTemplateURLID; |
| 794 Add(new TemplateURL(data)); |
786 | 795 |
787 // Possibly set the newly added |turl| as the default search provider. | 796 // Possibly set the newly added |turl| as the default search provider. |
788 SetDefaultSearchProviderIfNewlySynced(guid); | 797 SetDefaultSearchProviderIfNewlySynced(guid); |
789 } else if (iter->change_type() == SyncChange::ACTION_UPDATE && | 798 } else if (iter->change_type() == SyncChange::ACTION_UPDATE && |
790 existing_turl) { | 799 existing_turl) { |
791 // Possibly resolve a keyword conflict if they have the same keywords but | 800 // Possibly resolve a keyword conflict if they have the same keywords but |
792 // are not the same entry. | 801 // are not the same entry. |
793 TemplateURL updated_turl(*existing_turl); | |
794 UpdateTemplateURLWithSyncData(&updated_turl, iter->sync_data()); | |
795 if (existing_keyword_turl && existing_keyword_turl != existing_turl) | 802 if (existing_keyword_turl && existing_keyword_turl != existing_turl) |
796 ResolveSyncKeywordConflict(&updated_turl, &new_changes); | 803 ResolveSyncKeywordConflict(turl.get(), &new_changes); |
797 UpdateNoNotify(existing_turl, updated_turl); | 804 UpdateNoNotify(existing_turl, *turl); |
798 NotifyObservers(); | 805 NotifyObservers(); |
799 } else { | 806 } else { |
800 // Something really unexpected happened. Either we received an | 807 // Something really unexpected happened. Either we received an |
801 // ACTION_INVALID, or Sync is in a crazy state: | 808 // ACTION_INVALID, or Sync is in a crazy state: |
802 // . Trying to DELETE or UPDATE a non-existent search engine. | 809 // . Trying to DELETE or UPDATE a non-existent search engine. |
803 // . Trying to ADD a search engine that already exists. | 810 // . Trying to ADD a search engine that already exists. |
804 NOTREACHED() << "Unexpected sync change state."; | 811 NOTREACHED() << "Unexpected sync change state."; |
805 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " + | 812 error = SyncError(FROM_HERE, "ProcessSyncChanges failed on ChangeType " + |
806 SyncChange::ChangeTypeToString(iter->change_type()), | 813 SyncChange::ChangeTypeToString(iter->change_type()), |
807 syncable::SEARCH_ENGINES); | 814 syncable::SEARCH_ENGINES); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 | 853 |
847 SyncChangeList new_changes; | 854 SyncChangeList new_changes; |
848 | 855 |
849 // Build maps of our sync GUIDs to SyncData. | 856 // Build maps of our sync GUIDs to SyncData. |
850 SyncDataMap local_data_map = CreateGUIDToSyncDataMap( | 857 SyncDataMap local_data_map = CreateGUIDToSyncDataMap( |
851 GetAllSyncData(syncable::SEARCH_ENGINES)); | 858 GetAllSyncData(syncable::SEARCH_ENGINES)); |
852 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data); | 859 SyncDataMap sync_data_map = CreateGUIDToSyncDataMap(initial_sync_data); |
853 | 860 |
854 for (SyncDataMap::const_iterator iter = sync_data_map.begin(); | 861 for (SyncDataMap::const_iterator iter = sync_data_map.begin(); |
855 iter != sync_data_map.end(); ++iter) { | 862 iter != sync_data_map.end(); ++iter) { |
| 863 const TemplateURL* local_turl = GetTemplateURLForGUID(iter->first); |
856 scoped_ptr<TemplateURL> sync_turl( | 864 scoped_ptr<TemplateURL> sync_turl( |
857 CreateTemplateURLFromSyncData(iter->second)); | 865 CreateTemplateURLFromTemplateURLAndSyncData(local_turl, iter->second)); |
858 DCHECK(sync_turl.get()); | |
859 const TemplateURL* local_turl = GetTemplateURLForGUID(iter->first); | |
860 | 866 |
861 if (sync_turl->sync_guid().empty()) { | 867 if (sync_turl->sync_guid().empty()) { |
862 // Due to a bug, older search engine entries with no sync GUID | 868 // Due to a bug, older search engine entries with no sync GUID |
863 // may have been uploaded to the server. This is bad data, so | 869 // may have been uploaded to the server. This is bad data, so |
864 // just delete it. | 870 // just delete it. |
865 new_changes.push_back( | 871 new_changes.push_back( |
866 SyncChange(SyncChange::ACTION_DELETE, iter->second)); | 872 SyncChange(SyncChange::ACTION_DELETE, iter->second)); |
867 } else if (local_turl) { | 873 } else if (local_turl) { |
868 // This local search engine is already synced. If the timestamp differs | 874 // This local search engine is already synced. If the timestamp differs |
869 // from Sync, we need to update locally or to the cloud. Note that if the | 875 // from Sync, we need to update locally or to the cloud. Note that if the |
870 // timestamps are equal, we touch neither. | 876 // timestamps are equal, we touch neither. |
871 if (sync_turl->last_modified() > local_turl->last_modified()) { | 877 if (sync_turl->last_modified() > local_turl->last_modified()) { |
872 // We've received an update from Sync. We should replace all synced | 878 // We've received an update from Sync. We should replace all synced |
873 // fields in the local TemplateURL. Note that this includes the | 879 // fields in the local TemplateURL. Note that this includes the |
874 // TemplateURLID and the TemplateURL may have to be reparsed. This | 880 // TemplateURLID and the TemplateURL may have to be reparsed. This |
875 // also makes the local data's last_modified timestamp equal to Sync's, | 881 // also makes the local data's last_modified timestamp equal to Sync's, |
876 // avoiding an Update on the next MergeData call. | 882 // avoiding an Update on the next MergeData call. |
877 TemplateURL updated_turl(*local_turl); | 883 UpdateNoNotify(local_turl, *sync_turl); |
878 UpdateTemplateURLWithSyncData(&updated_turl, iter->second); | |
879 UpdateNoNotify(local_turl, updated_turl); | |
880 NotifyObservers(); | 884 NotifyObservers(); |
881 } else if (sync_turl->last_modified() < local_turl->last_modified()) { | 885 } else if (sync_turl->last_modified() < local_turl->last_modified()) { |
882 // Otherwise, we know we have newer data, so update Sync with our | 886 // Otherwise, we know we have newer data, so update Sync with our |
883 // data fields. | 887 // data fields. |
884 new_changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, | 888 new_changes.push_back(SyncChange(SyncChange::ACTION_UPDATE, |
885 local_data_map[local_turl->sync_guid()])); | 889 local_data_map[local_turl->sync_guid()])); |
886 } | 890 } |
887 local_data_map.erase(iter->first); | 891 local_data_map.erase(iter->first); |
888 } else { | 892 } else { |
889 // The search engine from the cloud has not been synced locally, but there | 893 // The search engine from the cloud has not been synced locally, but there |
(...skipping 10 matching lines...) Expand all Loading... |
900 &new_changes); | 904 &new_changes); |
901 local_data_map.erase(old_guid); | 905 local_data_map.erase(old_guid); |
902 } else { | 906 } else { |
903 std::string guid = sync_turl->sync_guid(); | 907 std::string guid = sync_turl->sync_guid(); |
904 // Keyword conflict is possible in this case. Resolve it first before | 908 // Keyword conflict is possible in this case. Resolve it first before |
905 // adding the new TemplateURL. Note that we don't remove the local TURL | 909 // adding the new TemplateURL. Note that we don't remove the local TURL |
906 // from local_data_map in this case as it may still need to be pushed to | 910 // from local_data_map in this case as it may still need to be pushed to |
907 // the cloud. | 911 // the cloud. |
908 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes); | 912 ResolveSyncKeywordConflict(sync_turl.get(), &new_changes); |
909 // Force the local ID to kInvalidTemplateURLID so we can add it. | 913 // Force the local ID to kInvalidTemplateURLID so we can add it. |
910 sync_turl->set_id(kInvalidTemplateURLID); | 914 TemplateURLData data(sync_turl->data()); |
911 Add(sync_turl.release()); | 915 data.id = kInvalidTemplateURLID; |
| 916 Add(new TemplateURL(data)); |
912 | 917 |
913 // Possibly set the newly added |turl| as the default search provider. | 918 // Possibly set the newly added |turl| as the default search provider. |
914 SetDefaultSearchProviderIfNewlySynced(guid); | 919 SetDefaultSearchProviderIfNewlySynced(guid); |
915 } | 920 } |
916 } | 921 } |
917 } // for | 922 } // for |
918 | 923 |
919 // The remaining SyncData in local_data_map should be everything that needs to | 924 // The remaining SyncData in local_data_map should be everything that needs to |
920 // be pushed as ADDs to sync. | 925 // be pushed as ADDs to sync. |
921 for (SyncDataMap::const_iterator iter = local_data_map.begin(); | 926 for (SyncDataMap::const_iterator iter = local_data_map.begin(); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
985 se_specifics->set_autogenerate_keyword(turl.autogenerate_keyword()); | 990 se_specifics->set_autogenerate_keyword(turl.autogenerate_keyword()); |
986 se_specifics->set_instant_url(turl.instant_url()); | 991 se_specifics->set_instant_url(turl.instant_url()); |
987 se_specifics->set_last_modified(turl.last_modified().ToInternalValue()); | 992 se_specifics->set_last_modified(turl.last_modified().ToInternalValue()); |
988 se_specifics->set_sync_guid(turl.sync_guid()); | 993 se_specifics->set_sync_guid(turl.sync_guid()); |
989 return SyncData::CreateLocalData(se_specifics->sync_guid(), | 994 return SyncData::CreateLocalData(se_specifics->sync_guid(), |
990 se_specifics->keyword(), | 995 se_specifics->keyword(), |
991 specifics); | 996 specifics); |
992 } | 997 } |
993 | 998 |
994 // static | 999 // static |
995 TemplateURL* TemplateURLService::CreateTemplateURLFromSyncData( | 1000 TemplateURL* TemplateURLService::CreateTemplateURLFromTemplateURLAndSyncData( |
| 1001 const TemplateURL* existing_turl, |
996 const SyncData& sync_data) { | 1002 const SyncData& sync_data) { |
997 TemplateURL* turl = new TemplateURL(); | 1003 sync_pb::SearchEngineSpecifics specifics = |
998 UpdateTemplateURLWithSyncData(turl, sync_data); | 1004 sync_data.GetSpecifics().search_engine(); |
999 return turl; | 1005 |
| 1006 TemplateURLData data; |
| 1007 data.short_name = UTF8ToUTF16(specifics.short_name()); |
| 1008 data.originating_url = GURL(specifics.originating_url()); |
| 1009 data.SetKeyword(UTF8ToUTF16(specifics.keyword())); |
| 1010 data.SetAutogenerateKeyword(specifics.autogenerate_keyword()); |
| 1011 data.SetURL(specifics.url()); |
| 1012 data.suggestions_url = specifics.suggestions_url(); |
| 1013 data.instant_url = specifics.instant_url(); |
| 1014 data.favicon_url = GURL(specifics.favicon_url()); |
| 1015 data.show_in_default_list = specifics.show_in_default_list(); |
| 1016 data.safe_for_autoreplace = specifics.safe_for_autoreplace(); |
| 1017 base::SplitString(specifics.input_encodings(), ';', &data.input_encodings); |
| 1018 data.date_created = base::Time::FromInternalValue(specifics.date_created()); |
| 1019 data.last_modified = base::Time::FromInternalValue(specifics.last_modified()); |
| 1020 data.prepopulate_id = specifics.prepopulate_id(); |
| 1021 data.sync_guid = specifics.sync_guid(); |
| 1022 if (existing_turl) { |
| 1023 data.id = existing_turl->id(); |
| 1024 data.created_by_policy = existing_turl->created_by_policy(); |
| 1025 data.usage_count = existing_turl->usage_count(); |
| 1026 } |
| 1027 return new TemplateURL(data); |
1000 } | 1028 } |
1001 | 1029 |
1002 // static | 1030 // static |
1003 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap( | 1031 SyncDataMap TemplateURLService::CreateGUIDToSyncDataMap( |
1004 const SyncDataList& sync_data) { | 1032 const SyncDataList& sync_data) { |
1005 SyncDataMap data_map; | 1033 SyncDataMap data_map; |
1006 SyncDataList::const_iterator iter; | 1034 SyncDataList::const_iterator iter; |
1007 for (iter = sync_data.begin(); iter != sync_data.end(); ++iter) { | 1035 for (iter = sync_data.begin(); iter != sync_data.end(); ++iter) { |
1008 data_map[iter->GetSpecifics().search_engine().sync_guid()] = *iter; | 1036 data_map[iter->GetSpecifics().search_engine().sync_guid()] = *iter; |
1009 } | 1037 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1050 | 1078 |
1051 size_t template_position = | 1079 size_t template_position = |
1052 std::string(initializers[i].url).find(kTemplateParameter); | 1080 std::string(initializers[i].url).find(kTemplateParameter); |
1053 DCHECK(template_position != std::string::npos); | 1081 DCHECK(template_position != std::string::npos); |
1054 std::string osd_url(initializers[i].url); | 1082 std::string osd_url(initializers[i].url); |
1055 osd_url.replace(template_position, arraysize(kTemplateParameter) - 1, | 1083 osd_url.replace(template_position, arraysize(kTemplateParameter) - 1, |
1056 kSearchTermParameter); | 1084 kSearchTermParameter); |
1057 | 1085 |
1058 // TemplateURLService ends up owning the TemplateURL, don't try and free | 1086 // TemplateURLService ends up owning the TemplateURL, don't try and free |
1059 // it. | 1087 // it. |
1060 TemplateURL* template_url = new TemplateURL(); | 1088 TemplateURLData data; |
1061 template_url->set_keyword(UTF8ToUTF16(initializers[i].keyword)); | 1089 data.short_name = UTF8ToUTF16(initializers[i].content); |
1062 template_url->set_short_name(UTF8ToUTF16(initializers[i].content)); | 1090 data.SetKeyword(UTF8ToUTF16(initializers[i].keyword)); |
1063 template_url->SetURL(osd_url); | 1091 data.SetURL(osd_url); |
1064 AddNoNotify(template_url); | 1092 AddNoNotify(new TemplateURL(data)); |
1065 } | 1093 } |
1066 } | 1094 } |
1067 | 1095 |
1068 // Initialize default search. | 1096 // Initialize default search. |
1069 UpdateDefaultSearch(); | 1097 UpdateDefaultSearch(); |
1070 | 1098 |
1071 // Request a server check for the correct Google URL if Google is the | 1099 // Request a server check for the correct Google URL if Google is the |
1072 // default search engine, not in headless mode and not in Chrome Frame. | 1100 // default search engine, not in headless mode and not in Chrome Frame. |
1073 if (initial_default_search_provider_.get() && | 1101 if (initial_default_search_provider_.get() && |
1074 initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) { | 1102 initial_default_search_provider_->url_ref().HasGoogleBaseURLs()) { |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1235 string16 keyword = | 1263 string16 keyword = |
1236 UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderKeyword)); | 1264 UTF8ToUTF16(prefs->GetString(prefs::kDefaultSearchProviderKeyword)); |
1237 std::string icon_url = | 1265 std::string icon_url = |
1238 prefs->GetString(prefs::kDefaultSearchProviderIconURL); | 1266 prefs->GetString(prefs::kDefaultSearchProviderIconURL); |
1239 std::string encodings = | 1267 std::string encodings = |
1240 prefs->GetString(prefs::kDefaultSearchProviderEncodings); | 1268 prefs->GetString(prefs::kDefaultSearchProviderEncodings); |
1241 std::string id_string = prefs->GetString(prefs::kDefaultSearchProviderID); | 1269 std::string id_string = prefs->GetString(prefs::kDefaultSearchProviderID); |
1242 std::string prepopulate_id = | 1270 std::string prepopulate_id = |
1243 prefs->GetString(prefs::kDefaultSearchProviderPrepopulateID); | 1271 prefs->GetString(prefs::kDefaultSearchProviderPrepopulateID); |
1244 | 1272 |
1245 default_provider->reset(new TemplateURL()); | 1273 TemplateURLData data; |
1246 (*default_provider)->set_short_name(name); | 1274 data.short_name = name; |
1247 (*default_provider)->SetURL(search_url); | 1275 data.SetKeyword(keyword); |
1248 (*default_provider)->SetSuggestionsURL(suggest_url); | 1276 data.SetURL(search_url); |
1249 (*default_provider)->SetInstantURL(instant_url); | 1277 data.suggestions_url = suggest_url; |
1250 (*default_provider)->set_keyword(keyword); | 1278 data.instant_url = instant_url; |
1251 (*default_provider)->set_favicon_url(GURL(icon_url)); | 1279 data.favicon_url = GURL(icon_url); |
1252 std::vector<std::string> encodings_vector; | 1280 data.show_in_default_list = true; |
1253 base::SplitString(encodings, ';', &encodings_vector); | 1281 base::SplitString(encodings, ';', &data.input_encodings); |
1254 (*default_provider)->set_input_encodings(encodings_vector); | |
1255 if (!id_string.empty() && !*is_managed) { | 1282 if (!id_string.empty() && !*is_managed) { |
1256 int64 value; | 1283 int64 value; |
1257 base::StringToInt64(id_string, &value); | 1284 base::StringToInt64(id_string, &value); |
1258 (*default_provider)->set_id(value); | 1285 data.id = value; |
1259 } | 1286 } |
1260 if (!prepopulate_id.empty() && !*is_managed) { | 1287 if (!prepopulate_id.empty() && !*is_managed) { |
1261 int value; | 1288 int value; |
1262 base::StringToInt(prepopulate_id, &value); | 1289 base::StringToInt(prepopulate_id, &value); |
1263 (*default_provider)->SetPrepopulateId(value); | 1290 data.prepopulate_id = value; |
1264 } | 1291 } |
1265 (*default_provider)->set_show_in_default_list(true); | 1292 default_provider->reset(new TemplateURL(data)); |
1266 return true; | 1293 return true; |
1267 } | 1294 } |
1268 | 1295 |
1269 bool TemplateURLService::CanReplaceKeywordForHost( | 1296 bool TemplateURLService::CanReplaceKeywordForHost( |
1270 const std::string& host, | 1297 const std::string& host, |
1271 const TemplateURL** to_replace) { | 1298 const TemplateURL** to_replace) { |
1272 const TemplateURLSet* urls = provider_map_.GetURLsForHost(host); | 1299 const TemplateURLSet* urls = provider_map_.GetURLsForHost(host); |
1273 if (urls) { | 1300 if (urls) { |
1274 for (TemplateURLSet::const_iterator i = urls->begin(); | 1301 for (TemplateURLSet::const_iterator i = urls->begin(); |
1275 i != urls->end(); ++i) { | 1302 i != urls->end(); ++i) { |
(...skipping 21 matching lines...) Expand all Loading... |
1297 DCHECK(loaded_); | 1324 DCHECK(loaded_); |
1298 DCHECK(existing_turl); | 1325 DCHECK(existing_turl); |
1299 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), existing_turl) | 1326 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), existing_turl) |
1300 != template_urls_.end()); | 1327 != template_urls_.end()); |
1301 | 1328 |
1302 if (!existing_turl->keyword().empty()) | 1329 if (!existing_turl->keyword().empty()) |
1303 keyword_to_template_map_.erase(existing_turl->keyword()); | 1330 keyword_to_template_map_.erase(existing_turl->keyword()); |
1304 if (!existing_turl->sync_guid().empty()) | 1331 if (!existing_turl->sync_guid().empty()) |
1305 guid_to_template_map_.erase(existing_turl->sync_guid()); | 1332 guid_to_template_map_.erase(existing_turl->sync_guid()); |
1306 | 1333 |
1307 // This call handles copying over the values (while retaining the id). | 1334 provider_map_.Remove(existing_turl); |
| 1335 TemplateURLID previous_id = existing_turl->id(); |
| 1336 TemplateURL* modifiable_turl = const_cast<TemplateURL*>(existing_turl); |
| 1337 *modifiable_turl = new_values; |
| 1338 modifiable_turl->data_.id = previous_id; |
1308 UIThreadSearchTermsData search_terms_data; | 1339 UIThreadSearchTermsData search_terms_data; |
1309 provider_map_.Update(existing_turl, new_values, search_terms_data); | 1340 provider_map_.Add(existing_turl, search_terms_data); |
1310 | 1341 |
1311 if (!existing_turl->keyword().empty()) | 1342 if (!existing_turl->keyword().empty()) |
1312 keyword_to_template_map_[existing_turl->keyword()] = existing_turl; | 1343 keyword_to_template_map_[existing_turl->keyword()] = existing_turl; |
1313 if (!existing_turl->sync_guid().empty()) | 1344 if (!existing_turl->sync_guid().empty()) |
1314 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; | 1345 guid_to_template_map_[existing_turl->sync_guid()] = existing_turl; |
1315 | 1346 |
1316 if (service_.get()) | 1347 if (service_.get()) |
1317 service_->UpdateKeyword(*existing_turl); | 1348 service_->UpdateKeyword(*existing_turl); |
1318 | 1349 |
1319 // Inform sync of the update. | 1350 // Inform sync of the update. |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1504 new_default_from_prefs.get())) | 1535 new_default_from_prefs.get())) |
1505 return; | 1536 return; |
1506 if (new_default_from_prefs.get() == NULL) { | 1537 if (new_default_from_prefs.get() == NULL) { |
1507 // default_search_provider_ can't be NULL otherwise | 1538 // default_search_provider_ can't be NULL otherwise |
1508 // TemplateURLsHaveSamePrefs would have returned true. Remove this now | 1539 // TemplateURLsHaveSamePrefs would have returned true. Remove this now |
1509 // invalid value. | 1540 // invalid value. |
1510 const TemplateURL* old_default = default_search_provider_; | 1541 const TemplateURL* old_default = default_search_provider_; |
1511 SetDefaultSearchProviderNoNotify(NULL); | 1542 SetDefaultSearchProviderNoNotify(NULL); |
1512 RemoveNoNotify(old_default); | 1543 RemoveNoNotify(old_default); |
1513 } else if (default_search_provider_) { | 1544 } else if (default_search_provider_) { |
1514 new_default_from_prefs->set_created_by_policy(true); | 1545 TemplateURLData data(new_default_from_prefs->data()); |
1515 UpdateNoNotify(default_search_provider_, *new_default_from_prefs.get()); | 1546 data.created_by_policy = true; |
| 1547 TemplateURL new_values(data); |
| 1548 UpdateNoNotify(default_search_provider_, new_values); |
1516 } else { | 1549 } else { |
1517 // AddNoNotify will take ownership of new_template, so it's safe to | 1550 // AddNoNotify will take ownership of new_template, so it's safe to |
1518 // release. | 1551 // release. |
1519 TemplateURL* new_template = new_default_from_prefs.release(); | 1552 TemplateURL* new_template = NULL; |
1520 if (new_template) { | 1553 if (new_default_from_prefs.get()) { |
1521 new_template->set_created_by_policy(true); | 1554 TemplateURLData data(new_default_from_prefs->data()); |
| 1555 data.created_by_policy = true; |
| 1556 new_template = new TemplateURL(data); |
1522 AddNoNotify(new_template); | 1557 AddNoNotify(new_template); |
1523 } | 1558 } |
1524 SetDefaultSearchProviderNoNotify(new_template); | 1559 SetDefaultSearchProviderNoNotify(new_template); |
1525 } | 1560 } |
1526 } else if (!is_default_search_managed_ && new_is_default_managed) { | 1561 } else if (!is_default_search_managed_ && new_is_default_managed) { |
1527 // The default used to be unmanaged and is now managed. Add the new | 1562 // The default used to be unmanaged and is now managed. Add the new |
1528 // managed default to the list of URLs and set it as default. | 1563 // managed default to the list of URLs and set it as default. |
1529 is_default_search_managed_ = new_is_default_managed; | 1564 is_default_search_managed_ = new_is_default_managed; |
1530 // AddNoNotify will take ownership of new_template, so it's safe to | 1565 // AddNoNotify will take ownership of new_template, so it's safe to |
1531 // release. | 1566 // release. |
1532 TemplateURL* new_template = new_default_from_prefs.release(); | 1567 TemplateURL* new_template = NULL; |
1533 if (new_template) { | 1568 if (new_default_from_prefs.get()) { |
1534 new_template->set_created_by_policy(true); | 1569 TemplateURLData data(new_default_from_prefs->data()); |
| 1570 data.created_by_policy = true; |
| 1571 new_template = new TemplateURL(data); |
1535 AddNoNotify(new_template); | 1572 AddNoNotify(new_template); |
1536 } | 1573 } |
1537 SetDefaultSearchProviderNoNotify(new_template); | 1574 SetDefaultSearchProviderNoNotify(new_template); |
1538 } else { | 1575 } else { |
1539 // The default was managed and is no longer. | 1576 // The default was managed and is no longer. |
1540 DCHECK(is_default_search_managed_ && !new_is_default_managed); | 1577 DCHECK(is_default_search_managed_ && !new_is_default_managed); |
1541 is_default_search_managed_ = new_is_default_managed; | 1578 is_default_search_managed_ = new_is_default_managed; |
1542 // If we had a default, delete the previous default if created by policy | 1579 // If we had a default, delete the previous default if created by policy |
1543 // and set a likely default. | 1580 // and set a likely default. |
1544 if (NULL != default_search_provider_ && | 1581 if (NULL != default_search_provider_ && |
1545 default_search_provider_->created_by_policy()) { | 1582 default_search_provider_->created_by_policy()) { |
1546 const TemplateURL* old_default = default_search_provider_; | 1583 const TemplateURL* old_default = default_search_provider_; |
1547 default_search_provider_ = NULL; | 1584 default_search_provider_ = NULL; |
1548 RemoveNoNotify(old_default); | 1585 RemoveNoNotify(old_default); |
1549 } | 1586 } |
1550 | 1587 |
1551 // The likely default should be from Sync if we were waiting on Sync. | 1588 // The likely default should be from Sync if we were waiting on Sync. |
1552 // Otherwise, it should be FindNewDefaultSearchProvider. | 1589 // Otherwise, it should be FindNewDefaultSearchProvider. |
1553 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); | 1590 const TemplateURL* synced_default = GetPendingSyncedDefaultSearchProvider(); |
1554 if (synced_default) | 1591 if (synced_default) |
1555 pending_synced_default_search_ = false; | 1592 pending_synced_default_search_ = false; |
1556 SetDefaultSearchProviderNoNotify(synced_default ? synced_default : | 1593 SetDefaultSearchProviderNoNotify(synced_default ? synced_default : |
1557 FindNewDefaultSearchProvider()); | 1594 FindNewDefaultSearchProvider()); |
1558 } | 1595 } |
1559 NotifyObservers(); | 1596 NotifyObservers(); |
1560 } | 1597 } |
1561 | 1598 |
1562 void TemplateURLService::SetDefaultSearchProviderNoNotify( | 1599 void TemplateURLService::SetDefaultSearchProviderNoNotify( |
1563 const TemplateURL* url) { | 1600 const TemplateURL* url) { |
1564 DCHECK(!url || std::find(template_urls_.begin(), template_urls_.end(), url) | 1601 DCHECK(!url || std::find(template_urls_.begin(), template_urls_.end(), url) != |
1565 != template_urls_.end()); | 1602 template_urls_.end()); |
1566 default_search_provider_ = url; | 1603 default_search_provider_ = url; |
1567 | 1604 |
1568 if (url) { | 1605 if (url) { |
1569 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url); | 1606 TemplateURL* modifiable_url = const_cast<TemplateURL*>(url); |
1570 // Don't mark the url as edited, otherwise we won't be able to rev the | 1607 // Don't mark the url as edited, otherwise we won't be able to rev the |
1571 // template urls we ship with. | 1608 // template urls we ship with. |
1572 modifiable_url->set_show_in_default_list(true); | 1609 modifiable_url->data_.show_in_default_list = true; |
1573 if (service_.get()) | 1610 if (service_.get()) |
1574 service_.get()->UpdateKeyword(*url); | 1611 service_->UpdateKeyword(*url); |
1575 | 1612 |
1576 if (url->url_ref().HasGoogleBaseURLs()) { | 1613 if (url->url_ref().HasGoogleBaseURLs()) { |
1577 GoogleURLTracker::RequestServerCheck(); | 1614 GoogleURLTracker::RequestServerCheck(); |
1578 #if defined(ENABLE_RLZ) | 1615 #if defined(ENABLE_RLZ) |
1579 // Needs to be evaluated. See http://crbug.com/62328. | 1616 // Needs to be evaluated. See http://crbug.com/62328. |
1580 base::ThreadRestrictions::ScopedAllowIO allow_io; | 1617 base::ThreadRestrictions::ScopedAllowIO allow_io; |
1581 RLZTracker::RecordProductEvent(rlz_lib::CHROME, | 1618 RLZTracker::RecordProductEvent(rlz_lib::CHROME, |
1582 rlz_lib::CHROME_OMNIBOX, | 1619 rlz_lib::CHROME_OMNIBOX, |
1583 rlz_lib::SET_TO_GOOGLE); | 1620 rlz_lib::SET_TO_GOOGLE); |
1584 #endif | 1621 #endif |
(...skipping 18 matching lines...) Expand all Loading... |
1603 // Inform sync the change to the show_in_default_list flag. | 1640 // Inform sync the change to the show_in_default_list flag. |
1604 if (url) | 1641 if (url) |
1605 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE); | 1642 ProcessTemplateURLChange(url, SyncChange::ACTION_UPDATE); |
1606 } | 1643 } |
1607 | 1644 |
1608 void TemplateURLService::AddNoNotify(TemplateURL* template_url) { | 1645 void TemplateURLService::AddNoNotify(TemplateURL* template_url) { |
1609 DCHECK(template_url); | 1646 DCHECK(template_url); |
1610 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); | 1647 DCHECK_EQ(kInvalidTemplateURLID, template_url->id()); |
1611 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), template_url) | 1648 DCHECK(std::find(template_urls_.begin(), template_urls_.end(), template_url) |
1612 == template_urls_.end()); | 1649 == template_urls_.end()); |
1613 template_url->set_id(++next_id_); | 1650 template_url->data_.id = ++next_id_; |
1614 template_urls_.push_back(template_url); | 1651 template_urls_.push_back(template_url); |
1615 AddToMaps(template_url); | 1652 AddToMaps(template_url); |
1616 | 1653 |
1617 if (service_.get()) | 1654 if (service_.get()) |
1618 service_->AddKeyword(*template_url); | 1655 service_->AddKeyword(*template_url); |
1619 | 1656 |
1620 // Inform sync of the addition. Note that this will assign a GUID to | 1657 // Inform sync of the addition. Note that this will assign a GUID to |
1621 // template_url and add it to the guid_to_template_map_. | 1658 // template_url and add it to the guid_to_template_map_. |
1622 ProcessTemplateURLChange(template_url, SyncChange::ACTION_ADD); | 1659 ProcessTemplateURLChange(template_url, SyncChange::ACTION_ADD); |
1623 } | 1660 } |
(...skipping 10 matching lines...) Expand all Loading... |
1634 NOTREACHED(); | 1671 NOTREACHED(); |
1635 return; | 1672 return; |
1636 } | 1673 } |
1637 | 1674 |
1638 RemoveFromMaps(template_url); | 1675 RemoveFromMaps(template_url); |
1639 | 1676 |
1640 // Remove it from the vector containing all TemplateURLs. | 1677 // Remove it from the vector containing all TemplateURLs. |
1641 template_urls_.erase(i); | 1678 template_urls_.erase(i); |
1642 | 1679 |
1643 if (service_.get()) | 1680 if (service_.get()) |
1644 service_->RemoveKeyword(*template_url); | 1681 service_->RemoveKeyword(template_url->id()); |
1645 | 1682 |
1646 // Inform sync of the deletion. | 1683 // Inform sync of the deletion. |
1647 ProcessTemplateURLChange(template_url, SyncChange::ACTION_DELETE); | 1684 ProcessTemplateURLChange(template_url, SyncChange::ACTION_DELETE); |
1648 | 1685 |
1649 if (profile_) { | 1686 if (profile_) { |
1650 content::Source<Profile> source(profile_); | 1687 content::Source<Profile> source(profile_); |
1651 TemplateURLID id = template_url->id(); | 1688 TemplateURLID id = template_url->id(); |
1652 content::NotificationService::current()->Notify( | 1689 content::NotificationService::current()->Notify( |
1653 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, | 1690 chrome::NOTIFICATION_TEMPLATE_URL_REMOVED, |
1654 source, | 1691 source, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1699 | 1736 |
1700 // The database loaded a managed |default_search_provider|, but it has | 1737 // The database loaded a managed |default_search_provider|, but it has |
1701 // been updated in the prefs. Remove it from the database, and update the | 1738 // been updated in the prefs. Remove it from the database, and update the |
1702 // |default_search_provider| pointer here. | 1739 // |default_search_provider| pointer here. |
1703 if (*default_search_provider && | 1740 if (*default_search_provider && |
1704 (*default_search_provider)->id() == template_url->id()) | 1741 (*default_search_provider)->id() == template_url->id()) |
1705 *default_search_provider = NULL; | 1742 *default_search_provider = NULL; |
1706 | 1743 |
1707 i = template_urls->erase(i); | 1744 i = template_urls->erase(i); |
1708 if (service_.get()) | 1745 if (service_.get()) |
1709 service_->RemoveKeyword(*template_url); | 1746 service_->RemoveKeyword(template_url->id()); |
1710 delete template_url; | 1747 delete template_url; |
1711 } else { | 1748 } else { |
1712 ++i; | 1749 ++i; |
1713 } | 1750 } |
1714 } | 1751 } |
1715 } | 1752 } |
1716 | 1753 |
1717 void TemplateURLService::ResetTemplateURLGUID(const TemplateURL* url, | 1754 void TemplateURLService::ResetTemplateURLGUID(const TemplateURL* url, |
1718 const std::string& guid) { | 1755 const std::string& guid) { |
1719 DCHECK(!guid.empty()); | 1756 DCHECK(!guid.empty()); |
1720 | 1757 |
1721 TemplateURL new_url(*url); | 1758 TemplateURLData data(url->data()); |
1722 new_url.set_sync_guid(guid); | 1759 data.sync_guid = guid; |
| 1760 TemplateURL new_url(data); |
1723 UpdateNoNotify(url, new_url); | 1761 UpdateNoNotify(url, new_url); |
1724 } | 1762 } |
1725 | 1763 |
1726 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl) const { | 1764 string16 TemplateURLService::UniquifyKeyword(const TemplateURL& turl) const { |
1727 // Already unique. | 1765 // Already unique. |
1728 if (!GetTemplateURLForKeyword(turl.keyword())) | 1766 if (!GetTemplateURLForKeyword(turl.keyword())) |
1729 return turl.keyword(); | 1767 return turl.keyword(); |
1730 | 1768 |
1731 // First, try to return the generated keyword for the TemplateURL. | 1769 // First, try to return the generated keyword for the TemplateURL. |
1732 GURL gurl(turl.url()); | 1770 GURL gurl(turl.url()); |
(...skipping 23 matching lines...) Expand all Loading... |
1756 const TemplateURL* existing_turl = | 1794 const TemplateURL* existing_turl = |
1757 GetTemplateURLForKeyword(sync_turl->keyword()); | 1795 GetTemplateURLForKeyword(sync_turl->keyword()); |
1758 // If there is no conflict, or it's just conflicting with itself, return. | 1796 // If there is no conflict, or it's just conflicting with itself, return. |
1759 if (!existing_turl || existing_turl->sync_guid() == sync_turl->sync_guid()) | 1797 if (!existing_turl || existing_turl->sync_guid() == sync_turl->sync_guid()) |
1760 return false; | 1798 return false; |
1761 | 1799 |
1762 if (existing_turl->last_modified() > sync_turl->last_modified() || | 1800 if (existing_turl->last_modified() > sync_turl->last_modified() || |
1763 existing_turl->created_by_policy()) { | 1801 existing_turl->created_by_policy()) { |
1764 string16 new_keyword = UniquifyKeyword(*sync_turl); | 1802 string16 new_keyword = UniquifyKeyword(*sync_turl); |
1765 DCHECK(!GetTemplateURLForKeyword(new_keyword)); | 1803 DCHECK(!GetTemplateURLForKeyword(new_keyword)); |
1766 sync_turl->set_keyword(new_keyword); | 1804 sync_turl->data_.SetKeyword(new_keyword); |
1767 // If we update the cloud TURL, we need to push an update back to sync | 1805 // If we update the cloud TURL, we need to push an update back to sync |
1768 // informing it that something has changed. | 1806 // informing it that something has changed. |
1769 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); | 1807 SyncData sync_data = CreateSyncDataFromTemplateURL(*sync_turl); |
1770 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 1808 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); |
1771 } else { | 1809 } else { |
1772 string16 new_keyword = UniquifyKeyword(*existing_turl); | 1810 string16 new_keyword = UniquifyKeyword(*existing_turl); |
1773 TemplateURL new_turl(*existing_turl); | 1811 TemplateURLData data(existing_turl->data()); |
1774 new_turl.set_keyword(new_keyword); | 1812 data.SetKeyword(new_keyword); |
| 1813 TemplateURL new_turl(data); |
1775 UpdateNoNotify(existing_turl, new_turl); | 1814 UpdateNoNotify(existing_turl, new_turl); |
1776 NotifyObservers(); | 1815 NotifyObservers(); |
1777 } | 1816 } |
1778 return true; | 1817 return true; |
1779 } | 1818 } |
1780 | 1819 |
1781 const TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( | 1820 const TemplateURL* TemplateURLService::FindDuplicateOfSyncTemplateURL( |
1782 const TemplateURL& sync_turl) { | 1821 const TemplateURL& sync_turl) { |
1783 const TemplateURL* existing_turl = | 1822 const TemplateURL* existing_turl = |
1784 GetTemplateURLForKeyword(sync_turl.keyword()); | 1823 GetTemplateURLForKeyword(sync_turl.keyword()); |
1785 if (!existing_turl) | 1824 if (!existing_turl) |
1786 return NULL; | 1825 return NULL; |
1787 | 1826 |
1788 if (!existing_turl->url().empty() && | 1827 if (!existing_turl->url().empty() && |
1789 existing_turl->url() == sync_turl.url()) { | 1828 existing_turl->url() == sync_turl.url()) { |
1790 return existing_turl; | 1829 return existing_turl; |
1791 } | 1830 } |
1792 return NULL; | 1831 return NULL; |
1793 } | 1832 } |
1794 | 1833 |
1795 void TemplateURLService::MergeSyncAndLocalURLDuplicates( | 1834 void TemplateURLService::MergeSyncAndLocalURLDuplicates( |
1796 TemplateURL* sync_turl, | 1835 TemplateURL* sync_turl, |
1797 TemplateURL* local_turl, | 1836 TemplateURL* local_turl, |
1798 SyncChangeList* change_list) { | 1837 SyncChangeList* change_list) { |
1799 DCHECK(sync_turl); | 1838 DCHECK(sync_turl); |
1800 DCHECK(local_turl); | 1839 DCHECK(local_turl); |
1801 DCHECK(change_list); | 1840 DCHECK(change_list); |
1802 | 1841 if (sync_turl->last_modified() > local_turl->last_modified()) { |
1803 scoped_ptr<TemplateURL> scoped_sync_turl(sync_turl); | |
1804 | |
1805 if (scoped_sync_turl->last_modified() > local_turl->last_modified()) { | |
1806 // Fully replace local_url with Sync's copy. Note that because use Add | 1842 // Fully replace local_url with Sync's copy. Note that because use Add |
1807 // rather than ResetTemplateURL, |sync_url| is added with a fresh | 1843 // rather than ResetTemplateURL, |sync_url| is added with a fresh |
1808 // TemplateURLID. We don't need to sync the new ID back to the server since | 1844 // TemplateURLID. We don't need to sync the new ID back to the server since |
1809 // it's only relevant locally. | 1845 // it's only relevant locally. |
1810 bool delete_default = (local_turl == GetDefaultSearchProvider()); | 1846 bool delete_default = (local_turl == GetDefaultSearchProvider()); |
1811 if (delete_default && is_default_search_managed_) { | 1847 if (delete_default && is_default_search_managed_) { |
1812 NOTREACHED() << "Tried to delete managed default search provider"; | 1848 NOTREACHED() << "Tried to delete managed default search provider"; |
1813 } else { | 1849 } else { |
1814 if (delete_default) | 1850 if (delete_default) |
1815 default_search_provider_ = NULL; | 1851 default_search_provider_ = NULL; |
1816 | 1852 |
1817 Remove(local_turl); | 1853 Remove(local_turl); |
1818 | 1854 |
1819 // Force the local ID to kInvalidTemplateURLID so we can add it. | 1855 // Force the local ID to kInvalidTemplateURLID so we can add it. |
1820 scoped_sync_turl->set_id(kInvalidTemplateURLID); | 1856 sync_turl->data_.id = kInvalidTemplateURLID; |
1821 TemplateURL* temp = scoped_sync_turl.release(); | 1857 Add(sync_turl); |
1822 Add(temp); | |
1823 if (delete_default) | 1858 if (delete_default) |
1824 SetDefaultSearchProvider(temp); | 1859 SetDefaultSearchProvider(sync_turl); |
1825 } | 1860 } |
1826 } else { | 1861 } else { |
1827 // Change the local TURL's GUID to the server's GUID and push an update to | 1862 // Change the local TURL's GUID to the server's GUID and push an update to |
1828 // Sync. This ensures that the rest of local_url's fields are sync'd up to | 1863 // Sync. This ensures that the rest of local_url's fields are sync'd up to |
1829 // the server, and the next time local_url is synced, it is recognized by | 1864 // the server, and the next time local_url is synced, it is recognized by |
1830 // having the same GUID. | 1865 // having the same GUID. |
1831 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); | 1866 ResetTemplateURLGUID(local_turl, sync_turl->sync_guid()); |
1832 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); | 1867 SyncData sync_data = CreateSyncDataFromTemplateURL(*local_turl); |
1833 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); | 1868 change_list->push_back(SyncChange(SyncChange::ACTION_UPDATE, sync_data)); |
| 1869 delete sync_turl; |
1834 } | 1870 } |
1835 } | 1871 } |
1836 | 1872 |
1837 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced( | 1873 void TemplateURLService::SetDefaultSearchProviderIfNewlySynced( |
1838 const std::string& guid) { | 1874 const std::string& guid) { |
1839 // If we're not syncing or if default search is managed by policy, ignore. | 1875 // If we're not syncing or if default search is managed by policy, ignore. |
1840 if (!sync_processor_.get() || is_default_search_managed_) | 1876 if (!sync_processor_.get() || is_default_search_managed_) |
1841 return; | 1877 return; |
1842 | 1878 |
1843 PrefService* prefs = GetPrefs(); | 1879 PrefService* prefs = GetPrefs(); |
(...skipping 19 matching lines...) Expand all Loading... |
1863 } | 1899 } |
1864 | 1900 |
1865 void TemplateURLService::PatchMissingSyncGUIDs( | 1901 void TemplateURLService::PatchMissingSyncGUIDs( |
1866 std::vector<TemplateURL*>* template_urls) { | 1902 std::vector<TemplateURL*>* template_urls) { |
1867 DCHECK(template_urls); | 1903 DCHECK(template_urls); |
1868 for (std::vector<TemplateURL*>::iterator i = template_urls->begin(); | 1904 for (std::vector<TemplateURL*>::iterator i = template_urls->begin(); |
1869 i != template_urls->end(); ++i) { | 1905 i != template_urls->end(); ++i) { |
1870 TemplateURL* template_url = *i; | 1906 TemplateURL* template_url = *i; |
1871 DCHECK(template_url); | 1907 DCHECK(template_url); |
1872 if (template_url->sync_guid().empty()) { | 1908 if (template_url->sync_guid().empty()) { |
1873 template_url->set_sync_guid(guid::GenerateGUID()); | 1909 template_url->data_.sync_guid = guid::GenerateGUID(); |
1874 if (service_.get()) | 1910 if (service_.get()) |
1875 service_->UpdateKeyword(*template_url); | 1911 service_->UpdateKeyword(*template_url); |
1876 } | 1912 } |
1877 } | 1913 } |
1878 } | 1914 } |
1879 | |
1880 // static | |
1881 void TemplateURLService::UpdateTemplateURLWithSyncData( | |
1882 TemplateURL* dst, | |
1883 const SyncData& sync_data) { | |
1884 sync_pb::SearchEngineSpecifics specifics = | |
1885 sync_data.GetSpecifics().search_engine(); | |
1886 dst->set_short_name(UTF8ToUTF16(specifics.short_name())); | |
1887 dst->set_keyword(UTF8ToUTF16(specifics.keyword())); | |
1888 dst->set_favicon_url(GURL(specifics.favicon_url())); | |
1889 dst->SetURL(specifics.url()); | |
1890 dst->set_safe_for_autoreplace(specifics.safe_for_autoreplace()); | |
1891 dst->set_originating_url(GURL(specifics.originating_url())); | |
1892 dst->set_date_created( | |
1893 base::Time::FromInternalValue(specifics.date_created())); | |
1894 std::vector<std::string> input_encodings; | |
1895 base::SplitString(specifics.input_encodings(), ';', &input_encodings); | |
1896 dst->set_input_encodings(input_encodings); | |
1897 dst->set_show_in_default_list(specifics.show_in_default_list()); | |
1898 dst->SetSuggestionsURL(specifics.suggestions_url()); | |
1899 dst->SetPrepopulateId(specifics.prepopulate_id()); | |
1900 dst->set_autogenerate_keyword(specifics.autogenerate_keyword()); | |
1901 dst->SetInstantURL(specifics.instant_url()); | |
1902 dst->set_last_modified( | |
1903 base::Time::FromInternalValue(specifics.last_modified())); | |
1904 dst->set_sync_guid(specifics.sync_guid()); | |
1905 } | |
OLD | NEW |