1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 // 5 // This class contains common functionality for search-based autocomplete 6 // providers. Search provider and zero suggest provider both use it for common 7 // functionality. 8 9 #ifndef CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ 10 #define CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ 11 12 #include <map> 13 #include <string> 14 #include <utility> 15 #include <vector> 16 17 #include "base/memory/scoped_vector.h" 18 #include "base/strings/string16.h" 19 #include "chrome/browser/autocomplete/autocomplete_input.h" 20 #include "chrome/browser/autocomplete/autocomplete_match.h" 21 #include "chrome/browser/autocomplete/autocomplete_provider.h" 22 #include "components/metrics/proto/omnibox_event.pb.h" 23 #include "net/url_request/url_fetcher_delegate.h" 24 25 class AutocompleteProviderListener; 26 class GURL; 27 class Profile; 28 class SuggestionDeletionHandler; 29 class TemplateURL; 30 31 namespace base { 32 class DictionaryValue; 33 class ListValue; 34 class Value; 35 } 36 37 // Base functionality for receiving suggestions from a search engine. 38 // This class is abstract and should only be used as a base for other 39 // autocomplete providers utilizing its functionality. 40 class BaseSearchProvider : public AutocompleteProvider, 41 public net::URLFetcherDelegate { 42 public: 43 // ID used in creating URLFetcher for default provider's suggest results. 44 static const int kDefaultProviderURLFetcherID; 45 46 // ID used in creating URLFetcher for keyword provider's suggest results. 47 static const int kKeywordProviderURLFetcherID; 48 49 // ID used in creating URLFetcher for deleting suggestion results. 50 static const int kDeletionURLFetcherID; 51 52 BaseSearchProvider(AutocompleteProviderListener* listener, 53 Profile* profile, 54 AutocompleteProvider::Type type); 55 56 // Returns whether |match| is flagged as a query that should be prefetched. 57 static bool ShouldPrefetch(const AutocompleteMatch& match); 58 59 // Returns a simpler AutocompleteMatch suitable for persistence like in 60 // ShortcutsDatabase. 61 // NOTE: Use with care. Most likely you want the other CreateSearchSuggestion 62 // with protected access. 63 static AutocompleteMatch CreateSearchSuggestion( 64 const base::string16& suggestion, 65 AutocompleteMatchType::Type type, 66 bool from_keyword_provider, 67 const TemplateURL* template_url, 68 const SearchTermsData& search_terms_data); 69 70 // AutocompleteProvider: 71 virtual void Stop(bool clear_cached_results) OVERRIDE; 72 virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE; 73 virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE; 74 field_trial_triggered_in_session()75 bool field_trial_triggered_in_session() const { 76 return field_trial_triggered_in_session_; 77 } 78 set_in_app_list()79 void set_in_app_list() { in_app_list_ = true; } 80 81 protected: 82 // The following keys are used to record additional information on matches. 83 84 // We annotate our AutocompleteMatches with whether their relevance scores 85 // were server-provided using this key in the |additional_info| field. 86 static const char kRelevanceFromServerKey[]; 87 88 // Indicates whether the server said a match should be prefetched. 89 static const char kShouldPrefetchKey[]; 90 91 // Used to store metadata from the server response, which is needed for 92 // prefetching. 93 static const char kSuggestMetadataKey[]; 94 95 // Used to store a deletion request url for server-provided suggestions. 96 static const char kDeletionUrlKey[]; 97 98 // These are the values for the above keys. 99 static const char kTrue[]; 100 static const char kFalse[]; 101 102 virtual ~BaseSearchProvider(); 103 104 // The Result classes are intermediate representations of AutocompleteMatches, 105 // simply containing relevance-ranked search and navigation suggestions. 106 // They may be cached to provide some synchronous matches while requests for 107 // new suggestions from updated input are in flight. 108 // TODO(msw) Extend these classes to generate their corresponding matches and 109 // other requisite data, in order to consolidate and simplify the 110 // highly fragmented SearchProvider logic for each Result type. 111 class Result { 112 public: 113 Result(bool from_keyword_provider, 114 int relevance, 115 bool relevance_from_server, 116 AutocompleteMatchType::Type type, 117 const std::string& deletion_url); 118 virtual ~Result(); 119 from_keyword_provider()120 bool from_keyword_provider() const { return from_keyword_provider_; } 121 match_contents()122 const base::string16& match_contents() const { return match_contents_; } match_contents_class()123 const ACMatchClassifications& match_contents_class() const { 124 return match_contents_class_; 125 } 126 type()127 AutocompleteMatchType::Type type() const { return type_; } relevance()128 int relevance() const { return relevance_; } set_relevance(int relevance)129 void set_relevance(int relevance) { relevance_ = relevance; } 130 relevance_from_server()131 bool relevance_from_server() const { return relevance_from_server_; } set_relevance_from_server(bool relevance_from_server)132 void set_relevance_from_server(bool relevance_from_server) { 133 relevance_from_server_ = relevance_from_server; 134 } 135 deletion_url()136 const std::string& deletion_url() const { return deletion_url_; } 137 138 // Returns if this result is inlineable against the current input |input|. 139 // Non-inlineable results are stale. 140 virtual bool IsInlineable(const base::string16& input) const = 0; 141 142 // Returns the default relevance value for this result (which may 143 // be left over from a previous omnibox input) given the current 144 // input and whether the current input caused a keyword provider 145 // to be active. 146 virtual int CalculateRelevance(const AutocompleteInput& input, 147 bool keyword_provider_requested) const = 0; 148 149 protected: 150 // The contents to be displayed and its style info. 151 base::string16 match_contents_; 152 ACMatchClassifications match_contents_class_; 153 154 // True if the result came from the keyword provider. 155 bool from_keyword_provider_; 156 157 AutocompleteMatchType::Type type_; 158 159 // The relevance score. 160 int relevance_; 161 162 private: 163 // Whether this result's relevance score was fully or partly calculated 164 // based on server information, and thus is assumed to be more accurate. 165 // This is ultimately used in 166 // SearchProvider::ConvertResultsToAutocompleteMatches(), see comments 167 // there. 168 bool relevance_from_server_; 169 170 // Optional deletion URL provided with suggestions. Fetching this URL 171 // should result in some reasonable deletion behaviour on the server, 172 // e.g. deleting this term out of a user's server-side search history. 173 std::string deletion_url_; 174 }; 175 176 class SuggestResult : public Result { 177 public: 178 SuggestResult(const base::string16& suggestion, 179 AutocompleteMatchType::Type type, 180 const base::string16& match_contents, 181 const base::string16& match_contents_prefix, 182 const base::string16& annotation, 183 const base::string16& answer_contents, 184 const base::string16& answer_type, 185 const std::string& suggest_query_params, 186 const std::string& deletion_url, 187 bool from_keyword_provider, 188 int relevance, 189 bool relevance_from_server, 190 bool should_prefetch, 191 const base::string16& input_text); 192 virtual ~SuggestResult(); 193 suggestion()194 const base::string16& suggestion() const { return suggestion_; } match_contents_prefix()195 const base::string16& match_contents_prefix() const { 196 return match_contents_prefix_; 197 } annotation()198 const base::string16& annotation() const { return annotation_; } suggest_query_params()199 const std::string& suggest_query_params() const { 200 return suggest_query_params_; 201 } 202 answer_contents()203 const base::string16& answer_contents() const { return answer_contents_; } answer_type()204 const base::string16& answer_type() const { return answer_type_; } 205 should_prefetch()206 bool should_prefetch() const { return should_prefetch_; } 207 208 // Fills in |match_contents_class_| to reflect how |match_contents_| should 209 // be displayed and bolded against the current |input_text|. If 210 // |allow_bolding_all| is false and |match_contents_class_| would have all 211 // of |match_contents_| bolded, do nothing. 212 void ClassifyMatchContents(const bool allow_bolding_all, 213 const base::string16& input_text); 214 215 // Result: 216 virtual bool IsInlineable(const base::string16& input) const OVERRIDE; 217 virtual int CalculateRelevance( 218 const AutocompleteInput& input, 219 bool keyword_provider_requested) const OVERRIDE; 220 221 private: 222 // The search terms to be used for this suggestion. 223 base::string16 suggestion_; 224 225 // The contents to be displayed as prefix of match contents. 226 // Used for postfix suggestions to display a leading ellipsis (or some 227 // equivalent character) to indicate omitted text. 228 // Only used to pass this information to about:omnibox's "Additional Info". 229 base::string16 match_contents_prefix_; 230 231 // Optional annotation for the |match_contents_| for disambiguation. 232 // This may be displayed in the autocomplete match contents, but is defined 233 // separately to facilitate different formatting. 234 base::string16 annotation_; 235 236 // Optional additional parameters to be added to the search URL. 237 std::string suggest_query_params_; 238 239 // Optional formatted Answers result. 240 base::string16 answer_contents_; 241 242 // Type of optional formatted Answers result. 243 base::string16 answer_type_; 244 245 // Should this result be prefetched? 246 bool should_prefetch_; 247 }; 248 249 class NavigationResult : public Result { 250 public: 251 // |provider| is necessary to use StringForURLDisplay() in order to 252 // compute |formatted_url_|. 253 NavigationResult(const AutocompleteProvider& provider, 254 const GURL& url, 255 AutocompleteMatchType::Type type, 256 const base::string16& description, 257 const std::string& deletion_url, 258 bool from_keyword_provider, 259 int relevance, 260 bool relevance_from_server, 261 const base::string16& input_text, 262 const std::string& languages); 263 virtual ~NavigationResult(); 264 url()265 const GURL& url() const { return url_; } description()266 const base::string16& description() const { return description_; } formatted_url()267 const base::string16& formatted_url() const { return formatted_url_; } 268 269 // Fills in |match_contents_| and |match_contents_class_| to reflect how 270 // the URL should be displayed and bolded against the current |input_text| 271 // and user |languages|. If |allow_bolding_nothing| is false and 272 // |match_contents_class_| would result in an entirely unbolded 273 // |match_contents_|, do nothing. 274 void CalculateAndClassifyMatchContents(const bool allow_bolding_nothing, 275 const base::string16& input_text, 276 const std::string& languages); 277 278 // Result: 279 virtual bool IsInlineable(const base::string16& input) const OVERRIDE; 280 virtual int CalculateRelevance( 281 const AutocompleteInput& input, 282 bool keyword_provider_requested) const OVERRIDE; 283 284 private: 285 // The suggested url for navigation. 286 GURL url_; 287 288 // The properly formatted ("fixed up") URL string with equivalent meaning 289 // to the one in |url_|. 290 base::string16 formatted_url_; 291 292 // The suggested navigational result description; generally the site name. 293 base::string16 description_; 294 }; 295 296 typedef std::vector<SuggestResult> SuggestResults; 297 typedef std::vector<NavigationResult> NavigationResults; 298 typedef std::pair<base::string16, std::string> MatchKey; 299 typedef std::map<MatchKey, AutocompleteMatch> MatchMap; 300 typedef ScopedVector<SuggestionDeletionHandler> SuggestionDeletionHandlers; 301 302 // A simple structure bundling most of the information (including 303 // both SuggestResults and NavigationResults) returned by a call to 304 // the suggest server. 305 // 306 // This has to be declared after the typedefs since it relies on some of them. 307 struct Results { 308 Results(); 309 ~Results(); 310 311 // Clears |suggest_results| and |navigation_results| and resets 312 // |verbatim_relevance| to -1 (implies unset). 313 void Clear(); 314 315 // Returns whether any of the results (including verbatim) have 316 // server-provided scores. 317 bool HasServerProvidedScores() const; 318 319 // Query suggestions sorted by relevance score. 320 SuggestResults suggest_results; 321 322 // Navigational suggestions sorted by relevance score. 323 NavigationResults navigation_results; 324 325 // The server supplied verbatim relevance scores. Negative values 326 // indicate that there is no suggested score; a value of 0 327 // suppresses the verbatim result. 328 int verbatim_relevance; 329 330 // The JSON metadata associated with this server response. 331 std::string metadata; 332 333 private: 334 DISALLOW_COPY_AND_ASSIGN(Results); 335 }; 336 337 // Returns an AutocompleteMatch with the given |autocomplete_provider| 338 // for the search |suggestion|, which represents a search via |template_url|. 339 // If |template_url| is NULL, returns a match with an invalid destination URL. 340 // 341 // |input| is the original user input. Text in the input is used to highlight 342 // portions of the match contents to distinguish locally-typed text from 343 // suggested text. 344 // 345 // |input| is also necessary for various other details, like whether we should 346 // allow inline autocompletion and what the transition type should be. 347 // |accepted_suggestion| and |omnibox_start_margin| are used to generate 348 // Assisted Query Stats. 349 // |append_extra_query_params| should be set if |template_url| is the default 350 // search engine, so the destination URL will contain any 351 // command-line-specified query params. 352 // |from_app_list| should be set if the search was made from the app list. 353 static AutocompleteMatch CreateSearchSuggestion( 354 AutocompleteProvider* autocomplete_provider, 355 const AutocompleteInput& input, 356 const SuggestResult& suggestion, 357 const TemplateURL* template_url, 358 const SearchTermsData& search_terms_data, 359 int accepted_suggestion, 360 int omnibox_start_margin, 361 bool append_extra_query_params, 362 bool from_app_list); 363 364 // Parses JSON response received from the provider, stripping XSSI 365 // protection if needed. Returns the parsed data if successful, NULL 366 // otherwise. 367 static scoped_ptr<base::Value> DeserializeJsonData(std::string json_data); 368 369 // Returns whether the requirements for requesting zero suggest results 370 // are met. The requirements are 371 // * The user is enrolled in a zero suggest experiment. 372 // * The user is not on the NTP. 373 // * The suggest request is sent over HTTPS. This avoids leaking the current 374 // page URL or personal data in unencrypted network traffic. 375 // * The user has suggest enabled in their settings and is not in incognito 376 // mode. (Incognito disables suggest entirely.) 377 // * The user's suggest provider is Google. We might want to allow other 378 // providers to see this data someday, but for now this has only been 379 // implemented for Google. 380 static bool ZeroSuggestEnabled( 381 const GURL& suggest_url, 382 const TemplateURL* template_url, 383 metrics::OmniboxEventProto::PageClassification page_classification, 384 Profile* profile); 385 386 // Returns whether we can send the URL of the current page in any suggest 387 // requests. Doing this requires that all the following hold: 388 // * ZeroSuggestEnabled() is true, so we meet the requirements above. 389 // * The current URL is HTTP, or HTTPS with the same domain as the suggest 390 // server. Non-HTTP[S] URLs (e.g. FTP/file URLs) may contain sensitive 391 // information. HTTPS URLs may also contain sensitive information, but if 392 // they're on the same domain as the suggest server, then the relevant 393 // entity could have already seen/logged this data. 394 // * The user is OK in principle with sending URLs of current pages to their 395 // provider. Today, there is no explicit setting that controls this, but if 396 // the user has tab sync enabled and tab sync is unencrypted, then they're 397 // already sending this data to Google for sync purposes. Thus we use this 398 // setting as a proxy for "it's OK to send such data". In the future, 399 // especially if we want to support suggest providers other than Google, we 400 // may change this to be a standalone setting or part of some explicit 401 // general opt-in. 402 static bool CanSendURL( 403 const GURL& current_page_url, 404 const GURL& suggest_url, 405 const TemplateURL* template_url, 406 metrics::OmniboxEventProto::PageClassification page_classification, 407 Profile* profile); 408 409 // net::URLFetcherDelegate: 410 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 411 412 // If the |deletion_url| is valid, then set |match.deletable| to true and 413 // save the |deletion_url| into the |match|'s additional info under 414 // the key |kDeletionUrlKey|. 415 void SetDeletionURL(const std::string& deletion_url, 416 AutocompleteMatch* match); 417 418 // Creates an AutocompleteMatch from |result| to search for the query in 419 // |result|. Adds the created match to |map|; if such a match 420 // already exists, whichever one has lower relevance is eliminated. 421 // |metadata| and |accepted_suggestion| are used for generating an 422 // AutocompleteMatch. 423 // |mark_as_deletable| indicates whether the match should be marked deletable. 424 // NOTE: Any result containing a deletion URL is always marked deletable. 425 void AddMatchToMap(const SuggestResult& result, 426 const std::string& metadata, 427 int accepted_suggestion, 428 bool mark_as_deletable, 429 MatchMap* map); 430 431 // Parses results from the suggest server and updates the appropriate suggest 432 // and navigation result lists in |results|. |is_keyword_result| indicates 433 // whether the response was received from the keyword provider. 434 // Returns whether the appropriate result list members were updated. 435 bool ParseSuggestResults(const base::Value& root_val, 436 bool is_keyword_result, 437 Results* results); 438 439 // Prefetches any images in Answers results. 440 void PrefetchAnswersImages(const base::DictionaryValue* answers_json); 441 442 // Called at the end of ParseSuggestResults to rank the |results|. 443 virtual void SortResults(bool is_keyword, 444 const base::ListValue* relevances, 445 Results* results); 446 447 // Optionally, cache the received |json_data| and return true if we want 448 // to stop processing results at this point. The |parsed_data| is the parsed 449 // version of |json_data| used to determine if we received an empty result. 450 virtual bool StoreSuggestionResponse(const std::string& json_data, 451 const base::Value& parsed_data); 452 453 // Returns the TemplateURL corresponding to the keyword or default 454 // provider based on the value of |is_keyword|. 455 virtual const TemplateURL* GetTemplateURL(bool is_keyword) const = 0; 456 457 // Returns the AutocompleteInput for keyword provider or default provider 458 // based on the value of |is_keyword|. 459 virtual const AutocompleteInput GetInput(bool is_keyword) const = 0; 460 461 // Returns a pointer to a Results object, which will hold suggest results. 462 virtual Results* GetResultsToFill(bool is_keyword) = 0; 463 464 // Returns whether the destination URL corresponding to the given |result| 465 // should contain command-line-specified query params. 466 virtual bool ShouldAppendExtraParams(const SuggestResult& result) const = 0; 467 468 // Stops the suggest query. 469 // NOTE: This does not update |done_|. Callers must do so. 470 virtual void StopSuggest() = 0; 471 472 // Clears the current results. 473 virtual void ClearAllResults() = 0; 474 475 // Returns the relevance to use if it was not explicitly set by the server. 476 virtual int GetDefaultResultRelevance() const = 0; 477 478 // Records in UMA whether the deletion request resulted in success. 479 virtual void RecordDeletionResult(bool success) = 0; 480 481 // Records UMA statistics about a suggest server response. 482 virtual void LogFetchComplete(bool succeeded, bool is_keyword) = 0; 483 484 // Modify provider-specific UMA statistics. 485 virtual void ModifyProviderInfo( 486 metrics::OmniboxEventProto_ProviderInfo* provider_info) const; 487 488 // Returns whether the |fetcher| is for the keyword provider. 489 virtual bool IsKeywordFetcher(const net::URLFetcher* fetcher) const = 0; 490 491 // Updates |matches_| from the latest results; applies calculated relevances 492 // if suggested relevances cause undesriable behavior. Updates |done_|. 493 virtual void UpdateMatches() = 0; 494 495 // Whether a field trial, if any, has triggered in the most recent 496 // autocomplete query. This field is set to true only if the suggestion 497 // provider has completed and the response contained 498 // '"google:fieldtrialtriggered":true'. 499 bool field_trial_triggered_; 500 501 // Same as above except that it is maintained across the current Omnibox 502 // session. 503 bool field_trial_triggered_in_session_; 504 505 // The number of suggest results that haven't yet arrived. If it's greater 506 // than 0, it indicates that one of the URLFetchers is still running. 507 int suggest_results_pending_; 508 509 private: 510 friend class SearchProviderTest; 511 FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, TestDeleteMatch); 512 513 // Removes the deleted |match| from the list of |matches_|. 514 void DeleteMatchFromMatches(const AutocompleteMatch& match); 515 516 // This gets called when we have requested a suggestion deletion from the 517 // server to handle the results of the deletion. It will be called after the 518 // deletion request completes. 519 void OnDeletionComplete(bool success, 520 SuggestionDeletionHandler* handler); 521 522 // Each deletion handler in this vector corresponds to an outstanding request 523 // that a server delete a personalized suggestion. Making this a ScopedVector 524 // causes us to auto-cancel all such requests on shutdown. 525 SuggestionDeletionHandlers deletion_handlers_; 526 527 // True if this provider's results are being displayed in the app list. By 528 // default this is false, meaning that the results will be shown in the 529 // omnibox. 530 bool in_app_list_; 531 532 DISALLOW_COPY_AND_ASSIGN(BaseSearchProvider); 533 }; 534 535 #endif // CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ 536