| 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 // See comments on |promote_type| below. |
| 92 enum PromoteType { |
| 93 WHAT_YOU_TYPED_MATCH, |
| 94 FRONT_HISTORY_MATCH, |
| 95 NEITHER, |
| 96 }; |
| 97 |
| 91 HistoryURLProviderParams(const AutocompleteInput& input, | 98 HistoryURLProviderParams(const AutocompleteInput& input, |
| 92 bool trim_http, | 99 bool trim_http, |
| 93 const AutocompleteMatch& what_you_typed_match, | 100 const AutocompleteMatch& what_you_typed_match, |
| 94 const std::string& languages, | 101 const std::string& languages, |
| 95 TemplateURL* default_search_provider, | 102 TemplateURL* default_search_provider, |
| 96 const SearchTermsData& search_terms_data); | 103 const SearchTermsData& search_terms_data); |
| 97 ~HistoryURLProviderParams(); | 104 ~HistoryURLProviderParams(); |
| 98 | 105 |
| 99 base::MessageLoop* message_loop; | 106 base::MessageLoop* message_loop; |
| 100 | 107 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 118 // running queries that are no longer needed. Since we don't care if we run | 125 // 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. | 126 // the extra queries, the lack of signaling is not a problem. |
| 120 base::CancellationFlag cancel_flag; | 127 base::CancellationFlag cancel_flag; |
| 121 | 128 |
| 122 // Set by ExecuteWithDB() on the history thread when the query could not be | 129 // 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. | 130 // 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 | 131 // 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. | 132 // |matches_| at all, so it won't delete the default match Start() creates. |
| 126 bool failed; | 133 bool failed; |
| 127 | 134 |
| 128 // List of matches written by the history thread. We keep this separate list | 135 // 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 | 136 // converts this list to ACMatches and places them in |matches_|. |
| 130 // history thread is manipulating them. The provider copies this list back | 137 history::HistoryMatches matches; |
| 131 // to matches_ on the main thread in QueryComplete(). | 138 |
| 132 ACMatches matches; | 139 // True if the suggestion for exactly what the user typed appears as a known |
| 140 // URL in the user's history. In this case, this will also be the first match |
| 141 // in |matches|. |
| 142 // |
| 143 // NOTE: There are some complications related to keeping things consistent |
| 144 // between passes and how we deal with intranet URLs, which are too complex to |
| 145 // explain here; see the implementations of DoAutocomplete() and |
| 146 // FixupExactSuggestion() for specific comments. |
| 147 bool exact_suggestion_is_in_history; |
| 148 |
| 149 // Tells the provider whether to promote the what you typed match, the first |
| 150 // element of |matches|, or neither as the first AutocompleteMatch. If |
| 151 // |exact_suggestion_is_in_history| is true (and thus "the what you typed |
| 152 // match" and "the first element of |matches|" represent the same thing), this |
| 153 // will be set to WHAT_YOU_TYPED_MATCH. |
| 154 // |
| 155 // NOTE: The second pass of DoAutocomplete() checks what the first pass set |
| 156 // this to. See comments in DoAutocomplete(). |
| 157 PromoteType promote_type; |
| 133 | 158 |
| 134 // Languages we should pass to gfx::GetCleanStringFromUrl. | 159 // Languages we should pass to gfx::GetCleanStringFromUrl. |
| 135 std::string languages; | 160 std::string languages; |
| 136 | 161 |
| 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 | 162 // 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 | 163 // 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 | 164 // 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 | 165 // 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> | 166 // 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 | 167 // so that we can store a snapshot of the SearchTermsData accessible from the |
| 146 // history thread. | 168 // history thread. |
| 147 scoped_ptr<TemplateURL> default_search_provider; | 169 scoped_ptr<TemplateURL> default_search_provider; |
| 148 scoped_ptr<SearchTermsData> search_terms_data; | 170 scoped_ptr<SearchTermsData> search_terms_data; |
| 149 | 171 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 166 | 188 |
| 167 // HistoryProvider: | 189 // HistoryProvider: |
| 168 virtual void Start(const AutocompleteInput& input, | 190 virtual void Start(const AutocompleteInput& input, |
| 169 bool minimal_changes) OVERRIDE; | 191 bool minimal_changes) OVERRIDE; |
| 170 virtual void Stop(bool clear_cached_results) OVERRIDE; | 192 virtual void Stop(bool clear_cached_results) OVERRIDE; |
| 171 | 193 |
| 172 // Returns a match representing a navigation to |destination_url| given user | 194 // 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| | 195 // 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 | 196 // and |contents| should have any HTTP scheme stripped off, and should not be |
| 175 // set to true if |text| contains an http prefix. | 197 // set to true if |text| contains an http prefix. |
| 176 // NOTE: This does not set the relevance of the returned match, as different | 198 // NOTES: This does not set the relevance of the returned match, as different |
| 177 // callers want different behavior. Callers must set this manually. | 199 // callers want different behavior. Callers must set this manually. |
| 200 // This function should only be called on the UI thread. |
| 178 AutocompleteMatch SuggestExactInput(const base::string16& text, | 201 AutocompleteMatch SuggestExactInput(const base::string16& text, |
| 179 const GURL& destination_url, | 202 const GURL& destination_url, |
| 180 bool trim_http); | 203 bool trim_http); |
| 181 | 204 |
| 182 // Runs the history query on the history thread, called by the history | 205 // 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 | 206 // 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 | 207 // available and we should return no data. Also schedules returning the |
| 185 // results to the main thread | 208 // results to the main thread |
| 186 void ExecuteWithDB(history::HistoryBackend* backend, | 209 void ExecuteWithDB(history::HistoryBackend* backend, |
| 187 history::URLDatabase* db, | 210 history::URLDatabase* db, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 213 const base::string16& input_text, | 236 const base::string16& input_text, |
| 214 const base::string16& description); | 237 const base::string16& description); |
| 215 | 238 |
| 216 // Actually runs the autocomplete job on the given database, which is | 239 // Actually runs the autocomplete job on the given database, which is |
| 217 // guaranteed not to be NULL. Used by both autocomplete passes, and therefore | 240 // guaranteed not to be NULL. Used by both autocomplete passes, and therefore |
| 218 // called on multiple different threads (though not simultaneously). | 241 // called on multiple different threads (though not simultaneously). |
| 219 void DoAutocomplete(history::HistoryBackend* backend, | 242 void DoAutocomplete(history::HistoryBackend* backend, |
| 220 history::URLDatabase* db, | 243 history::URLDatabase* db, |
| 221 HistoryURLProviderParams* params); | 244 HistoryURLProviderParams* params); |
| 222 | 245 |
| 246 // May promote either the what you typed match or first history match in |
| 247 // params->matches to the front of |matches_|, depending on the value of |
| 248 // params->promote_type. |
| 249 void PromoteMatchIfNecessary(const HistoryURLProviderParams& params); |
| 250 |
| 223 // Dispatches the results to the autocomplete controller. Called on the | 251 // Dispatches the results to the autocomplete controller. Called on the |
| 224 // main thread by ExecuteWithDB when the results are available. | 252 // main thread by ExecuteWithDB when the results are available. |
| 225 // Frees params_gets_deleted on exit. | 253 // Frees params_gets_deleted on exit. |
| 226 void QueryComplete(HistoryURLProviderParams* params_gets_deleted); | 254 void QueryComplete(HistoryURLProviderParams* params_gets_deleted); |
| 227 | 255 |
| 228 // Looks up the info for params->what_you_typed_match in the DB. If found, | 256 // 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 | 257 // 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 | 258 // 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 | 259 // 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. | 260 // when culling redirects to/from it). Returns whether a match was promoted. |
| 233 bool FixupExactSuggestion(history::URLDatabase* db, | 261 bool FixupExactSuggestion(history::URLDatabase* db, |
| 234 const VisitClassifier& classifier, | 262 const VisitClassifier& classifier, |
| 235 HistoryURLProviderParams* params, | 263 HistoryURLProviderParams* params) const; |
| 236 history::HistoryMatches* matches) const; | |
| 237 | 264 |
| 238 // Helper function for FixupExactSuggestion, this returns true if the input | 265 // Helper function for FixupExactSuggestion, this returns true if the input |
| 239 // corresponds to some intranet URL where the user has previously visited the | 266 // 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. | 267 // host in question. In this case the input should be treated as a URL. |
| 241 bool CanFindIntranetURL(history::URLDatabase* db, | 268 bool CanFindIntranetURL(history::URLDatabase* db, |
| 242 const AutocompleteInput& input) const; | 269 const AutocompleteInput& input) const; |
| 243 | 270 |
| 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 | 271 // 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 | 272 // 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 | 273 // 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 | 274 // 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, | 275 // 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. | 276 // once, we'll suggest http://example.com/ even if they've never been to it. |
| 255 void PromoteOrCreateShorterSuggestion( | 277 // Returns true if a match was successfully created/promoted that we're |
| 278 // willing to inline autocomplete. |
| 279 bool PromoteOrCreateShorterSuggestion( |
| 256 history::URLDatabase* db, | 280 history::URLDatabase* db, |
| 257 const HistoryURLProviderParams& params, | |
| 258 bool have_what_you_typed_match, | 281 bool have_what_you_typed_match, |
| 259 history::HistoryMatches* matches); | 282 HistoryURLProviderParams* params); |
| 260 | 283 |
| 261 // Removes results that have been rarely typed or visited, and not any time | 284 // 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 | 285 // recently. The exact parameters for this heuristic can be found in the |
| 263 // function body. Also culls results corresponding to queries from the default | 286 // function body. Also culls results corresponding to queries from the default |
| 264 // search engine. These are low-quality, difficult-to-understand matches for | 287 // search engine. These are low-quality, difficult-to-understand matches for |
| 265 // users, and the SearchProvider should surface past queries in a better way | 288 // users, and the SearchProvider should surface past queries in a better way |
| 266 // anyway. | 289 // anyway. |
| 267 void CullPoorMatches(const HistoryURLProviderParams& params, | 290 void CullPoorMatches(HistoryURLProviderParams* params) const; |
| 268 history::HistoryMatches* matches) const; | |
| 269 | 291 |
| 270 // Removes results that redirect to each other, leaving at most |max_results| | 292 // Removes results that redirect to each other, leaving at most |max_results| |
| 271 // results. | 293 // results. |
| 272 void CullRedirects(history::HistoryBackend* backend, | 294 void CullRedirects(history::HistoryBackend* backend, |
| 273 history::HistoryMatches* matches, | 295 history::HistoryMatches* matches, |
| 274 size_t max_results) const; | 296 size_t max_results) const; |
| 275 | 297 |
| 276 // Helper function for CullRedirects, this removes all but the first | 298 // Helper function for CullRedirects, this removes all but the first |
| 277 // occurance of [any of the set of strings in |remove|] from the |matches| | 299 // occurance of [any of the set of strings in |remove|] from the |matches| |
| 278 // list. | 300 // list. |
| 279 // | 301 // |
| 280 // The return value is the index of the item that is after the item in the | 302 // 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 | 303 // 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 | 304 // is removed, the next item will be shifted, and this allows the caller to |
| 283 // pick up on the next one when this happens. | 305 // pick up on the next one when this happens. |
| 284 size_t RemoveSubsequentMatchesOf(history::HistoryMatches* matches, | 306 size_t RemoveSubsequentMatchesOf(history::HistoryMatches* matches, |
| 285 size_t source_index, | 307 size_t source_index, |
| 286 const std::vector<GURL>& remove) const; | 308 const std::vector<GURL>& remove) const; |
| 287 | 309 |
| 288 // Converts a line from the database into an autocomplete match for display. | 310 // Converts a specified |match_number| from params.matches into an |
| 289 // If experimental scoring is enabled, the final relevance score might be | 311 // autocomplete match for display. If experimental scoring is enabled, the |
| 290 // different from the given |relevance|. | 312 // final relevance score might be different from the given |relevance|. |
| 313 // NOTE: This function should only be called on the UI thread. |
| 291 AutocompleteMatch HistoryMatchToACMatch( | 314 AutocompleteMatch HistoryMatchToACMatch( |
| 292 const HistoryURLProviderParams& params, | 315 const HistoryURLProviderParams& params, |
| 293 const history::HistoryMatch& history_match, | 316 size_t match_number, |
| 294 MatchType match_type, | 317 MatchType match_type, |
| 295 int relevance); | 318 int relevance); |
| 296 | 319 |
| 297 // Params for the current query. The provider should not free this directly; | 320 // 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 | 321 // 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 | 322 // 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. | 323 // keep this member is so we can set the cancel bit on it. |
| 301 HistoryURLProviderParams* params_; | 324 HistoryURLProviderParams* params_; |
| 302 | 325 |
| 303 // Params controlling experimental behavior of this provider. | 326 // Params controlling experimental behavior of this provider. |
| 304 HUPScoringParams scoring_params_; | 327 HUPScoringParams scoring_params_; |
| 305 | 328 |
| 306 // If true, HistoryURL provider should lookup and cull redirects. If | 329 // If true, HistoryURL provider should lookup and cull redirects. If |
| 307 // false, it returns matches that may be redirects to each other and | 330 // false, it returns matches that may be redirects to each other and |
| 308 // simply hopes the default AutoCompleteController behavior to remove | 331 // simply hopes the default AutoCompleteController behavior to remove |
| 309 // URLs that are likely duplicates (http://google.com <-> | 332 // URLs that are likely duplicates (http://google.com <-> |
| 310 // https://www.google.com/, etc.) will do a good enough job. | 333 // https://www.google.com/, etc.) will do a good enough job. |
| 311 bool cull_redirects_; | 334 bool cull_redirects_; |
| 312 | 335 |
| 313 // Used in PromoteOrCreateShorterSuggestion(). If true, we may create | 336 // Used in PromoteOrCreateShorterSuggestion(). If true, we may create |
| 314 // shorter suggestions even when they haven't been visited before: | 337 // shorter suggestions even when they haven't been visited before: |
| 315 // if the user visited http://example.com/asdf once, we'll suggest | 338 // if the user visited http://example.com/asdf once, we'll suggest |
| 316 // http://example.com/ even if they've never been to it. | 339 // http://example.com/ even if they've never been to it. |
| 317 bool create_shorter_match_; | 340 bool create_shorter_match_; |
| 318 | 341 |
| 319 DISALLOW_COPY_AND_ASSIGN(HistoryURLProvider); | 342 DISALLOW_COPY_AND_ASSIGN(HistoryURLProvider); |
| 320 }; | 343 }; |
| 321 | 344 |
| 322 #endif // CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_ | 345 #endif // CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_ |
| OLD | NEW |