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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_
6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "chrome/browser/autocomplete/autocomplete_input.h"
16 #include "chrome/browser/search_engines/template_url_id.h"
17 #include "ui/gfx/size.h"
19 #include "url/url_parse.h"
22 class SearchTermsData;
26 // TemplateURLRef -------------------------------------------------------------
28 // A TemplateURLRef represents a single URL within the larger TemplateURL class
29 // (which represents an entire "search engine", see below). If
30 // SupportsReplacement() is true, this URL has placeholders in it, for which
31 // callers can substitute values to get a "real" URL using ReplaceSearchTerms().
33 // TemplateURLRefs always have a non-NULL |owner_| TemplateURL, which they
34 // access in order to get at important data like the underlying URL string or
35 // the associated Profile.
36 class TemplateURLRef {
38 // Magic numbers to pass to ReplaceSearchTerms() for the |accepted_suggestion|
39 // parameter. Most callers aren't using Suggest capabilities and should just
40 // pass NO_SUGGESTIONS_AVAILABLE.
41 // NOTE: Because positive values are meaningful, make sure these are negative!
42 enum AcceptedSuggestion {
43 NO_SUGGESTION_CHOSEN = -1,
44 NO_SUGGESTIONS_AVAILABLE = -2,
47 // Which kind of URL within our owner we are. This allows us to get at the
48 // correct string field. Use |INDEXED| to indicate that the numerical
49 // |index_in_owner_| should be used instead.
59 // Type to store <content_type, post_data> pair for POST URLs.
60 // The |content_type|(first part of the pair) is the content-type of
61 // the |post_data|(second part of the pair) which is encoded in
62 // "multipart/form-data" format, it also contains the MIME boundary used in
63 // the |post_data|. See http://tools.ietf.org/html/rfc2046 for the details.
64 typedef std::pair<std::string, std::string> PostContent;
66 // This struct encapsulates arguments passed to
67 // TemplateURLRef::ReplaceSearchTerms methods. By default, only search_terms
68 // is required and is passed in the constructor.
69 struct SearchTermsArgs {
70 explicit SearchTermsArgs(const base::string16& search_terms);
73 // The search terms (query).
74 base::string16 search_terms;
76 // The original (input) query.
77 base::string16 original_query;
79 // The optional assisted query stats, aka AQS, used for logging purposes.
80 // This string contains impressions of all autocomplete matches shown
81 // at the query submission time. For privacy reasons, we require the
82 // search provider to support HTTPS protocol in order to receive the AQS
84 // For more details, see http://goto.google.com/binary-clients-logging .
85 std::string assisted_query_stats;
87 // TODO: Remove along with "aq" CGI param.
88 int accepted_suggestion;
90 // The 0-based position of the cursor within the query string at the time
91 // the request was issued. Set to base::string16::npos if not used.
92 size_t cursor_position;
94 // The start-edge margin of the omnibox in pixels, used in extended Instant
95 // to align the preview contents with the omnibox.
96 int omnibox_start_margin;
98 // The URL of the current webpage to be used for experimental zero-prefix
100 std::string current_page_url;
102 // Which omnibox the user used to type the prefix.
103 AutocompleteInput::PageClassification page_classification;
105 // True for searches issued with the bookmark bar pref set to shown.
106 bool bookmark_bar_pinned;
108 // Additional query params provided by the suggest server.
109 std::string suggest_query_params;
111 // If set, ReplaceSearchTerms() will automatically append any extra query
112 // params specified via the --extra-search-query-params command-line
113 // argument. Generally, this should be set when dealing with the search or
114 // instant TemplateURLRefs of the default search engine and the caller cares
115 // about the query portion of the URL. Since neither TemplateURLRef nor
116 // indeed TemplateURL know whether a TemplateURL is the default search
117 // engine, callers instead must set this manually.
118 bool append_extra_query_params;
120 // The raw content of an image thumbnail that will be used as a query for
121 // search-by-image frontend.
122 std::string image_thumbnail_content;
124 // When searching for an image, the URL of the original image. Callers
125 // should leave this empty for images specified via data: URLs.
128 // When searching for an image, the original size of the image.
129 gfx::Size image_original_size;
131 // If set, ReplaceSearchTerms() will append a param to the TemplateURLRef to
132 // update the search results page incrementally even if that is otherwise
133 // disabled by google.com preferences. See comments on
134 // chrome::ForceInstantResultsParam().
135 bool force_instant_results;
137 // True if the search was made using the app list search box. Otherwise, the
138 // search was made using the omnibox.
142 TemplateURLRef(TemplateURL* owner, Type type);
143 TemplateURLRef(TemplateURL* owner, size_t index_in_owner);
146 // Returns the raw URL. None of the parameters will have been replaced.
147 std::string GetURL() const;
149 // Returns the raw string of the post params. Please see comments in
150 // prepopulated_engines_schema.json for the format.
151 std::string GetPostParamsString() const;
153 // Returns true if this URL supports search term replacement.
154 bool SupportsReplacement() const;
156 // Like SupportsReplacement but usable on threads other than the UI thread.
157 bool SupportsReplacementUsingTermsData(
158 const SearchTermsData& search_terms_data) const;
160 // Returns a string that is the result of replacing the search terms in
161 // the url with the specified arguments. We use our owner's input encoding.
163 // If this TemplateURLRef does not support replacement (SupportsReplacement
164 // returns false), an empty string is returned.
165 // If this TemplateURLRef uses POST, and |post_content| is not NULL, the
166 // |post_params_| will be replaced, encoded in "multipart/form-data" format
167 // and stored into |post_content|.
168 std::string ReplaceSearchTerms(
169 const SearchTermsArgs& search_terms_args,
170 PostContent* post_content) const;
171 // TODO(jnd): remove the following ReplaceSearchTerms definition which does
172 // not have |post_content| parameter once all reference callers pass
173 // |post_content| parameter.
174 std::string ReplaceSearchTerms(
175 const SearchTermsArgs& search_terms_args) const {
176 return ReplaceSearchTerms(search_terms_args, NULL);
179 // Just like ReplaceSearchTerms except that it takes SearchTermsData to supply
180 // the data for some search terms. Most of the time ReplaceSearchTerms should
182 std::string ReplaceSearchTermsUsingTermsData(
183 const SearchTermsArgs& search_terms_args,
184 const SearchTermsData& search_terms_data,
185 PostContent* post_content) const;
187 // Returns true if the TemplateURLRef is valid. An invalid TemplateURLRef is
188 // one that contains unknown terms, or invalid characters.
189 bool IsValid() const;
191 // Like IsValid but usable on threads other than the UI thread.
192 bool IsValidUsingTermsData(const SearchTermsData& search_terms_data) const;
194 // Returns a string representation of this TemplateURLRef suitable for
195 // display. The display format is the same as the format used by Firefox.
196 base::string16 DisplayURL() const;
198 // Converts a string as returned by DisplayURL back into a string as
199 // understood by TemplateURLRef.
200 static std::string DisplayURLToURLRef(const base::string16& display_url);
202 // If this TemplateURLRef is valid and contains one search term, this returns
203 // the host/path of the URL, otherwise this returns an empty string.
204 const std::string& GetHost() const;
205 const std::string& GetPath() const;
207 // If this TemplateURLRef is valid and contains one search term, this returns
208 // the key of the search term, otherwise this returns an empty string.
209 const std::string& GetSearchTermKey() const;
211 // Converts the specified term in our owner's encoding to a base::string16.
212 base::string16 SearchTermToString16(const std::string& term) const;
214 // Returns true if this TemplateURLRef has a replacement term of
215 // {google:baseURL} or {google:baseSuggestURL}.
216 bool HasGoogleBaseURLs() const;
218 // Use the pattern referred to by this TemplateURLRef to match the provided
219 // |url| and extract |search_terms| from it. Returns true if the pattern
220 // matches, even if |search_terms| is empty. In this case
221 // |search_term_component|, if not NULL, indicates whether the search terms
222 // were found in the query or the ref parameters; and |search_terms_position|,
223 // if not NULL, contains the position of the search terms in the query or the
224 // ref parameters. Returns false and an empty |search_terms| if the pattern
226 bool ExtractSearchTermsFromURL(
228 base::string16* search_terms,
229 const SearchTermsData& search_terms_data,
230 url::Parsed::ComponentType* search_term_component,
231 url::Component* search_terms_position) const;
233 // Whether the URL uses POST (as opposed to GET).
234 bool UsesPOSTMethodUsingTermsData(
235 const SearchTermsData* search_terms_data) const;
236 bool UsesPOSTMethod() const {
237 return UsesPOSTMethodUsingTermsData(NULL);
241 friend class TemplateURL;
242 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, SetPrepopulatedAndParse);
243 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterKnown);
244 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterUnknown);
245 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLEmpty);
246 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNoTemplateEnd);
247 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNoKnownParameters);
248 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLTwoParameters);
249 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNestedParameter);
250 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, URLRefTestImageURLWithPOST);
251 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ReflectsBookmarkBarPinned);
253 // Enumeration of the known types.
254 enum ReplacementType {
256 GOOGLE_ASSISTED_QUERY_STATS,
258 GOOGLE_BASE_SUGGEST_URL,
259 GOOGLE_BOOKMARK_BAR_PINNED,
260 GOOGLE_CURRENT_PAGE_URL,
261 GOOGLE_CURSOR_POSITION,
262 GOOGLE_IMAGE_ORIGINAL_HEIGHT,
263 GOOGLE_IMAGE_ORIGINAL_WIDTH,
264 GOOGLE_IMAGE_SEARCH_SOURCE,
265 GOOGLE_IMAGE_THUMBNAIL,
267 GOOGLE_FORCE_INSTANT_RESULTS,
268 GOOGLE_INSTANT_EXTENDED_ENABLED,
269 GOOGLE_NTP_IS_THEMED,
270 GOOGLE_OMNIBOX_START_MARGIN,
271 GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION,
272 GOOGLE_PAGE_CLASSIFICATION,
274 GOOGLE_SEARCH_CLIENT,
275 GOOGLE_SEARCH_FIELDTRIAL_GROUP,
276 GOOGLE_SUGGEST_CLIENT,
277 GOOGLE_SUGGEST_REQUEST_ID,
278 GOOGLE_UNESCAPED_SEARCH_TERMS,
283 // Used to identify an element of the raw url that can be replaced.
285 Replacement(ReplacementType type, size_t index)
286 : type(type), index(index), is_post_param(false) {}
287 ReplacementType type;
289 // Indicates the location in where the replacement is replaced. If
290 // |is_post_param| is false, |index| indicates the byte position in
291 // |parsed_url_|. Otherwise, |index| is the index of |post_params_|.
295 // The list of elements to replace.
296 typedef std::vector<struct Replacement> Replacements;
297 // Type to store <key, value> pairs for POST URLs.
298 typedef std::pair<std::string, std::string> PostParam;
299 typedef std::vector<PostParam> PostParams;
301 // TemplateURLRef internally caches values to make replacement quick. This
302 // method invalidates any cached values.
303 void InvalidateCachedValues() const;
305 // Parses the parameter in url at the specified offset. start/end specify the
306 // range of the parameter in the url, including the braces. If the parameter
307 // is valid, url is updated to reflect the appropriate parameter. If
308 // the parameter is one of the known parameters an element is added to
309 // replacements indicating the type and range of the element. The original
310 // parameter is erased from the url.
312 // If the parameter is not a known parameter, false is returned. If this is a
313 // prepopulated URL, the parameter is erased, otherwise it is left alone.
314 bool ParseParameter(size_t start,
317 Replacements* replacements) const;
319 // Parses the specified url, replacing parameters as necessary. If
320 // successful, valid is set to true, and the parsed url is returned. For all
321 // known parameters that are encountered an entry is added to replacements.
322 // If there is an error parsing the url, valid is set to false, and an empty
323 // string is returned. If the URL has the POST parameters, they will be
324 // parsed into |post_params| which will be further replaced with real search
325 // terms data and encoded in "multipart/form-data" format to generate the
327 std::string ParseURL(const std::string& url,
328 Replacements* replacements,
329 PostParams* post_params,
332 // If the url has not yet been parsed, ParseURL is invoked.
333 // NOTE: While this is const, it modifies parsed_, valid_, parsed_url_ and
335 void ParseIfNecessary() const;
337 // Like ParseIfNecessary but usable on threads other than the UI thread.
338 void ParseIfNecessaryUsingTermsData(
339 const SearchTermsData& search_terms_data) const;
341 // Extracts the query key and host from the url.
342 void ParseHostAndSearchTermKey(
343 const SearchTermsData& search_terms_data) const;
345 // Encode post parameters in "multipart/form-data" format and store it
346 // inside |post_content|. Returns false if errors are encountered during
347 // encoding. This method is called each time ReplaceSearchTerms gets called.
348 bool EncodeFormData(const PostParams& post_params,
349 PostContent* post_content) const;
351 // Handles a replacement by using real term data. If the replacement
352 // belongs to a PostParam, the PostParam will be replaced by the term data.
353 // Otherwise, the term data will be inserted at the place that the
354 // replacement points to.
355 void HandleReplacement(const std::string& name,
356 const std::string& value,
357 const Replacement& replacement,
358 std::string* url) const;
360 // Replaces all replacements in |parsed_url_| with their actual values and
361 // returns the result. This is the main functionality of
362 // ReplaceSearchTermsUsingTermsData().
363 std::string HandleReplacements(
364 const SearchTermsArgs& search_terms_args,
365 const SearchTermsData& search_terms_data,
366 PostContent* post_content) const;
368 // The TemplateURL that contains us. This should outlive us.
369 TemplateURL* const owner_;
371 // What kind of URL we are.
374 // If |type_| is |INDEXED|, this |index_in_owner_| is used instead to refer to
375 // a url within our owner.
376 const size_t index_in_owner_;
378 // Whether the URL has been parsed.
379 mutable bool parsed_;
381 // Whether the url was successfully parsed.
384 // The parsed URL. All terms have been stripped out of this with
385 // replacements_ giving the index of the terms to replace.
386 mutable std::string parsed_url_;
388 // Do we support search term replacement?
389 mutable bool supports_replacements_;
391 // The replaceable parts of url (parsed_url_). These are ordered by index
392 // into the string, and may be empty.
393 mutable Replacements replacements_;
395 // Host, path, key and location of the search term. These are only set if the
396 // url contains one search term.
397 mutable std::string host_;
398 mutable std::string path_;
399 mutable std::string search_term_key_;
400 mutable url::Parsed::ComponentType search_term_key_location_;
402 mutable PostParams post_params_;
404 // Whether the contained URL is a pre-populated URL.
407 // Whether search terms are shown in the omnibox on search results pages.
408 // This is kept as a member so it can be overridden by tests.
409 bool showing_search_terms_;
411 DISALLOW_COPY_AND_ASSIGN(TemplateURLRef);
415 // TemplateURLData ------------------------------------------------------------
417 // The data for the TemplateURL. Separating this into its own class allows most
418 // users to do SSA-style usage of TemplateURL: construct a TemplateURLData with
419 // whatever fields are desired, then create an immutable TemplateURL from it.
420 struct TemplateURLData {
424 // A short description of the template. This is the name we show to the user
425 // in various places that use TemplateURLs. For example, the location bar
426 // shows this when the user selects a substituting match.
427 base::string16 short_name;
429 // The shortcut for this TemplateURL. |keyword| must be non-empty.
430 void SetKeyword(const base::string16& keyword);
431 const base::string16& keyword() const { return keyword_; }
433 // The raw URL for the TemplateURL, which may not be valid as-is (e.g. because
434 // it requires substitutions first). This must be non-empty.
435 void SetURL(const std::string& url);
436 const std::string& url() const { return url_; }
438 // Optional additional raw URLs.
439 std::string suggestions_url;
440 std::string instant_url;
441 std::string image_url;
442 std::string new_tab_url;
444 // The following post_params are comma-separated lists used to specify the
445 // post parameters for the corresponding URL.
446 std::string search_url_post_params;
447 std::string suggestions_url_post_params;
448 std::string instant_url_post_params;
449 std::string image_url_post_params;
451 // Optional favicon for the TemplateURL.
454 // URL to the OSD file this came from. May be empty.
455 GURL originating_url;
457 // Whether this TemplateURL is shown in the default list of search providers.
458 // This is just a property and does not indicate whether the TemplateURL has a
459 // TemplateURLRef that supports replacement. Use
460 // TemplateURL::ShowInDefaultList() to test both.
461 bool show_in_default_list;
463 // Whether it's safe for auto-modification code (the autogenerator and the
464 // code that imports data from other browsers) to replace the TemplateURL.
465 // This should be set to false for any TemplateURL the user edits, or any
466 // TemplateURL that the user clearly manually edited in the past, like a
467 // bookmark keyword from another browser.
468 bool safe_for_autoreplace;
470 // The list of supported encodings for the search terms. This may be empty,
471 // which indicates the terms should be encoded with UTF-8.
472 std::vector<std::string> input_encodings;
474 // Unique identifier of this TemplateURL. The unique ID is set by the
475 // TemplateURLService when the TemplateURL is added to it.
478 // Date this TemplateURL was created.
480 // NOTE: this may be 0, which indicates the TemplateURL was created before we
481 // started tracking creation time.
482 base::Time date_created;
484 // The last time this TemplateURL was modified by a user, since creation.
486 // NOTE: Like date_created above, this may be 0.
487 base::Time last_modified;
489 // True if this TemplateURL was automatically created by the administrator via
491 bool created_by_policy;
493 // Number of times this TemplateURL has been explicitly used to load a URL.
494 // We don't increment this for uses as the "default search engine" since
495 // that's not really "explicit" usage and incrementing would result in pinning
496 // the user's default search engine(s) to the top of the list of searches on
497 // the New Tab page, de-emphasizing the omnibox as "where you go to search".
500 // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0.
503 // The primary unique identifier for Sync. This set on all TemplateURLs
504 // regardless of whether they have been associated with Sync.
505 std::string sync_guid;
507 // A list of URL patterns that can be used, in addition to |url_|, to extract
508 // search terms from a URL.
509 std::vector<std::string> alternate_urls;
511 // A parameter that, if present in the query or ref parameters of a search_url
512 // or instant_url, causes Chrome to replace the URL with the search term.
513 std::string search_terms_replacement_key;
516 // Private so we can enforce using the setters and thus enforce that these
517 // fields are never empty.
518 base::string16 keyword_;
523 // AssociatedExtensionInfo ----------------------------------------------------
525 // An AssociatedExtensionInfo represents information about the extension that
526 // added the search engine using the Override Settings API.
527 struct AssociatedExtensionInfo {
528 std::string extension_id;
530 // Whether the search engine is supposed to be default.
531 bool wants_to_be_default_engine;
533 // Used to resolve conflicts when there are multiple extensions specifying the
534 // default search engine. The most recently-installed wins.
535 base::Time install_time;
539 // TemplateURL ----------------------------------------------------------------
541 // A TemplateURL represents a single "search engine", defined primarily as a
542 // subset of the Open Search Description Document
543 // (http://www.opensearch.org/Specifications/OpenSearch) plus some extensions.
544 // One TemplateURL contains several TemplateURLRefs, which correspond to various
545 // different capabilities (e.g. doing searches or getting suggestions), as well
546 // as a TemplateURLData containing other details like the name, keyword, etc.
548 // TemplateURLs are intended to be read-only for most users; the only public
549 // non-const method is the Profile getter, which returns a non-const Profile*.
550 // The TemplateURLService, which handles storing and manipulating TemplateURLs,
551 // is made a friend so that it can be the exception to this pattern.
555 // Regular search engine.
557 // Installed by extension through Override Settings API.
558 NORMAL_CONTROLLED_BY_EXTENSION,
559 // The keyword associated with an extension that uses the Omnibox API.
560 OMNIBOX_API_EXTENSION,
562 // |profile| may be NULL. This will affect the results of e.g. calling
563 // ReplaceSearchTerms() on the member TemplateURLRefs.
564 TemplateURL(Profile* profile, const TemplateURLData& data);
567 // Generates a favicon URL from the specified url.
568 static GURL GenerateFaviconURL(const GURL& url);
570 // Returns true if |t_url| and |data| are equal in all meaningful respects.
571 // Static to allow either or both params to be NULL.
572 static bool MatchesData(const TemplateURL* t_url,
573 const TemplateURLData* data);
575 Profile* profile() { return profile_; }
576 const TemplateURLData& data() const { return data_; }
578 const base::string16& short_name() const { return data_.short_name; }
579 // An accessor for the short_name, but adjusted so it can be appropriately
580 // displayed even if it is LTR and the UI is RTL.
581 base::string16 AdjustedShortNameForLocaleDirection() const;
583 const base::string16& keyword() const { return data_.keyword(); }
585 const std::string& url() const { return data_.url(); }
586 const std::string& suggestions_url() const { return data_.suggestions_url; }
587 const std::string& instant_url() const { return data_.instant_url; }
588 const std::string& image_url() const { return data_.image_url; }
589 const std::string& new_tab_url() const { return data_.new_tab_url; }
590 const std::string& search_url_post_params() const {
591 return data_.search_url_post_params;
593 const std::string& suggestions_url_post_params() const {
594 return data_.suggestions_url_post_params;
596 const std::string& instant_url_post_params() const {
597 return data_.instant_url_post_params;
599 const std::string& image_url_post_params() const {
600 return data_.image_url_post_params;
602 const std::vector<std::string>& alternate_urls() const {
603 return data_.alternate_urls;
605 const GURL& favicon_url() const { return data_.favicon_url; }
607 const GURL& originating_url() const { return data_.originating_url; }
609 bool show_in_default_list() const { return data_.show_in_default_list; }
610 // Returns true if show_in_default_list() is true and this TemplateURL has a
611 // TemplateURLRef that supports replacement.
612 bool ShowInDefaultList() const;
614 bool safe_for_autoreplace() const { return data_.safe_for_autoreplace; }
616 const std::vector<std::string>& input_encodings() const {
617 return data_.input_encodings;
620 TemplateURLID id() const { return data_.id; }
622 base::Time date_created() const { return data_.date_created; }
623 base::Time last_modified() const { return data_.last_modified; }
625 bool created_by_policy() const { return data_.created_by_policy; }
627 int usage_count() const { return data_.usage_count; }
629 int prepopulate_id() const { return data_.prepopulate_id; }
631 const std::string& sync_guid() const { return data_.sync_guid; }
633 // TODO(beaudoin): Rename this when renaming HasSearchTermsReplacementKey().
634 const std::string& search_terms_replacement_key() const {
635 return data_.search_terms_replacement_key;
638 const TemplateURLRef& url_ref() const { return url_ref_; }
639 const TemplateURLRef& suggestions_url_ref() const {
640 return suggestions_url_ref_;
642 const TemplateURLRef& instant_url_ref() const { return instant_url_ref_; }
643 const TemplateURLRef& image_url_ref() const { return image_url_ref_; }
644 const TemplateURLRef& new_tab_url_ref() const { return new_tab_url_ref_; }
646 // Returns true if |url| supports replacement.
647 bool SupportsReplacement() const;
649 // Like SupportsReplacement but usable on threads other than the UI thread.
650 bool SupportsReplacementUsingTermsData(
651 const SearchTermsData& search_terms_data) const;
653 // Returns true if any URLRefs use Googe base URLs.
654 bool HasGoogleBaseURLs() const;
656 // Returns true if this TemplateURL uses Google base URLs and has a keyword
657 // of "google.TLD". We use this to decide whether we can automatically
658 // update the keyword to reflect the current Google base URL TLD.
659 bool IsGoogleSearchURLWithReplaceableKeyword() const;
661 // Returns true if the keywords match or if
662 // IsGoogleSearchURLWithReplaceableKeyword() is true for both |this| and
664 bool HasSameKeywordAs(const TemplateURLData& other) const;
666 Type GetType() const;
668 // Returns the id of the extension that added this search engine. Only call
669 // this for TemplateURLs of type NORMAL_CONTROLLED_BY_EXTENSION or
670 // OMNIBOX_API_EXTENSION.
671 std::string GetExtensionId() const;
673 // Returns the total number of URLs comprised in this template, including
674 // search and alternate URLs.
675 size_t URLCount() const;
677 // Gets the search URL at the given index. The alternate URLs, if any, are
678 // numbered starting at 0, and the primary search URL follows. This is used
679 // to decode the search term given a search URL (see
680 // ExtractSearchTermsFromURL()).
681 const std::string& GetURL(size_t index) const;
683 // Use the alternate URLs and the search URL to match the provided |url|
684 // and extract |search_terms| from it. Returns false and an empty
685 // |search_terms| if no search terms can be matched. The order in which the
686 // alternate URLs are listed dictates their priority, the URL at index 0 is
687 // treated as the highest priority and the primary search URL is treated as
688 // the lowest priority (see GetURL()). For example, if a TemplateURL has
689 // alternate URL "http://foo/#q={searchTerms}" and search URL
690 // "http://foo/?q={searchTerms}", and the URL to be decoded is
691 // "http://foo/?q=a#q=b", the alternate URL will match first and the decoded
692 // search term will be "b".
693 bool ExtractSearchTermsFromURL(const GURL& url, base::string16* search_terms);
695 // Like ExtractSearchTermsFromURL but usable on threads other than the UI
697 bool ExtractSearchTermsFromURLUsingTermsData(
699 base::string16* search_terms,
700 const SearchTermsData& search_terms_data);
702 // Returns true if non-empty search terms could be extracted from |url| using
703 // ExtractSearchTermsFromURL(). In other words, this returns whether |url|
704 // could be the result of performing a search with |this|.
705 bool IsSearchURL(const GURL& url);
707 // Like IsSearchURL but usable on threads other than the UI thread.
708 bool IsSearchURLUsingTermsData(
710 const SearchTermsData& search_terms_data);
712 // Returns true if the specified |url| contains the search terms replacement
713 // key in either the query or the ref. This method does not verify anything
714 // else about the URL. In particular, it does not check that the domain
715 // matches that of this TemplateURL.
716 // TODO(beaudoin): Rename this to reflect that it really checks for an
717 // InstantExtended capable URL.
718 bool HasSearchTermsReplacementKey(const GURL& url) const;
720 // Given a |url| corresponding to this TemplateURL, identifies the search
721 // terms and replaces them with the ones in |search_terms_args|, leaving the
722 // other parameters untouched. If the replacement fails, returns false and
723 // leaves |result| untouched. This is used by mobile ports to perform query
725 bool ReplaceSearchTermsInURL(
727 const TemplateURLRef::SearchTermsArgs& search_terms_args,
730 // Encodes the search terms from |search_terms_args| so that we know the
731 // |input_encoding|. Returns the |encoded_terms| and the
732 // |encoded_original_query|. |encoded_terms| may be escaped as path or query
733 // depending on |is_in_query|; |encoded_original_query| is always escaped as
735 void EncodeSearchTerms(
736 const TemplateURLRef::SearchTermsArgs& search_terms_args,
738 std::string* input_encoding,
739 base::string16* encoded_terms,
740 base::string16* encoded_original_query) const;
743 friend class TemplateURLService;
744 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ReflectsBookmarkBarPinned);
746 void CopyFrom(const TemplateURL& other);
748 void SetURL(const std::string& url);
749 void SetPrepopulateId(int id);
751 // Resets the keyword if IsGoogleSearchURLWithReplaceableKeyword() or |force|.
752 // The |force| parameter is useful when the existing keyword is known to be
753 // a placeholder. The resulting keyword is generated using
754 // TemplateURLService::GenerateSearchURL() and
755 // TemplateURLService::GenerateKeyword().
756 void ResetKeywordIfNecessary(bool force);
758 // Uses the alternate URLs and the search URL to match the provided |url|
759 // and extract |search_terms| from it as well as the |search_terms_component|
760 // (either REF or QUERY) and |search_terms_component| at which the
761 // |search_terms| are found in |url|. See also ExtractSearchTermsFromURL().
762 bool FindSearchTermsInURL(const GURL& url,
763 const SearchTermsData& search_terms_data,
764 base::string16* search_terms,
765 url::Parsed::ComponentType* search_terms_component,
766 url::Component* search_terms_position);
769 TemplateURLData data_;
770 TemplateURLRef url_ref_;
771 TemplateURLRef suggestions_url_ref_;
772 TemplateURLRef instant_url_ref_;
773 TemplateURLRef image_url_ref_;
774 TemplateURLRef new_tab_url_ref_;
775 scoped_ptr<AssociatedExtensionInfo> extension_info_;
777 // TODO(sky): Add date last parsed OSD file.
779 DISALLOW_COPY_AND_ASSIGN(TemplateURL);
782 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_