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 // Optional session token.
109 std::string session_token
;
111 // Additional query params provided by the suggest server.
112 std::string suggest_query_params
;
114 // If set, ReplaceSearchTerms() will automatically append any extra query
115 // params specified via the --extra-search-query-params command-line
116 // argument. Generally, this should be set when dealing with the search or
117 // instant TemplateURLRefs of the default search engine and the caller cares
118 // about the query portion of the URL. Since neither TemplateURLRef nor
119 // indeed TemplateURL know whether a TemplateURL is the default search
120 // engine, callers instead must set this manually.
121 bool append_extra_query_params
;
123 // The raw content of an image thumbnail that will be used as a query for
124 // search-by-image frontend.
125 std::string image_thumbnail_content
;
127 // When searching for an image, the URL of the original image. Callers
128 // should leave this empty for images specified via data: URLs.
131 // When searching for an image, the original size of the image.
132 gfx::Size image_original_size
;
134 // If set, ReplaceSearchTerms() will append a param to the TemplateURLRef to
135 // update the search results page incrementally even if that is otherwise
136 // disabled by google.com preferences. See comments on
137 // chrome::ForceInstantResultsParam().
138 bool force_instant_results
;
140 // True if the search was made using the app list search box. Otherwise, the
141 // search was made using the omnibox.
145 TemplateURLRef(TemplateURL
* owner
, Type type
);
146 TemplateURLRef(TemplateURL
* owner
, size_t index_in_owner
);
149 // Returns the raw URL. None of the parameters will have been replaced.
150 std::string
GetURL() const;
152 // Returns the raw string of the post params. Please see comments in
153 // prepopulated_engines_schema.json for the format.
154 std::string
GetPostParamsString() const;
156 // Returns true if this URL supports search term replacement.
157 bool SupportsReplacement() const;
159 // Like SupportsReplacement but usable on threads other than the UI thread.
160 bool SupportsReplacementUsingTermsData(
161 const SearchTermsData
& search_terms_data
) const;
163 // Returns a string that is the result of replacing the search terms in
164 // the url with the specified arguments. We use our owner's input encoding.
166 // If this TemplateURLRef does not support replacement (SupportsReplacement
167 // returns false), an empty string is returned.
168 // If this TemplateURLRef uses POST, and |post_content| is not NULL, the
169 // |post_params_| will be replaced, encoded in "multipart/form-data" format
170 // and stored into |post_content|.
171 std::string
ReplaceSearchTerms(
172 const SearchTermsArgs
& search_terms_args
,
173 PostContent
* post_content
) const;
174 // TODO(jnd): remove the following ReplaceSearchTerms definition which does
175 // not have |post_content| parameter once all reference callers pass
176 // |post_content| parameter.
177 std::string
ReplaceSearchTerms(
178 const SearchTermsArgs
& search_terms_args
) const {
179 return ReplaceSearchTerms(search_terms_args
, NULL
);
182 // Just like ReplaceSearchTerms except that it takes SearchTermsData to supply
183 // the data for some search terms. Most of the time ReplaceSearchTerms should
185 std::string
ReplaceSearchTermsUsingTermsData(
186 const SearchTermsArgs
& search_terms_args
,
187 const SearchTermsData
& search_terms_data
,
188 PostContent
* post_content
) const;
190 // Returns true if the TemplateURLRef is valid. An invalid TemplateURLRef is
191 // one that contains unknown terms, or invalid characters.
192 bool IsValid() const;
194 // Like IsValid but usable on threads other than the UI thread.
195 bool IsValidUsingTermsData(const SearchTermsData
& search_terms_data
) const;
197 // Returns a string representation of this TemplateURLRef suitable for
198 // display. The display format is the same as the format used by Firefox.
199 base::string16
DisplayURL() const;
201 // Converts a string as returned by DisplayURL back into a string as
202 // understood by TemplateURLRef.
203 static std::string
DisplayURLToURLRef(const base::string16
& display_url
);
205 // If this TemplateURLRef is valid and contains one search term, this returns
206 // the host/path of the URL, otherwise this returns an empty string.
207 const std::string
& GetHost() const;
208 const std::string
& GetPath() const;
210 // If this TemplateURLRef is valid and contains one search term, this returns
211 // the key of the search term, otherwise this returns an empty string.
212 const std::string
& GetSearchTermKey() const;
214 // Converts the specified term in our owner's encoding to a base::string16.
215 base::string16
SearchTermToString16(const std::string
& term
) const;
217 // Returns true if this TemplateURLRef has a replacement term of
218 // {google:baseURL} or {google:baseSuggestURL}.
219 bool HasGoogleBaseURLs() const;
221 // Use the pattern referred to by this TemplateURLRef to match the provided
222 // |url| and extract |search_terms| from it. Returns true if the pattern
223 // matches, even if |search_terms| is empty. In this case
224 // |search_term_component|, if not NULL, indicates whether the search terms
225 // were found in the query or the ref parameters; and |search_terms_position|,
226 // if not NULL, contains the position of the search terms in the query or the
227 // ref parameters. Returns false and an empty |search_terms| if the pattern
229 bool ExtractSearchTermsFromURL(
231 base::string16
* search_terms
,
232 const SearchTermsData
& search_terms_data
,
233 url::Parsed::ComponentType
* search_term_component
,
234 url::Component
* search_terms_position
) const;
236 // Whether the URL uses POST (as opposed to GET).
237 bool UsesPOSTMethodUsingTermsData(
238 const SearchTermsData
* search_terms_data
) const;
239 bool UsesPOSTMethod() const {
240 return UsesPOSTMethodUsingTermsData(NULL
);
244 friend class TemplateURL
;
245 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest
, SetPrepopulatedAndParse
);
246 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest
, ParseParameterKnown
);
247 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest
, ParseParameterUnknown
);
248 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest
, ParseURLEmpty
);
249 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest
, ParseURLNoTemplateEnd
);
250 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest
, ParseURLNoKnownParameters
);
251 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest
, ParseURLTwoParameters
);
252 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest
, ParseURLNestedParameter
);
253 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest
, URLRefTestImageURLWithPOST
);
254 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest
, ReflectsBookmarkBarPinned
);
256 // Enumeration of the known types.
257 enum ReplacementType
{
259 GOOGLE_ASSISTED_QUERY_STATS
,
261 GOOGLE_BASE_SUGGEST_URL
,
262 GOOGLE_BOOKMARK_BAR_PINNED
,
263 GOOGLE_CURRENT_PAGE_URL
,
264 GOOGLE_CURSOR_POSITION
,
265 GOOGLE_IMAGE_ORIGINAL_HEIGHT
,
266 GOOGLE_IMAGE_ORIGINAL_WIDTH
,
267 GOOGLE_IMAGE_SEARCH_SOURCE
,
268 GOOGLE_IMAGE_THUMBNAIL
,
270 GOOGLE_FORCE_INSTANT_RESULTS
,
271 GOOGLE_INSTANT_EXTENDED_ENABLED
,
272 GOOGLE_NTP_IS_THEMED
,
273 GOOGLE_OMNIBOX_START_MARGIN
,
274 GOOGLE_ORIGINAL_QUERY_FOR_SUGGESTION
,
275 GOOGLE_PAGE_CLASSIFICATION
,
277 GOOGLE_SEARCH_CLIENT
,
278 GOOGLE_SEARCH_FIELDTRIAL_GROUP
,
279 GOOGLE_SESSION_TOKEN
,
280 GOOGLE_SUGGEST_CLIENT
,
281 GOOGLE_SUGGEST_REQUEST_ID
,
282 GOOGLE_UNESCAPED_SEARCH_TERMS
,
287 // Used to identify an element of the raw url that can be replaced.
289 Replacement(ReplacementType type
, size_t index
)
290 : type(type
), index(index
), is_post_param(false) {}
291 ReplacementType type
;
293 // Indicates the location in where the replacement is replaced. If
294 // |is_post_param| is false, |index| indicates the byte position in
295 // |parsed_url_|. Otherwise, |index| is the index of |post_params_|.
299 // The list of elements to replace.
300 typedef std::vector
<struct Replacement
> Replacements
;
301 // Type to store <key, value> pairs for POST URLs.
302 typedef std::pair
<std::string
, std::string
> PostParam
;
303 typedef std::vector
<PostParam
> PostParams
;
305 // TemplateURLRef internally caches values to make replacement quick. This
306 // method invalidates any cached values.
307 void InvalidateCachedValues() const;
309 // Parses the parameter in url at the specified offset. start/end specify the
310 // range of the parameter in the url, including the braces. If the parameter
311 // is valid, url is updated to reflect the appropriate parameter. If
312 // the parameter is one of the known parameters an element is added to
313 // replacements indicating the type and range of the element. The original
314 // parameter is erased from the url.
316 // If the parameter is not a known parameter, false is returned. If this is a
317 // prepopulated URL, the parameter is erased, otherwise it is left alone.
318 bool ParseParameter(size_t start
,
321 Replacements
* replacements
) const;
323 // Parses the specified url, replacing parameters as necessary. If
324 // successful, valid is set to true, and the parsed url is returned. For all
325 // known parameters that are encountered an entry is added to replacements.
326 // If there is an error parsing the url, valid is set to false, and an empty
327 // string is returned. If the URL has the POST parameters, they will be
328 // parsed into |post_params| which will be further replaced with real search
329 // terms data and encoded in "multipart/form-data" format to generate the
331 std::string
ParseURL(const std::string
& url
,
332 Replacements
* replacements
,
333 PostParams
* post_params
,
336 // If the url has not yet been parsed, ParseURL is invoked.
337 // NOTE: While this is const, it modifies parsed_, valid_, parsed_url_ and
339 void ParseIfNecessary() const;
341 // Like ParseIfNecessary but usable on threads other than the UI thread.
342 void ParseIfNecessaryUsingTermsData(
343 const SearchTermsData
& search_terms_data
) const;
345 // Extracts the query key and host from the url.
346 void ParseHostAndSearchTermKey(
347 const SearchTermsData
& search_terms_data
) const;
349 // Encode post parameters in "multipart/form-data" format and store it
350 // inside |post_content|. Returns false if errors are encountered during
351 // encoding. This method is called each time ReplaceSearchTerms gets called.
352 bool EncodeFormData(const PostParams
& post_params
,
353 PostContent
* post_content
) const;
355 // Handles a replacement by using real term data. If the replacement
356 // belongs to a PostParam, the PostParam will be replaced by the term data.
357 // Otherwise, the term data will be inserted at the place that the
358 // replacement points to.
359 void HandleReplacement(const std::string
& name
,
360 const std::string
& value
,
361 const Replacement
& replacement
,
362 std::string
* url
) const;
364 // Replaces all replacements in |parsed_url_| with their actual values and
365 // returns the result. This is the main functionality of
366 // ReplaceSearchTermsUsingTermsData().
367 std::string
HandleReplacements(
368 const SearchTermsArgs
& search_terms_args
,
369 const SearchTermsData
& search_terms_data
,
370 PostContent
* post_content
) const;
372 // The TemplateURL that contains us. This should outlive us.
373 TemplateURL
* const owner_
;
375 // What kind of URL we are.
378 // If |type_| is |INDEXED|, this |index_in_owner_| is used instead to refer to
379 // a url within our owner.
380 const size_t index_in_owner_
;
382 // Whether the URL has been parsed.
383 mutable bool parsed_
;
385 // Whether the url was successfully parsed.
388 // The parsed URL. All terms have been stripped out of this with
389 // replacements_ giving the index of the terms to replace.
390 mutable std::string parsed_url_
;
392 // Do we support search term replacement?
393 mutable bool supports_replacements_
;
395 // The replaceable parts of url (parsed_url_). These are ordered by index
396 // into the string, and may be empty.
397 mutable Replacements replacements_
;
399 // Host, path, key and location of the search term. These are only set if the
400 // url contains one search term.
401 mutable std::string host_
;
402 mutable std::string path_
;
403 mutable std::string search_term_key_
;
404 mutable url::Parsed::ComponentType search_term_key_location_
;
406 mutable PostParams post_params_
;
408 // Whether the contained URL is a pre-populated URL.
411 // Whether search terms are shown in the omnibox on search results pages.
412 // This is kept as a member so it can be overridden by tests.
413 bool showing_search_terms_
;
415 DISALLOW_COPY_AND_ASSIGN(TemplateURLRef
);
419 // TemplateURLData ------------------------------------------------------------
421 // The data for the TemplateURL. Separating this into its own class allows most
422 // users to do SSA-style usage of TemplateURL: construct a TemplateURLData with
423 // whatever fields are desired, then create an immutable TemplateURL from it.
424 struct TemplateURLData
{
428 // A short description of the template. This is the name we show to the user
429 // in various places that use TemplateURLs. For example, the location bar
430 // shows this when the user selects a substituting match.
431 base::string16 short_name
;
433 // The shortcut for this TemplateURL. |keyword| must be non-empty.
434 void SetKeyword(const base::string16
& keyword
);
435 const base::string16
& keyword() const { return keyword_
; }
437 // The raw URL for the TemplateURL, which may not be valid as-is (e.g. because
438 // it requires substitutions first). This must be non-empty.
439 void SetURL(const std::string
& url
);
440 const std::string
& url() const { return url_
; }
442 // Optional additional raw URLs.
443 std::string suggestions_url
;
444 std::string instant_url
;
445 std::string image_url
;
446 std::string new_tab_url
;
448 // The following post_params are comma-separated lists used to specify the
449 // post parameters for the corresponding URL.
450 std::string search_url_post_params
;
451 std::string suggestions_url_post_params
;
452 std::string instant_url_post_params
;
453 std::string image_url_post_params
;
455 // Optional favicon for the TemplateURL.
458 // URL to the OSD file this came from. May be empty.
459 GURL originating_url
;
461 // Whether this TemplateURL is shown in the default list of search providers.
462 // This is just a property and does not indicate whether the TemplateURL has a
463 // TemplateURLRef that supports replacement. Use
464 // TemplateURL::ShowInDefaultList() to test both.
465 bool show_in_default_list
;
467 // Whether it's safe for auto-modification code (the autogenerator and the
468 // code that imports data from other browsers) to replace the TemplateURL.
469 // This should be set to false for any TemplateURL the user edits, or any
470 // TemplateURL that the user clearly manually edited in the past, like a
471 // bookmark keyword from another browser.
472 bool safe_for_autoreplace
;
474 // The list of supported encodings for the search terms. This may be empty,
475 // which indicates the terms should be encoded with UTF-8.
476 std::vector
<std::string
> input_encodings
;
478 // Unique identifier of this TemplateURL. The unique ID is set by the
479 // TemplateURLService when the TemplateURL is added to it.
482 // Date this TemplateURL was created.
484 // NOTE: this may be 0, which indicates the TemplateURL was created before we
485 // started tracking creation time.
486 base::Time date_created
;
488 // The last time this TemplateURL was modified by a user, since creation.
490 // NOTE: Like date_created above, this may be 0.
491 base::Time last_modified
;
493 // True if this TemplateURL was automatically created by the administrator via
495 bool created_by_policy
;
497 // Number of times this TemplateURL has been explicitly used to load a URL.
498 // We don't increment this for uses as the "default search engine" since
499 // that's not really "explicit" usage and incrementing would result in pinning
500 // the user's default search engine(s) to the top of the list of searches on
501 // the New Tab page, de-emphasizing the omnibox as "where you go to search".
504 // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0.
507 // The primary unique identifier for Sync. This set on all TemplateURLs
508 // regardless of whether they have been associated with Sync.
509 std::string sync_guid
;
511 // A list of URL patterns that can be used, in addition to |url_|, to extract
512 // search terms from a URL.
513 std::vector
<std::string
> alternate_urls
;
515 // A parameter that, if present in the query or ref parameters of a search_url
516 // or instant_url, causes Chrome to replace the URL with the search term.
517 std::string search_terms_replacement_key
;
520 // Private so we can enforce using the setters and thus enforce that these
521 // fields are never empty.
522 base::string16 keyword_
;
527 // AssociatedExtensionInfo ----------------------------------------------------
529 // An AssociatedExtensionInfo represents information about the extension that
530 // added the search engine using the Override Settings API.
531 struct AssociatedExtensionInfo
{
532 std::string extension_id
;
534 // Whether the search engine is supposed to be default.
535 bool wants_to_be_default_engine
;
537 // Used to resolve conflicts when there are multiple extensions specifying the
538 // default search engine. The most recently-installed wins.
539 base::Time install_time
;
543 // TemplateURL ----------------------------------------------------------------
545 // A TemplateURL represents a single "search engine", defined primarily as a
546 // subset of the Open Search Description Document
547 // (http://www.opensearch.org/Specifications/OpenSearch) plus some extensions.
548 // One TemplateURL contains several TemplateURLRefs, which correspond to various
549 // different capabilities (e.g. doing searches or getting suggestions), as well
550 // as a TemplateURLData containing other details like the name, keyword, etc.
552 // TemplateURLs are intended to be read-only for most users; the only public
553 // non-const method is the Profile getter, which returns a non-const Profile*.
554 // The TemplateURLService, which handles storing and manipulating TemplateURLs,
555 // is made a friend so that it can be the exception to this pattern.
559 // Regular search engine.
561 // Installed by extension through Override Settings API.
562 NORMAL_CONTROLLED_BY_EXTENSION
,
563 // The keyword associated with an extension that uses the Omnibox API.
564 OMNIBOX_API_EXTENSION
,
566 // |profile| may be NULL. This will affect the results of e.g. calling
567 // ReplaceSearchTerms() on the member TemplateURLRefs.
568 TemplateURL(Profile
* profile
, const TemplateURLData
& data
);
571 // Generates a favicon URL from the specified url.
572 static GURL
GenerateFaviconURL(const GURL
& url
);
574 // Returns true if |t_url| and |data| are equal in all meaningful respects.
575 // Static to allow either or both params to be NULL.
576 static bool MatchesData(const TemplateURL
* t_url
,
577 const TemplateURLData
* data
);
579 Profile
* profile() { return profile_
; }
580 const TemplateURLData
& data() const { return data_
; }
582 const base::string16
& short_name() const { return data_
.short_name
; }
583 // An accessor for the short_name, but adjusted so it can be appropriately
584 // displayed even if it is LTR and the UI is RTL.
585 base::string16
AdjustedShortNameForLocaleDirection() const;
587 const base::string16
& keyword() const { return data_
.keyword(); }
589 const std::string
& url() const { return data_
.url(); }
590 const std::string
& suggestions_url() const { return data_
.suggestions_url
; }
591 const std::string
& instant_url() const { return data_
.instant_url
; }
592 const std::string
& image_url() const { return data_
.image_url
; }
593 const std::string
& new_tab_url() const { return data_
.new_tab_url
; }
594 const std::string
& search_url_post_params() const {
595 return data_
.search_url_post_params
;
597 const std::string
& suggestions_url_post_params() const {
598 return data_
.suggestions_url_post_params
;
600 const std::string
& instant_url_post_params() const {
601 return data_
.instant_url_post_params
;
603 const std::string
& image_url_post_params() const {
604 return data_
.image_url_post_params
;
606 const std::vector
<std::string
>& alternate_urls() const {
607 return data_
.alternate_urls
;
609 const GURL
& favicon_url() const { return data_
.favicon_url
; }
611 const GURL
& originating_url() const { return data_
.originating_url
; }
613 bool show_in_default_list() const { return data_
.show_in_default_list
; }
614 // Returns true if show_in_default_list() is true and this TemplateURL has a
615 // TemplateURLRef that supports replacement.
616 bool ShowInDefaultList() const;
618 bool safe_for_autoreplace() const { return data_
.safe_for_autoreplace
; }
620 const std::vector
<std::string
>& input_encodings() const {
621 return data_
.input_encodings
;
624 TemplateURLID
id() const { return data_
.id
; }
626 base::Time
date_created() const { return data_
.date_created
; }
627 base::Time
last_modified() const { return data_
.last_modified
; }
629 bool created_by_policy() const { return data_
.created_by_policy
; }
631 int usage_count() const { return data_
.usage_count
; }
633 int prepopulate_id() const { return data_
.prepopulate_id
; }
635 const std::string
& sync_guid() const { return data_
.sync_guid
; }
637 // TODO(beaudoin): Rename this when renaming HasSearchTermsReplacementKey().
638 const std::string
& search_terms_replacement_key() const {
639 return data_
.search_terms_replacement_key
;
642 const TemplateURLRef
& url_ref() const { return url_ref_
; }
643 const TemplateURLRef
& suggestions_url_ref() const {
644 return suggestions_url_ref_
;
646 const TemplateURLRef
& instant_url_ref() const { return instant_url_ref_
; }
647 const TemplateURLRef
& image_url_ref() const { return image_url_ref_
; }
648 const TemplateURLRef
& new_tab_url_ref() const { return new_tab_url_ref_
; }
650 // Returns true if |url| supports replacement.
651 bool SupportsReplacement() const;
653 // Like SupportsReplacement but usable on threads other than the UI thread.
654 bool SupportsReplacementUsingTermsData(
655 const SearchTermsData
& search_terms_data
) const;
657 // Returns true if any URLRefs use Googe base URLs.
658 bool HasGoogleBaseURLs() const;
660 // Returns true if this TemplateURL uses Google base URLs and has a keyword
661 // of "google.TLD". We use this to decide whether we can automatically
662 // update the keyword to reflect the current Google base URL TLD.
663 bool IsGoogleSearchURLWithReplaceableKeyword() const;
665 // Returns true if the keywords match or if
666 // IsGoogleSearchURLWithReplaceableKeyword() is true for both |this| and
668 bool HasSameKeywordAs(const TemplateURLData
& other
) const;
670 Type
GetType() const;
672 // Returns the id of the extension that added this search engine. Only call
673 // this for TemplateURLs of type NORMAL_CONTROLLED_BY_EXTENSION or
674 // OMNIBOX_API_EXTENSION.
675 std::string
GetExtensionId() const;
677 // Returns the total number of URLs comprised in this template, including
678 // search and alternate URLs.
679 size_t URLCount() const;
681 // Gets the search URL at the given index. The alternate URLs, if any, are
682 // numbered starting at 0, and the primary search URL follows. This is used
683 // to decode the search term given a search URL (see
684 // ExtractSearchTermsFromURL()).
685 const std::string
& GetURL(size_t index
) const;
687 // Use the alternate URLs and the search URL to match the provided |url|
688 // and extract |search_terms| from it. Returns false and an empty
689 // |search_terms| if no search terms can be matched. The order in which the
690 // alternate URLs are listed dictates their priority, the URL at index 0 is
691 // treated as the highest priority and the primary search URL is treated as
692 // the lowest priority (see GetURL()). For example, if a TemplateURL has
693 // alternate URL "http://foo/#q={searchTerms}" and search URL
694 // "http://foo/?q={searchTerms}", and the URL to be decoded is
695 // "http://foo/?q=a#q=b", the alternate URL will match first and the decoded
696 // search term will be "b".
697 bool ExtractSearchTermsFromURL(const GURL
& url
, base::string16
* search_terms
);
699 // Like ExtractSearchTermsFromURL but usable on threads other than the UI
701 bool ExtractSearchTermsFromURLUsingTermsData(
703 base::string16
* search_terms
,
704 const SearchTermsData
& search_terms_data
);
706 // Returns true if non-empty search terms could be extracted from |url| using
707 // ExtractSearchTermsFromURL(). In other words, this returns whether |url|
708 // could be the result of performing a search with |this|.
709 bool IsSearchURL(const GURL
& url
);
711 // Like IsSearchURL but usable on threads other than the UI thread.
712 bool IsSearchURLUsingTermsData(
714 const SearchTermsData
& search_terms_data
);
716 // Returns true if the specified |url| contains the search terms replacement
717 // key in either the query or the ref. This method does not verify anything
718 // else about the URL. In particular, it does not check that the domain
719 // matches that of this TemplateURL.
720 // TODO(beaudoin): Rename this to reflect that it really checks for an
721 // InstantExtended capable URL.
722 bool HasSearchTermsReplacementKey(const GURL
& url
) const;
724 // Given a |url| corresponding to this TemplateURL, identifies the search
725 // terms and replaces them with the ones in |search_terms_args|, leaving the
726 // other parameters untouched. If the replacement fails, returns false and
727 // leaves |result| untouched. This is used by mobile ports to perform query
729 bool ReplaceSearchTermsInURL(
731 const TemplateURLRef::SearchTermsArgs
& search_terms_args
,
734 // Encodes the search terms from |search_terms_args| so that we know the
735 // |input_encoding|. Returns the |encoded_terms| and the
736 // |encoded_original_query|. |encoded_terms| may be escaped as path or query
737 // depending on |is_in_query|; |encoded_original_query| is always escaped as
739 void EncodeSearchTerms(
740 const TemplateURLRef::SearchTermsArgs
& search_terms_args
,
742 std::string
* input_encoding
,
743 base::string16
* encoded_terms
,
744 base::string16
* encoded_original_query
) const;
747 friend class TemplateURLService
;
748 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest
, ReflectsBookmarkBarPinned
);
750 void CopyFrom(const TemplateURL
& other
);
752 void SetURL(const std::string
& url
);
753 void SetPrepopulateId(int id
);
755 // Resets the keyword if IsGoogleSearchURLWithReplaceableKeyword() or |force|.
756 // The |force| parameter is useful when the existing keyword is known to be
757 // a placeholder. The resulting keyword is generated using
758 // TemplateURLService::GenerateSearchURL() and
759 // TemplateURLService::GenerateKeyword().
760 void ResetKeywordIfNecessary(bool force
);
762 // Uses the alternate URLs and the search URL to match the provided |url|
763 // and extract |search_terms| from it as well as the |search_terms_component|
764 // (either REF or QUERY) and |search_terms_component| at which the
765 // |search_terms| are found in |url|. See also ExtractSearchTermsFromURL().
766 bool FindSearchTermsInURL(const GURL
& url
,
767 const SearchTermsData
& search_terms_data
,
768 base::string16
* search_terms
,
769 url::Parsed::ComponentType
* search_terms_component
,
770 url::Component
* search_terms_position
);
773 TemplateURLData data_
;
774 TemplateURLRef url_ref_
;
775 TemplateURLRef suggestions_url_ref_
;
776 TemplateURLRef instant_url_ref_
;
777 TemplateURLRef image_url_ref_
;
778 TemplateURLRef new_tab_url_ref_
;
779 scoped_ptr
<AssociatedExtensionInfo
> extension_info_
;
781 // TODO(sky): Add date last parsed OSD file.
783 DISALLOW_COPY_AND_ASSIGN(TemplateURL
);
786 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_