Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(723)

Side by Side Diff: chrome/browser/autocomplete/search_provider.h

Issue 66073: Makes the omnibox show past searches and suggestions for keywords.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2009 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 Search autocomplete provider. This provider is 5 // This file contains the Search autocomplete provider. This provider is
6 // responsible for all non-keyword autocomplete entries that start with 6 // responsible for all non-keyword autocomplete entries that start with
7 // "Search <engine> for ...", including searching for the current input string, 7 // "Search <engine> for ...", including searching for the current input string,
8 // search history, and search suggestions. An instance of it gets created and 8 // search history, and search suggestions. An instance of it gets created and
9 // managed by the autocomplete controller. 9 // managed by the autocomplete controller.
10 // 10 //
11 // For more information on the autocomplete system in general, including how 11 // For more information on the autocomplete system in general, including how
(...skipping 23 matching lines...) Expand all
35 // 35 //
36 // Initially the provider creates a match that searches for the current input 36 // Initially the provider creates a match that searches for the current input
37 // text. It also starts a task to query the Suggest servers. When that data 37 // text. It also starts a task to query the Suggest servers. When that data
38 // comes back, the provider creates and returns matches for the best 38 // comes back, the provider creates and returns matches for the best
39 // suggestions. 39 // suggestions.
40 class SearchProvider : public AutocompleteProvider, 40 class SearchProvider : public AutocompleteProvider,
41 public URLFetcher::Delegate { 41 public URLFetcher::Delegate {
42 public: 42 public:
43 SearchProvider(ACProviderListener* listener, Profile* profile) 43 SearchProvider(ACProviderListener* listener, Profile* profile)
44 : AutocompleteProvider(listener, profile, "Search"), 44 : AutocompleteProvider(listener, profile, "Search"),
45 last_default_provider_(NULL),
46 have_history_results_(false), 45 have_history_results_(false),
47 history_request_pending_(false), 46 history_request_pending_(false),
48 suggest_results_pending_(false), 47 suggest_results_pending_(0),
49 fetcher_(NULL),
50 have_suggest_results_(false) { 48 have_suggest_results_(false) {
51 } 49 }
52 50
53 // AutocompleteProvider 51 // AutocompleteProvider
54 virtual void Start(const AutocompleteInput& input, 52 virtual void Start(const AutocompleteInput& input,
55 bool minimal_changes); 53 bool minimal_changes);
56 virtual void Stop(); 54 virtual void Stop();
57 55
58 // URLFetcher::Delegate 56 // URLFetcher::Delegate
59 virtual void OnURLFetchComplete(const URLFetcher* source, 57 virtual void OnURLFetchComplete(const URLFetcher* source,
60 const GURL& url, 58 const GURL& url,
61 const URLRequestStatus& status, 59 const URLRequestStatus& status,
62 int response_code, 60 int response_code,
63 const ResponseCookies& cookies, 61 const ResponseCookies& cookies,
64 const std::string& data); 62 const std::string& data);
65 63
66 private: 64 private:
65 // Manages the providers (TemplateURLs) used by SearchProvider. Two providers
66 // may be used:
67 // . The default provider. This corresponds to the user's default search
68 // engine. This is always used, except for the rare case of no default
69 // engine.
70 // . The keyword provider. This is used if the user has typed in a keyword.
71 class Providers {
72 public:
73 Providers() : default_provider_(NULL), keyword_provider_(NULL) {}
74
75 // Returns true if the specified providers match the two providers managed
76 // by this class.
77 bool equals(const TemplateURL* default_provider,
78 const TemplateURL* keyword_provider) {
79 return (default_provider == default_provider_ &&
80 keyword_provider == keyword_provider_);
81 }
82
83 // Resets the providers.
84 void Set(const TemplateURL* default_provider,
85 const TemplateURL* keyword_provider);
86
87 const TemplateURL& default_provider() const {
88 DCHECK(valid_default_provider());
89 return cached_default_provider_;
90 }
91
92 const TemplateURL& keyword_provider() const {
93 DCHECK(valid_keyword_provider());
94 return cached_keyword_provider_;
95 }
96
97 // Returns true of the keyword provider is valid.
98 bool valid_keyword_provider() const { return !!keyword_provider_; }
99
100 // Returns true if the keyword provider is valid and has a valid suggest
101 // url.
102 bool valid_suggest_for_keyword_provider() const {
103 return keyword_provider_ && cached_keyword_provider_.suggestions_url();
104 }
105
106 // Returns true of the default provider is valid.
107 bool valid_default_provider() const { return !!default_provider_; }
108
109 // Returns true if the default provider is valid and has a valid suggest
110 // url.
111 bool valid_suggest_for_default_provider() const {
112 return default_provider_ && cached_default_provider_.suggestions_url();
113 }
114
115 // Returns true if |from_keyword_provider| is true, or
116 // the keyword provider is not valid.
117 bool is_primary_provider(bool from_keyword_provider) const {
118 return from_keyword_provider || !valid_keyword_provider();
119 }
120
121 private:
122 // Cached across the life of a query so we behave consistently even if the
123 // user changes their default while the query is running.
124 TemplateURL cached_default_provider_;
125 TemplateURL cached_keyword_provider_;
126
127 // TODO(pkasting): http://b/1162970 We shouldn't need these.
128 const TemplateURL* default_provider_;
129 const TemplateURL* keyword_provider_;
130 };
131
67 struct NavigationResult { 132 struct NavigationResult {
68 NavigationResult(const GURL& url, const std::wstring& site_name) 133 NavigationResult(const GURL& url, const std::wstring& site_name)
69 : url(url), 134 : url(url),
70 site_name(site_name) { 135 site_name(site_name) {
71 } 136 }
72 137
73 // The URL. 138 // The URL.
74 GURL url; 139 GURL url;
75 140
76 // Name for the site. 141 // Name for the site.
(...skipping 17 matching lines...) Expand all
94 // Returns true when the current query can be sent to the Suggest service. 159 // Returns true when the current query can be sent to the Suggest service.
95 // This will be false e.g. when Suggest is disabled, the query contains 160 // This will be false e.g. when Suggest is disabled, the query contains
96 // potentially private data, etc. 161 // potentially private data, etc.
97 bool IsQuerySuitableForSuggest() const; 162 bool IsQuerySuitableForSuggest() const;
98 163
99 // Functions to stop the separate asynchronous subcomponents. 164 // Functions to stop the separate asynchronous subcomponents.
100 // NOTE: These functions do not update |done_|. Callers must do so. 165 // NOTE: These functions do not update |done_|. Callers must do so.
101 void StopHistory(); 166 void StopHistory();
102 void StopSuggest(); 167 void StopSuggest();
103 168
169 // Schedules a history query requesting past searches against the engine
170 // whose id is |search_id| and whose text starts with |text|.
171 void ScheduleHistoryQuery(TemplateURL::IDType search_id,
172 const std::wstring& text);
173
104 // Called back by the history system to return searches that begin with the 174 // Called back by the history system to return searches that begin with the
105 // input text. 175 // input text.
106 void OnGotMostRecentKeywordSearchTerms( 176 void OnGotMostRecentKeywordSearchTerms(
107 CancelableRequestProvider::Handle handle, 177 CancelableRequestProvider::Handle handle,
108 HistoryResults* results); 178 HistoryResults* results);
109 179
180 // Creates a URLFetcher requesting suggest results for the specified
181 // TemplateURL. Ownership of the returned URLFetchet passes to the caller.
182 URLFetcher* CreateSuggestFetcher(const TemplateURL& provider,
183 const std::wstring& text);
184
110 // Parses the results from the Suggest server and stores up to kMaxMatches of 185 // Parses the results from the Suggest server and stores up to kMaxMatches of
111 // them in server_results_. Returns whether parsing succeeded. 186 // them in server_results_. Returns whether parsing succeeded.
112 bool ParseSuggestResults(Value* root_val); 187 bool ParseSuggestResults(Value* root_val,
188 bool is_keyword,
189 const std::wstring& input_text,
190 SuggestResults* suggest_results);
113 191
114 // Converts the parsed server results in server_results_ to a set of 192 // Converts the parsed server results in server_results_ to a set of
115 // AutocompleteMatches and adds them to |matches_|. This also sets |done_| 193 // AutocompleteMatches and adds them to |matches_|. This also sets |done_|
116 // correctly. 194 // correctly.
117 void ConvertResultsToAutocompleteMatches(); 195 void ConvertResultsToAutocompleteMatches();
118 196
197 // Converts the first navigation result in |navigation_results| to an
198 // AutocompleteMatch and adds it to |matches_|.
199 void AddNavigationResultsToMatches(
200 const NavigationResults& navigation_results,
201 bool is_keyword);
202
203 // Adds a match for each result in |results| to |map|. |is_keyword| indicates
204 // whether the results correspond to the keyword provider or default provider.
205 void AddHistoryResultsToMap(const HistoryResults& results,
206 bool is_keyword,
207 int did_not_accept_suggestion,
208 MatchMap* map);
209
210 // Adds a match for each result in |suggest_results| to |map|. |is_keyword|
211 // indicates whether the results correspond to the keyword provider or default
212 // provider.
213 void AddSuggestResultsToMap(const SuggestResults& suggest_results,
214 bool is_keyword,
215 int did_not_accept_suggestion,
216 MatchMap* map);
217
119 // Determines the relevance for a particular match. We use different scoring 218 // Determines the relevance for a particular match. We use different scoring
120 // algorithms for the different types of matches. 219 // algorithms for the different types of matches.
121 int CalculateRelevanceForWhatYouTyped() const; 220 int CalculateRelevanceForWhatYouTyped() const;
122 // |time| is the time at which this query was last seen. 221 // |time| is the time at which this query was last seen. |is_keyword| is true
123 int CalculateRelevanceForHistory(const base::Time& time) const; 222 // if the search is from the keyword provider.
124 // |suggestion_value| is which suggestion this is in the list returned from 223 int CalculateRelevanceForHistory(const base::Time& time,
125 // the server; the best suggestion is suggestion number 0. 224 bool is_keyword) const;
126 int CalculateRelevanceForSuggestion(size_t suggestion_value) const; 225 // |suggestion_value| is the index of the suggestion in |suggest_results| that
127 // |suggestion_value| is same as above. 226 // was returned from the server; the best suggestion is suggestion number 0.
128 int CalculateRelevanceForNavigation(size_t suggestion_value) const; 227 // |is_keyword| is true if the search is from the keyword provider.
228 int CalculateRelevanceForSuggestion(const SuggestResults& suggest_results,
229 size_t suggestion_number,
230 bool is_keyword) const;
231 // |suggestion_value| is same as above. |is_keyword| is true if the navigation
232 // result was suggested by the keyword provider.
233 int CalculateRelevanceForNavigation(size_t suggestion_value,
234 bool is_keyword) const;
129 235
130 // Creates an AutocompleteMatch for "Search <engine> for |query_string|" with 236 // Creates an AutocompleteMatch for "Search <engine> for |query_string|" with
131 // the supplied relevance. Adds this match to |map|; if such a match already 237 // the supplied relevance. Adds this match to |map|; if such a match already
132 // exists, whichever one has lower relevance is eliminated. 238 // exists, whichever one has lower relevance is eliminated.
133 void AddMatchToMap(const std::wstring& query_string, 239 void AddMatchToMap(const std::wstring& query_string,
134 int relevance, 240 int relevance,
135 AutocompleteMatch::Type type, 241 AutocompleteMatch::Type type,
136 int accepted_suggestion, 242 int accepted_suggestion,
243 bool is_keyword,
137 MatchMap* map); 244 MatchMap* map);
138 // Returns an AutocompleteMatch for a navigational suggestion. 245 // Returns an AutocompleteMatch for a navigational suggestion.
139 AutocompleteMatch NavigationToMatch(const NavigationResult& query_string, 246 AutocompleteMatch NavigationToMatch(const NavigationResult& query_string,
140 int relevance); 247 int relevance,
248 bool is_keyword);
141 249
142 // Trims "http:" and up to two subsequent slashes from |url|. Returns the 250 // Trims "http:" and up to two subsequent slashes from |url|. Returns the
143 // number of characters that were trimmed. 251 // number of characters that were trimmed.
144 // TODO(kochi): this is duplicate from history_autocomplete 252 // TODO(kochi): this is duplicate from history_autocomplete
145 static size_t TrimHttpPrefix(std::wstring* url); 253 static size_t TrimHttpPrefix(std::wstring* url);
146 254
255 // Maintains the TemplateURLs used.
256 Providers providers_;
257
147 // The user's input. 258 // The user's input.
148 AutocompleteInput input_; 259 AutocompleteInput input_;
149 260
150 // Cached across the life of a query so we behave consistently even if the 261 // Input text when searching against the keyword provider.
151 // user changes their default while the query is running. 262 std::wstring keyword_input_text_;
152 TemplateURL default_provider_;
153 263
154 // TODO(pkasting): http://b/1162970 We shouldn't need this. 264 // An object we can use to cancel history requests. The client data
155 const TemplateURL* last_default_provider_; 265 // corresponds to the id of the search engine and is used in the callback to
156 266 // determine whether the request corresponds to the keyword of default
157 // An object we can use to cancel history requests. 267 // provider.
158 CancelableRequestConsumer history_request_consumer_; 268 CancelableRequestConsumerTSimple<TemplateURL::IDType>
269 history_request_consumer_;
159 270
160 // Searches in the user's history that begin with the input text. 271 // Searches in the user's history that begin with the input text.
161 HistoryResults history_results_; 272 HistoryResults keyword_history_results_;
273 HistoryResults default_history_results_;
162 274
163 // Whether history_results_ is valid (so we can tell invalid apart from 275 // Whether history_results_ is valid (so we can tell invalid apart from
164 // empty). 276 // empty).
165 bool have_history_results_; 277 bool have_history_results_;
166 278
167 // Whether we are waiting for a history request to finish. 279 // Whether we are waiting for a history request to finish.
168 bool history_request_pending_; 280 bool history_request_pending_;
169 281
170 // True if we're expecting suggest results that haven't yet arrived. This 282 // Number of suggest results that haven't yet arrived. If greater than 0 it
171 // could be because either |timer_| or |fetcher| is still running (see below). 283 // indicates either |timer_| or one of the URLFetchers is still running.
172 bool suggest_results_pending_; 284 int suggest_results_pending_;
173 285
174 // A timer to start a query to the suggest server after the user has stopped 286 // A timer to start a query to the suggest server after the user has stopped
175 // typing for long enough. 287 // typing for long enough.
176 base::OneShotTimer<SearchProvider> timer_; 288 base::OneShotTimer<SearchProvider> timer_;
177 289
178 // The fetcher that retrieves suggest results from the server. 290 // The fetcher that retrieves suggest results for the keyword from the server.
179 scoped_ptr<URLFetcher> fetcher_; 291 scoped_ptr<URLFetcher> keyword_fetcher_;
292
293 // The fetcher that retrieves suggest results for the default engine from the
294 // server.
295 scoped_ptr<URLFetcher> default_fetcher_;
180 296
181 // Suggestions returned by the Suggest server for the input text. 297 // Suggestions returned by the Suggest server for the input text.
182 SuggestResults suggest_results_; 298 SuggestResults keyword_suggest_results_;
299 SuggestResults default_suggest_results_;
183 300
184 // Navigational suggestions returned by the server. 301 // Navigational suggestions returned by the server.
185 NavigationResults navigation_results_; 302 NavigationResults keyword_navigation_results_;
303 NavigationResults default_navigation_results_;
186 304
187 // Whether suggest_results_ is valid. 305 // Whether suggest_results_ is valid.
188 bool have_suggest_results_; 306 bool have_suggest_results_;
189 307
190 DISALLOW_EVIL_CONSTRUCTORS(SearchProvider); 308 DISALLOW_EVIL_CONSTRUCTORS(SearchProvider);
191 }; 309 };
192 310
193 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ 311 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/keyword_provider.cc ('k') | chrome/browser/autocomplete/search_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698