| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/autocomplete/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 19 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
| 20 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 20 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
| 21 #include "chrome/browser/autocomplete/chrome_autocomplete_provider_delegate.h" |
| 21 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" | 22 #include "chrome/browser/autocomplete/chrome_autocomplete_scheme_classifier.h" |
| 22 #include "chrome/browser/autocomplete/history_url_provider.h" | 23 #include "chrome/browser/autocomplete/history_url_provider.h" |
| 23 #include "chrome/browser/history/history_service.h" | 24 #include "chrome/browser/history/history_service.h" |
| 24 #include "chrome/browser/history/history_service_factory.h" | 25 #include "chrome/browser/history/history_service_factory.h" |
| 25 #include "chrome/browser/search_engines/template_url_service_factory.h" | 26 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 26 #include "chrome/browser/signin/signin_manager_factory.h" | 27 #include "chrome/browser/signin/signin_manager_factory.h" |
| 27 #include "chrome/browser/sync/profile_sync_service.h" | 28 #include "chrome/browser/sync/profile_sync_service.h" |
| 28 #include "chrome/browser/sync/profile_sync_service_factory.h" | 29 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 29 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
| 30 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 private: | 80 private: |
| 80 virtual void RecordDeletionResult(bool success) OVERRIDE; | 81 virtual void RecordDeletionResult(bool success) OVERRIDE; |
| 81 bool is_success_; | 82 bool is_success_; |
| 82 DISALLOW_COPY_AND_ASSIGN(SearchProviderForTest); | 83 DISALLOW_COPY_AND_ASSIGN(SearchProviderForTest); |
| 83 }; | 84 }; |
| 84 | 85 |
| 85 SearchProviderForTest::SearchProviderForTest( | 86 SearchProviderForTest::SearchProviderForTest( |
| 86 AutocompleteProviderListener* listener, | 87 AutocompleteProviderListener* listener, |
| 87 TemplateURLService* template_url_service, | 88 TemplateURLService* template_url_service, |
| 88 Profile* profile) | 89 Profile* profile) |
| 89 : SearchProvider(listener, template_url_service, profile), | 90 : SearchProvider(listener, template_url_service, |
| 91 scoped_ptr<AutocompleteProviderDelegate>( |
| 92 new ChromeAutocompleteProviderDelegate(profile))), |
| 90 is_success_(false) { | 93 is_success_(false) { |
| 91 } | 94 } |
| 92 | 95 |
| 93 SearchProviderForTest::~SearchProviderForTest() { | 96 SearchProviderForTest::~SearchProviderForTest() { |
| 94 } | 97 } |
| 95 | 98 |
| 96 void SearchProviderForTest::RecordDeletionResult(bool success) { | 99 void SearchProviderForTest::RecordDeletionResult(bool success) { |
| 97 is_success_ = success; | 100 is_success_ = success; |
| 98 } | 101 } |
| 99 | 102 |
| (...skipping 2838 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2938 template_url_data.SetURL("http://www.google.com/{searchTerms}"); | 2941 template_url_data.SetURL("http://www.google.com/{searchTerms}"); |
| 2939 template_url_data.suggestions_url = "http://www.google.com/{searchTerms}"; | 2942 template_url_data.suggestions_url = "http://www.google.com/{searchTerms}"; |
| 2940 template_url_data.instant_url = "http://does/not/exist?strk=1"; | 2943 template_url_data.instant_url = "http://does/not/exist?strk=1"; |
| 2941 template_url_data.search_terms_replacement_key = "strk"; | 2944 template_url_data.search_terms_replacement_key = "strk"; |
| 2942 template_url_data.id = SEARCH_ENGINE_GOOGLE; | 2945 template_url_data.id = SEARCH_ENGINE_GOOGLE; |
| 2943 TemplateURL google_template_url(template_url_data); | 2946 TemplateURL google_template_url(template_url_data); |
| 2944 | 2947 |
| 2945 // Create field trial. | 2948 // Create field trial. |
| 2946 CreateZeroSuggestFieldTrial(true); | 2949 CreateZeroSuggestFieldTrial(true); |
| 2947 | 2950 |
| 2951 ChromeAutocompleteProviderDelegate delegate(&profile_); |
| 2952 |
| 2948 // Not signed in. | 2953 // Not signed in. |
| 2949 EXPECT_FALSE(SearchProvider::CanSendURL( | 2954 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 2950 GURL("http://www.google.com/search"), | 2955 GURL("http://www.google.com/search"), |
| 2951 GURL("https://www.google.com/complete/search"), &google_template_url, | 2956 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 2952 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); | 2957 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &delegate)); |
| 2953 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(&profile_); | 2958 SigninManagerBase* signin = SigninManagerFactory::GetForProfile(&profile_); |
| 2954 signin->SetAuthenticatedUsername("test"); | 2959 signin->SetAuthenticatedUsername("test"); |
| 2955 | 2960 |
| 2956 // All conditions should be met. | 2961 // All conditions should be met. |
| 2957 EXPECT_TRUE(SearchProvider::CanSendURL( | 2962 EXPECT_TRUE(SearchProvider::CanSendURL( |
| 2958 GURL("http://www.google.com/search"), | 2963 GURL("http://www.google.com/search"), |
| 2959 GURL("https://www.google.com/complete/search"), &google_template_url, | 2964 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 2960 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); | 2965 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &delegate)); |
| 2961 | 2966 |
| 2962 // Not in field trial. | 2967 // Not in field trial. |
| 2963 ResetFieldTrialList(); | 2968 ResetFieldTrialList(); |
| 2964 CreateZeroSuggestFieldTrial(false); | 2969 CreateZeroSuggestFieldTrial(false); |
| 2965 EXPECT_FALSE(SearchProvider::CanSendURL( | 2970 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 2966 GURL("http://www.google.com/search"), | 2971 GURL("http://www.google.com/search"), |
| 2967 GURL("https://www.google.com/complete/search"), &google_template_url, | 2972 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 2968 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); | 2973 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &delegate)); |
| 2969 ResetFieldTrialList(); | 2974 ResetFieldTrialList(); |
| 2970 CreateZeroSuggestFieldTrial(true); | 2975 CreateZeroSuggestFieldTrial(true); |
| 2971 | 2976 |
| 2972 // Invalid page URL. | 2977 // Invalid page URL. |
| 2973 EXPECT_FALSE(SearchProvider::CanSendURL( | 2978 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 2974 GURL("badpageurl"), | 2979 GURL("badpageurl"), |
| 2975 GURL("https://www.google.com/complete/search"), &google_template_url, | 2980 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 2976 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); | 2981 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &delegate)); |
| 2977 | 2982 |
| 2978 // Invalid page classification. | 2983 // Invalid page classification. |
| 2979 EXPECT_FALSE(SearchProvider::CanSendURL( | 2984 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 2980 GURL("http://www.google.com/search"), | 2985 GURL("http://www.google.com/search"), |
| 2981 GURL("https://www.google.com/complete/search"), &google_template_url, | 2986 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 2982 metrics::OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS, | 2987 metrics::OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS, |
| 2983 SearchTermsData(), &profile_)); | 2988 SearchTermsData(), &delegate)); |
| 2984 | 2989 |
| 2985 // Invalid page classification. | 2990 // Invalid page classification. |
| 2986 EXPECT_FALSE(SearchProvider::CanSendURL( | 2991 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 2987 GURL("http://www.google.com/search"), | 2992 GURL("http://www.google.com/search"), |
| 2988 GURL("https://www.google.com/complete/search"), &google_template_url, | 2993 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 2989 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, | 2994 metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS, |
| 2990 SearchTermsData(), &profile_)); | 2995 SearchTermsData(), &delegate)); |
| 2991 | 2996 |
| 2992 // HTTPS page URL on same domain as provider. | 2997 // HTTPS page URL on same domain as provider. |
| 2993 EXPECT_TRUE(SearchProvider::CanSendURL( | 2998 EXPECT_TRUE(SearchProvider::CanSendURL( |
| 2994 GURL("https://www.google.com/search"), | 2999 GURL("https://www.google.com/search"), |
| 2995 GURL("https://www.google.com/complete/search"), | 3000 GURL("https://www.google.com/complete/search"), |
| 2996 &google_template_url, metrics::OmniboxEventProto::OTHER, | 3001 &google_template_url, metrics::OmniboxEventProto::OTHER, |
| 2997 SearchTermsData(), &profile_)); | 3002 SearchTermsData(), &delegate)); |
| 2998 | 3003 |
| 2999 // Non-HTTP[S] page URL on same domain as provider. | 3004 // Non-HTTP[S] page URL on same domain as provider. |
| 3000 EXPECT_FALSE(SearchProvider::CanSendURL( | 3005 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3001 GURL("ftp://www.google.com/search"), | 3006 GURL("ftp://www.google.com/search"), |
| 3002 GURL("https://www.google.com/complete/search"), &google_template_url, | 3007 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3003 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); | 3008 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &delegate)); |
| 3004 | 3009 |
| 3005 // Non-HTTP page URL on different domain. | 3010 // Non-HTTP page URL on different domain. |
| 3006 EXPECT_FALSE(SearchProvider::CanSendURL( | 3011 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3007 GURL("https://www.notgoogle.com/search"), | 3012 GURL("https://www.notgoogle.com/search"), |
| 3008 GURL("https://www.google.com/complete/search"), &google_template_url, | 3013 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3009 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); | 3014 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &delegate)); |
| 3010 | 3015 |
| 3011 // Non-HTTPS provider. | 3016 // Non-HTTPS provider. |
| 3012 EXPECT_FALSE(SearchProvider::CanSendURL( | 3017 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3013 GURL("http://www.google.com/search"), | 3018 GURL("http://www.google.com/search"), |
| 3014 GURL("http://www.google.com/complete/search"), &google_template_url, | 3019 GURL("http://www.google.com/complete/search"), &google_template_url, |
| 3015 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); | 3020 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &delegate)); |
| 3016 | 3021 |
| 3017 // Suggest disabled. | 3022 // Suggest disabled. |
| 3018 profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); | 3023 profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false); |
| 3019 EXPECT_FALSE(SearchProvider::CanSendURL( | 3024 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3020 GURL("http://www.google.com/search"), | 3025 GURL("http://www.google.com/search"), |
| 3021 GURL("https://www.google.com/complete/search"), &google_template_url, | 3026 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3022 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); | 3027 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &delegate)); |
| 3023 profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); | 3028 profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true); |
| 3024 | 3029 |
| 3025 // Incognito. | 3030 // Incognito. |
| 3031 ChromeAutocompleteProviderDelegate delegate_incognito( |
| 3032 profile_.GetOffTheRecordProfile()); |
| 3026 EXPECT_FALSE(SearchProvider::CanSendURL( | 3033 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3027 GURL("http://www.google.com/search"), | 3034 GURL("http://www.google.com/search"), |
| 3028 GURL("https://www.google.com/complete/search"), &google_template_url, | 3035 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3029 metrics::OmniboxEventProto::OTHER, SearchTermsData(), | 3036 metrics::OmniboxEventProto::OTHER, SearchTermsData(), |
| 3030 profile_.GetOffTheRecordProfile())); | 3037 &delegate_incognito)); |
| 3031 | 3038 |
| 3032 // Tab sync not enabled. | 3039 // Tab sync not enabled. |
| 3033 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncKeepEverythingSynced, | 3040 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncKeepEverythingSynced, |
| 3034 false); | 3041 false); |
| 3035 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncTabs, false); | 3042 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncTabs, false); |
| 3036 EXPECT_FALSE(SearchProvider::CanSendURL( | 3043 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3037 GURL("http://www.google.com/search"), | 3044 GURL("http://www.google.com/search"), |
| 3038 GURL("https://www.google.com/complete/search"), &google_template_url, | 3045 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3039 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); | 3046 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &delegate)); |
| 3040 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncTabs, true); | 3047 profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncTabs, true); |
| 3041 | 3048 |
| 3042 // Tab sync is encrypted. | 3049 // Tab sync is encrypted. |
| 3043 ProfileSyncService* service = | 3050 ProfileSyncService* service = |
| 3044 ProfileSyncServiceFactory::GetInstance()->GetForProfile(&profile_); | 3051 ProfileSyncServiceFactory::GetInstance()->GetForProfile(&profile_); |
| 3045 syncer::ModelTypeSet encrypted_types = service->GetEncryptedDataTypes(); | 3052 syncer::ModelTypeSet encrypted_types = service->GetEncryptedDataTypes(); |
| 3046 encrypted_types.Put(syncer::SESSIONS); | 3053 encrypted_types.Put(syncer::SESSIONS); |
| 3047 service->OnEncryptedTypesChanged(encrypted_types, false); | 3054 service->OnEncryptedTypesChanged(encrypted_types, false); |
| 3048 EXPECT_FALSE(SearchProvider::CanSendURL( | 3055 EXPECT_FALSE(SearchProvider::CanSendURL( |
| 3049 GURL("http://www.google.com/search"), | 3056 GURL("http://www.google.com/search"), |
| 3050 GURL("https://www.google.com/complete/search"), &google_template_url, | 3057 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3051 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); | 3058 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &delegate)); |
| 3052 encrypted_types.Remove(syncer::SESSIONS); | 3059 encrypted_types.Remove(syncer::SESSIONS); |
| 3053 service->OnEncryptedTypesChanged(encrypted_types, false); | 3060 service->OnEncryptedTypesChanged(encrypted_types, false); |
| 3054 | 3061 |
| 3055 // Check that there were no side effects from previous tests. | 3062 // Check that there were no side effects from previous tests. |
| 3056 EXPECT_TRUE(SearchProvider::CanSendURL( | 3063 EXPECT_TRUE(SearchProvider::CanSendURL( |
| 3057 GURL("http://www.google.com/search"), | 3064 GURL("http://www.google.com/search"), |
| 3058 GURL("https://www.google.com/complete/search"), &google_template_url, | 3065 GURL("https://www.google.com/complete/search"), &google_template_url, |
| 3059 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_)); | 3066 metrics::OmniboxEventProto::OTHER, SearchTermsData(), &delegate)); |
| 3060 } | 3067 } |
| 3061 | 3068 |
| 3062 TEST_F(SearchProviderTest, TestDeleteMatch) { | 3069 TEST_F(SearchProviderTest, TestDeleteMatch) { |
| 3063 AutocompleteMatch match(provider_, 0, true, | 3070 AutocompleteMatch match(provider_, 0, true, |
| 3064 AutocompleteMatchType::SEARCH_SUGGEST); | 3071 AutocompleteMatchType::SEARCH_SUGGEST); |
| 3065 match.RecordAdditionalInfo( | 3072 match.RecordAdditionalInfo( |
| 3066 SearchProvider::kDeletionUrlKey, | 3073 SearchProvider::kDeletionUrlKey, |
| 3067 "https://www.google.com/complete/deleteitem?q=foo"); | 3074 "https://www.google.com/complete/deleteitem?q=foo"); |
| 3068 | 3075 |
| 3069 // Test a successful deletion request. | 3076 // Test a successful deletion request. |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3252 AutocompleteInput input(base::ASCIIToUTF16("weather l"), | 3259 AutocompleteInput input(base::ASCIIToUTF16("weather l"), |
| 3253 base::string16::npos, base::string16(), GURL(), | 3260 base::string16::npos, base::string16(), GURL(), |
| 3254 metrics::OmniboxEventProto::INVALID_SPEC, false, | 3261 metrics::OmniboxEventProto::INVALID_SPEC, false, |
| 3255 false, true, true, | 3262 false, true, true, |
| 3256 ChromeAutocompleteSchemeClassifier(&profile_)); | 3263 ChromeAutocompleteSchemeClassifier(&profile_)); |
| 3257 provider_->DoAnswersQuery(input); | 3264 provider_->DoAnswersQuery(input); |
| 3258 EXPECT_EQ(base::ASCIIToUTF16("weather los angeles"), | 3265 EXPECT_EQ(base::ASCIIToUTF16("weather los angeles"), |
| 3259 provider_->prefetch_data_.full_query_text); | 3266 provider_->prefetch_data_.full_query_text); |
| 3260 EXPECT_EQ(base::ASCIIToUTF16("2334"), provider_->prefetch_data_.query_type); | 3267 EXPECT_EQ(base::ASCIIToUTF16("2334"), provider_->prefetch_data_.query_type); |
| 3261 } | 3268 } |
| OLD | NEW |