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 3 // found in the LICENSE file. 4 // 5 // This file contains the zero-suggest autocomplete provider. This experimental 6 // provider is invoked when the user focuses in the omnibox prior to editing, 7 // and generates search query suggestions based on the current URL. To enable 8 // this provider, point --experimental-zero-suggest-url-prefix at an 9 // appropriate suggestion service. 10 // 11 // HUGE DISCLAIMER: This is just here for experimenting and will probably be 12 // deleted entirely as we revise how suggestions work with the omnibox. 13 14 #ifndef CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ 15 #define CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ 16 17 #include "base/basictypes.h" 18 #include "base/compiler_specific.h" 19 #include "base/memory/scoped_ptr.h" 20 #include "chrome/browser/autocomplete/base_search_provider.h" 21 #include "chrome/browser/autocomplete/search_provider.h" 22 #include "components/metrics/proto/omnibox_event.pb.h" 23 24 class TemplateURLService; 25 26 namespace base { 27 class ListValue; 28 class Value; 29 } 30 31 namespace net { 32 class URLFetcher; 33 } 34 35 namespace user_prefs { 36 class PrefRegistrySyncable; 37 } 38 39 // Autocomplete provider for searches based on the current URL. 40 // 41 // The controller will call StartZeroSuggest when the user focuses in the 42 // omnibox. After construction, the autocomplete controller repeatedly calls 43 // Start() with some user input, each time expecting to receive an updated 44 // set of matches. 45 // 46 // TODO(jered): Consider deleting this class and building this functionality 47 // into SearchProvider after dogfood and after we break the association between 48 // omnibox text and suggestions. 49 class ZeroSuggestProvider : public BaseSearchProvider { 50 public: 51 // Creates and returns an instance of this provider. 52 static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener, 53 Profile* profile); 54 55 // Registers a preference used to cache zero suggest results. 56 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 57 58 // AutocompleteProvider: 59 virtual void Start(const AutocompleteInput& input, 60 bool minimal_changes) OVERRIDE; 61 virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE; 62 63 // Sets |field_trial_triggered_| to false. 64 virtual void ResetSession() OVERRIDE; 65 66 protected: 67 // BaseSearchProvider: 68 virtual void ModifyProviderInfo( 69 metrics::OmniboxEventProto_ProviderInfo* provider_info) const OVERRIDE; 70 71 private: 72 ZeroSuggestProvider(AutocompleteProviderListener* listener, 73 Profile* profile); 74 75 virtual ~ZeroSuggestProvider(); 76 77 // BaseSearchProvider: 78 virtual bool StoreSuggestionResponse(const std::string& json_data, 79 const base::Value& parsed_data) OVERRIDE; 80 virtual const TemplateURL* GetTemplateURL(bool is_keyword) const OVERRIDE; 81 virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE; 82 virtual Results* GetResultsToFill(bool is_keyword) OVERRIDE; 83 virtual bool ShouldAppendExtraParams( 84 const SuggestResult& result) const OVERRIDE; 85 virtual void StopSuggest() OVERRIDE; 86 virtual void ClearAllResults() OVERRIDE; 87 virtual int GetDefaultResultRelevance() const OVERRIDE; 88 virtual void RecordDeletionResult(bool success) OVERRIDE; 89 virtual void LogFetchComplete(bool success, bool is_keyword) OVERRIDE; 90 virtual bool IsKeywordFetcher(const net::URLFetcher* fetcher) const OVERRIDE; 91 virtual void UpdateMatches() OVERRIDE; 92 93 // Adds AutocompleteMatches for each of the suggestions in |results| to 94 // |map|. 95 void AddSuggestResultsToMap(const SuggestResults& results, MatchMap* map); 96 97 // Returns an AutocompleteMatch for a navigational suggestion |navigation|. 98 AutocompleteMatch NavigationToMatch(const NavigationResult& navigation); 99 100 // Fetches zero-suggest suggestions by sending a request using |suggest_url|. 101 void Run(const GURL& suggest_url); 102 103 // Converts the parsed results to a set of AutocompleteMatches and adds them 104 // to |matches_|. Also update the histograms for how many results were 105 // received. 106 void ConvertResultsToAutocompleteMatches(); 107 108 // Returns an AutocompleteMatch for the current URL. The match should be in 109 // the top position so that pressing enter has the effect of reloading the 110 // page. 111 AutocompleteMatch MatchForCurrentURL(); 112 113 // When the user is in the Most Visited field trial, we ask the TopSites 114 // service for the most visited URLs during Run(). It calls back to this 115 // function to return those |urls|. 116 void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls); 117 118 // Returns the relevance score for the verbatim result. 119 int GetVerbatimRelevance() const; 120 121 // Whether we can show zero suggest on |current_page_url| without 122 // sending |current_page_url| as a parameter to the server at |suggest_url|. 123 bool CanShowZeroSuggestWithoutSendingURL(const GURL& suggest_url, 124 const GURL& current_page_url) const; 125 126 // Checks whether we have a set of zero suggest results cached, and if so 127 // populates |matches_| with cached results. 128 void MaybeUseCachedSuggestions(); 129 130 // Used to build default search engine URLs for suggested queries. 131 TemplateURLService* template_url_service_; 132 133 // The URL for which a suggestion fetch is pending. 134 std::string current_query_; 135 136 // The type of page the user is viewing (a search results page doing search 137 // term replacement, an arbitrary URL, etc.). 138 metrics::OmniboxEventProto::PageClassification current_page_classification_; 139 140 // Copy of OmniboxEditModel::permanent_text_. 141 base::string16 permanent_text_; 142 143 // Fetcher used to retrieve results. 144 scoped_ptr<net::URLFetcher> fetcher_; 145 146 // Suggestion for the current URL. 147 AutocompleteMatch current_url_match_; 148 149 // Contains suggest and navigation results as well as relevance parsed from 150 // the response for the most recent zero suggest input URL. 151 Results results_; 152 153 // Whether we are currently showing cached zero suggest results. 154 bool results_from_cache_; 155 156 history::MostVisitedURLList most_visited_urls_; 157 158 // For callbacks that may be run after destruction. 159 base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_; 160 161 DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider); 162 }; 163 164 #endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ 165