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