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 #ifndef CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_ | 5 #ifndef CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_ |
6 #define CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_ | 6 #define CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 // request. This can happen frequently when the user is typing quickly. In | 81 // request. This can happen frequently when the user is typing quickly. In |
82 // this case, the main thread sets params_->cancel, which the background thread | 82 // this case, the main thread sets params_->cancel, which the background thread |
83 // checks periodically. If it finds the flag set, it stops what it's doing | 83 // checks periodically. If it finds the flag set, it stops what it's doing |
84 // immediately and calls back to the main thread. (We don't delete the params | 84 // immediately and calls back to the main thread. (We don't delete the params |
85 // on the history thread, because we should only do that when we can safely | 85 // on the history thread, because we should only do that when we can safely |
86 // NULL out params_, and that must be done on the main thread.) | 86 // NULL out params_, and that must be done on the main thread.) |
87 | 87 |
88 // Used to communicate autocomplete parameters between threads via the history | 88 // Used to communicate autocomplete parameters between threads via the history |
89 // service. | 89 // service. |
90 struct HistoryURLProviderParams { | 90 struct HistoryURLProviderParams { |
91 enum PromoteType { | |
92 WHAT_YOU_TYPED_MATCH, | |
93 FRONT_HISTORY_MATCH, | |
94 NEITHER, | |
95 }; | |
96 | |
91 HistoryURLProviderParams(const AutocompleteInput& input, | 97 HistoryURLProviderParams(const AutocompleteInput& input, |
92 bool trim_http, | 98 bool trim_http, |
93 const AutocompleteMatch& what_you_typed_match, | 99 const AutocompleteMatch& what_you_typed_match, |
94 const std::string& languages, | 100 const std::string& languages, |
95 TemplateURL* default_search_provider, | 101 TemplateURL* default_search_provider, |
96 const SearchTermsData& search_terms_data); | 102 const SearchTermsData& search_terms_data); |
97 ~HistoryURLProviderParams(); | 103 ~HistoryURLProviderParams(); |
98 | 104 |
99 base::MessageLoop* message_loop; | 105 base::MessageLoop* message_loop; |
100 | 106 |
(...skipping 17 matching lines...) Expand all Loading... | |
118 // running queries that are no longer needed. Since we don't care if we run | 124 // running queries that are no longer needed. Since we don't care if we run |
119 // the extra queries, the lack of signaling is not a problem. | 125 // the extra queries, the lack of signaling is not a problem. |
120 base::CancellationFlag cancel_flag; | 126 base::CancellationFlag cancel_flag; |
121 | 127 |
122 // Set by ExecuteWithDB() on the history thread when the query could not be | 128 // Set by ExecuteWithDB() on the history thread when the query could not be |
123 // performed because the history system failed to properly init the database. | 129 // performed because the history system failed to properly init the database. |
124 // If this is set when the main thread is called back, it avoids changing | 130 // If this is set when the main thread is called back, it avoids changing |
125 // |matches_| at all, so it won't delete the default match Start() creates. | 131 // |matches_| at all, so it won't delete the default match Start() creates. |
126 bool failed; | 132 bool failed; |
127 | 133 |
128 // List of matches written by the history thread. We keep this separate list | 134 // List of matches written by DoAutocomplete(). Upon its return the provider |
129 // to avoid having the main thread read the provider's matches while the | 135 // converts this list to ACMatches and places them in |matches_|. |
130 // history thread is manipulating them. The provider copies this list back | 136 history::HistoryMatches matches; |
131 // to matches_ on the main thread in QueryComplete(). | 137 |
132 ACMatches matches; | 138 // True if the suggestion for exactly what the user typed appears as a known |
139 // URL in the user's history. In this case, this will also be the first match | |
140 // in |matches|. | |
Mark P
2014/06/19 03:53:14
nit: You might want to mention that there are some
Peter Kasting
2014/06/19 20:55:23
Done.
| |
141 bool exact_suggestion_is_in_history; | |
142 | |
143 // Tells the provider whether to promote the what you typed match, the first | |
144 // element of |matches|, or neither as the first AutocompleteMatch. If | |
145 // |exact_suggestion_is_in_history| is true (and thus "the what you typed | |
146 // match" and "the first element of |matches|" represent the same thing), this | |
147 // will be set to WHAT_YOU_TYPED_MATCH. | |
148 PromoteType promote_type; | |
133 | 149 |
134 // Languages we should pass to gfx::GetCleanStringFromUrl. | 150 // Languages we should pass to gfx::GetCleanStringFromUrl. |
135 std::string languages; | 151 std::string languages; |
136 | 152 |
137 // When true, we should avoid calling SuggestExactInput(). | |
138 bool dont_suggest_exact_input; | |
139 | |
140 // The default search provider and search terms data necessary to cull results | 153 // The default search provider and search terms data necessary to cull results |
141 // that correspond to searches (on the default engine). These can only be | 154 // that correspond to searches (on the default engine). These can only be |
142 // obtained on the UI thread, so we have to copy them into here to pass them | 155 // obtained on the UI thread, so we have to copy them into here to pass them |
143 // to the history thread. We use a scoped_ptr<TemplateURL> for the DSP since | 156 // to the history thread. We use a scoped_ptr<TemplateURL> for the DSP since |
144 // TemplateURLs can't be copied by value. We use a scoped_ptr<SearchTermsData> | 157 // TemplateURLs can't be copied by value. We use a scoped_ptr<SearchTermsData> |
145 // so that we can store a snapshot of the SearchTermsData accessible from the | 158 // so that we can store a snapshot of the SearchTermsData accessible from the |
146 // history thread. | 159 // history thread. |
147 scoped_ptr<TemplateURL> default_search_provider; | 160 scoped_ptr<TemplateURL> default_search_provider; |
148 scoped_ptr<SearchTermsData> search_terms_data; | 161 scoped_ptr<SearchTermsData> search_terms_data; |
149 | 162 |
(...skipping 16 matching lines...) Expand all Loading... | |
166 | 179 |
167 // HistoryProvider: | 180 // HistoryProvider: |
168 virtual void Start(const AutocompleteInput& input, | 181 virtual void Start(const AutocompleteInput& input, |
169 bool minimal_changes) OVERRIDE; | 182 bool minimal_changes) OVERRIDE; |
170 virtual void Stop(bool clear_cached_results) OVERRIDE; | 183 virtual void Stop(bool clear_cached_results) OVERRIDE; |
171 | 184 |
172 // Returns a match representing a navigation to |destination_url| given user | 185 // Returns a match representing a navigation to |destination_url| given user |
173 // input of |text|. |trim_http| controls whether the match's |fill_into_edit| | 186 // input of |text|. |trim_http| controls whether the match's |fill_into_edit| |
174 // and |contents| should have any HTTP scheme stripped off, and should not be | 187 // and |contents| should have any HTTP scheme stripped off, and should not be |
175 // set to true if |text| contains an http prefix. | 188 // set to true if |text| contains an http prefix. |
176 // NOTE: This does not set the relevance of the returned match, as different | 189 // NOTES: This does not set the relevance of the returned match, as different |
177 // callers want different behavior. Callers must set this manually. | 190 // callers want different behavior. Callers must set this manually. |
191 // This function should only be called on the UI thread. | |
178 AutocompleteMatch SuggestExactInput(const base::string16& text, | 192 AutocompleteMatch SuggestExactInput(const base::string16& text, |
179 const GURL& destination_url, | 193 const GURL& destination_url, |
180 bool trim_http); | 194 bool trim_http); |
181 | 195 |
182 // Runs the history query on the history thread, called by the history | 196 // Runs the history query on the history thread, called by the history |
183 // system. The history database MAY BE NULL in which case it is not | 197 // system. The history database MAY BE NULL in which case it is not |
184 // available and we should return no data. Also schedules returning the | 198 // available and we should return no data. Also schedules returning the |
185 // results to the main thread | 199 // results to the main thread |
186 void ExecuteWithDB(history::HistoryBackend* backend, | 200 void ExecuteWithDB(history::HistoryBackend* backend, |
187 history::URLDatabase* db, | 201 history::URLDatabase* db, |
(...skipping 25 matching lines...) Expand all Loading... | |
213 const base::string16& input_text, | 227 const base::string16& input_text, |
214 const base::string16& description); | 228 const base::string16& description); |
215 | 229 |
216 // Actually runs the autocomplete job on the given database, which is | 230 // Actually runs the autocomplete job on the given database, which is |
217 // guaranteed not to be NULL. Used by both autocomplete passes, and therefore | 231 // guaranteed not to be NULL. Used by both autocomplete passes, and therefore |
218 // called on multiple different threads (though not simultaneously). | 232 // called on multiple different threads (though not simultaneously). |
219 void DoAutocomplete(history::HistoryBackend* backend, | 233 void DoAutocomplete(history::HistoryBackend* backend, |
220 history::URLDatabase* db, | 234 history::URLDatabase* db, |
221 HistoryURLProviderParams* params); | 235 HistoryURLProviderParams* params); |
222 | 236 |
237 // May promote either the what you typed match or first history match in | |
238 // |params| to the front of |matches_|, depending on the value of | |
Mark P
2014/06/19 03:53:15
nit for clarity: |params| -> |params->matches|
(to
Peter Kasting
2014/06/19 20:55:23
Done.
| |
239 // params->promote_type. | |
240 void PromoteMatchIfNecessary(const HistoryURLProviderParams& params); | |
241 | |
223 // Dispatches the results to the autocomplete controller. Called on the | 242 // Dispatches the results to the autocomplete controller. Called on the |
224 // main thread by ExecuteWithDB when the results are available. | 243 // main thread by ExecuteWithDB when the results are available. |
225 // Frees params_gets_deleted on exit. | 244 // Frees params_gets_deleted on exit. |
226 void QueryComplete(HistoryURLProviderParams* params_gets_deleted); | 245 void QueryComplete(HistoryURLProviderParams* params_gets_deleted); |
227 | 246 |
228 // Looks up the info for params->what_you_typed_match in the DB. If found, | 247 // Looks up the info for params->what_you_typed_match in the DB. If found, |
229 // fills in the title, promotes the match's priority to that of an inline | 248 // fills in the title, promotes the match's priority to that of an inline |
230 // autocomplete match (maybe it should be slightly better?), and places it on | 249 // autocomplete match (maybe it should be slightly better?), and places it on |
231 // the front of |matches| (so we pick the right matches to throw away when | 250 // the front of params->matches (so we pick the right matches to throw away |
232 // culling redirects to/from it). Returns whether a match was promoted. | 251 // when culling redirects to/from it). Returns whether a match was promoted. |
233 bool FixupExactSuggestion(history::URLDatabase* db, | 252 bool FixupExactSuggestion(history::URLDatabase* db, |
234 const VisitClassifier& classifier, | 253 const VisitClassifier& classifier, |
235 HistoryURLProviderParams* params, | 254 HistoryURLProviderParams* params) const; |
236 history::HistoryMatches* matches) const; | |
237 | 255 |
238 // Helper function for FixupExactSuggestion, this returns true if the input | 256 // Helper function for FixupExactSuggestion, this returns true if the input |
239 // corresponds to some intranet URL where the user has previously visited the | 257 // corresponds to some intranet URL where the user has previously visited the |
240 // host in question. In this case the input should be treated as a URL. | 258 // host in question. In this case the input should be treated as a URL. |
241 bool CanFindIntranetURL(history::URLDatabase* db, | 259 bool CanFindIntranetURL(history::URLDatabase* db, |
242 const AutocompleteInput& input) const; | 260 const AutocompleteInput& input) const; |
243 | 261 |
244 // Determines if |match| is suitable for inline autocomplete. If so, promotes | |
245 // the match. Returns whether |match| was promoted. | |
246 bool PromoteMatchForInlineAutocomplete(const history::HistoryMatch& match, | |
247 HistoryURLProviderParams* params); | |
248 | |
249 // Sees if a shorter version of the best match should be created, and if so | 262 // Sees if a shorter version of the best match should be created, and if so |
250 // places it at the front of |matches|. This can suggest history URLs that | 263 // places it at the front of params->matches. This can suggest history URLs |
251 // are prefixes of the best match (if they've been visited enough, compared to | 264 // that are prefixes of the best match (if they've been visited enough, |
252 // the best match), or create host-only suggestions even when they haven't | 265 // compared to the best match), or create host-only suggestions even when they |
253 // been visited before: if the user visited http://example.com/asdf once, | 266 // haven't been visited before: if the user visited http://example.com/asdf |
254 // we'll suggest http://example.com/ even if they've never been to it. | 267 // once, we'll suggest http://example.com/ even if they've never been to it. |
255 void PromoteOrCreateShorterSuggestion( | 268 void PromoteOrCreateShorterSuggestion( |
256 history::URLDatabase* db, | 269 history::URLDatabase* db, |
257 const HistoryURLProviderParams& params, | |
258 bool have_what_you_typed_match, | 270 bool have_what_you_typed_match, |
259 history::HistoryMatches* matches); | 271 HistoryURLProviderParams* params); |
260 | 272 |
261 // Removes results that have been rarely typed or visited, and not any time | 273 // Removes results that have been rarely typed or visited, and not any time |
262 // recently. The exact parameters for this heuristic can be found in the | 274 // recently. The exact parameters for this heuristic can be found in the |
263 // function body. Also culls results corresponding to queries from the default | 275 // function body. Also culls results corresponding to queries from the default |
264 // search engine. These are low-quality, difficult-to-understand matches for | 276 // search engine. These are low-quality, difficult-to-understand matches for |
265 // users, and the SearchProvider should surface past queries in a better way | 277 // users, and the SearchProvider should surface past queries in a better way |
266 // anyway. | 278 // anyway. |
267 void CullPoorMatches(const HistoryURLProviderParams& params, | 279 void CullPoorMatches(HistoryURLProviderParams* params) const; |
268 history::HistoryMatches* matches) const; | |
269 | 280 |
270 // Removes results that redirect to each other, leaving at most |max_results| | 281 // Removes results that redirect to each other, leaving at most |max_results| |
271 // results. | 282 // results. |
272 void CullRedirects(history::HistoryBackend* backend, | 283 void CullRedirects(history::HistoryBackend* backend, |
273 history::HistoryMatches* matches, | 284 history::HistoryMatches* matches, |
274 size_t max_results) const; | 285 size_t max_results) const; |
275 | 286 |
276 // Helper function for CullRedirects, this removes all but the first | 287 // Helper function for CullRedirects, this removes all but the first |
277 // occurance of [any of the set of strings in |remove|] from the |matches| | 288 // occurance of [any of the set of strings in |remove|] from the |matches| |
278 // list. | 289 // list. |
279 // | 290 // |
280 // The return value is the index of the item that is after the item in the | 291 // The return value is the index of the item that is after the item in the |
281 // input identified by |source_index|. If |source_index| or an item before | 292 // input identified by |source_index|. If |source_index| or an item before |
282 // is removed, the next item will be shifted, and this allows the caller to | 293 // is removed, the next item will be shifted, and this allows the caller to |
283 // pick up on the next one when this happens. | 294 // pick up on the next one when this happens. |
284 size_t RemoveSubsequentMatchesOf(history::HistoryMatches* matches, | 295 size_t RemoveSubsequentMatchesOf(history::HistoryMatches* matches, |
285 size_t source_index, | 296 size_t source_index, |
286 const std::vector<GURL>& remove) const; | 297 const std::vector<GURL>& remove) const; |
287 | 298 |
288 // Converts a line from the database into an autocomplete match for display. | 299 // Converts a specified |match_number| from params.matches into an |
289 // If experimental scoring is enabled, the final relevance score might be | 300 // autocomplete match for display. If experimental scoring is enabled, the |
290 // different from the given |relevance|. | 301 // final relevance score might be different from the given |relevance|. |
302 // NOTE: This function should only be called on the UI thread. | |
291 AutocompleteMatch HistoryMatchToACMatch( | 303 AutocompleteMatch HistoryMatchToACMatch( |
292 const HistoryURLProviderParams& params, | 304 const HistoryURLProviderParams& params, |
293 const history::HistoryMatch& history_match, | 305 size_t match_number, |
294 MatchType match_type, | 306 MatchType match_type, |
295 int relevance); | 307 int relevance); |
296 | 308 |
297 // Params for the current query. The provider should not free this directly; | 309 // Params for the current query. The provider should not free this directly; |
298 // instead, it is passed as a parameter through the history backend, and the | 310 // instead, it is passed as a parameter through the history backend, and the |
299 // parameter itself is freed once it's no longer needed. The only reason we | 311 // parameter itself is freed once it's no longer needed. The only reason we |
300 // keep this member is so we can set the cancel bit on it. | 312 // keep this member is so we can set the cancel bit on it. |
301 HistoryURLProviderParams* params_; | 313 HistoryURLProviderParams* params_; |
302 | 314 |
303 // Params controlling experimental behavior of this provider. | 315 // Params controlling experimental behavior of this provider. |
304 HUPScoringParams scoring_params_; | 316 HUPScoringParams scoring_params_; |
305 | 317 |
306 // If true, HistoryURL provider should lookup and cull redirects. If | 318 // If true, HistoryURL provider should lookup and cull redirects. If |
307 // false, it returns matches that may be redirects to each other and | 319 // false, it returns matches that may be redirects to each other and |
308 // simply hopes the default AutoCompleteController behavior to remove | 320 // simply hopes the default AutoCompleteController behavior to remove |
309 // URLs that are likely duplicates (http://google.com <-> | 321 // URLs that are likely duplicates (http://google.com <-> |
310 // https://www.google.com/, etc.) will do a good enough job. | 322 // https://www.google.com/, etc.) will do a good enough job. |
311 bool cull_redirects_; | 323 bool cull_redirects_; |
312 | 324 |
313 // Used in PromoteOrCreateShorterSuggestion(). If true, we may create | 325 // Used in PromoteOrCreateShorterSuggestion(). If true, we may create |
314 // shorter suggestions even when they haven't been visited before: | 326 // shorter suggestions even when they haven't been visited before: |
315 // if the user visited http://example.com/asdf once, we'll suggest | 327 // if the user visited http://example.com/asdf once, we'll suggest |
316 // http://example.com/ even if they've never been to it. | 328 // http://example.com/ even if they've never been to it. |
317 bool create_shorter_match_; | 329 bool create_shorter_match_; |
318 | 330 |
319 DISALLOW_COPY_AND_ASSIGN(HistoryURLProvider); | 331 DISALLOW_COPY_AND_ASSIGN(HistoryURLProvider); |
320 }; | 332 }; |
321 | 333 |
322 #endif // CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_ | 334 #endif // CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_ |
OLD | NEW |