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