| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 #ifndef CHROME_BROWSER_TEMPLATE_URL_H__ | |
| 6 #define CHROME_BROWSER_TEMPLATE_URL_H__ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/time.h" | |
| 12 #include "googleurl/src/gurl.h" | |
| 13 | |
| 14 class TemplateURL; | |
| 15 | |
| 16 // TemplateURL represents the relevant portions of the Open Search Description | |
| 17 // Document (http://www.opensearch.org/Specifications/OpenSearch). | |
| 18 // The main use case for TemplateURL is to use the TemplateURLRef returned by | |
| 19 // suggestions_url or url for keyword/suggestion expansion: | |
| 20 // . suggestions_url describes a URL that is ideal for as you type suggestions. | |
| 21 // The returned results are in the mime type application/x-suggestions+json. | |
| 22 // . url describes a URL that may be used as a shortcut. Returned results are | |
| 23 // are text/html. | |
| 24 // Before using either one, make sure it's non-NULL, and if you intend to use | |
| 25 // it to replace search terms, make sure SupportsReplacement returns true. | |
| 26 // To use either URL invoke the ReplaceSearchTerms method on the corresponding | |
| 27 // TemplateURLRef. | |
| 28 // | |
| 29 // For files parsed from the Web, be sure and invoke IsValid. IsValid returns | |
| 30 // true if the URL could be parsed. | |
| 31 // | |
| 32 // Both TemplateURL and TemplateURLRef have value semantics. This allows the | |
| 33 // UI to create a copy while the user modifies the values. | |
| 34 class TemplateURLRef { | |
| 35 public: | |
| 36 // Magic numbers to pass to ReplaceSearchTerms() for the |accepted_suggestion| | |
| 37 // parameter. Most callers aren't using Suggest capabilities and should just | |
| 38 // pass NO_SUGGESTIONS_AVAILABLE. | |
| 39 // NOTE: Because positive values are meaningful, make sure these are negative! | |
| 40 enum AcceptedSuggestion { | |
| 41 NO_SUGGESTION_CHOSEN = -1, | |
| 42 NO_SUGGESTIONS_AVAILABLE = -2, | |
| 43 }; | |
| 44 | |
| 45 TemplateURLRef(); | |
| 46 | |
| 47 TemplateURLRef(const std::wstring& url, int index_offset, int page_offset) | |
| 48 : url_(url), | |
| 49 index_offset_(index_offset), | |
| 50 page_offset_(page_offset), | |
| 51 parsed_(false), | |
| 52 valid_(false), | |
| 53 supports_replacements_(false) { | |
| 54 } | |
| 55 | |
| 56 // Returns true if this URL supports replacement. | |
| 57 bool SupportsReplacement() const; | |
| 58 | |
| 59 // Returns a string that is the result of replacing the search terms in | |
| 60 // the url with the specified value. | |
| 61 // | |
| 62 // If this TemplateURLRef does not support replacement (SupportsReplacement | |
| 63 // returns false), an empty string is returned. | |
| 64 // | |
| 65 // The TemplateURL is used to determine the input encoding for the term. | |
| 66 GURL ReplaceSearchTerms( | |
| 67 const TemplateURL& host, | |
| 68 const std::wstring& terms, | |
| 69 int accepted_suggestion, | |
| 70 const std::wstring& original_query_for_suggestion) const; | |
| 71 | |
| 72 // Returns the raw URL. None of the parameters will have been replaced. | |
| 73 const std::wstring& url() const { return url_; } | |
| 74 | |
| 75 // Returns the index number of the first search result. | |
| 76 int index_offset() const { return index_offset_; } | |
| 77 | |
| 78 // Returns the page number of the first search results. | |
| 79 int page_offset() const { return page_offset_; } | |
| 80 | |
| 81 // Returns true if the TemplateURLRef is valid. An invalid TemplateURLRef is | |
| 82 // one that contains unknown terms, or invalid characters. | |
| 83 bool IsValid() const; | |
| 84 | |
| 85 // Returns a string representation of this TemplateURLRef suitable for | |
| 86 // display. The display format is the same as the format used by Firefox. | |
| 87 std::wstring DisplayURL() const; | |
| 88 | |
| 89 // Converts a string as returned by DisplayURL back into a string as | |
| 90 // understood by TemplateURLRef. | |
| 91 static std::wstring DisplayURLToURLRef(const std::wstring& display_url); | |
| 92 | |
| 93 // If this TemplateURLRef is valid and contains one search term, this returns | |
| 94 // the host/path of the URL, otherwise this returns an empty string. | |
| 95 const std::string& GetHost() const; | |
| 96 const std::string& GetPath() const; | |
| 97 | |
| 98 // If this TemplateURLRef is valid and contains one search term, this returns | |
| 99 // the key of the search term, otherwise this returns an empty string. | |
| 100 const std::string& GetSearchTermKey() const; | |
| 101 | |
| 102 // Converts the specified term in the encoding of the host TemplateURL to a | |
| 103 // wide string. | |
| 104 std::wstring SearchTermToWide(const TemplateURL& host, | |
| 105 const std::string& term) const; | |
| 106 | |
| 107 // Returns true if this TemplateURLRef has a replacement term of | |
| 108 // {google:baseURL} or {google:baseSuggestURL}. | |
| 109 bool HasGoogleBaseURLs() const; | |
| 110 | |
| 111 private: | |
| 112 friend class TemplateURL; | |
| 113 friend class TemplateURLModelTest; | |
| 114 friend class TemplateURLTest; | |
| 115 | |
| 116 // Enumeration of the known types. | |
| 117 enum ReplacementType { | |
| 118 ENCODING, | |
| 119 GOOGLE_ACCEPTED_SUGGESTION, | |
| 120 GOOGLE_BASE_URL, | |
| 121 GOOGLE_BASE_SUGGEST_URL, | |
| 122 GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION, | |
| 123 GOOGLE_RLZ, | |
| 124 GOOGLE_UNESCAPED_SEARCH_TERMS, | |
| 125 LANGUAGE, | |
| 126 SEARCH_TERMS, | |
| 127 }; | |
| 128 | |
| 129 // Used to identify an element of the raw url that can be replaced. | |
| 130 struct Replacement { | |
| 131 Replacement(ReplacementType type, int index) : type(type), index(index) {} | |
| 132 ReplacementType type; | |
| 133 int index; | |
| 134 }; | |
| 135 | |
| 136 // The list of elements to replace. | |
| 137 typedef std::vector<struct Replacement> Replacements; | |
| 138 | |
| 139 // TemplateURLRef internally caches values to make replacement quick. This | |
| 140 // method invalidates any cached values. | |
| 141 void InvalidateCachedValues() const; | |
| 142 | |
| 143 // Resets the url. | |
| 144 void Set(const std::wstring& url, int index_offset, int page_offset); | |
| 145 | |
| 146 // Parses the parameter in url at the specified offset. start/end specify the | |
| 147 // range of the parameter in the url, including the braces. If the parameter | |
| 148 // is valid, url is updated to reflect the appropriate parameter. If | |
| 149 // the parameter is one of the known parameters an element is added to | |
| 150 // replacements indicating the type and range of the element. | |
| 151 // | |
| 152 // If the parameter is not a known parameter, false is returned. | |
| 153 bool ParseParameter(size_t start, | |
| 154 size_t end, | |
| 155 std::wstring* url, | |
| 156 Replacements* replacements) const; | |
| 157 | |
| 158 // Parses the specified url, replacing parameters as necessary. If | |
| 159 // successful, valid is set to true, and the parsed url is returned. For all | |
| 160 // known parameters that are encountered an entry is added to replacements. | |
| 161 // If there is an error parsing (unknown parameter, or bogus url), valid is | |
| 162 // set to false, and an empty string is returned. | |
| 163 std::wstring ParseURL(const std::wstring& url, | |
| 164 Replacements* replacements, | |
| 165 bool* valid) const; | |
| 166 | |
| 167 // If the url has not yet been parsed, ParseURL is invoked. | |
| 168 // NOTE: While this is const, it modifies parsed_, valid_, parsed_url_ and | |
| 169 // search_offset_. | |
| 170 void ParseIfNecessary() const; | |
| 171 | |
| 172 // Extracts the query key and host from the url. | |
| 173 void ParseHostAndSearchTermKey() const; | |
| 174 | |
| 175 // Returns the value for the GOOGLE_BASE_URL term. | |
| 176 static std::wstring GoogleBaseURLValue(); | |
| 177 | |
| 178 // Returns the value for the GOOGLE_BASE_SUGGEST_URL term. | |
| 179 static std::wstring GoogleBaseSuggestURLValue(); | |
| 180 | |
| 181 // The raw URL. Where as this contains all the terms (such as {searchTerms}), | |
| 182 // parsed_url_ has them all stripped out. | |
| 183 std::wstring url_; | |
| 184 | |
| 185 // indexOffset defined for the Url element. | |
| 186 int index_offset_; | |
| 187 | |
| 188 // searchOffset defined for the Url element. | |
| 189 int page_offset_; | |
| 190 | |
| 191 // Whether the URL has been parsed. | |
| 192 mutable bool parsed_; | |
| 193 | |
| 194 // Whether the url was successfully parsed. | |
| 195 mutable bool valid_; | |
| 196 | |
| 197 // The parsed URL. All terms have been stripped out of this with | |
| 198 // replacements_ giving the index of the terms to replace. | |
| 199 mutable std::wstring parsed_url_; | |
| 200 | |
| 201 // Do we support replacement? | |
| 202 mutable bool supports_replacements_; | |
| 203 | |
| 204 // The replaceable parts of url (parsed_url_). These are ordered by index | |
| 205 // into the string, and may be empty. | |
| 206 mutable Replacements replacements_; | |
| 207 | |
| 208 // Host, path and key of the search term. These are only set if the url | |
| 209 // contains one search term. | |
| 210 mutable std::string host_; | |
| 211 mutable std::string path_; | |
| 212 mutable std::string search_term_key_; | |
| 213 | |
| 214 // For testing. If non-null this is the replacement value for GOOGLE_BASE_URL | |
| 215 // terms. | |
| 216 static std::wstring* google_base_url_; | |
| 217 }; | |
| 218 | |
| 219 // Describes the relevant portions of a single OSD document. | |
| 220 class TemplateURL { | |
| 221 public: | |
| 222 typedef int64 IDType; | |
| 223 | |
| 224 // Describes a single image reference. Each TemplateURL may have | |
| 225 // any number (including 0) of ImageRefs. | |
| 226 // | |
| 227 // If a TemplateURL has no images, the favicon for the generated URL | |
| 228 // should be used. | |
| 229 struct ImageRef { | |
| 230 ImageRef(const std::wstring& type, int width, int height) | |
| 231 : type(type), width(width), height(height) { | |
| 232 } | |
| 233 | |
| 234 ImageRef(const std::wstring& type, int width, int height, const GURL& url) | |
| 235 : type(type), width(width), height(height), url(url) { | |
| 236 } | |
| 237 | |
| 238 // Mime type for the image. | |
| 239 // ICO image will have the format: image/x-icon or image/vnd.microsoft.icon | |
| 240 std::wstring type; | |
| 241 | |
| 242 // Size of the image | |
| 243 int width; | |
| 244 int height; | |
| 245 | |
| 246 // URL of the image. | |
| 247 GURL url; | |
| 248 }; | |
| 249 | |
| 250 // Generates a favicon URL from the specified url. | |
| 251 static GURL GenerateFaviconURL(const GURL& url); | |
| 252 | |
| 253 TemplateURL() | |
| 254 : autogenerate_keyword_(false), | |
| 255 show_in_default_list_(false), | |
| 256 safe_for_autoreplace_(false), | |
| 257 id_(0), | |
| 258 date_created_(base::Time::Now()), | |
| 259 usage_count_(0), | |
| 260 prepopulate_id_(0) {} | |
| 261 ~TemplateURL() {} | |
| 262 | |
| 263 // A short description of the template. This is the name we show to the user | |
| 264 // in various places that use keywords. For example, the location bar shows | |
| 265 // this when the user selects the keyword. | |
| 266 void set_short_name(const std::wstring& short_name) { | |
| 267 short_name_ = short_name; | |
| 268 } | |
| 269 const std::wstring& short_name() const { return short_name_; } | |
| 270 | |
| 271 // A description of the template; this may be empty. | |
| 272 void set_description(const std::wstring& description) { | |
| 273 description_ = description; | |
| 274 } | |
| 275 const std::wstring& description() const { return description_; } | |
| 276 | |
| 277 // URL providing JSON results. This is typically used to provide suggestions | |
| 278 // as your type. If NULL, this url does not support suggestions. | |
| 279 // Be sure and check the resulting TemplateURLRef for SupportsReplacement | |
| 280 // before using. | |
| 281 void SetSuggestionsURL(const std::wstring& suggestions_url, | |
| 282 int index_offset, | |
| 283 int page_offset); | |
| 284 const TemplateURLRef* suggestions_url() const { | |
| 285 if (suggestions_url_.url().empty()) | |
| 286 return NULL; | |
| 287 return &suggestions_url_; | |
| 288 } | |
| 289 | |
| 290 // Parameterized URL for providing the results. This may be NULL. | |
| 291 // Be sure and check the resulting TemplateURLRef for SupportsReplacement | |
| 292 // before using. | |
| 293 void SetURL(const std::wstring& url, int index_offset, int page_offset); | |
| 294 // Returns the TemplateURLRef that may be used for search results. This | |
| 295 // returns NULL if a url element was not specified. | |
| 296 const TemplateURLRef* url() const { | |
| 297 if (url_.url().empty()) | |
| 298 return NULL; | |
| 299 return &url_; | |
| 300 } | |
| 301 | |
| 302 // URL to the OSD file this came from. May be empty. | |
| 303 void set_originating_url(const GURL& url) { | |
| 304 originating_url_ = url; | |
| 305 } | |
| 306 const GURL& originating_url() const { return originating_url_; } | |
| 307 | |
| 308 // The shortcut for this template url. May be empty. | |
| 309 void set_keyword(const std::wstring& keyword); | |
| 310 const std::wstring& keyword() const; | |
| 311 | |
| 312 // Whether to autogenerate a keyword from the url() in GetKeyword(). Most | |
| 313 // consumers should not need this. | |
| 314 // NOTE: Calling set_keyword() turns this back off. Manual and automatic | |
| 315 // keywords are mutually exclusive. | |
| 316 void set_autogenerate_keyword(bool autogenerate_keyword) { | |
| 317 autogenerate_keyword_ = autogenerate_keyword; | |
| 318 if (autogenerate_keyword_) | |
| 319 keyword_.clear(); | |
| 320 } | |
| 321 bool autogenerate_keyword() const { | |
| 322 return autogenerate_keyword_; | |
| 323 } | |
| 324 | |
| 325 // Whether this keyword is shown in the default list of search providers. This | |
| 326 // is just a property and does not indicate whether this TemplateURL has | |
| 327 // a TemplateURLRef that supports replacement. Use ShowInDefaultList to | |
| 328 // test both. | |
| 329 // The default value is false. | |
| 330 void set_show_in_default_list(bool show_in_default_list) { | |
| 331 show_in_default_list_ = show_in_default_list; | |
| 332 } | |
| 333 bool show_in_default_list() const { return show_in_default_list_; } | |
| 334 | |
| 335 // Returns true if show_in_default_list() is true and this TemplateURL has a | |
| 336 // TemplateURLRef that supports replacement. | |
| 337 bool ShowInDefaultList() const; | |
| 338 | |
| 339 // Whether it's safe for auto-modification code (the autogenerator and the | |
| 340 // code that imports data from other browsers) to replace the TemplateURL. | |
| 341 // This should be set to false for any keyword the user edits, or any keyword | |
| 342 // that the user clearly manually edited in the past, like a bookmark keyword | |
| 343 // from another browser. | |
| 344 void set_safe_for_autoreplace(bool safe_for_autoreplace) { | |
| 345 safe_for_autoreplace_ = safe_for_autoreplace; | |
| 346 } | |
| 347 bool safe_for_autoreplace() const { return safe_for_autoreplace_; } | |
| 348 | |
| 349 // Images for this URL. May be empty. | |
| 350 void add_image_ref(const ImageRef& ref) { image_refs_.push_back(ref); } | |
| 351 const std::vector<ImageRef>& image_refs() const { return image_refs_; } | |
| 352 | |
| 353 // Convenience methods for getting/setting an ImageRef that points to a | |
| 354 // favicon. A TemplateURL need not have an ImageRef for a favicon. In such | |
| 355 // a situation GetFavIconURL returns an invalid url. | |
| 356 // | |
| 357 // If url is empty and there is an image ref for a favicon, it is removed. | |
| 358 void SetFavIconURL(const GURL& url); | |
| 359 GURL GetFavIconURL() const; | |
| 360 | |
| 361 // Set of languages supported. This may be empty. | |
| 362 void add_language(const std::wstring& language) { | |
| 363 languages_.push_back(language); | |
| 364 } | |
| 365 const std::vector<std::wstring>& languages() const { return languages_; } | |
| 366 | |
| 367 // Date this keyword was created. | |
| 368 // | |
| 369 // NOTE: this may be 0, which indicates the keyword was created before we | |
| 370 // started tracking creation time. | |
| 371 void set_date_created(base::Time time) { date_created_ = time; } | |
| 372 base::Time date_created() const { return date_created_; } | |
| 373 | |
| 374 // Number of times this keyword has been explicitly used to load a URL. We | |
| 375 // don't increment this for uses as the "default search engine" since that's | |
| 376 // not really "explicit" usage and incrementing would result in pinning the | |
| 377 // user's default search engine(s) to the top of the list of searches on the | |
| 378 // New Tab page, de-emphasizing the omnibox as "where you go to search". | |
| 379 void set_usage_count(int count) { usage_count_ = count; } | |
| 380 int usage_count() const { return usage_count_; } | |
| 381 | |
| 382 // The list of supported encodings for the search terms. This may be empty, | |
| 383 // which indicates the terms should be encoded with UTF-8. | |
| 384 void set_input_encodings(const std::vector<std::string>& encodings) { | |
| 385 input_encodings_ = encodings; | |
| 386 } | |
| 387 void add_input_encoding(const std::string& encoding) { | |
| 388 input_encodings_.push_back(encoding); | |
| 389 } | |
| 390 const std::vector<std::string>& input_encodings() const { | |
| 391 return input_encodings_; | |
| 392 } | |
| 393 | |
| 394 // Returns the unique identifier of this TemplateURL. The unique ID is set | |
| 395 // by the TemplateURLModel when the TemplateURL is added to it. | |
| 396 IDType id() const { return id_; } | |
| 397 | |
| 398 // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0. | |
| 399 void set_prepopulate_id(int id) { prepopulate_id_ = id; } | |
| 400 int prepopulate_id() const { return prepopulate_id_; } | |
| 401 | |
| 402 private: | |
| 403 friend class WebDatabaseTest; | |
| 404 friend class WebDatabase; | |
| 405 friend class TemplateURLModel; | |
| 406 | |
| 407 // Invalidates cached values on this object and its child TemplateURLRefs. | |
| 408 void InvalidateCachedValues() const; | |
| 409 | |
| 410 // Unique identifier, used when archived to the database. | |
| 411 void set_id(IDType id) { id_ = id;} | |
| 412 | |
| 413 std::wstring short_name_; | |
| 414 std::wstring description_; | |
| 415 TemplateURLRef suggestions_url_; | |
| 416 TemplateURLRef url_; | |
| 417 GURL originating_url_; | |
| 418 mutable std::wstring keyword_; | |
| 419 bool autogenerate_keyword_; // If this is set, |keyword_| holds the cached | |
| 420 // generated keyword if available. | |
| 421 bool show_in_default_list_; | |
| 422 bool safe_for_autoreplace_; | |
| 423 std::vector<ImageRef> image_refs_; | |
| 424 std::vector<std::wstring> languages_; | |
| 425 // List of supported input encodings. | |
| 426 std::vector<std::string> input_encodings_; | |
| 427 IDType id_; | |
| 428 base::Time date_created_; | |
| 429 int usage_count_; | |
| 430 int prepopulate_id_; | |
| 431 | |
| 432 // TODO(sky): Add date last parsed OSD file. | |
| 433 }; | |
| 434 | |
| 435 #endif // CHROME_BROWSER_TEMPLATE_URL_PARSER_H__ | |
| 436 | |
| OLD | NEW |