template_url_data.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_DATA_H_
  5. #define COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_DATA_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/time/time.h"
  9. #include "base/values.h"
  10. #include "components/search_engines/template_url_id.h"
  11. #include "url/gurl.h"
  12. // The data for the TemplateURL. Separating this into its own class allows most
  13. // users to do SSA-style usage of TemplateURL: construct a TemplateURLData with
  14. // whatever fields are desired, then create an immutable TemplateURL from it.
  15. struct TemplateURLData {
  16. TemplateURLData();
  17. TemplateURLData(const TemplateURLData& other);
  18. TemplateURLData& operator=(const TemplateURLData& other);
  19. // Creates a TemplateURLData suitable for prepopulated engines.
  20. // Note that unlike in the default constructor, |safe_for_autoreplace| will
  21. // be set to true. date_created and last_modified will be set to null time
  22. // value, instead of current time.
  23. // StringPiece in arguments is used to pass const char* pointer members
  24. // of PrepopulatedEngine structure which can be nullptr.
  25. TemplateURLData(const std::u16string& name,
  26. const std::u16string& keyword,
  27. base::StringPiece search_url,
  28. base::StringPiece suggest_url,
  29. base::StringPiece image_url,
  30. base::StringPiece new_tab_url,
  31. base::StringPiece contextual_search_url,
  32. base::StringPiece logo_url,
  33. base::StringPiece doodle_url,
  34. base::StringPiece search_url_post_params,
  35. base::StringPiece suggest_url_post_params,
  36. base::StringPiece image_url_post_params,
  37. base::StringPiece side_search_param,
  38. base::StringPiece favicon_url,
  39. base::StringPiece encoding,
  40. const base::Value& alternate_urls_list,
  41. bool preconnect_to_search_url,
  42. bool prefetch_likely_navigations,
  43. int prepopulate_id);
  44. ~TemplateURLData();
  45. // A short description of the template. This is the name we show to the user
  46. // in various places that use TemplateURLs. For example, the location bar
  47. // shows this when the user selects a substituting match.
  48. void SetShortName(const std::u16string& short_name);
  49. const std::u16string& short_name() const { return short_name_; }
  50. // The shortcut for this TemplateURL. |keyword| must be non-empty.
  51. void SetKeyword(const std::u16string& keyword);
  52. const std::u16string& keyword() const { return keyword_; }
  53. // The raw URL for the TemplateURL, which may not be valid as-is (e.g. because
  54. // it requires substitutions first). This must be non-empty.
  55. void SetURL(const std::string& url);
  56. const std::string& url() const { return url_; }
  57. // Recomputes |sync_guid| using the same logic as in the constructor. This
  58. // means a random GUID is generated, except for built-in search engines,
  59. // which generate GUIDs deterministically based on |prepopulate_id| or
  60. // |starter_pack_id|.
  61. void GenerateSyncGUID();
  62. // Estimates dynamic memory usage.
  63. // See base/trace_event/memory_usage_estimator.h for more info.
  64. size_t EstimateMemoryUsage() const;
  65. // Optional additional raw URLs.
  66. std::string suggestions_url;
  67. std::string image_url;
  68. std::string new_tab_url;
  69. std::string contextual_search_url;
  70. // Optional URL for the logo.
  71. GURL logo_url;
  72. // Optional URL for the Doodle.
  73. GURL doodle_url;
  74. // The following post_params are comma-separated lists used to specify the
  75. // post parameters for the corresponding URL.
  76. std::string search_url_post_params;
  77. std::string suggestions_url_post_params;
  78. std::string image_url_post_params;
  79. // The parameter appended to the engine's search URL when constructing the URL
  80. // for the side search side panel.
  81. std::string side_search_param;
  82. // Favicon for the TemplateURL.
  83. GURL favicon_url;
  84. // URL to the OSD file this came from. May be empty.
  85. GURL originating_url;
  86. // Whether it's safe for auto-modification code (the autogenerator and the
  87. // code that imports data from other browsers) to replace the TemplateURL.
  88. // This should be set to false for any TemplateURL the user edits, or any
  89. // TemplateURL that the user clearly manually edited in the past, like a
  90. // bookmark keyword from another browser.
  91. bool safe_for_autoreplace;
  92. // The list of supported encodings for the search terms. This may be empty,
  93. // which indicates the terms should be encoded with UTF-8.
  94. std::vector<std::string> input_encodings;
  95. // Unique identifier of this TemplateURL. The unique ID is set by the
  96. // TemplateURLService when the TemplateURL is added to it.
  97. TemplateURLID id;
  98. // Date this TemplateURL was created.
  99. //
  100. // NOTE: this may be 0, which indicates the TemplateURL was created before we
  101. // started tracking creation time.
  102. base::Time date_created;
  103. // The last time this TemplateURL was modified by a user, since creation.
  104. //
  105. // NOTE: Like date_created above, this may be 0.
  106. base::Time last_modified;
  107. // Date when this TemplateURL was last visited.
  108. //
  109. // NOTE: This might be 0 if the TemplateURL has never been visited.
  110. base::Time last_visited;
  111. // True if this TemplateURL was automatically created by the administrator via
  112. // group policy.
  113. bool created_by_policy;
  114. // True if this TemplateURL was created from metadata received from Play API.
  115. bool created_from_play_api;
  116. // Number of times this TemplateURL has been explicitly used to load a URL.
  117. // We don't increment this for uses as the "default search engine" since
  118. // that's not really "explicit" usage and incrementing would result in pinning
  119. // the user's default search engine(s) to the top of the list of searches on
  120. // the New Tab page, de-emphasizing the omnibox as "where you go to search".
  121. int usage_count;
  122. // If this TemplateURL comes from prepopulated data the prepopulate_id is > 0.
  123. int prepopulate_id;
  124. // The primary unique identifier for Sync. This set on all TemplateURLs
  125. // regardless of whether they have been associated with Sync.
  126. std::string sync_guid;
  127. // A list of URL patterns that can be used, in addition to |url_|, to extract
  128. // search terms from a URL.
  129. std::vector<std::string> alternate_urls;
  130. // Whether a connection to |url_| should regularly be established when this is
  131. // set as the "default search engine".
  132. bool preconnect_to_search_url = false;
  133. // Whether the client is allowed to prefetch Search queries that are likely
  134. // (in addition to queries that are recommended via suggestion server). This
  135. // is experimental.
  136. bool prefetch_likely_navigations = false;
  137. enum class ActiveStatus {
  138. kUnspecified = 0, // The default value when a search engine is auto-added.
  139. kTrue, // Search engine is active.
  140. kFalse, // SE has been manually deactivated by a user.
  141. };
  142. // Whether this entry is "active". Active entries can be invoked by keyword
  143. // via the omnibox. Inactive search engines do nothing until they have been
  144. // activated. A search engine is inactive if it's unspecified or false.
  145. ActiveStatus is_active{ActiveStatus::kUnspecified};
  146. // This TemplateURL is part of the built-in "starter pack" if
  147. // starter_pack_id > 0.
  148. int starter_pack_id{0};
  149. private:
  150. // Private so we can enforce using the setters and thus enforce that these
  151. // fields are never empty.
  152. std::u16string short_name_;
  153. std::u16string keyword_;
  154. std::string url_;
  155. };
  156. #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_DATA_H_