OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_ | 5 #ifndef CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_ |
6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_ | 6 #define CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 public: | 43 public: |
44 // Magic numbers to pass to ReplaceSearchTerms() for the |accepted_suggestion| | 44 // Magic numbers to pass to ReplaceSearchTerms() for the |accepted_suggestion| |
45 // parameter. Most callers aren't using Suggest capabilities and should just | 45 // parameter. Most callers aren't using Suggest capabilities and should just |
46 // pass NO_SUGGESTIONS_AVAILABLE. | 46 // pass NO_SUGGESTIONS_AVAILABLE. |
47 // NOTE: Because positive values are meaningful, make sure these are negative! | 47 // NOTE: Because positive values are meaningful, make sure these are negative! |
48 enum AcceptedSuggestion { | 48 enum AcceptedSuggestion { |
49 NO_SUGGESTION_CHOSEN = -1, | 49 NO_SUGGESTION_CHOSEN = -1, |
50 NO_SUGGESTIONS_AVAILABLE = -2, | 50 NO_SUGGESTIONS_AVAILABLE = -2, |
51 }; | 51 }; |
52 | 52 |
53 explicit TemplateURLRef(TemplateURL* owner); | 53 // Which kind of URL within our owner we are. This allows us to get at the |
54 TemplateURLRef(TemplateURL* owner, const std::string& url); | 54 // correct string field. |
55 enum Type { | |
56 SEARCH, | |
57 SUGGEST, | |
58 INSTANT, | |
59 }; | |
60 | |
61 TemplateURLRef(TemplateURL* owner, Type type); | |
55 ~TemplateURLRef(); | 62 ~TemplateURLRef(); |
56 | 63 |
64 // Returns the raw URL. None of the parameters will have been replaced. | |
65 std::string GetURL() const; | |
66 | |
57 // Returns true if this URL supports replacement. | 67 // Returns true if this URL supports replacement. |
58 bool SupportsReplacement() const; | 68 bool SupportsReplacement() const; |
59 | 69 |
60 // Like SupportsReplacement but usable on threads other than the UI thread. | 70 // Like SupportsReplacement but usable on threads other than the UI thread. |
61 bool SupportsReplacementUsingTermsData( | 71 bool SupportsReplacementUsingTermsData( |
62 const SearchTermsData& search_terms_data) const; | 72 const SearchTermsData& search_terms_data) const; |
63 | 73 |
64 // Returns a string that is the result of replacing the search terms in | 74 // Returns a string that is the result of replacing the search terms in |
65 // the url with the specified value. We use our owner's input encoding. | 75 // the url with the specified value. We use our owner's input encoding. |
66 // | 76 // |
(...skipping 15 matching lines...) Expand all Loading... | |
82 | 92 |
83 // Just like ReplaceSearchTerms except that it takes SearchTermsData to supply | 93 // Just like ReplaceSearchTerms except that it takes SearchTermsData to supply |
84 // the data for some search terms. Most of the time ReplaceSearchTerms should | 94 // the data for some search terms. Most of the time ReplaceSearchTerms should |
85 // be called. | 95 // be called. |
86 std::string ReplaceSearchTermsUsingTermsData( | 96 std::string ReplaceSearchTermsUsingTermsData( |
87 const string16& terms, | 97 const string16& terms, |
88 int accepted_suggestion, | 98 int accepted_suggestion, |
89 const string16& original_query_for_suggestion, | 99 const string16& original_query_for_suggestion, |
90 const SearchTermsData& search_terms_data) const; | 100 const SearchTermsData& search_terms_data) const; |
91 | 101 |
92 // Returns the raw URL. None of the parameters will have been replaced. | |
93 const std::string& url() const { return url_; } | |
94 | |
95 // Returns true if the TemplateURLRef is valid. An invalid TemplateURLRef is | 102 // Returns true if the TemplateURLRef is valid. An invalid TemplateURLRef is |
96 // one that contains unknown terms, or invalid characters. | 103 // one that contains unknown terms, or invalid characters. |
97 bool IsValid() const; | 104 bool IsValid() const; |
98 | 105 |
99 // Like IsValid but usable on threads other than the UI thread. | 106 // Like IsValid but usable on threads other than the UI thread. |
100 bool IsValidUsingTermsData(const SearchTermsData& search_terms_data) const; | 107 bool IsValidUsingTermsData(const SearchTermsData& search_terms_data) const; |
101 | 108 |
102 // Returns a string representation of this TemplateURLRef suitable for | 109 // Returns a string representation of this TemplateURLRef suitable for |
103 // display. The display format is the same as the format used by Firefox. | 110 // display. The display format is the same as the format used by Firefox. |
104 string16 DisplayURL() const; | 111 string16 DisplayURL() const; |
(...skipping 11 matching lines...) Expand all Loading... | |
116 // the key of the search term, otherwise this returns an empty string. | 123 // the key of the search term, otherwise this returns an empty string. |
117 const std::string& GetSearchTermKey() const; | 124 const std::string& GetSearchTermKey() const; |
118 | 125 |
119 // Converts the specified term in our owner's encoding to a string16. | 126 // Converts the specified term in our owner's encoding to a string16. |
120 string16 SearchTermToString16(const std::string& term) const; | 127 string16 SearchTermToString16(const std::string& term) const; |
121 | 128 |
122 // Returns true if this TemplateURLRef has a replacement term of | 129 // Returns true if this TemplateURLRef has a replacement term of |
123 // {google:baseURL} or {google:baseSuggestURL}. | 130 // {google:baseURL} or {google:baseSuggestURL}. |
124 bool HasGoogleBaseURLs() const; | 131 bool HasGoogleBaseURLs() const; |
125 | 132 |
126 // Returns true if both refs are NULL or have the same values. | |
127 static bool SameUrlRefs(const TemplateURLRef* ref1, | |
128 const TemplateURLRef* ref2); | |
129 | |
130 // Collects metrics whether searches through Google are sent with RLZ string. | 133 // Collects metrics whether searches through Google are sent with RLZ string. |
131 void CollectRLZMetrics() const; | 134 void CollectRLZMetrics() const; |
132 | 135 |
133 private: | 136 private: |
134 friend class TemplateURL; | 137 friend class TemplateURL; |
135 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, SetPrepopulatedAndParse); | 138 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, SetPrepopulatedAndParse); |
136 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterKnown); | 139 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterKnown); |
137 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterUnknown); | 140 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseParameterUnknown); |
138 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLEmpty); | 141 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLEmpty); |
139 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNoTemplateEnd); | 142 FRIEND_TEST_ALL_PREFIXES(TemplateURLTest, ParseURLNoTemplateEnd); |
(...skipping 25 matching lines...) Expand all Loading... | |
165 size_t index; | 168 size_t index; |
166 }; | 169 }; |
167 | 170 |
168 // The list of elements to replace. | 171 // The list of elements to replace. |
169 typedef std::vector<struct Replacement> Replacements; | 172 typedef std::vector<struct Replacement> Replacements; |
170 | 173 |
171 // TemplateURLRef internally caches values to make replacement quick. This | 174 // TemplateURLRef internally caches values to make replacement quick. This |
172 // method invalidates any cached values. | 175 // method invalidates any cached values. |
173 void InvalidateCachedValues() const; | 176 void InvalidateCachedValues() const; |
174 | 177 |
175 // Resets the url. | |
176 void Set(const std::string& url); | |
177 | |
178 // Parses the parameter in url at the specified offset. start/end specify the | 178 // Parses the parameter in url at the specified offset. start/end specify the |
179 // range of the parameter in the url, including the braces. If the parameter | 179 // range of the parameter in the url, including the braces. If the parameter |
180 // is valid, url is updated to reflect the appropriate parameter. If | 180 // is valid, url is updated to reflect the appropriate parameter. If |
181 // the parameter is one of the known parameters an element is added to | 181 // the parameter is one of the known parameters an element is added to |
182 // replacements indicating the type and range of the element. The original | 182 // replacements indicating the type and range of the element. The original |
183 // parameter is erased from the url. | 183 // parameter is erased from the url. |
184 // | 184 // |
185 // If the parameter is not a known parameter, false is returned. If this is a | 185 // If the parameter is not a known parameter, false is returned. If this is a |
186 // prepopulated URL, the parameter is erased, otherwise it is left alone. | 186 // prepopulated URL, the parameter is erased, otherwise it is left alone. |
187 bool ParseParameter(size_t start, | 187 bool ParseParameter(size_t start, |
(...skipping 19 matching lines...) Expand all Loading... | |
207 void ParseIfNecessaryUsingTermsData( | 207 void ParseIfNecessaryUsingTermsData( |
208 const SearchTermsData& search_terms_data) const; | 208 const SearchTermsData& search_terms_data) const; |
209 | 209 |
210 // Extracts the query key and host from the url. | 210 // Extracts the query key and host from the url. |
211 void ParseHostAndSearchTermKey( | 211 void ParseHostAndSearchTermKey( |
212 const SearchTermsData& search_terms_data) const; | 212 const SearchTermsData& search_terms_data) const; |
213 | 213 |
214 // The TemplateURL that contains us. This should outlive us. | 214 // The TemplateURL that contains us. This should outlive us. |
215 TemplateURL* owner_; | 215 TemplateURL* owner_; |
216 | 216 |
217 // The raw URL. Where as this contains all the terms (such as {searchTerms}), | 217 // What kind of URL we are. |
218 // parsed_url_ has them all stripped out. | 218 Type type_; |
sky
2012/03/30 16:45:14
const (wow, I'm suggesting you make something cons
| |
219 std::string url_; | |
220 | 219 |
221 // Whether the URL has been parsed. | 220 // Whether the URL has been parsed. |
222 mutable bool parsed_; | 221 mutable bool parsed_; |
223 | 222 |
224 // Whether the url was successfully parsed. | 223 // Whether the url was successfully parsed. |
225 mutable bool valid_; | 224 mutable bool valid_; |
226 | 225 |
227 // The parsed URL. All terms have been stripped out of this with | 226 // The parsed URL. All terms have been stripped out of this with |
228 // replacements_ giving the index of the terms to replace. | 227 // replacements_ giving the index of the terms to replace. |
229 mutable std::string parsed_url_; | 228 mutable std::string parsed_url_; |
(...skipping 30 matching lines...) Expand all Loading... | |
260 // Generates a favicon URL from the specified url. | 259 // Generates a favicon URL from the specified url. |
261 static GURL GenerateFaviconURL(const GURL& url); | 260 static GURL GenerateFaviconURL(const GURL& url); |
262 | 261 |
263 // A short description of the template. This is the name we show to the user | 262 // 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 | 263 // in various places that use keywords. For example, the location bar shows |
265 // this when the user selects the keyword. | 264 // this when the user selects the keyword. |
266 void set_short_name(const string16& short_name) { | 265 void set_short_name(const string16& short_name) { |
267 short_name_ = short_name; | 266 short_name_ = short_name; |
268 } | 267 } |
269 const string16& short_name() const { return short_name_; } | 268 const string16& short_name() const { return short_name_; } |
270 | |
271 // An accessor for the short_name, but adjusted so it can be appropriately | 269 // An accessor for the short_name, but adjusted so it can be appropriately |
272 // displayed even if it is LTR and the UI is RTL. | 270 // displayed even if it is LTR and the UI is RTL. |
273 string16 AdjustedShortNameForLocaleDirection() const; | 271 string16 AdjustedShortNameForLocaleDirection() const; |
274 | 272 |
275 // Parameterized URL for providing the results. This may be NULL. | 273 // Parameterized URL for providing the results. |
276 // Be sure and check the resulting TemplateURLRef for SupportsReplacement | |
277 // before using. | |
278 void SetURL(const std::string& url); | 274 void SetURL(const std::string& url); |
279 // Returns the TemplateURLRef that may be used for search results. This | 275 const std::string& url() const { return url_; } |
280 // returns NULL if a url element was not specified. | |
281 const TemplateURLRef* url() const { | |
282 return url_.url().empty() ? NULL : &url_; | |
283 } | |
284 | 276 |
285 // URL providing JSON results. This is typically used to provide suggestions | 277 // URL providing JSON results. This is typically used to provide suggestions |
286 // as your type. If NULL, this url does not support suggestions. | 278 // as you type. |
287 // Be sure and check the resulting TemplateURLRef for SupportsReplacement | |
288 // before using. | |
289 void SetSuggestionsURL(const std::string& url); | 279 void SetSuggestionsURL(const std::string& url); |
290 const TemplateURLRef* suggestions_url() const { | 280 const std::string& suggestions_url() const { return suggestions_url_; } |
291 return suggestions_url_.url().empty() ? NULL : &suggestions_url_; | |
292 } | |
293 | 281 |
294 // Parameterized URL for instant results. This may be NULL. Be sure and check | 282 // Parameterized URL for instant results. |
295 // the resulting TemplateURLRef for SupportsReplacement before using. | |
296 void SetInstantURL(const std::string& url); | 283 void SetInstantURL(const std::string& url); |
297 // Returns the TemplateURLRef that may be used for search results. This | 284 const std::string& instant_url() const { return instant_url_; } |
298 // returns NULL if a url element was not specified. | |
299 const TemplateURLRef* instant_url() const { | |
300 return instant_url_.url().empty() ? NULL : &instant_url_; | |
301 } | |
302 | 285 |
303 // URL to the OSD file this came from. May be empty. | 286 // URL to the OSD file this came from. May be empty. |
304 void set_originating_url(const GURL& url) { | 287 void set_originating_url(const GURL& url) { |
305 originating_url_ = url; | 288 originating_url_ = url; |
306 } | 289 } |
307 const GURL& originating_url() const { return originating_url_; } | 290 const GURL& originating_url() const { return originating_url_; } |
308 | 291 |
309 // The shortcut for this template url. May be empty. | 292 // The shortcut for this template url. May be empty. |
310 void set_keyword(const string16& keyword); | 293 void set_keyword(const string16& keyword); |
311 const string16& keyword() const; | 294 const string16& keyword() const; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
407 | 390 |
408 // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0. | 391 // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0. |
409 // SetPrepopulateId also sets any TemplateURLRef's prepopulated flag to true | 392 // SetPrepopulateId also sets any TemplateURLRef's prepopulated flag to true |
410 // if |id| > 0 and false otherwise. | 393 // if |id| > 0 and false otherwise. |
411 void SetPrepopulateId(int id); | 394 void SetPrepopulateId(int id); |
412 int prepopulate_id() const { return prepopulate_id_; } | 395 int prepopulate_id() const { return prepopulate_id_; } |
413 | 396 |
414 const std::string& sync_guid() const { return sync_guid_; } | 397 const std::string& sync_guid() const { return sync_guid_; } |
415 void set_sync_guid(const std::string& guid) { sync_guid_ = guid; } | 398 void set_sync_guid(const std::string& guid) { sync_guid_ = guid; } |
416 | 399 |
400 const TemplateURLRef& url_ref() const { return url_ref_; } | |
401 const TemplateURLRef& suggestions_url_ref() const { | |
402 return suggestions_url_ref_; | |
403 } | |
404 const TemplateURLRef& instant_url_ref() const { return instant_url_ref_; } | |
405 | |
417 // Returns true if |url| supports replacement. | 406 // Returns true if |url| supports replacement. |
418 bool SupportsReplacement() const; | 407 bool SupportsReplacement() const; |
419 | 408 |
420 // Like SupportsReplacement but usable on threads other than the UI thread. | 409 // Like SupportsReplacement but usable on threads other than the UI thread. |
421 bool SupportsReplacementUsingTermsData( | 410 bool SupportsReplacementUsingTermsData( |
422 const SearchTermsData& search_terms_data) const; | 411 const SearchTermsData& search_terms_data) const; |
423 | 412 |
424 std::string GetExtensionId() const; | 413 std::string GetExtensionId() const; |
425 bool IsExtensionKeyword() const; | 414 bool IsExtensionKeyword() const; |
426 | 415 |
(...skipping 11 matching lines...) Expand all Loading... | |
438 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 427 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, |
439 ResolveSyncKeywordConflict); | 428 ResolveSyncKeywordConflict); |
440 | 429 |
441 // Invalidates cached values on this object and its child TemplateURLRefs. | 430 // Invalidates cached values on this object and its child TemplateURLRefs. |
442 void InvalidateCachedValues() const; | 431 void InvalidateCachedValues() const; |
443 | 432 |
444 // Unique identifier, used when archived to the database. | 433 // Unique identifier, used when archived to the database. |
445 void set_id(TemplateURLID id) { id_ = id; } | 434 void set_id(TemplateURLID id) { id_ = id; } |
446 | 435 |
447 string16 short_name_; | 436 string16 short_name_; |
448 TemplateURLRef url_; | 437 std::string url_; |
449 TemplateURLRef suggestions_url_; | 438 std::string suggestions_url_; |
450 TemplateURLRef instant_url_; | 439 std::string instant_url_; |
451 GURL originating_url_; | 440 GURL originating_url_; |
452 mutable string16 keyword_; | 441 mutable string16 keyword_; |
453 bool autogenerate_keyword_; // If this is set, |keyword_| holds the cached | 442 bool autogenerate_keyword_; // If this is set, |keyword_| holds the cached |
454 // generated keyword if available. | 443 // generated keyword if available. |
455 mutable bool keyword_generated_; // True if the keyword was generated. This | 444 mutable bool keyword_generated_; // True if the keyword was generated. This |
456 // is used to avoid multiple attempts if | 445 // is used to avoid multiple attempts if |
457 // generating a keyword failed. | 446 // generating a keyword failed. |
458 bool show_in_default_list_; | 447 bool show_in_default_list_; |
459 bool safe_for_autoreplace_; | 448 bool safe_for_autoreplace_; |
460 GURL favicon_url_; | 449 GURL favicon_url_; |
461 // List of supported input encodings. | 450 // List of supported input encodings. |
462 std::vector<std::string> input_encodings_; | 451 std::vector<std::string> input_encodings_; |
463 TemplateURLID id_; | 452 TemplateURLID id_; |
464 base::Time date_created_; | 453 base::Time date_created_; |
465 base::Time last_modified_; | 454 base::Time last_modified_; |
466 bool created_by_policy_; | 455 bool created_by_policy_; |
467 int usage_count_; | 456 int usage_count_; |
468 int prepopulate_id_; | 457 int prepopulate_id_; |
469 // The primary unique identifier for Sync. This is only set on TemplateURLs | 458 // The primary unique identifier for Sync. This is only set on TemplateURLs |
470 // that have been associated with Sync. | 459 // that have been associated with Sync. |
471 std::string sync_guid_; | 460 std::string sync_guid_; |
472 | 461 |
462 TemplateURLRef url_ref_; | |
463 TemplateURLRef suggestions_url_ref_; | |
464 TemplateURLRef instant_url_ref_; | |
465 | |
473 // TODO(sky): Add date last parsed OSD file. | 466 // TODO(sky): Add date last parsed OSD file. |
474 }; | 467 }; |
475 | 468 |
476 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_ | 469 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_ |
OLD | NEW |