| 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 Search autocomplete provider. This provider is | 5 // This file contains the Search autocomplete provider. This provider is |
| 6 // responsible for all autocomplete entries that start with "Search <engine> | 6 // responsible for all autocomplete entries that start with "Search <engine> |
| 7 // for ...", including searching for the current input string, search | 7 // for ...", including searching for the current input string, search |
| 8 // history, and search suggestions. An instance of it gets created and | 8 // 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 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 11 #ifndef CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
| 12 #define CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 12 #define CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
| 13 | 13 |
| 14 #include <map> | |
| 15 #include <string> | 14 #include <string> |
| 16 #include <vector> | 15 #include <vector> |
| 17 | 16 |
| 18 #include "base/basictypes.h" | |
| 19 #include "base/compiler_specific.h" | 17 #include "base/compiler_specific.h" |
| 20 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/memory/scoped_vector.h" | |
| 22 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 23 #include "base/timer/timer.h" | 20 #include "base/timer/timer.h" |
| 24 #include "chrome/browser/autocomplete/autocomplete_input.h" | 21 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 25 #include "chrome/browser/autocomplete/autocomplete_match.h" | 22 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 26 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 23 #include "chrome/browser/autocomplete/base_search_provider.h" |
| 27 #include "chrome/browser/history/history_types.h" | 24 #include "chrome/browser/history/history_types.h" |
| 28 #include "chrome/browser/search_engines/template_url.h" | 25 #include "chrome/browser/search_engines/template_url.h" |
| 29 #include "net/url_request/url_fetcher_delegate.h" | |
| 30 | 26 |
| 31 class Profile; | 27 class Profile; |
| 32 class SearchProviderTest; | 28 class SearchProviderTest; |
| 33 class SuggestionDeletionHandler; | |
| 34 class TemplateURLService; | 29 class TemplateURLService; |
| 35 | 30 |
| 36 namespace base { | 31 namespace base { |
| 37 class Value; | 32 class Value; |
| 38 } | 33 } |
| 39 | 34 |
| 40 namespace net { | 35 namespace net { |
| 41 class URLFetcher; | 36 class URLFetcher; |
| 42 } | 37 } |
| 43 | 38 |
| 44 // Autocomplete provider for searches and suggestions from a search engine. | 39 // Autocomplete provider for searches and suggestions from a search engine. |
| 45 // | 40 // |
| 46 // After construction, the autocomplete controller repeatedly calls Start() | 41 // After construction, the autocomplete controller repeatedly calls Start() |
| 47 // with some user input, each time expecting to receive a small set of the best | 42 // with some user input, each time expecting to receive a small set of the best |
| 48 // matches (either synchronously or asynchronously). | 43 // matches (either synchronously or asynchronously). |
| 49 // | 44 // |
| 50 // Initially the provider creates a match that searches for the current input | 45 // Initially the provider creates a match that searches for the current input |
| 51 // text. It also starts a task to query the Suggest servers. When that data | 46 // text. It also starts a task to query the Suggest servers. When that data |
| 52 // comes back, the provider creates and returns matches for the best | 47 // comes back, the provider creates and returns matches for the best |
| 53 // suggestions. | 48 // suggestions. |
| 54 class SearchProvider : public AutocompleteProvider, | 49 class SearchProvider : public BaseSearchProvider { |
| 55 public net::URLFetcherDelegate { | |
| 56 public: | 50 public: |
| 57 // ID used in creating URLFetcher for default provider's suggest results. | 51 // ID used in creating URLFetcher for default provider's suggest results. |
| 58 static const int kDefaultProviderURLFetcherID; | 52 static const int kDefaultProviderURLFetcherID; |
| 59 | 53 |
| 60 // ID used in creating URLFetcher for keyword provider's suggest results. | 54 // ID used in creating URLFetcher for keyword provider's suggest results. |
| 61 static const int kKeywordProviderURLFetcherID; | 55 static const int kKeywordProviderURLFetcherID; |
| 62 | 56 |
| 63 // ID used in creating URLFetcher for deleting suggestion results. | |
| 64 static const int kDeletionURLFetcherID; | |
| 65 | |
| 66 SearchProvider(AutocompleteProviderListener* listener, Profile* profile); | 57 SearchProvider(AutocompleteProviderListener* listener, Profile* profile); |
| 67 | 58 |
| 68 // Returns whether the SearchProvider previously flagged |match| as a query | |
| 69 // that should be prefetched. | |
| 70 static bool ShouldPrefetch(const AutocompleteMatch& match); | |
| 71 | |
| 72 // Extracts the suggest response metadata which SearchProvider previously | 59 // Extracts the suggest response metadata which SearchProvider previously |
| 73 // stored for |match|. | 60 // stored for |match|. |
| 74 static std::string GetSuggestMetadata(const AutocompleteMatch& match); | 61 static std::string GetSuggestMetadata(const AutocompleteMatch& match); |
| 75 | 62 |
| 76 // AutocompleteProvider: | 63 // AutocompleteProvider: |
| 77 virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE; | |
| 78 virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE; | |
| 79 virtual void ResetSession() OVERRIDE; | 64 virtual void ResetSession() OVERRIDE; |
| 80 | 65 |
| 81 bool field_trial_triggered_in_session() const { | |
| 82 return field_trial_triggered_in_session_; | |
| 83 } | |
| 84 | |
| 85 // This URL may be sent with suggest requests; see comments on CanSendURL(). | 66 // This URL may be sent with suggest requests; see comments on CanSendURL(). |
| 86 void set_current_page_url(const GURL& current_page_url) { | 67 void set_current_page_url(const GURL& current_page_url) { |
| 87 current_page_url_ = current_page_url; | 68 current_page_url_ = current_page_url; |
| 88 } | 69 } |
| 89 | 70 |
| 90 protected: | 71 protected: |
| 91 virtual ~SearchProvider(); | 72 virtual ~SearchProvider(); |
| 92 | 73 |
| 93 private: | 74 private: |
| 94 // TODO(hfung): Remove ZeroSuggestProvider as a friend class after | |
| 95 // refactoring common code to a new base class. | |
| 96 friend class SearchProviderTest; | 75 friend class SearchProviderTest; |
| 97 friend class ZeroSuggestProvider; | |
| 98 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, CanSendURL); | 76 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, CanSendURL); |
| 99 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInline); | 77 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInline); |
| 100 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInlineDomainClassify); | 78 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInlineDomainClassify); |
| 101 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInlineSchemeSubstring); | 79 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInlineSchemeSubstring); |
| 102 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, RemoveStaleResultsTest); | 80 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, RemoveStaleResultsTest); |
| 103 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, SuggestRelevanceExperiment); | 81 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, SuggestRelevanceExperiment); |
| 104 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, TestDeleteMatch); | 82 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, TestDeleteMatch); |
| 105 FRIEND_TEST_ALL_PREFIXES(AutocompleteProviderTest, GetDestinationURL); | 83 FRIEND_TEST_ALL_PREFIXES(AutocompleteProviderTest, GetDestinationURL); |
| 106 FRIEND_TEST_ALL_PREFIXES(InstantExtendedPrefetchTest, ClearPrefetchedResults); | 84 FRIEND_TEST_ALL_PREFIXES(InstantExtendedPrefetchTest, ClearPrefetchedResults); |
| 107 FRIEND_TEST_ALL_PREFIXES(InstantExtendedPrefetchTest, SetPrefetchQuery); | 85 FRIEND_TEST_ALL_PREFIXES(InstantExtendedPrefetchTest, SetPrefetchQuery); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 TemplateURLService* template_url_service_; | 124 TemplateURLService* template_url_service_; |
| 147 | 125 |
| 148 // Cached across the life of a query so we behave consistently even if the | 126 // Cached across the life of a query so we behave consistently even if the |
| 149 // user changes their default while the query is running. | 127 // user changes their default while the query is running. |
| 150 base::string16 default_provider_; | 128 base::string16 default_provider_; |
| 151 base::string16 keyword_provider_; | 129 base::string16 keyword_provider_; |
| 152 | 130 |
| 153 DISALLOW_COPY_AND_ASSIGN(Providers); | 131 DISALLOW_COPY_AND_ASSIGN(Providers); |
| 154 }; | 132 }; |
| 155 | 133 |
| 156 // The Result classes are intermediate representations of AutocompleteMatches, | |
| 157 // simply containing relevance-ranked search and navigation suggestions. | |
| 158 // They may be cached to provide some synchronous matches while requests for | |
| 159 // new suggestions from updated input are in flight. | |
| 160 // TODO(msw) Extend these classes to generate their corresponding matches and | |
| 161 // other requisite data, in order to consolidate and simplify the | |
| 162 // highly fragmented SearchProvider logic for each Result type. | |
| 163 class Result { | |
| 164 public: | |
| 165 Result(bool from_keyword_provider, | |
| 166 int relevance, | |
| 167 bool relevance_from_server); | |
| 168 virtual ~Result(); | |
| 169 | |
| 170 bool from_keyword_provider() const { return from_keyword_provider_; } | |
| 171 | |
| 172 const base::string16& match_contents() const { return match_contents_; } | |
| 173 const ACMatchClassifications& match_contents_class() const { | |
| 174 return match_contents_class_; | |
| 175 } | |
| 176 | |
| 177 int relevance() const { return relevance_; } | |
| 178 void set_relevance(int relevance) { relevance_ = relevance; } | |
| 179 | |
| 180 bool relevance_from_server() const { return relevance_from_server_; } | |
| 181 void set_relevance_from_server(bool relevance_from_server) { | |
| 182 relevance_from_server_ = relevance_from_server; | |
| 183 } | |
| 184 | |
| 185 // Returns if this result is inlineable against the current input |input|. | |
| 186 // Non-inlineable results are stale. | |
| 187 virtual bool IsInlineable(const base::string16& input) const = 0; | |
| 188 | |
| 189 // Returns the default relevance value for this result (which may | |
| 190 // be left over from a previous omnibox input) given the current | |
| 191 // input and whether the current input caused a keyword provider | |
| 192 // to be active. | |
| 193 virtual int CalculateRelevance(const AutocompleteInput& input, | |
| 194 bool keyword_provider_requested) const = 0; | |
| 195 | |
| 196 protected: | |
| 197 // The contents to be displayed and its style info. | |
| 198 base::string16 match_contents_; | |
| 199 ACMatchClassifications match_contents_class_; | |
| 200 | |
| 201 // True if the result came from the keyword provider. | |
| 202 bool from_keyword_provider_; | |
| 203 | |
| 204 // The relevance score. | |
| 205 int relevance_; | |
| 206 | |
| 207 private: | |
| 208 // Whether this result's relevance score was fully or partly calculated | |
| 209 // based on server information, and thus is assumed to be more accurate. | |
| 210 // This is ultimately used in | |
| 211 // SearchProvider::ConvertResultsToAutocompleteMatches(), see comments | |
| 212 // there. | |
| 213 bool relevance_from_server_; | |
| 214 }; | |
| 215 | |
| 216 class SuggestResult : public Result { | |
| 217 public: | |
| 218 SuggestResult(const base::string16& suggestion, | |
| 219 AutocompleteMatchType::Type type, | |
| 220 const base::string16& match_contents, | |
| 221 const base::string16& annotation, | |
| 222 const std::string& suggest_query_params, | |
| 223 const std::string& deletion_url, | |
| 224 bool from_keyword_provider, | |
| 225 int relevance, | |
| 226 bool relevance_from_server, | |
| 227 bool should_prefetch, | |
| 228 const base::string16& input_text); | |
| 229 virtual ~SuggestResult(); | |
| 230 | |
| 231 const base::string16& suggestion() const { return suggestion_; } | |
| 232 AutocompleteMatchType::Type type() const { return type_; } | |
| 233 const base::string16& annotation() const { return annotation_; } | |
| 234 const std::string& suggest_query_params() const { | |
| 235 return suggest_query_params_; | |
| 236 } | |
| 237 const std::string& deletion_url() const { return deletion_url_; } | |
| 238 bool should_prefetch() const { return should_prefetch_; } | |
| 239 | |
| 240 // Fills in |match_contents_class_| to reflect how |match_contents_| should | |
| 241 // be displayed and bolded against the current |input_text|. If | |
| 242 // |allow_bolding_all| is false and |match_contents_class_| would have all | |
| 243 // of |match_contents_| bolded, do nothing. | |
| 244 void ClassifyMatchContents(const bool allow_bolding_all, | |
| 245 const base::string16& input_text); | |
| 246 | |
| 247 // Result: | |
| 248 virtual bool IsInlineable(const base::string16& input) const OVERRIDE; | |
| 249 virtual int CalculateRelevance( | |
| 250 const AutocompleteInput& input, | |
| 251 bool keyword_provider_requested) const OVERRIDE; | |
| 252 | |
| 253 private: | |
| 254 // The search terms to be used for this suggestion. | |
| 255 base::string16 suggestion_; | |
| 256 | |
| 257 AutocompleteMatchType::Type type_; | |
| 258 | |
| 259 // Optional annotation for the |match_contents_| for disambiguation. | |
| 260 // This may be displayed in the autocomplete match contents, but is defined | |
| 261 // separately to facilitate different formatting. | |
| 262 base::string16 annotation_; | |
| 263 | |
| 264 // Optional additional parameters to be added to the search URL. | |
| 265 std::string suggest_query_params_; | |
| 266 | |
| 267 // Optional deletion URL provided with suggestions. Fetching this URL | |
| 268 // should result in some reasonable deletion behaviour on the server, | |
| 269 // e.g. deleting this term out of a user's server-side search history. | |
| 270 std::string deletion_url_; | |
| 271 | |
| 272 // Should this result be prefetched? | |
| 273 bool should_prefetch_; | |
| 274 }; | |
| 275 | |
| 276 class NavigationResult : public Result { | |
| 277 public: | |
| 278 // |provider| is necessary to use StringForURLDisplay() in order to | |
| 279 // compute |formatted_url_|. | |
| 280 NavigationResult(const AutocompleteProvider& provider, | |
| 281 const GURL& url, | |
| 282 const base::string16& description, | |
| 283 bool from_keyword_provider, | |
| 284 int relevance, | |
| 285 bool relevance_from_server, | |
| 286 const base::string16& input_text, | |
| 287 const std::string& languages); | |
| 288 virtual ~NavigationResult(); | |
| 289 | |
| 290 const GURL& url() const { return url_; } | |
| 291 const base::string16& description() const { return description_; } | |
| 292 const base::string16& formatted_url() const { return formatted_url_; } | |
| 293 | |
| 294 // Fills in |match_contents_| and |match_contents_class_| to reflect how | |
| 295 // the URL should be displayed and bolded against the current |input_text| | |
| 296 // and user |languages|. If |allow_bolding_nothing| is false and | |
| 297 // |match_contents_class_| would result in an entirely unbolded | |
| 298 // |match_contents_|, do nothing. | |
| 299 void CalculateAndClassifyMatchContents(const bool allow_bolding_nothing, | |
| 300 const base::string16& input_text, | |
| 301 const std::string& languages); | |
| 302 | |
| 303 // Result: | |
| 304 virtual bool IsInlineable(const base::string16& input) const OVERRIDE; | |
| 305 virtual int CalculateRelevance( | |
| 306 const AutocompleteInput& input, | |
| 307 bool keyword_provider_requested) const OVERRIDE; | |
| 308 | |
| 309 private: | |
| 310 // The suggested url for navigation. | |
| 311 GURL url_; | |
| 312 | |
| 313 // The properly formatted ("fixed up") URL string with equivalent meaning | |
| 314 // to the one in |url_|. | |
| 315 base::string16 formatted_url_; | |
| 316 | |
| 317 // The suggested navigational result description; generally the site name. | |
| 318 base::string16 description_; | |
| 319 }; | |
| 320 | |
| 321 class CompareScoredResults; | 134 class CompareScoredResults; |
| 322 | 135 |
| 323 typedef std::vector<SuggestResult> SuggestResults; | |
| 324 typedef std::vector<NavigationResult> NavigationResults; | |
| 325 typedef std::vector<history::KeywordSearchTermVisit> HistoryResults; | 136 typedef std::vector<history::KeywordSearchTermVisit> HistoryResults; |
| 326 typedef std::pair<base::string16, std::string> MatchKey; | |
| 327 typedef std::map<MatchKey, AutocompleteMatch> MatchMap; | |
| 328 typedef ScopedVector<SuggestionDeletionHandler> SuggestionDeletionHandlers; | |
| 329 | |
| 330 // A simple structure bundling most of the information (including | |
| 331 // both SuggestResults and NavigationResults) returned by a call to | |
| 332 // the suggest server. | |
| 333 // | |
| 334 // This has to be declared after the typedefs since it relies on some of them. | |
| 335 struct Results { | |
| 336 Results(); | |
| 337 ~Results(); | |
| 338 | |
| 339 // Clears |suggest_results| and |navigation_results| and resets | |
| 340 // |verbatim_relevance| to -1 (implies unset). | |
| 341 void Clear(); | |
| 342 | |
| 343 // Returns whether any of the results (including verbatim) have | |
| 344 // server-provided scores. | |
| 345 bool HasServerProvidedScores() const; | |
| 346 | |
| 347 // Query suggestions sorted by relevance score. | |
| 348 SuggestResults suggest_results; | |
| 349 | |
| 350 // Navigational suggestions sorted by relevance score. | |
| 351 NavigationResults navigation_results; | |
| 352 | |
| 353 // The server supplied verbatim relevance scores. Negative values | |
| 354 // indicate that there is no suggested score; a value of 0 | |
| 355 // suppresses the verbatim result. | |
| 356 int verbatim_relevance; | |
| 357 | |
| 358 // The JSON metadata associated with this server response. | |
| 359 std::string metadata; | |
| 360 | |
| 361 private: | |
| 362 DISALLOW_COPY_AND_ASSIGN(Results); | |
| 363 }; | |
| 364 | |
| 365 // Returns an AutocompleteMatch with the given |autocomplete_provider| | |
| 366 // for the search |suggestion|, which represents a search via |template_url|. | |
| 367 // If |template_url| is NULL, returns a match with an invalid destination URL. | |
| 368 // | |
| 369 // |input_text| is the original user input. This is used to highlight | |
| 370 // portions of the match contents to distinguish locally-typed text from | |
| 371 // suggested text. | |
| 372 // | |
| 373 // |input| is necessary for various other details, like whether we should | |
| 374 // allow inline autocompletion and what the transition type should be. | |
| 375 // |accepted_suggestion| and |omnibox_start_margin| are used along with | |
| 376 // |input_text| to generate Assisted Query Stats. | |
| 377 // |append_extra_query_params| should be set if |template_url| is the default | |
| 378 // search engine, so the destination URL will contain any | |
| 379 // command-line-specified query params. | |
| 380 static AutocompleteMatch CreateSearchSuggestion( | |
| 381 AutocompleteProvider* autocomplete_provider, | |
| 382 const AutocompleteInput& input, | |
| 383 const base::string16& input_text, | |
| 384 const SuggestResult& suggestion, | |
| 385 const TemplateURL* template_url, | |
| 386 int accepted_suggestion, | |
| 387 int omnibox_start_margin, | |
| 388 bool append_extra_query_params); | |
| 389 | 137 |
| 390 // Removes non-inlineable results until either the top result can inline | 138 // Removes non-inlineable results until either the top result can inline |
| 391 // autocomplete the current input or verbatim outscores the top result. | 139 // autocomplete the current input or verbatim outscores the top result. |
| 392 static void RemoveStaleResults(const base::string16& input, | 140 static void RemoveStaleResults(const base::string16& input, |
| 393 int verbatim_relevance, | 141 int verbatim_relevance, |
| 394 SuggestResults* suggest_results, | 142 SuggestResults* suggest_results, |
| 395 NavigationResults* navigation_results); | 143 NavigationResults* navigation_results); |
| 396 | 144 |
| 397 // Recalculates the match contents class of |results| to better display | 145 // Recalculates the match contents class of |results| to better display |
| 398 // against the current input and user's language. | 146 // against the current input and user's language. |
| 399 void UpdateMatchContentsClass(const base::string16& input_text, | 147 void UpdateMatchContentsClass(const base::string16& input_text, |
| 400 Results* results); | 148 Results* results); |
| 401 | 149 |
| 402 // Calculates the relevance score for the keyword verbatim result (if the | 150 // Calculates the relevance score for the keyword verbatim result (if the |
| 403 // input matches one of the profile's keyword). | 151 // input matches one of the profile's keyword). |
| 404 static int CalculateRelevanceForKeywordVerbatim(AutocompleteInput::Type type, | 152 static int CalculateRelevanceForKeywordVerbatim(AutocompleteInput::Type type, |
| 405 bool prefer_keyword); | 153 bool prefer_keyword); |
| 406 | 154 |
| 407 // AutocompleteProvider: | 155 // AutocompleteProvider: |
| 408 virtual void Start(const AutocompleteInput& input, | 156 virtual void Start(const AutocompleteInput& input, |
| 409 bool minimal_changes) OVERRIDE; | 157 bool minimal_changes) OVERRIDE; |
| 410 virtual void Stop(bool clear_cached_results) OVERRIDE; | |
| 411 | 158 |
| 412 // net::URLFetcherDelegate: | 159 virtual bool IsKeywordRequest(const net::URLFetcher* source) OVERRIDE; |
| 413 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 414 | 160 |
| 415 // This gets called when we have requested a suggestion deletion from the | 161 virtual bool IsRequestSuccessful(const net::URLFetcher* source) OVERRIDE; |
| 416 // server to handle the results of the deletion. | |
| 417 void OnDeletionComplete(bool success, | |
| 418 SuggestionDeletionHandler* handler); | |
| 419 | 162 |
| 420 // Records in UMA whether the deletion request resulted in success. | 163 virtual void LogFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 421 // This is virtual so test code can override it to check that we | |
| 422 // correctly handle the request result. | |
| 423 virtual void RecordDeletionResult(bool success); | |
| 424 | 164 |
| 425 // Removes the deleted match from the list of |matches_|. | 165 virtual bool IsValidQuery(const base::string16 query, |
| 426 void DeleteMatchFromMatches(const AutocompleteMatch& match); | 166 const net::URLFetcher* source) OVERRIDE; |
| 167 |
| 168 virtual void RecordDeletionResult(bool success) OVERRIDE; |
| 169 |
| 170 virtual void StopSuggest() OVERRIDE; |
| 171 |
| 172 virtual void ClearAllResults() OVERRIDE; |
| 173 |
| 174 virtual int GetDefaultRelevance() OVERRIDE; |
| 175 |
| 176 virtual const base::string16 GetInputText(const net::URLFetcher* source) |
| 177 OVERRIDE; |
| 178 |
| 179 virtual bool ShouldAllowNavSuggest(const net::URLFetcher* source) OVERRIDE; |
| 180 |
| 181 virtual Results* GetResultsObjectToFill(const net::URLFetcher* source) |
| 182 OVERRIDE; |
| 183 |
| 184 virtual void SortResults(const net::URLFetcher* source, |
| 185 const base::ListValue* relevances, |
| 186 Results* results) OVERRIDE; |
| 187 |
| 188 virtual void UpdateMatches() OVERRIDE; |
| 189 |
| 190 virtual bool ShouldSendProviderUpdate(bool results_updated) OVERRIDE; |
| 427 | 191 |
| 428 // Called when timer_ expires. | 192 // Called when timer_ expires. |
| 429 void Run(); | 193 void Run(); |
| 430 | 194 |
| 431 // Runs the history query, if necessary. The history query is synchronous. | 195 // Runs the history query, if necessary. The history query is synchronous. |
| 432 // This does not update |done_|. | 196 // This does not update |done_|. |
| 433 void DoHistoryQuery(bool minimal_changes); | 197 void DoHistoryQuery(bool minimal_changes); |
| 434 | 198 |
| 435 // Determines whether an asynchronous subcomponent query should run for the | 199 // Determines whether an asynchronous subcomponent query should run for the |
| 436 // current input. If so, starts it if necessary; otherwise stops it. | 200 // current input. If so, starts it if necessary; otherwise stops it. |
| 437 // NOTE: This function does not update |done_|. Callers must do so. | 201 // NOTE: This function does not update |done_|. Callers must do so. |
| 438 void StartOrStopSuggestQuery(bool minimal_changes); | 202 void StartOrStopSuggestQuery(bool minimal_changes); |
| 439 | 203 |
| 440 // Returns true when the current query can be sent to the Suggest service. | 204 // Returns true when the current query can be sent to the Suggest service. |
| 441 // This will be false e.g. when Suggest is disabled, the query contains | 205 // This will be false e.g. when Suggest is disabled, the query contains |
| 442 // potentially private data, etc. | 206 // potentially private data, etc. |
| 443 bool IsQuerySuitableForSuggest() const; | 207 bool IsQuerySuitableForSuggest() const; |
| 444 | 208 |
| 445 // Stops the suggest query. | 209 // Removes stale results for both default and keyword providers. |
| 446 // NOTE: This does not update |done_|. Callers must do so. | 210 // See comments on RemoveStaleResults(). |
| 447 void StopSuggest(); | |
| 448 | |
| 449 // Clears the current results. | |
| 450 void ClearAllResults(); | |
| 451 | |
| 452 // Removes stale results for both default and keyword providers. See comments | |
| 453 // on RemoveStaleResults(). | |
| 454 void RemoveAllStaleResults(); | 211 void RemoveAllStaleResults(); |
| 455 | 212 |
| 456 // Apply calculated relevance scores to the current results. | 213 // Apply calculated relevance scores to the current results. |
| 457 void ApplyCalculatedRelevance(); | 214 void ApplyCalculatedRelevance(); |
| 458 void ApplyCalculatedSuggestRelevance(SuggestResults* list); | 215 void ApplyCalculatedSuggestRelevance(SuggestResults* list); |
| 459 void ApplyCalculatedNavigationRelevance(NavigationResults* list); | 216 void ApplyCalculatedNavigationRelevance(NavigationResults* list); |
| 460 | 217 |
| 461 // Starts a new URLFetcher requesting suggest results from |template_url|; | 218 // Starts a new URLFetcher requesting suggest results from |template_url|; |
| 462 // callers own the returned URLFetcher, which is NULL for invalid providers. | 219 // callers own the returned URLFetcher, which is NULL for invalid providers. |
| 463 net::URLFetcher* CreateSuggestFetcher(int id, | 220 net::URLFetcher* CreateSuggestFetcher(int id, |
| 464 const TemplateURL* template_url, | 221 const TemplateURL* template_url, |
| 465 const AutocompleteInput& input); | 222 const AutocompleteInput& input); |
| 466 | 223 |
| 467 // Parses JSON response received from the provider, stripping XSSI | |
| 468 // protection if needed. Returns the parsed data if successful, NULL | |
| 469 // otherwise. | |
| 470 static scoped_ptr<base::Value> DeserializeJsonData(std::string json_data); | |
| 471 | |
| 472 // Parses results from the suggest server and updates the appropriate suggest | |
| 473 // and navigation result lists, depending on whether |is_keyword| is true. | |
| 474 // Returns whether the appropriate result list members were updated. | |
| 475 bool ParseSuggestResults(base::Value* root_val, bool is_keyword); | |
| 476 | |
| 477 // Converts the parsed results to a set of AutocompleteMatches, |matches_|. | 224 // Converts the parsed results to a set of AutocompleteMatches, |matches_|. |
| 478 void ConvertResultsToAutocompleteMatches(); | 225 void ConvertResultsToAutocompleteMatches(); |
| 479 | 226 |
| 480 // Returns an iterator to the first match in |matches_| which might | 227 // Returns an iterator to the first match in |matches_| which might |
| 481 // be chosen as default. If | 228 // be chosen as default. If |
| 482 // |autocomplete_result_will_reorder_for_default_match| is false, | 229 // |autocomplete_result_will_reorder_for_default_match| is false, |
| 483 // this simply means the first match; otherwise, it means the first | 230 // this simply means the first match; otherwise, it means the first |
| 484 // match for which the |allowed_to_be_default_match| member is true. | 231 // match for which the |allowed_to_be_default_match| member is true. |
| 485 ACMatches::const_iterator FindTopMatch( | 232 ACMatches::const_iterator FindTopMatch( |
| 486 bool autocomplete_result_will_reorder_for_default_match) const; | 233 bool autocomplete_result_will_reorder_for_default_match) const; |
| 487 | 234 |
| 488 // Checks if suggested relevances violate certain expected constraints. | 235 // Checks if suggested relevances violate certain expected constraints. |
| 489 // See UpdateMatches() for the use and explanation of these constraints. | 236 // See UpdateMatches() for the use and explanation of these constraints. |
| 490 bool IsTopMatchNavigationInKeywordMode( | 237 bool IsTopMatchNavigationInKeywordMode( |
| 491 bool autocomplete_result_will_reorder_for_default_match) const; | 238 bool autocomplete_result_will_reorder_for_default_match) const; |
| 492 bool HasKeywordDefaultMatchInKeywordMode() const; | 239 bool HasKeywordDefaultMatchInKeywordMode() const; |
| 493 bool IsTopMatchScoreTooLow( | 240 bool IsTopMatchScoreTooLow( |
| 494 bool autocomplete_result_will_reorder_for_default_match) const; | 241 bool autocomplete_result_will_reorder_for_default_match) const; |
| 495 bool IsTopMatchSearchWithURLInput( | 242 bool IsTopMatchSearchWithURLInput( |
| 496 bool autocomplete_result_will_reorder_for_default_match) const; | 243 bool autocomplete_result_will_reorder_for_default_match) const; |
| 497 bool HasValidDefaultMatch( | 244 bool HasValidDefaultMatch( |
| 498 bool autocomplete_result_will_reorder_for_default_match) const; | 245 bool autocomplete_result_will_reorder_for_default_match) const; |
| 499 | 246 |
| 500 // Updates |matches_| from the latest results; applies calculated relevances | |
| 501 // if suggested relevances cause undesriable behavior. Updates |done_|. | |
| 502 void UpdateMatches(); | |
| 503 | |
| 504 // Converts an appropriate number of navigation results in | 247 // Converts an appropriate number of navigation results in |
| 505 // |navigation_results| to matches and adds them to |matches|. | 248 // |navigation_results| to matches and adds them to |matches|. |
| 506 void AddNavigationResultsToMatches( | 249 void AddNavigationResultsToMatches( |
| 507 const NavigationResults& navigation_results, | 250 const NavigationResults& navigation_results, |
| 508 ACMatches* matches); | 251 ACMatches* matches); |
| 509 | 252 |
| 510 // Adds a match for each result in |results| to |map|. |is_keyword| indicates | 253 // Adds a match for each result in |results| to |map|. |is_keyword| indicates |
| 511 // whether the results correspond to the keyword provider or default provider. | 254 // whether the results correspond to the keyword provider or default |
| 255 // provider. |
| 512 void AddHistoryResultsToMap(const HistoryResults& results, | 256 void AddHistoryResultsToMap(const HistoryResults& results, |
| 513 bool is_keyword, | 257 bool is_keyword, |
| 514 int did_not_accept_suggestion, | 258 int did_not_accept_suggestion, |
| 515 MatchMap* map); | 259 MatchMap* map); |
| 516 | 260 |
| 517 // Calculates relevance scores for all |results|. | 261 // Calculates relevance scores for all |results|. |
| 518 SuggestResults ScoreHistoryResults(const HistoryResults& results, | 262 SuggestResults ScoreHistoryResults(const HistoryResults& results, |
| 519 bool base_prevent_inline_autocomplete, | 263 bool base_prevent_inline_autocomplete, |
| 520 bool input_multiple_words, | 264 bool input_multiple_words, |
| 521 const base::string16& input_text, | 265 const base::string16& input_text, |
| 522 bool is_keyword); | 266 bool is_keyword); |
| 523 | 267 |
| 524 // Adds matches for |results| to |map|. | 268 // Adds matches for |results| to |map|. |
| 525 void AddSuggestResultsToMap(const SuggestResults& results, | 269 void AddSuggestResultsToMap(const SuggestResults& results, |
| 526 const std::string& metadata, | 270 const std::string& metadata, |
| 527 MatchMap* map); | 271 MatchMap* map); |
| 528 | 272 |
| 273 // Returns the right template URL for the given |result|. |
| 274 const TemplateURL* GetTemplateURL(const SuggestResult& result); |
| 275 |
| 276 // Returns whether we should append extra query params to the match. |
| 277 bool ShouldAppendExtraQueryParams(const SuggestResult& result); |
| 278 |
| 529 // Gets the relevance score for the verbatim result. This value may be | 279 // Gets the relevance score for the verbatim result. This value may be |
| 530 // provided by the suggest server or calculated locally; if | 280 // provided by the suggest server or calculated locally; if |
| 531 // |relevance_from_server| is non-NULL, it will be set to indicate which of | 281 // |relevance_from_server| is non-NULL, it will be set to indicate which of |
| 532 // those is true. | 282 // those is true. |
| 533 int GetVerbatimRelevance(bool* relevance_from_server) const; | 283 int GetVerbatimRelevance(bool* relevance_from_server) const; |
| 534 | 284 |
| 535 // Calculates the relevance score for the verbatim result from the | 285 // Calculates the relevance score for the verbatim result from the |
| 536 // default search engine. This version takes into account context: | 286 // default search engine. This version takes into account context: |
| 537 // i.e., whether the user has entered a keyword-based search or not. | 287 // i.e., whether the user has entered a keyword-based search or not. |
| 538 int CalculateRelevanceForVerbatim() const; | 288 int CalculateRelevanceForVerbatim() const; |
| 539 | 289 |
| 540 // Calculates the relevance score for the verbatim result from the default | 290 // Calculates the relevance score for the verbatim result from the default |
| 541 // search engine *ignoring* whether the input is a keyword-based search | 291 // search engine *ignoring* whether the input is a keyword-based search |
| 542 // or not. This function should only be used to determine the minimum | 292 // or not. This function should only be used to determine the minimum |
| 543 // relevance score that the best result from this provider should have. | 293 // relevance score that the best result from this provider should have. |
| 544 // For normal use, prefer the above function. | 294 // For normal use, prefer the above function. |
| 545 int CalculateRelevanceForVerbatimIgnoringKeywordModeState() const; | 295 int CalculateRelevanceForVerbatimIgnoringKeywordModeState() const; |
| 546 | 296 |
| 547 // Gets the relevance score for the keyword verbatim result. | 297 // Gets the relevance score for the keyword verbatim result. |
| 548 // |relevance_from_server| is handled as in GetVerbatimRelevance(). | 298 // |relevance_from_server| is handled as in GetVerbatimRelevance(). |
| 549 // TODO(mpearson): Refactor so this duplication isn't necessary or | 299 // TODO(mpearson): Refactor so this duplication isn't necessary or |
| 550 // restructure so one static function takes all the parameters it needs | 300 // restructure so one static function takes all the parameters it needs |
| 551 // (rather than looking at internal state). | 301 // (rather than looking at internal state). |
| 552 int GetKeywordVerbatimRelevance(bool* relevance_from_server) const; | 302 int GetKeywordVerbatimRelevance(bool* relevance_from_server) const; |
| 553 | 303 |
| 554 // |time| is the time at which this query was last seen. |is_keyword| | 304 // |time| is the time at which this query was last seen. |is_keyword| |
| 555 // indicates whether the results correspond to the keyword provider or default | 305 // indicates whether the results correspond to the keyword provider or |
| 556 // provider. |use_aggressive_method| says whether this function can use a | 306 // default provider. |use_aggressive_method| says whether this function can |
| 557 // method that gives high scores (1200+) rather than one that gives lower | 307 // use a method that gives high scores (1200+) rather than one that gives |
| 558 // scores. When using the aggressive method, scores may exceed 1300 | 308 // lower scores. When using the aggressive method, scores may exceed 1300 |
| 559 // unless |prevent_search_history_inlining| is set. | 309 // unless |prevent_search_history_inlining| is set. |
| 560 int CalculateRelevanceForHistory(const base::Time& time, | 310 int CalculateRelevanceForHistory(const base::Time& time, |
| 561 bool is_keyword, | 311 bool is_keyword, |
| 562 bool use_aggressive_method, | 312 bool use_aggressive_method, |
| 563 bool prevent_search_history_inlining) const; | 313 bool prevent_search_history_inlining) const; |
| 564 | 314 |
| 565 // Creates an AutocompleteMatch for "Search <engine> for |query_string|" with | |
| 566 // the supplied details. Adds this match to |map|; if such a match already | |
| 567 // exists, whichever one has lower relevance is eliminated. | |
| 568 void AddMatchToMap(const SuggestResult& result, | |
| 569 const base::string16& input_text, | |
| 570 const std::string& metadata, | |
| 571 int accepted_suggestion, | |
| 572 MatchMap* map); | |
| 573 | |
| 574 // Returns an AutocompleteMatch for a navigational suggestion. | 315 // Returns an AutocompleteMatch for a navigational suggestion. |
| 575 AutocompleteMatch NavigationToMatch(const NavigationResult& navigation); | 316 AutocompleteMatch NavigationToMatch(const NavigationResult& navigation); |
| 576 | 317 |
| 577 // Resets the scores of all |keyword_navigation_results_| matches to | 318 // Resets the scores of all |keyword_navigation_results_| matches to |
| 578 // be below that of the top keyword query match (the verbatim match | 319 // be below that of the top keyword query match (the verbatim match |
| 579 // as expressed by |keyword_verbatim_relevance_| or keyword query | 320 // as expressed by |keyword_verbatim_relevance_| or keyword query |
| 580 // suggestions stored in |keyword_suggest_results_|). If there | 321 // suggestions stored in |keyword_suggest_results_|). If there |
| 581 // are no keyword suggestions and keyword verbatim is suppressed, | 322 // are no keyword suggestions and keyword verbatim is suppressed, |
| 582 // then drops the suggested relevance scores for the navsuggestions | 323 // then drops the suggested relevance scores for the navsuggestions |
| 583 // and drops the request to suppress verbatim, thereby introducing the | 324 // and drops the request to suppress verbatim, thereby introducing the |
| 584 // keyword verbatim match which will naturally outscore the navsuggestions. | 325 // keyword verbatim match which will naturally outscore the navsuggestions. |
| 585 void DemoteKeywordNavigationMatchesPastTopQuery(); | 326 void DemoteKeywordNavigationMatchesPastTopQuery(); |
| 586 | 327 |
| 587 // Updates the value of |done_| from the internal state. | 328 // Updates the value of |done_| from the internal state. |
| 588 void UpdateDone(); | 329 void UpdateDone(); |
| 589 | 330 |
| 590 // Returns whether we can send the URL of the current page in any suggest | |
| 591 // requests. Doing this requires that all the following hold: | |
| 592 // * The user has suggest enabled in their settings and is not in incognito | |
| 593 // mode. (Incognito disables suggest entirely.) | |
| 594 // * The current URL is HTTP, or HTTPS with the same domain as the suggest | |
| 595 // server. Non-HTTP[S] URLs (e.g. FTP/file URLs) may contain sensitive | |
| 596 // information. HTTPS URLs may also contain sensitive information, but if | |
| 597 // they're on the same domain as the suggest server, then the relevant | |
| 598 // entity could have already seen/logged this data. | |
| 599 // * The suggest request is sent over HTTPS. This avoids leaking the current | |
| 600 // page URL in world-readable network traffic. | |
| 601 // * The user's suggest provider is Google. We might want to allow other | |
| 602 // providers to see this data someday, but for now this has only been | |
| 603 // implemented for Google. Also see next bullet. | |
| 604 // * The user is OK in principle with sending URLs of current pages to their | |
| 605 // provider. Today, there is no explicit setting that controls this, but if | |
| 606 // the user has tab sync enabled and tab sync is unencrypted, then they're | |
| 607 // already sending this data to Google for sync purposes. Thus we use this | |
| 608 // setting as a proxy for "it's OK to send such data". In the future, | |
| 609 // especially if we want to support suggest providers other than Google, we | |
| 610 // may change this to be a standalone setting or part of some explicit | |
| 611 // general opt-in. | |
| 612 static bool CanSendURL( | |
| 613 const GURL& current_page_url, | |
| 614 const GURL& suggest_url, | |
| 615 const TemplateURL* template_url, | |
| 616 AutocompleteInput::PageClassification page_classification, | |
| 617 Profile* profile); | |
| 618 | |
| 619 // The amount of time to wait before sending a new suggest request after the | 331 // The amount of time to wait before sending a new suggest request after the |
| 620 // previous one. Non-const because some unittests modify this value. | 332 // previous one. Non-const because some unittests modify this value. |
| 621 static int kMinimumTimeBetweenSuggestQueriesMs; | 333 static int kMinimumTimeBetweenSuggestQueriesMs; |
| 622 | 334 |
| 623 // The following keys are used to record additional information on matches. | |
| 624 | |
| 625 // We annotate our AutocompleteMatches with whether their relevance scores | |
| 626 // were server-provided using this key in the |additional_info| field. | |
| 627 static const char kRelevanceFromServerKey[]; | |
| 628 | |
| 629 // Indicates whether the server said a match should be prefetched. | |
| 630 static const char kShouldPrefetchKey[]; | |
| 631 | |
| 632 // Used to store metadata from the server response, which is needed for | |
| 633 // prefetching. | |
| 634 static const char kSuggestMetadataKey[]; | |
| 635 | |
| 636 // Used to store a deletion request url for server-provided suggestions. | |
| 637 static const char kDeletionUrlKey[]; | |
| 638 | |
| 639 // These are the values for the above keys. | |
| 640 static const char kTrue[]; | |
| 641 static const char kFalse[]; | |
| 642 | |
| 643 // Maintains the TemplateURLs used. | 335 // Maintains the TemplateURLs used. |
| 644 Providers providers_; | 336 Providers providers_; |
| 645 | 337 |
| 646 // The user's input. | 338 // The user's input. |
| 647 AutocompleteInput input_; | 339 AutocompleteInput input_; |
| 648 | 340 |
| 649 // Input when searching against the keyword provider. | 341 // Input when searching against the keyword provider. |
| 650 AutocompleteInput keyword_input_; | 342 AutocompleteInput keyword_input_; |
| 651 | 343 |
| 652 // Searches in the user's history that begin with the input text. | 344 // Searches in the user's history that begin with the input text. |
| 653 HistoryResults keyword_history_results_; | 345 HistoryResults keyword_history_results_; |
| 654 HistoryResults default_history_results_; | 346 HistoryResults default_history_results_; |
| 655 | 347 |
| 656 // Number of suggest results that haven't yet arrived. If greater than 0 it | |
| 657 // indicates one of the URLFetchers is still running. | |
| 658 int suggest_results_pending_; | |
| 659 | |
| 660 // A timer to start a query to the suggest server after the user has stopped | 348 // A timer to start a query to the suggest server after the user has stopped |
| 661 // typing for long enough. | 349 // typing for long enough. |
| 662 base::OneShotTimer<SearchProvider> timer_; | 350 base::OneShotTimer<SearchProvider> timer_; |
| 663 | 351 |
| 664 // The time at which we sent a query to the suggest server. | 352 // The time at which we sent a query to the suggest server. |
| 665 base::TimeTicks time_suggest_request_sent_; | 353 base::TimeTicks time_suggest_request_sent_; |
| 666 | 354 |
| 667 // Fetchers used to retrieve results for the keyword and default providers. | 355 // Fetchers used to retrieve results for the keyword and default providers. |
| 668 scoped_ptr<net::URLFetcher> keyword_fetcher_; | 356 scoped_ptr<net::URLFetcher> keyword_fetcher_; |
| 669 scoped_ptr<net::URLFetcher> default_fetcher_; | 357 scoped_ptr<net::URLFetcher> default_fetcher_; |
| 670 | 358 |
| 671 // Results from the default and keyword search providers. | 359 // Results from the default and keyword search providers. |
| 672 Results default_results_; | 360 Results default_results_; |
| 673 Results keyword_results_; | 361 Results keyword_results_; |
| 674 | 362 |
| 675 // Each deletion handler in this vector corresponds to an outstanding request | |
| 676 // that a server delete a personalized suggestion. Making this a ScopedVector | |
| 677 // causes us to auto-cancel all such requests on shutdown. | |
| 678 SuggestionDeletionHandlers deletion_handlers_; | |
| 679 | |
| 680 // Whether a field trial, if any, has triggered in the most recent | |
| 681 // autocomplete query. This field is set to false in Start() and may be set | |
| 682 // to true if either the default provider or keyword provider has completed | |
| 683 // and their corresponding suggest response contained | |
| 684 // '"google:fieldtrialtriggered":true'. | |
| 685 // If the autocomplete query has not returned, this field is set to false. | |
| 686 bool field_trial_triggered_; | |
| 687 | |
| 688 // Same as above except that it is maintained across the current Omnibox | |
| 689 // session. | |
| 690 bool field_trial_triggered_in_session_; | |
| 691 | |
| 692 // If true, search history query suggestions will score low enough that | 363 // If true, search history query suggestions will score low enough that |
| 693 // they will not be inlined. | 364 // they will not be inlined. |
| 694 bool prevent_search_history_inlining_; | 365 bool prevent_search_history_inlining_; |
| 695 | 366 |
| 696 GURL current_page_url_; | 367 GURL current_page_url_; |
| 697 | 368 |
| 698 DISALLOW_COPY_AND_ASSIGN(SearchProvider); | 369 DISALLOW_COPY_AND_ASSIGN(SearchProvider); |
| 699 }; | 370 }; |
| 700 | 371 |
| 701 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ | 372 #endif // CHROME_BROWSER_AUTOCOMPLETE_SEARCH_PROVIDER_H_ |
| OLD | NEW |