Chromium Code Reviews| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/auto_reset.h" | 10 #include "base/auto_reset.h" |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 #include "chrome/browser/extensions/extension_service.h" | 26 #include "chrome/browser/extensions/extension_service.h" |
| 27 #include "chrome/browser/google/google_url_tracker.h" | 27 #include "chrome/browser/google/google_url_tracker.h" |
| 28 #include "chrome/browser/history/history_notifications.h" | 28 #include "chrome/browser/history/history_notifications.h" |
| 29 #include "chrome/browser/history/history_service.h" | 29 #include "chrome/browser/history/history_service.h" |
| 30 #include "chrome/browser/history/history_service_factory.h" | 30 #include "chrome/browser/history/history_service_factory.h" |
| 31 #include "chrome/browser/profiles/profile.h" | 31 #include "chrome/browser/profiles/profile.h" |
| 32 #include "chrome/browser/rlz/rlz.h" | 32 #include "chrome/browser/rlz/rlz.h" |
| 33 #include "chrome/browser/search_engines/search_host_to_urls_map.h" | 33 #include "chrome/browser/search_engines/search_host_to_urls_map.h" |
| 34 #include "chrome/browser/search_engines/search_terms_data.h" | 34 #include "chrome/browser/search_engines/search_terms_data.h" |
| 35 #include "chrome/browser/search_engines/template_url.h" | 35 #include "chrome/browser/search_engines/template_url.h" |
| 36 #include "chrome/browser/search_engines/template_url_keys.h" | |
| 36 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" | 37 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 37 #include "chrome/browser/search_engines/template_url_service_observer.h" | 38 #include "chrome/browser/search_engines/template_url_service_observer.h" |
| 38 #include "chrome/browser/search_engines/util.h" | 39 #include "chrome/browser/search_engines/util.h" |
| 39 #include "chrome/browser/webdata/web_data_service.h" | 40 #include "chrome/browser/webdata/web_data_service.h" |
| 40 #include "chrome/common/chrome_switches.h" | 41 #include "chrome/common/chrome_switches.h" |
| 41 #include "chrome/common/env_vars.h" | 42 #include "chrome/common/env_vars.h" |
| 42 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" | 43 #include "chrome/common/extensions/api/omnibox/omnibox_handler.h" |
| 43 #include "chrome/common/net/url_fixer_upper.h" | 44 #include "chrome/common/net/url_fixer_upper.h" |
| 44 #include "chrome/common/pref_names.h" | 45 #include "chrome/common/pref_names.h" |
| 45 #include "chrome/common/url_constants.h" | 46 #include "chrome/common/url_constants.h" |
| 46 #include "content/public/browser/notification_service.h" | 47 #include "content/public/browser/notification_service.h" |
| 47 #include "extensions/common/constants.h" | 48 #include "extensions/common/constants.h" |
| 48 #include "net/base/net_util.h" | 49 #include "net/base/net_util.h" |
| 49 #include "sync/api/sync_change.h" | 50 #include "sync/api/sync_change.h" |
| 50 #include "sync/api/sync_error_factory.h" | 51 #include "sync/api/sync_error_factory.h" |
| 51 #include "sync/protocol/search_engine_specifics.pb.h" | 52 #include "sync/protocol/search_engine_specifics.pb.h" |
| 52 #include "sync/protocol/sync.pb.h" | 53 #include "sync/protocol/sync.pb.h" |
| 53 #include "ui/base/l10n/l10n_util.h" | 54 #include "ui/base/l10n/l10n_util.h" |
| 54 | 55 |
| 55 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; | 56 typedef SearchHostToURLsMap::TemplateURLSet TemplateURLSet; |
| 56 typedef TemplateURLService::SyncDataMap SyncDataMap; | 57 typedef TemplateURLService::SyncDataMap SyncDataMap; |
| 57 | 58 |
| 58 namespace { | 59 namespace { |
| 59 | |
|
erikwright (departed)
2014/04/22 15:47:24
nit: restore blank line.
Cait (Slow)
2014/04/22 19:54:13
Done.
| |
| 60 bool TemplateURLsHaveSamePrefs(const TemplateURL* url1, | 60 bool TemplateURLsHaveSamePrefs(const TemplateURL* url1, |
| 61 const TemplateURL* url2) { | 61 const TemplateURL* url2) { |
| 62 if (url1 == url2) | 62 if (url1 == url2) |
| 63 return true; | 63 return true; |
| 64 return (url1 != NULL) && (url2 != NULL) && | 64 return (url1 != NULL) && (url2 != NULL) && |
| 65 (url1->short_name() == url2->short_name()) && | 65 (url1->short_name() == url2->short_name()) && |
| 66 url1->HasSameKeywordAs(*url2) && | 66 url1->HasSameKeywordAs(*url2) && |
| 67 (url1->url() == url2->url()) && | 67 (url1->url() == url2->url()) && |
| 68 (url1->suggestions_url() == url2->suggestions_url()) && | 68 (url1->suggestions_url() == url2->suggestions_url()) && |
| 69 (url1->instant_url() == url2->instant_url()) && | 69 (url1->instant_url() == url2->instant_url()) && |
| (...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 669 return !is_default_search_managed() && | 669 return !is_default_search_managed() && |
| 670 !IsExtensionControlledDefaultSearch() && | 670 !IsExtensionControlledDefaultSearch() && |
| 671 (url != GetDefaultSearchProvider()) && | 671 (url != GetDefaultSearchProvider()) && |
| 672 url->url_ref().SupportsReplacement() && | 672 url->url_ref().SupportsReplacement() && |
| 673 (url->GetType() == TemplateURL::NORMAL); | 673 (url->GetType() == TemplateURL::NORMAL); |
| 674 } | 674 } |
| 675 | 675 |
| 676 void TemplateURLService::SetUserSelectedDefaultSearchProvider( | 676 void TemplateURLService::SetUserSelectedDefaultSearchProvider( |
| 677 TemplateURL* url) { | 677 TemplateURL* url) { |
| 678 SetDefaultSearchProvider(url); | 678 SetDefaultSearchProvider(url); |
| 679 WriteDataToPrefsDictionary(url); | |
| 679 } | 680 } |
| 680 | 681 |
| 681 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { | 682 TemplateURL* TemplateURLService::GetDefaultSearchProvider() { |
| 682 if (loaded_ && !load_failed_) | 683 if (loaded_ && !load_failed_) |
| 683 return default_search_provider_; | 684 return default_search_provider_; |
| 684 // We're not loaded, rely on the default search provider stored in prefs. | 685 // We're not loaded, rely on the default search provider stored in prefs. |
| 685 return initial_default_search_provider_.get(); | 686 return initial_default_search_provider_.get(); |
| 686 } | 687 } |
| 687 | 688 |
| 688 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( | 689 bool TemplateURLService::IsSearchResultsPageFromDefaultSearchProvider( |
| (...skipping 2056 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2745 new_dse = *i; | 2746 new_dse = *i; |
| 2746 break; | 2747 break; |
| 2747 } | 2748 } |
| 2748 } | 2749 } |
| 2749 } | 2750 } |
| 2750 } | 2751 } |
| 2751 if (!new_dse) | 2752 if (!new_dse) |
| 2752 new_dse = FindNewDefaultSearchProvider(); | 2753 new_dse = FindNewDefaultSearchProvider(); |
| 2753 SetDefaultSearchProviderNoNotify(new_dse); | 2754 SetDefaultSearchProviderNoNotify(new_dse); |
| 2754 } | 2755 } |
| 2756 | |
| 2757 bool TemplateURLService::ReadDataFromPrefsDictionary(TemplateURL** url) { | |
| 2758 PrefService* prefs = GetPrefs(); | |
| 2759 if (!prefs) | |
| 2760 return false; | |
| 2761 | |
| 2762 const base::DictionaryValue* url_dict = | |
| 2763 prefs->GetDictionary(prefs::kDefaultSearchProviderData); | |
| 2764 if (!url_dict) | |
| 2765 return false; | |
| 2766 | |
| 2767 TemplateURLData data; | |
| 2768 std::string search_url; | |
| 2769 base::string16 keyword; | |
| 2770 | |
| 2771 url_dict->GetString(default_search::kKeyword, &keyword); | |
| 2772 url_dict->GetString(default_search::kURL, &search_url); | |
| 2773 | |
| 2774 DCHECK(!keyword.empty()); | |
| 2775 DCHECK(!search_url.empty()); | |
| 2776 | |
| 2777 data.SetKeyword(keyword); | |
| 2778 data.SetURL(search_url); | |
| 2779 | |
| 2780 std::string id; | |
| 2781 url_dict->GetString(default_search::kID, &id); | |
| 2782 base::StringToInt64(id, &data.id); | |
| 2783 url_dict->GetString(default_search::kShortName, &data.short_name); | |
| 2784 url_dict->GetInteger(default_search::kPrepopulateID, &data.prepopulate_id); | |
| 2785 url_dict->GetString(default_search::kSyncGUID, &data.sync_guid); | |
| 2786 | |
| 2787 url_dict->GetString(default_search::kSuggestionsURL, &data.suggestions_url); | |
| 2788 url_dict->GetString(default_search::kInstantURL, &data.instant_url); | |
| 2789 url_dict->GetString(default_search::kImageURL, &data.image_url); | |
| 2790 url_dict->GetString(default_search::kNewTabURL, &data.new_tab_url); | |
| 2791 | |
| 2792 std::string favicon_url; | |
| 2793 std::string originating_url; | |
| 2794 url_dict->GetString(default_search::kFaviconURL, &favicon_url); | |
| 2795 url_dict->GetString(default_search::kOriginatingURL, &originating_url); | |
| 2796 data.favicon_url = GURL(favicon_url); | |
| 2797 data.originating_url = GURL(originating_url); | |
| 2798 | |
| 2799 url_dict->GetString(default_search::kSearchURLPostParams, | |
| 2800 &data.search_url_post_params); | |
| 2801 url_dict->GetString(default_search::kSuggestionsURLPostParams, | |
| 2802 &data.suggestions_url_post_params); | |
| 2803 url_dict->GetString(default_search::kInstantURLPostParams, | |
| 2804 &data.instant_url_post_params); | |
| 2805 url_dict->GetString(default_search::kImageURLPostParams, | |
| 2806 &data.image_url_post_params); | |
| 2807 | |
| 2808 url_dict->GetBoolean(default_search::kSafeForAutoReplace, | |
| 2809 &data.safe_for_autoreplace); | |
| 2810 | |
| 2811 double date_created = 0.0; | |
| 2812 double last_modified = 0.0; | |
| 2813 url_dict->GetDouble(default_search::kDateCreated, &date_created); | |
| 2814 url_dict->GetDouble(default_search::kLastModified, &last_modified); | |
| 2815 data.date_created = base::Time::FromInternalValue(date_created); | |
| 2816 data.last_modified = base::Time::FromInternalValue(last_modified); | |
| 2817 | |
| 2818 url_dict->GetInteger(default_search::kUsageCount, &data.usage_count); | |
| 2819 | |
| 2820 const base::ListValue* alternate_urls = new base::ListValue; | |
| 2821 url_dict->GetList(default_search::kAlternateURLs, &alternate_urls); | |
| 2822 data.alternate_urls.clear(); | |
| 2823 for (size_t i = 0; i < alternate_urls->GetSize(); ++i) { | |
| 2824 std::string alternate_url; | |
| 2825 if (alternate_urls->GetString(i, &alternate_url)) | |
| 2826 data.alternate_urls.push_back(alternate_url); | |
| 2827 } | |
| 2828 | |
| 2829 const base::ListValue* encodings = new base::ListValue; | |
| 2830 url_dict->GetList(default_search::kInputEncodings, &encodings); | |
| 2831 data.input_encodings.clear(); | |
| 2832 for (size_t i = 0; i < encodings->GetSize(); ++i) { | |
| 2833 std::string encoding; | |
| 2834 if (encodings->GetString(i, &encoding)) | |
| 2835 data.input_encodings.push_back(encoding); | |
| 2836 } | |
| 2837 | |
| 2838 url_dict->GetString(default_search::kSearchTermsReplacementKey, | |
| 2839 &data.search_terms_replacement_key); | |
| 2840 | |
| 2841 url_dict->GetBoolean(default_search::kCreatedByPolicy, | |
| 2842 &data.created_by_policy); | |
| 2843 | |
| 2844 data.show_in_default_list = true; | |
| 2845 | |
| 2846 *url = new TemplateURL(profile_, data); | |
| 2847 | |
| 2848 return true; | |
| 2849 } | |
| 2850 | |
| 2851 void TemplateURLService::WriteDataToPrefsDictionary(const TemplateURL* url) { | |
| 2852 PrefService* prefs = GetPrefs(); | |
| 2853 if (!prefs) | |
| 2854 return; | |
| 2855 | |
| 2856 base::DictionaryValue* url_dict = new base::DictionaryValue; | |
| 2857 url_dict->SetString(default_search::kID, base::Int64ToString(url->id())); | |
|
gab
2014/04/17 00:37:54
Can you push this latest update to argon18? I can'
| |
| 2858 url_dict->SetString(default_search::kShortName, url->short_name()); | |
| 2859 url_dict->SetString(default_search::kKeyword, url->keyword()); | |
| 2860 url_dict->SetInteger(default_search::kPrepopulateID, url->prepopulate_id()); | |
| 2861 url_dict->SetString(default_search::kSyncGUID, url->sync_guid()); | |
| 2862 | |
| 2863 url_dict->SetString(default_search::kURL, url->url()); | |
| 2864 url_dict->SetString(default_search::kSuggestionsURL, url->suggestions_url()); | |
| 2865 url_dict->SetString(default_search::kInstantURL, url->instant_url()); | |
| 2866 url_dict->SetString(default_search::kImageURL, url->image_url()); | |
| 2867 url_dict->SetString(default_search::kNewTabURL, url->new_tab_url()); | |
| 2868 url_dict->SetString(default_search::kFaviconURL, url->favicon_url().spec()); | |
| 2869 url_dict->SetString(default_search::kOriginatingURL, | |
| 2870 url->originating_url().spec()); | |
| 2871 | |
| 2872 url_dict->SetString(default_search::kSearchURLPostParams, | |
| 2873 url->search_url_post_params()); | |
| 2874 url_dict->SetString(default_search::kSuggestionsURLPostParams, | |
| 2875 url->suggestions_url_post_params()); | |
| 2876 url_dict->SetString(default_search::kInstantURLPostParams, | |
| 2877 url->instant_url_post_params()); | |
| 2878 url_dict->SetString(default_search::kImageURLPostParams, | |
| 2879 url->image_url_post_params()); | |
| 2880 | |
| 2881 url_dict->SetBoolean(default_search::kSafeForAutoReplace, | |
| 2882 url->safe_for_autoreplace()); | |
| 2883 | |
| 2884 url_dict->SetDouble(default_search::kDateCreated, | |
| 2885 url->date_created().ToInternalValue()); | |
| 2886 url_dict->SetDouble(default_search::kLastModified, | |
| 2887 url->last_modified().ToInternalValue()); | |
| 2888 url_dict->SetInteger(default_search::kUsageCount, | |
| 2889 url->usage_count()); | |
| 2890 | |
| 2891 base::ListValue alternate_urls; | |
| 2892 for (size_t i = 0; i < url->alternate_urls().size(); ++i) | |
| 2893 alternate_urls.AppendString(url->alternate_urls()[i]); | |
| 2894 url_dict->Set(default_search::kAlternateURLs, &alternate_urls); | |
|
erikwright (departed)
2014/04/22 15:47:24
Doesn't Set() take ownership of alternate_urls?
Cait (Slow)
2014/04/22 19:54:13
Done.
| |
| 2895 | |
| 2896 base::ListValue encodings; | |
| 2897 for (size_t i = 0; i < url->input_encodings().size(); ++i) | |
| 2898 encodings.AppendString(url->input_encodings()[i]); | |
| 2899 url_dict->Set(default_search::kInputEncodings, &encodings); | |
| 2900 | |
| 2901 url_dict->SetString(default_search::kSearchTermsReplacementKey, | |
| 2902 url->search_terms_replacement_key()); | |
| 2903 | |
| 2904 url_dict->SetBoolean(default_search::kCreatedByPolicy, | |
| 2905 url->created_by_policy()); | |
| 2906 | |
| 2907 prefs->Set(prefs::kDefaultSearchProviderData, *url_dict); | |
| 2908 } | |
| OLD | NEW |