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