OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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/strings/string16.h" |
| 18 #include "chrome/browser/autocomplete/autocomplete_input.h" |
| 19 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 20 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 21 #include "net/url_request/url_fetcher_delegate.h" |
| 22 |
| 23 class AutocompleteProviderListener; |
| 24 class GURL; |
| 25 class Profile; |
| 26 |
| 27 // Base functionality for receiving suggestions from a search engine. |
| 28 // This class is abstract and should only be used as a base for other |
| 29 // autocomplete providers utilizing its functionality. |
| 30 class BaseSearchProvider : public AutocompleteProvider, |
| 31 public net::URLFetcherDelegate { |
| 32 public: |
| 33 BaseSearchProvider(AutocompleteProviderListener* listener, |
| 34 Profile* profile, |
| 35 AutocompleteProvider::Type type); |
| 36 |
| 37 protected: |
| 38 virtual ~BaseSearchProvider(); |
| 39 |
| 40 // The Result classes are intermediate representations of AutocompleteMatches, |
| 41 // simply containing relevance-ranked search and navigation suggestions. |
| 42 // They may be cached to provide some synchronous matches while requests for |
| 43 // new suggestions from updated input are in flight. |
| 44 // TODO(msw) Extend these classes to generate their corresponding matches and |
| 45 // other requisite data, in order to consolidate and simplify the |
| 46 // highly fragmented SearchProvider logic for each Result type. |
| 47 class Result { |
| 48 public: |
| 49 Result(bool from_keyword_provider, |
| 50 int relevance, |
| 51 bool relevance_from_server); |
| 52 virtual ~Result(); |
| 53 |
| 54 bool from_keyword_provider() const { return from_keyword_provider_; } |
| 55 |
| 56 const base::string16& match_contents() const { return match_contents_; } |
| 57 const ACMatchClassifications& match_contents_class() const { |
| 58 return match_contents_class_; |
| 59 } |
| 60 |
| 61 int relevance() const { return relevance_; } |
| 62 void set_relevance(int relevance) { relevance_ = relevance; } |
| 63 |
| 64 bool relevance_from_server() const { return relevance_from_server_; } |
| 65 void set_relevance_from_server(bool relevance_from_server) { |
| 66 relevance_from_server_ = relevance_from_server; |
| 67 } |
| 68 |
| 69 // Returns if this result is inlineable against the current input |input|. |
| 70 // Non-inlineable results are stale. |
| 71 virtual bool IsInlineable(const base::string16& input) const = 0; |
| 72 |
| 73 // Returns the default relevance value for this result (which may |
| 74 // be left over from a previous omnibox input) given the current |
| 75 // input and whether the current input caused a keyword provider |
| 76 // to be active. |
| 77 virtual int CalculateRelevance(const AutocompleteInput& input, |
| 78 bool keyword_provider_requested) const = 0; |
| 79 |
| 80 protected: |
| 81 // The contents to be displayed and its style info. |
| 82 base::string16 match_contents_; |
| 83 ACMatchClassifications match_contents_class_; |
| 84 |
| 85 // True if the result came from the keyword provider. |
| 86 bool from_keyword_provider_; |
| 87 |
| 88 // The relevance score. |
| 89 int relevance_; |
| 90 |
| 91 private: |
| 92 // Whether this result's relevance score was fully or partly calculated |
| 93 // based on server information, and thus is assumed to be more accurate. |
| 94 // This is ultimately used in |
| 95 // SearchProvider::ConvertResultsToAutocompleteMatches(), see comments |
| 96 // there. |
| 97 bool relevance_from_server_; |
| 98 }; |
| 99 |
| 100 class SuggestResult : public Result { |
| 101 public: |
| 102 SuggestResult(const base::string16& suggestion, |
| 103 AutocompleteMatchType::Type type, |
| 104 const base::string16& match_contents, |
| 105 const base::string16& annotation, |
| 106 const std::string& suggest_query_params, |
| 107 const std::string& deletion_url, |
| 108 bool from_keyword_provider, |
| 109 int relevance, |
| 110 bool relevance_from_server, |
| 111 bool should_prefetch, |
| 112 const base::string16& input_text); |
| 113 virtual ~SuggestResult(); |
| 114 |
| 115 const base::string16& suggestion() const { return suggestion_; } |
| 116 AutocompleteMatchType::Type type() const { return type_; } |
| 117 const base::string16& annotation() const { return annotation_; } |
| 118 const std::string& suggest_query_params() const { |
| 119 return suggest_query_params_; |
| 120 } |
| 121 const std::string& deletion_url() const { return deletion_url_; } |
| 122 bool should_prefetch() const { return should_prefetch_; } |
| 123 |
| 124 // Fills in |match_contents_class_| to reflect how |match_contents_| should |
| 125 // be displayed and bolded against the current |input_text|. If |
| 126 // |allow_bolding_all| is false and |match_contents_class_| would have all |
| 127 // of |match_contents_| bolded, do nothing. |
| 128 void ClassifyMatchContents(const bool allow_bolding_all, |
| 129 const base::string16& input_text); |
| 130 |
| 131 // Result: |
| 132 virtual bool IsInlineable(const base::string16& input) const OVERRIDE; |
| 133 virtual int CalculateRelevance( |
| 134 const AutocompleteInput& input, |
| 135 bool keyword_provider_requested) const OVERRIDE; |
| 136 |
| 137 private: |
| 138 // The search terms to be used for this suggestion. |
| 139 base::string16 suggestion_; |
| 140 |
| 141 AutocompleteMatchType::Type type_; |
| 142 |
| 143 // Optional annotation for the |match_contents_| for disambiguation. |
| 144 // This may be displayed in the autocomplete match contents, but is defined |
| 145 // separately to facilitate different formatting. |
| 146 base::string16 annotation_; |
| 147 |
| 148 // Optional additional parameters to be added to the search URL. |
| 149 std::string suggest_query_params_; |
| 150 |
| 151 // Optional deletion URL provided with suggestions. Fetching this URL |
| 152 // should result in some reasonable deletion behaviour on the server, |
| 153 // e.g. deleting this term out of a user's server-side search history. |
| 154 std::string deletion_url_; |
| 155 |
| 156 // Should this result be prefetched? |
| 157 bool should_prefetch_; |
| 158 }; |
| 159 |
| 160 class NavigationResult : public Result { |
| 161 public: |
| 162 // |provider| is necessary to use StringForURLDisplay() in order to |
| 163 // compute |formatted_url_|. |
| 164 NavigationResult(const AutocompleteProvider& provider, |
| 165 const GURL& url, |
| 166 const base::string16& description, |
| 167 bool from_keyword_provider, |
| 168 int relevance, |
| 169 bool relevance_from_server, |
| 170 const base::string16& input_text, |
| 171 const std::string& languages); |
| 172 virtual ~NavigationResult(); |
| 173 |
| 174 const GURL& url() const { return url_; } |
| 175 const base::string16& description() const { return description_; } |
| 176 const base::string16& formatted_url() const { return formatted_url_; } |
| 177 |
| 178 // Fills in |match_contents_| and |match_contents_class_| to reflect how |
| 179 // the URL should be displayed and bolded against the current |input_text| |
| 180 // and user |languages|. If |allow_bolding_nothing| is false and |
| 181 // |match_contents_class_| would result in an entirely unbolded |
| 182 // |match_contents_|, do nothing. |
| 183 void CalculateAndClassifyMatchContents(const bool allow_bolding_nothing, |
| 184 const base::string16& input_text, |
| 185 const std::string& languages); |
| 186 |
| 187 // Result: |
| 188 virtual bool IsInlineable(const base::string16& input) const OVERRIDE; |
| 189 virtual int CalculateRelevance( |
| 190 const AutocompleteInput& input, |
| 191 bool keyword_provider_requested) const OVERRIDE; |
| 192 |
| 193 private: |
| 194 // The suggested url for navigation. |
| 195 GURL url_; |
| 196 |
| 197 // The properly formatted ("fixed up") URL string with equivalent meaning |
| 198 // to the one in |url_|. |
| 199 base::string16 formatted_url_; |
| 200 |
| 201 // The suggested navigational result description; generally the site name. |
| 202 base::string16 description_; |
| 203 }; |
| 204 |
| 205 typedef std::vector<SuggestResult> SuggestResults; |
| 206 typedef std::vector<NavigationResult> NavigationResults; |
| 207 typedef std::pair<base::string16, std::string> MatchKey; |
| 208 typedef std::map<MatchKey, AutocompleteMatch> MatchMap; |
| 209 |
| 210 // A simple structure bundling most of the information (including |
| 211 // both SuggestResults and NavigationResults) returned by a call to |
| 212 // the suggest server. |
| 213 // |
| 214 // This has to be declared after the typedefs since it relies on some of them. |
| 215 struct Results { |
| 216 Results(); |
| 217 ~Results(); |
| 218 |
| 219 // Clears |suggest_results| and |navigation_results| and resets |
| 220 // |verbatim_relevance| to -1 (implies unset). |
| 221 void Clear(); |
| 222 |
| 223 // Returns whether any of the results (including verbatim) have |
| 224 // server-provided scores. |
| 225 bool HasServerProvidedScores() const; |
| 226 |
| 227 // Query suggestions sorted by relevance score. |
| 228 SuggestResults suggest_results; |
| 229 |
| 230 // Navigational suggestions sorted by relevance score. |
| 231 NavigationResults navigation_results; |
| 232 |
| 233 // The server supplied verbatim relevance scores. Negative values |
| 234 // indicate that there is no suggested score; a value of 0 |
| 235 // suppresses the verbatim result. |
| 236 int verbatim_relevance; |
| 237 |
| 238 // The JSON metadata associated with this server response. |
| 239 std::string metadata; |
| 240 |
| 241 private: |
| 242 DISALLOW_COPY_AND_ASSIGN(Results); |
| 243 }; |
| 244 |
| 245 private: |
| 246 DISALLOW_COPY_AND_ASSIGN(BaseSearchProvider); |
| 247 }; |
| 248 |
| 249 #endif // CHROME_BROWSER_AUTOCOMPLETE_BASE_SEARCH_PROVIDER_H_ |
OLD | NEW |