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> |
11 | 11 |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/time.h" | 13 #include "base/time.h" |
14 #include "chrome/browser/search_engines/template_url_id.h" | 14 #include "chrome/browser/search_engines/template_url_id.h" |
15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
16 | 16 |
17 class PrefService; | |
18 class Profile; | 17 class Profile; |
19 class SearchTermsData; | 18 class SearchTermsData; |
20 class TemplateURL; | 19 class TemplateURL; |
21 class TemplateURLParsingContext; | |
22 class WebDataService; | |
23 | 20 |
24 // TemplateURL represents the relevant portions of the Open Search Description | 21 |
25 // Document (http://www.opensearch.org/Specifications/OpenSearch). | 22 // TemplateURLRef ------------------------------------------------------------- |
26 // The main use case for TemplateURL is to use the TemplateURLRef returned by | 23 |
27 // suggestions_url or url for keyword/suggestion expansion: | 24 // A TemplateURLRef represents a single URL within the larger TemplateURL class |
28 // . suggestions_url describes a URL that is ideal for as you type suggestions. | 25 // (which represents an entire "search engine", see below). If |
29 // The returned results are in the mime type application/x-suggestions+json. | 26 // SupportsReplacement() is true, this URL has placeholders in it, for which |
30 // . url describes a URL that may be used as a shortcut. Returned results are | 27 // callers can substitute values to get a "real" URL using ReplaceSearchTerms(). |
31 // are text/html. | |
32 // Before using either one, make sure it's non-NULL, and if you intend to use | |
33 // it to replace search terms, make sure SupportsReplacement returns true. | |
34 // To use either URL invoke the ReplaceSearchTerms method on the corresponding | |
35 // TemplateURLRef. | |
36 // | 28 // |
37 // For files parsed from the Web, be sure and invoke IsValid. IsValid returns | 29 // TemplateURLRefs always have a non-NULL |owner_| TemplateURL, which they |
38 // true if the URL could be parsed. | 30 // access in order to get at important data like the underlying URL string. |
39 // | |
40 // Both TemplateURL and TemplateURLRef have value semantics. This allows the | |
41 // UI to create a copy while the user modifies the values. | |
42 class TemplateURLRef { | 31 class TemplateURLRef { |
43 public: | 32 public: |
44 // Magic numbers to pass to ReplaceSearchTerms() for the |accepted_suggestion| | 33 // Magic numbers to pass to ReplaceSearchTerms() for the |accepted_suggestion| |
45 // parameter. Most callers aren't using Suggest capabilities and should just | 34 // parameter. Most callers aren't using Suggest capabilities and should just |
46 // pass NO_SUGGESTIONS_AVAILABLE. | 35 // pass NO_SUGGESTIONS_AVAILABLE. |
47 // NOTE: Because positive values are meaningful, make sure these are negative! | 36 // NOTE: Because positive values are meaningful, make sure these are negative! |
48 enum AcceptedSuggestion { | 37 enum AcceptedSuggestion { |
49 NO_SUGGESTION_CHOSEN = -1, | 38 NO_SUGGESTION_CHOSEN = -1, |
50 NO_SUGGESTIONS_AVAILABLE = -2, | 39 NO_SUGGESTIONS_AVAILABLE = -2, |
51 }; | 40 }; |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 mutable std::string host_; | 228 mutable std::string host_; |
240 mutable std::string path_; | 229 mutable std::string path_; |
241 mutable std::string search_term_key_; | 230 mutable std::string search_term_key_; |
242 | 231 |
243 // Whether the contained URL is a pre-populated URL. | 232 // Whether the contained URL is a pre-populated URL. |
244 bool prepopulated_; | 233 bool prepopulated_; |
245 | 234 |
246 DISALLOW_COPY_AND_ASSIGN(TemplateURLRef); | 235 DISALLOW_COPY_AND_ASSIGN(TemplateURLRef); |
247 }; | 236 }; |
248 | 237 |
249 // Describes the relevant portions of a single OSD document. | 238 |
| 239 // TemplateURLData ------------------------------------------------------------ |
| 240 |
| 241 // The data for the TemplateURL. Separating this into its own class allows most |
| 242 // users to do SSA-style usage of TemplateURL: construct a TemplateURLData with |
| 243 // whatever fields are desired, then create an immutable TemplateURL from it. |
| 244 struct TemplateURLData { |
| 245 TemplateURLData(); |
| 246 ~TemplateURLData(); |
| 247 |
| 248 // A short description of the template. This is the name we show to the user |
| 249 // in various places that use TemplateURLs. For example, the location bar |
| 250 // shows this when the user selects a substituting match. |
| 251 string16 short_name; |
| 252 |
| 253 // The shortcut for this TemplateURL. May be empty. |
| 254 void SetKeyword(const string16& keyword) const; |
| 255 const string16& keyword(const TemplateURL* t_url) const; |
| 256 // TODO(pkasting): This should only be necessary until we eliminate keyword |
| 257 // autogeneration. |
| 258 const string16& raw_keyword() const { return keyword_; } |
| 259 |
| 260 // Whether to autogenerate a keyword in TemplateURL::GetKeyword(). Most |
| 261 // consumers should not need this. |
| 262 // NOTE: Calling SetKeyword() turns this back off. Manual and automatic |
| 263 // keywords are mutually exclusive. |
| 264 void SetAutogenerateKeyword(bool autogenerate_keyword) const; |
| 265 bool autogenerate_keyword() const { return autogenerate_keyword_; } |
| 266 |
| 267 // Ensures that the keyword is generated. Most consumers should not need this |
| 268 // because it is done automatically. Use this method on the UI thread, so |
| 269 // the keyword may be accessed on another thread. |
| 270 void EnsureKeyword(const TemplateURL* t_url) const; |
| 271 |
| 272 // The raw URL for the TemplateURL, which may not be valid as-is (e.g. because |
| 273 // it requires substitutions first). |
| 274 void SetURL(const std::string& url); |
| 275 const std::string& url() const { return url_; } |
| 276 |
| 277 // Optional additional raw URLs. |
| 278 std::string suggestions_url; |
| 279 std::string instant_url; |
| 280 |
| 281 // Optional favicon for the TemplateURL. |
| 282 GURL favicon_url; |
| 283 |
| 284 // URL to the OSD file this came from. May be empty. |
| 285 GURL originating_url; |
| 286 |
| 287 // Whether this TemplateURL is shown in the default list of search providers. |
| 288 // This is just a property and does not indicate whether the TemplateURL has a |
| 289 // TemplateURLRef that supports replacement. Use |
| 290 // TemplateURL::ShowInDefaultList() to test both. |
| 291 bool show_in_default_list; |
| 292 |
| 293 // Whether it's safe for auto-modification code (the autogenerator and the |
| 294 // code that imports data from other browsers) to replace the TemplateURL. |
| 295 // This should be set to false for any TemplateURL the user edits, or any |
| 296 // TemplateURL that the user clearly manually edited in the past, like a |
| 297 // bookmark keyword from another browser. |
| 298 bool safe_for_autoreplace; |
| 299 |
| 300 // The list of supported encodings for the search terms. This may be empty, |
| 301 // which indicates the terms should be encoded with UTF-8. |
| 302 std::vector<std::string> input_encodings; |
| 303 |
| 304 // Unique identifier of this TemplateURL. The unique ID is set by the |
| 305 // TemplateURLService when the TemplateURL is added to it. |
| 306 TemplateURLID id; |
| 307 |
| 308 // Date this TemplateURL was created. |
| 309 // |
| 310 // NOTE: this may be 0, which indicates the TemplateURL was created before we |
| 311 // started tracking creation time. |
| 312 base::Time date_created; |
| 313 |
| 314 // The last time this TemplateURL was modified by a user, since creation. |
| 315 // |
| 316 // NOTE: Like date_created above, this may be 0. |
| 317 base::Time last_modified; |
| 318 |
| 319 // True if this TemplateURL was automatically created by the administrator via |
| 320 // group policy. |
| 321 bool created_by_policy; |
| 322 |
| 323 // Number of times this TemplateURL has been explicitly used to load a URL. |
| 324 // We don't increment this for uses as the "default search engine" since |
| 325 // that's not really "explicit" usage and incrementing would result in pinning |
| 326 // the user's default search engine(s) to the top of the list of searches on |
| 327 // the New Tab page, de-emphasizing the omnibox as "where you go to search". |
| 328 int usage_count; |
| 329 |
| 330 // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0. |
| 331 int prepopulate_id; |
| 332 |
| 333 // The primary unique identifier for Sync. This set on all TemplateURLs |
| 334 // regardless of whether they have been associated with Sync. |
| 335 std::string sync_guid; |
| 336 |
| 337 private: |
| 338 // Private so we can enforce using the setters. |
| 339 // TODO(pkasting): For now these setters are not critical, but later we will |
| 340 // begin using them to ensure that these fields are non-empty. |
| 341 mutable string16 keyword_; |
| 342 std::string url_; |
| 343 |
| 344 // TODO(pkasting): These fields will go away soon. |
| 345 mutable bool autogenerate_keyword_; |
| 346 // True if the keyword was generated. This is used to avoid multiple attempts |
| 347 // if generating a keyword failed. |
| 348 mutable bool keyword_generated_; |
| 349 }; |
| 350 |
| 351 |
| 352 // TemplateURL ---------------------------------------------------------------- |
| 353 |
| 354 // A TemplateURL represents a single "search engine", defined primarily as a |
| 355 // subset of the Open Search Description Document |
| 356 // (http://www.opensearch.org/Specifications/OpenSearch) plus some extensions. |
| 357 // One TemplateURL contains several TemplateURLRefs, which correspond to various |
| 358 // different capabilities (e.g. doing searches or getting suggestions), as well |
| 359 // as a TemplateURLData containing other details like the name, keyword, etc. |
| 360 // |
| 361 // TemplateURLs are intended to be read-only for most users; the only public |
| 362 // non-const method is the Profile getter, which returns a non-const Profile*. |
| 363 // The TemplateURLService, which handles storing and manipulating TemplateURLs, |
| 364 // is made a friend so that it can be the exception to this pattern. |
250 class TemplateURL { | 365 class TemplateURL { |
251 public: | 366 public: |
252 TemplateURL(); | 367 explicit TemplateURL(const TemplateURLData& data); |
253 | 368 |
254 TemplateURL(const TemplateURL& other); | 369 TemplateURL(const TemplateURL& other); |
255 TemplateURL& operator=(const TemplateURL& other); | 370 TemplateURL& operator=(const TemplateURL& other); |
256 | 371 |
257 ~TemplateURL(); | 372 ~TemplateURL(); |
258 | 373 |
259 // Generates a favicon URL from the specified url. | 374 // Generates a favicon URL from the specified url. |
260 static GURL GenerateFaviconURL(const GURL& url); | 375 static GURL GenerateFaviconURL(const GURL& url); |
261 | 376 |
262 // A short description of the template. This is the name we show to the user | 377 const TemplateURLData& data() const { return data_; } |
263 // in various places that use keywords. For example, the location bar shows | 378 |
264 // this when the user selects the keyword. | 379 const string16& short_name() const { return data_.short_name; } |
265 void set_short_name(const string16& short_name) { | |
266 short_name_ = short_name; | |
267 } | |
268 const string16& short_name() const { return short_name_; } | |
269 // An accessor for the short_name, but adjusted so it can be appropriately | 380 // An accessor for the short_name, but adjusted so it can be appropriately |
270 // displayed even if it is LTR and the UI is RTL. | 381 // displayed even if it is LTR and the UI is RTL. |
271 string16 AdjustedShortNameForLocaleDirection() const; | 382 string16 AdjustedShortNameForLocaleDirection() const; |
272 | 383 |
273 // Parameterized URL for providing the results. | 384 const string16& keyword() const { return data_.keyword(this); } |
274 void SetURL(const std::string& url); | 385 bool autogenerate_keyword() const { |
275 const std::string& url() const { return url_; } | 386 return data_.autogenerate_keyword(); |
| 387 } |
| 388 void EnsureKeyword() const { data_.EnsureKeyword(this); } |
276 | 389 |
277 // URL providing JSON results. This is typically used to provide suggestions | 390 const std::string& url() const { return data_.url(); } |
278 // as you type. | 391 const std::string& suggestions_url() const { return data_.suggestions_url; } |
279 void SetSuggestionsURL(const std::string& url); | 392 const std::string& instant_url() const { return data_.instant_url; } |
280 const std::string& suggestions_url() const { return suggestions_url_; } | 393 const GURL& favicon_url() const { return data_.favicon_url; } |
281 | 394 |
282 // Parameterized URL for instant results. | 395 const GURL& originating_url() const { return data_.originating_url; } |
283 void SetInstantURL(const std::string& url); | |
284 const std::string& instant_url() const { return instant_url_; } | |
285 | 396 |
286 // URL to the OSD file this came from. May be empty. | 397 bool show_in_default_list() const { return data_.show_in_default_list; } |
287 void set_originating_url(const GURL& url) { | |
288 originating_url_ = url; | |
289 } | |
290 const GURL& originating_url() const { return originating_url_; } | |
291 | |
292 // The shortcut for this template url. May be empty. | |
293 void set_keyword(const string16& keyword); | |
294 const string16& keyword() const; | |
295 | |
296 // Whether to autogenerate a keyword from the url() in GetKeyword(). Most | |
297 // consumers should not need this. | |
298 // NOTE: Calling set_keyword() turns this back off. Manual and automatic | |
299 // keywords are mutually exclusive. | |
300 void set_autogenerate_keyword(bool autogenerate_keyword) { | |
301 autogenerate_keyword_ = autogenerate_keyword; | |
302 if (autogenerate_keyword_) { | |
303 keyword_.clear(); | |
304 keyword_generated_ = false; | |
305 } | |
306 } | |
307 bool autogenerate_keyword() const { | |
308 return autogenerate_keyword_; | |
309 } | |
310 | |
311 // Ensures that the keyword is generated. Most consumers should not need this | |
312 // because it is done automatically. Use this method on the UI thread, so | |
313 // the keyword may be accessed on another thread. | |
314 void EnsureKeyword() const; | |
315 | |
316 // Whether this keyword is shown in the default list of search providers. This | |
317 // is just a property and does not indicate whether this TemplateURL has | |
318 // a TemplateURLRef that supports replacement. Use ShowInDefaultList to | |
319 // test both. | |
320 // The default value is false. | |
321 void set_show_in_default_list(bool show_in_default_list) { | |
322 show_in_default_list_ = show_in_default_list; | |
323 } | |
324 bool show_in_default_list() const { return show_in_default_list_; } | |
325 | 398 |
326 // Returns true if show_in_default_list() is true and this TemplateURL has a | 399 // Returns true if show_in_default_list() is true and this TemplateURL has a |
327 // TemplateURLRef that supports replacement. | 400 // TemplateURLRef that supports replacement. |
328 bool ShowInDefaultList() const; | 401 bool ShowInDefaultList() const; |
329 | 402 |
330 // Whether it's safe for auto-modification code (the autogenerator and the | 403 bool safe_for_autoreplace() const { return data_.safe_for_autoreplace; } |
331 // code that imports data from other browsers) to replace the TemplateURL. | |
332 // This should be set to false for any keyword the user edits, or any keyword | |
333 // that the user clearly manually edited in the past, like a bookmark keyword | |
334 // from another browser. | |
335 void set_safe_for_autoreplace(bool safe_for_autoreplace) { | |
336 safe_for_autoreplace_ = safe_for_autoreplace; | |
337 } | |
338 bool safe_for_autoreplace() const { return safe_for_autoreplace_; } | |
339 | 404 |
340 // The favicon. This is optional. | |
341 void set_favicon_url(const GURL& url) { favicon_url_ = url; } | |
342 const GURL& favicon_url() const { return favicon_url_; } | |
343 | |
344 // Date this keyword was created. | |
345 // | |
346 // NOTE: this may be 0, which indicates the keyword was created before we | |
347 // started tracking creation time. | |
348 void set_date_created(base::Time time) { date_created_ = time; } | |
349 base::Time date_created() const { return date_created_; } | |
350 | |
351 // The last time this keyword was modified by a user, since creation. | |
352 // | |
353 // NOTE: Like date_created above, this may be 0. | |
354 void set_last_modified(base::Time time) { last_modified_ = time; } | |
355 base::Time last_modified() const { return last_modified_; } | |
356 | |
357 // True if this TemplateURL was automatically created by the administrator via | |
358 // group policy. | |
359 void set_created_by_policy(bool created_by_policy) { | |
360 created_by_policy_ = created_by_policy; | |
361 } | |
362 bool created_by_policy() const { return created_by_policy_; } | |
363 | |
364 // Number of times this keyword has been explicitly used to load a URL. We | |
365 // don't increment this for uses as the "default search engine" since that's | |
366 // not really "explicit" usage and incrementing would result in pinning the | |
367 // user's default search engine(s) to the top of the list of searches on the | |
368 // New Tab page, de-emphasizing the omnibox as "where you go to search". | |
369 void set_usage_count(int count) { usage_count_ = count; } | |
370 int usage_count() const { return usage_count_; } | |
371 | |
372 // The list of supported encodings for the search terms. This may be empty, | |
373 // which indicates the terms should be encoded with UTF-8. | |
374 void set_input_encodings(const std::vector<std::string>& encodings) { | |
375 input_encodings_ = encodings; | |
376 } | |
377 void add_input_encoding(const std::string& encoding) { | |
378 input_encodings_.push_back(encoding); | |
379 } | |
380 const std::vector<std::string>& input_encodings() const { | 405 const std::vector<std::string>& input_encodings() const { |
381 return input_encodings_; | 406 return data_.input_encodings; |
382 } | 407 } |
383 | 408 |
384 // Returns the unique identifier of this TemplateURL. The unique ID is set | 409 TemplateURLID id() const { return data_.id; } |
385 // by the TemplateURLService when the TemplateURL is added to it. | |
386 TemplateURLID id() const { return id_; } | |
387 | 410 |
388 // Copies the data from |other|'s TemplateURLRef members into |this|. | 411 base::Time date_created() const { return data_.date_created; } |
389 void CopyURLRefs(const TemplateURL& other); | 412 base::Time last_modified() const { return data_.last_modified; } |
390 | 413 |
391 // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0. | 414 bool created_by_policy() const { return data_.created_by_policy; } |
392 // SetPrepopulateId also sets any TemplateURLRef's prepopulated flag to true | |
393 // if |id| > 0 and false otherwise. | |
394 void SetPrepopulateId(int id); | |
395 int prepopulate_id() const { return prepopulate_id_; } | |
396 | 415 |
397 const std::string& sync_guid() const { return sync_guid_; } | 416 int usage_count() const { return data_.usage_count; } |
398 void set_sync_guid(const std::string& guid) { sync_guid_ = guid; } | 417 |
| 418 int prepopulate_id() const { return data_.prepopulate_id; } |
| 419 |
| 420 const std::string& sync_guid() const { return data_.sync_guid; } |
399 | 421 |
400 const TemplateURLRef& url_ref() const { return url_ref_; } | 422 const TemplateURLRef& url_ref() const { return url_ref_; } |
401 const TemplateURLRef& suggestions_url_ref() const { | 423 const TemplateURLRef& suggestions_url_ref() const { |
402 return suggestions_url_ref_; | 424 return suggestions_url_ref_; |
403 } | 425 } |
404 const TemplateURLRef& instant_url_ref() const { return instant_url_ref_; } | 426 const TemplateURLRef& instant_url_ref() const { return instant_url_ref_; } |
405 | 427 |
406 // Returns true if |url| supports replacement. | 428 // Returns true if |url| supports replacement. |
407 bool SupportsReplacement() const; | 429 bool SupportsReplacement() const; |
408 | 430 |
409 // Like SupportsReplacement but usable on threads other than the UI thread. | 431 // Like SupportsReplacement but usable on threads other than the UI thread. |
410 bool SupportsReplacementUsingTermsData( | 432 bool SupportsReplacementUsingTermsData( |
411 const SearchTermsData& search_terms_data) const; | 433 const SearchTermsData& search_terms_data) const; |
412 | 434 |
413 std::string GetExtensionId() const; | 435 std::string GetExtensionId() const; |
414 bool IsExtensionKeyword() const; | 436 bool IsExtensionKeyword() const; |
415 | 437 |
416 private: | 438 private: |
417 friend void MergeEnginesFromPrepopulateData( | |
418 PrefService* prefs, | |
419 WebDataService* service, | |
420 std::vector<TemplateURL*>* template_urls, | |
421 const TemplateURL** default_search_provider); | |
422 friend class KeywordTable; | |
423 friend class KeywordTableTest; | |
424 friend class SearchHostToURLsMap; | |
425 friend class TemplateURLParsingContext; | |
426 friend class TemplateURLService; | 439 friend class TemplateURLService; |
427 FRIEND_TEST_ALL_PREFIXES(TemplateURLServiceSyncTest, | 440 |
428 ResolveSyncKeywordConflict); | 441 void SetURL(const std::string& url); |
| 442 void SetPrepopulateId(int id); |
429 | 443 |
430 // Invalidates cached values on this object and its child TemplateURLRefs. | 444 // Invalidates cached values on this object and its child TemplateURLRefs. |
431 void InvalidateCachedValues() const; | 445 void InvalidateCachedValues() const; |
432 | 446 |
433 // Unique identifier, used when archived to the database. | 447 TemplateURLData data_; |
434 void set_id(TemplateURLID id) { id_ = id; } | |
435 | |
436 string16 short_name_; | |
437 std::string url_; | |
438 std::string suggestions_url_; | |
439 std::string instant_url_; | |
440 GURL originating_url_; | |
441 mutable string16 keyword_; | |
442 bool autogenerate_keyword_; // If this is set, |keyword_| holds the cached | |
443 // generated keyword if available. | |
444 mutable bool keyword_generated_; // True if the keyword was generated. This | |
445 // is used to avoid multiple attempts if | |
446 // generating a keyword failed. | |
447 bool show_in_default_list_; | |
448 bool safe_for_autoreplace_; | |
449 GURL favicon_url_; | |
450 // List of supported input encodings. | |
451 std::vector<std::string> input_encodings_; | |
452 TemplateURLID id_; | |
453 base::Time date_created_; | |
454 base::Time last_modified_; | |
455 bool created_by_policy_; | |
456 int usage_count_; | |
457 int prepopulate_id_; | |
458 // The primary unique identifier for Sync. This is only set on TemplateURLs | |
459 // that have been associated with Sync. | |
460 std::string sync_guid_; | |
461 | |
462 TemplateURLRef url_ref_; | 448 TemplateURLRef url_ref_; |
463 TemplateURLRef suggestions_url_ref_; | 449 TemplateURLRef suggestions_url_ref_; |
464 TemplateURLRef instant_url_ref_; | 450 TemplateURLRef instant_url_ref_; |
465 | 451 |
466 // TODO(sky): Add date last parsed OSD file. | 452 // TODO(sky): Add date last parsed OSD file. |
467 }; | 453 }; |
468 | 454 |
469 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_ | 455 #endif // CHROME_BROWSER_SEARCH_ENGINES_TEMPLATE_URL_H_ |
OLD | NEW |