| 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 // This file contains the zero-suggest autocomplete provider. This experimental | 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, | 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 | 7 // and generates search query suggestions based on the current URL. To enable |
| 8 // this provider, point --experimental-zero-suggest-url-prefix at an | 8 // this provider, point --experimental-zero-suggest-url-prefix at an |
| 9 // appropriate suggestion service. | 9 // appropriate suggestion service. |
| 10 // | 10 // |
| 11 // HUGE DISCLAIMER: This is just here for experimenting and will probably be | 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. | 12 // deleted entirely as we revise how suggestions work with the omnibox. |
| 13 | 13 |
| 14 #ifndef CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ | 14 #ifndef CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ |
| 15 #define CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ | 15 #define CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ |
| 16 | 16 |
| 17 #include <map> | 17 #include <map> |
| 18 #include <string> | 18 #include <string> |
| 19 #include <vector> | 19 #include <vector> |
| 20 | 20 |
| 21 #include "base/basictypes.h" | 21 #include "base/memory/weak_ptr.h" |
| 22 #include "base/compiler_specific.h" | |
| 23 #include "base/memory/scoped_ptr.h" | |
| 24 #include "base/strings/string16.h" | 22 #include "base/strings/string16.h" |
| 25 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 23 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 26 #include "chrome/browser/autocomplete/search_provider.h" | 24 #include "chrome/browser/autocomplete/base_search_provider.h" |
| 27 #include "net/url_request/url_fetcher_delegate.h" | 25 #include "chrome/browser/history/history_types.h" |
| 28 | 26 |
| 29 class AutocompleteInput; | 27 class AutocompleteInput; |
| 30 class GURL; | 28 class GURL; |
| 31 class TemplateURLService; | 29 class TemplateURLService; |
| 32 | 30 |
| 33 namespace base { | 31 namespace base { |
| 34 class ListValue; | 32 class ListValue; |
| 35 class Value; | 33 class Value; |
| 36 } | 34 } |
| 37 | 35 |
| 38 namespace net { | 36 namespace net { |
| 39 class URLFetcher; | 37 class URLFetcher; |
| 40 } | 38 } |
| 41 | 39 |
| 42 // Autocomplete provider for searches based on the current URL. | 40 // Autocomplete provider for searches based on the current URL. |
| 43 // | 41 // |
| 44 // The controller will call StartZeroSuggest when the user focuses in the | 42 // The controller will call StartZeroSuggest when the user focuses in the |
| 45 // omnibox. After construction, the autocomplete controller repeatedly calls | 43 // omnibox. After construction, the autocomplete controller repeatedly calls |
| 46 // Start() with some user input, each time expecting to receive an updated | 44 // Start() with some user input, each time expecting to receive an updated |
| 47 // set of matches. | 45 // set of matches. |
| 48 // | 46 // |
| 49 // TODO(jered): Consider deleting this class and building this functionality | 47 // TODO(jered): Consider deleting this class and building this functionality |
| 50 // into SearchProvider after dogfood and after we break the association between | 48 // into SearchProvider after dogfood and after we break the association between |
| 51 // omnibox text and suggestions. | 49 // omnibox text and suggestions. |
| 52 class ZeroSuggestProvider : public AutocompleteProvider, | 50 class ZeroSuggestProvider : public BaseSearchProvider { |
| 53 public net::URLFetcherDelegate { | |
| 54 public: | 51 public: |
| 55 // Creates and returns an instance of this provider. | 52 // Creates and returns an instance of this provider. |
| 56 static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener, | 53 static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener, |
| 57 Profile* profile); | 54 Profile* profile); |
| 58 | 55 |
| 59 // AutocompleteProvider: | 56 // AutocompleteProvider: |
| 60 virtual void Start(const AutocompleteInput& input, | |
| 61 bool /*minimal_changes*/) OVERRIDE; | |
| 62 virtual void Stop(bool clear_cached_results) OVERRIDE; | |
| 63 | |
| 64 // Adds provider-specific information to omnibox event logs. | |
| 65 virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE; | |
| 66 | 57 |
| 67 // Sets |field_trial_triggered_| to false. | 58 // Sets |field_trial_triggered_| to false. |
| 68 virtual void ResetSession() OVERRIDE; | 59 virtual void ResetSession() OVERRIDE; |
| 69 | 60 |
| 70 // net::URLFetcherDelegate | |
| 71 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 72 | |
| 73 // Initiates a new fetch for the given |url| of classification | 61 // Initiates a new fetch for the given |url| of classification |
| 74 // |page_classification|. |permanent_text| is the omnibox text | 62 // |page_classification|. |permanent_text| is the omnibox text |
| 75 // for the current page. | 63 // for the current page. |
| 76 void StartZeroSuggest( | 64 void StartZeroSuggest( |
| 77 const GURL& curent_page_url, | 65 const GURL& curent_page_url, |
| 78 AutocompleteInput::PageClassification page_classification, | 66 AutocompleteInput::PageClassification page_classification, |
| 79 const base::string16& permanent_text); | 67 const base::string16& permanent_text); |
| 80 | 68 |
| 81 bool field_trial_triggered_in_session() const { | 69 bool field_trial_triggered_in_session() const { |
| 82 return field_trial_triggered_in_session_; | 70 return field_trial_triggered_in_session_; |
| 83 } | 71 } |
| 84 | 72 |
| 85 private: | 73 private: |
| 74 // BaseSearchProvider: |
| 75 // Stops the suggest query. |
| 76 // NOTE: This does not update |done_|. Callers must do so. |
| 77 virtual void StopSuggest() OVERRIDE; |
| 78 |
| 79 // Clears the current results. |
| 80 virtual void ClearAllResults() OVERRIDE; |
| 81 |
| 82 virtual void LogFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 83 |
| 84 virtual bool ShouldSendProviderUpdate(bool results_updated) OVERRIDE; |
| 85 |
| 86 virtual bool IsValidQuery(const base::string16 query, |
| 87 const net::URLFetcher* source) OVERRIDE; |
| 88 virtual const base::string16 GetInputText(const net::URLFetcher* source) |
| 89 OVERRIDE; |
| 90 virtual int GetDefaultRelevance() OVERRIDE; |
| 91 |
| 92 virtual void UpdateMatches() OVERRIDE; |
| 93 |
| 94 virtual Results* GetResultsObjectToFill(const net::URLFetcher* source) |
| 95 OVERRIDE; |
| 96 |
| 97 virtual void RecordDeletionResult(bool success) OVERRIDE; |
| 98 |
| 86 ZeroSuggestProvider(AutocompleteProviderListener* listener, | 99 ZeroSuggestProvider(AutocompleteProviderListener* listener, |
| 87 Profile* profile); | 100 Profile* profile); |
| 88 | 101 |
| 89 virtual ~ZeroSuggestProvider(); | 102 virtual ~ZeroSuggestProvider(); |
| 90 | 103 |
| 91 // The 4 functions below (that take classes defined in SearchProvider as | 104 // Creates AutocompleteMatches to search |template_url| for "<suggestion>" |
| 92 // arguments) were copied and trimmed from SearchProvider. | 105 // for all suggestions in |results|, and adds them to |map|. |
| 93 // TODO(hfung): Refactor them into a new base class common to both | 106 void AddSuggestResultsToMap(const SuggestResults& results, |
| 94 // ZeroSuggestProvider and SearchProvider. | |
| 95 | |
| 96 // From the OpenSearch formatted response |root_val|, populate query | |
| 97 // suggestions into |suggest_results|, navigation suggestions into | |
| 98 // |navigation_results|, and the verbatim relevance score into | |
| 99 // |verbatim_relevance|. | |
| 100 void FillResults(const base::Value& root_val, | |
| 101 int* verbatim_relevance, | |
| 102 SearchProvider::SuggestResults* suggest_results, | |
| 103 SearchProvider::NavigationResults* navigation_results); | |
| 104 | |
| 105 // Creates AutocompleteMatches to search |template_url| for "<suggestion>" for | |
| 106 // all suggestions in |results|, and adds them to |map|. | |
| 107 void AddSuggestResultsToMap(const SearchProvider::SuggestResults& results, | |
| 108 const TemplateURL* template_url, | 107 const TemplateURL* template_url, |
| 109 SearchProvider::MatchMap* map); | 108 MatchMap* map); |
| 110 | |
| 111 // Creates an AutocompleteMatch with the provided |relevance| and |type| to | |
| 112 // search |template_url| for |query_string|. |accepted_suggestion| will be | |
| 113 // used to generate Assisted Query Stats. | |
| 114 // | |
| 115 // Adds this match to |map|; if such a match already exists, whichever one | |
| 116 // has lower relevance is eliminated. | |
| 117 void AddMatchToMap(int relevance, | |
| 118 AutocompleteMatch::Type type, | |
| 119 const TemplateURL* template_url, | |
| 120 const base::string16& query_string, | |
| 121 int accepted_suggestion, | |
| 122 SearchProvider::MatchMap* map); | |
| 123 | 109 |
| 124 // Returns an AutocompleteMatch for a navigational suggestion |navigation|. | 110 // Returns an AutocompleteMatch for a navigational suggestion |navigation|. |
| 125 AutocompleteMatch NavigationToMatch( | 111 AutocompleteMatch NavigationToMatch(const NavigationResult& navigation); |
| 126 const SearchProvider::NavigationResult& navigation); | |
| 127 | 112 |
| 128 // Fetches zero-suggest suggestions by sending a request using |suggest_url|. | 113 // Fetches zero-suggest suggestions by sending a request using |suggest_url|. |
| 129 void Run(const GURL& suggest_url); | 114 void Run(const GURL& suggest_url); |
| 130 | 115 |
| 131 // Parses results from the zero-suggest server and updates results. | |
| 132 void ParseSuggestResults(const base::Value& root_val); | |
| 133 | |
| 134 // Converts the parsed results to a set of AutocompleteMatches and adds them | 116 // Converts the parsed results to a set of AutocompleteMatches and adds them |
| 135 // to |matches_|. Also update the histograms for how many results were | 117 // to |matches_|. Also update the histograms for how many results were |
| 136 // received. | 118 // received. |
| 137 void ConvertResultsToAutocompleteMatches(); | 119 void ConvertResultsToAutocompleteMatches(); |
| 138 | 120 |
| 139 // Returns an AutocompleteMatch for the current URL. The match should be in | 121 // Returns an AutocompleteMatch for the current URL. The match should be in |
| 140 // the top position so that pressing enter has the effect of reloading the | 122 // the top position so that pressing enter has the effect of reloading the |
| 141 // page. | 123 // page. |
| 142 AutocompleteMatch MatchForCurrentURL(); | 124 AutocompleteMatch MatchForCurrentURL(); |
| 143 | 125 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 154 | 136 |
| 155 // The type of page the user is viewing (a search results page doing search | 137 // The type of page the user is viewing (a search results page doing search |
| 156 // term replacement, an arbitrary URL, etc.). | 138 // term replacement, an arbitrary URL, etc.). |
| 157 AutocompleteInput::PageClassification current_page_classification_; | 139 AutocompleteInput::PageClassification current_page_classification_; |
| 158 | 140 |
| 159 // Copy of OmniboxEditModel::permanent_text_. | 141 // Copy of OmniboxEditModel::permanent_text_. |
| 160 base::string16 permanent_text_; | 142 base::string16 permanent_text_; |
| 161 | 143 |
| 162 // Fetcher used to retrieve results. | 144 // Fetcher used to retrieve results. |
| 163 scoped_ptr<net::URLFetcher> fetcher_; | 145 scoped_ptr<net::URLFetcher> fetcher_; |
| 164 // Whether there's a pending request in flight. | |
| 165 bool have_pending_request_; | |
| 166 | 146 |
| 167 // Suggestion for the current URL. | 147 // Suggestion for the current URL. |
| 168 AutocompleteMatch current_url_match_; | 148 AutocompleteMatch current_url_match_; |
| 169 // Navigation suggestions for the most recent ZeroSuggest input URL. | 149 |
| 170 SearchProvider::NavigationResults navigation_results_; | 150 // Suggestions received from the most recent run. |
| 171 // Query suggestions for the most recent ZeroSuggest input URL. | 151 Results results_; |
| 172 SearchProvider::MatchMap query_matches_map_; | 152 |
| 173 // The relevance score for the URL of the current page. | 153 // Generally an empty string since this is zero-suggest. |
| 174 int verbatim_relevance_; | 154 base::string16 input_text_; |
| 175 | 155 |
| 176 // Whether a field trial, if any, has triggered in the most recent | 156 // Whether a field trial, if any, has triggered in the most recent |
| 177 // autocomplete query. This field is set to true if the last request | 157 // autocomplete query. This field is set to true if the last request |
| 178 // was a zero suggest request, the provider has completed and their | 158 // was a zero suggest request, the provider has completed and their |
| 179 // corresponding response contained '"google:fieldtrialtriggered":true'. | 159 // corresponding response contained '"google:fieldtrialtriggered":true'. |
| 180 bool field_trial_triggered_; | 160 bool field_trial_triggered_; |
| 181 // Whether a zero suggest request triggered a field trial in the omnibox | 161 // Whether a zero suggest request triggered a field trial in the omnibox |
| 182 // session. The user could have clicked on a suggestion when zero suggest | 162 // session. The user could have clicked on a suggestion when zero suggest |
| 183 // triggered (same condition as field_trial_triggered_), or triggered zero | 163 // triggered (same condition as field_trial_triggered_), or triggered zero |
| 184 // suggest but kept typing. | 164 // suggest but kept typing. |
| 185 bool field_trial_triggered_in_session_; | 165 bool field_trial_triggered_in_session_; |
| 186 | 166 |
| 187 history::MostVisitedURLList most_visited_urls_; | 167 history::MostVisitedURLList most_visited_urls_; |
| 188 | 168 |
| 189 // For callbacks that may be run after destruction. | 169 // For callbacks that may be run after destruction. |
| 190 base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_; | 170 base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_; |
| 191 | 171 |
| 192 DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider); | 172 DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider); |
| 193 }; | 173 }; |
| 194 | 174 |
| 195 #endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ | 175 #endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ |
| OLD | NEW |