keyword_table.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_KEYWORD_TABLE_H_
  5. #define COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/compiler_specific.h"
  11. #include "base/gtest_prod_util.h"
  12. #include "components/search_engines/template_url_id.h"
  13. #include "components/webdata/common/web_database_table.h"
  14. struct TemplateURLData;
  15. class WebDatabase;
  16. namespace sql {
  17. class Statement;
  18. } // namespace sql
  19. // This class manages the |keywords| MetaTable within the SQLite database
  20. // passed to the constructor. It expects the following schema:
  21. //
  22. // Note: The database stores time in seconds, UTC.
  23. //
  24. // keywords Most of the columns mirror that of a field in
  25. // TemplateURLData. See that struct for more details.
  26. // id
  27. // short_name
  28. // keyword
  29. // favicon_url
  30. // url
  31. // safe_for_autoreplace This is set to false for any entry that was manually
  32. // added or edited by the user.
  33. // originating_url
  34. // date_created This column was added after we allowed keywords.
  35. // Keywords created before we started tracking
  36. // creation date have a value of 0 for this.
  37. // usage_count
  38. // input_encodings Semicolon separated list of supported input
  39. // encodings, may be empty.
  40. // suggest_url
  41. // prepopulate_id See TemplateURLData::prepopulate_id.
  42. // created_by_policy See TemplateURLData::created_by_policy. This was
  43. // added in version 26.
  44. // last_modified See TemplateURLData::last_modified. This was added
  45. // in version 38.
  46. // sync_guid See TemplateURLData::sync_guid. This was added in
  47. // version 39.
  48. // alternate_urls See TemplateURLData::alternate_urls. This was added
  49. // in version 47.
  50. // image_url See TemplateURLData::image_url. This was added in
  51. // version 52.
  52. // search_url_post_params See TemplateURLData::search_url_post_params. This
  53. // was added in version 52.
  54. // suggest_url_post_params See TemplateURLData::suggestions_url_post_params.
  55. // This was added in version 52.
  56. // image_url_post_params See TemplateURLData::image_url_post_params. This
  57. // was added in version 52.
  58. // new_tab_url See TemplateURLData::new_tab_url. This was added in
  59. // version 53.
  60. // last_visited See TemplateURLData::last_visited. This was added in
  61. // version 69.
  62. // created_from_play_api See TemplateURLData::created_from_play_api. This was
  63. // added in version 82.
  64. // is_active See TemplateURLData::is_active. This was added
  65. // in version 97.
  66. // starter_pack_id See TemplateURLData::starter_pack_id. This was
  67. // added in version 103.
  68. //
  69. // This class also manages some fields in the |meta| table:
  70. //
  71. // Default Search Provider ID The id of the default search provider.
  72. // Builtin Keyword Version The version of builtin keywords data.
  73. // Starter Pack Keyword Version The version of starter pack data.
  74. //
  75. class KeywordTable : public WebDatabaseTable {
  76. public:
  77. enum OperationType {
  78. ADD,
  79. REMOVE,
  80. UPDATE,
  81. };
  82. typedef std::pair<OperationType, TemplateURLData> Operation;
  83. typedef std::vector<Operation> Operations;
  84. typedef std::vector<TemplateURLData> Keywords;
  85. // Constants exposed for the benefit of test code:
  86. static const char kDefaultSearchProviderKey[];
  87. KeywordTable();
  88. KeywordTable(const KeywordTable&) = delete;
  89. KeywordTable& operator=(const KeywordTable&) = delete;
  90. ~KeywordTable() override;
  91. // Retrieves the KeywordTable* owned by |database|.
  92. static KeywordTable* FromWebDatabase(WebDatabase* db);
  93. WebDatabaseTable::TypeKey GetTypeKey() const override;
  94. bool CreateTablesIfNecessary() override;
  95. bool IsSyncable() override;
  96. bool MigrateToVersion(int version, bool* update_compatible_version) override;
  97. // Performs an arbitrary number of Add/Remove/Update operations as a single
  98. // transaction. This is provided for efficiency reasons: if the caller needs
  99. // to perform a large number of operations, doing them in a single transaction
  100. // instead of one-per-transaction can be dramatically more efficient.
  101. bool PerformOperations(const Operations& operations);
  102. // Loads the keywords into the specified vector. It's up to the caller to
  103. // delete the returned objects.
  104. // Returns true on success.
  105. bool GetKeywords(Keywords* keywords);
  106. // ID (TemplateURLData->id) of the default search provider.
  107. bool SetDefaultSearchProviderID(int64_t id);
  108. int64_t GetDefaultSearchProviderID();
  109. // Version of the built-in keywords.
  110. bool SetBuiltinKeywordVersion(int version);
  111. int GetBuiltinKeywordVersion();
  112. // Version of built-in starter pack keywords (@bookmarks, @settings, etc.).
  113. bool SetStarterPackKeywordVersion(int version);
  114. int GetStarterPackKeywordVersion();
  115. // Returns a comma-separated list of the keyword columns for the current
  116. // version of the table.
  117. static std::string GetKeywordColumns();
  118. // Table migration functions.
  119. bool MigrateToVersion53AddNewTabURLColumn();
  120. bool MigrateToVersion59RemoveExtensionKeywords();
  121. bool MigrateToVersion68RemoveShowInDefaultListColumn();
  122. bool MigrateToVersion69AddLastVisitedColumn();
  123. bool MigrateToVersion76RemoveInstantColumns();
  124. bool MigrateToVersion77IncreaseTimePrecision();
  125. bool MigrateToVersion82AddCreatedFromPlayApiColumn();
  126. bool MigrateToVersion97AddIsActiveColumn();
  127. bool MigrateToVersion103AddStarterPackIdColumn();
  128. private:
  129. friend class KeywordTableTest;
  130. // NOTE: Since the table columns have changed in different versions, many
  131. // functions below take a |table_version| argument which dictates which
  132. // version number's column set to use.
  133. // Fills |data| with the data in |s|. Returns false if we couldn't fill
  134. // |data| for some reason, e.g. |s| tried to set one of the fields to an
  135. // illegal value.
  136. static bool GetKeywordDataFromStatement(sql::Statement& s,
  137. TemplateURLData* data);
  138. // Adds a new keyword, updating the id field on success.
  139. // Returns true if successful.
  140. bool AddKeyword(const TemplateURLData& data);
  141. // Removes the specified keyword.
  142. // Returns true if successful.
  143. bool RemoveKeyword(TemplateURLID id);
  144. // Updates the database values for the specified url.
  145. // Returns true on success.
  146. bool UpdateKeyword(const TemplateURLData& data);
  147. // Gets a string representation for keyword with id specified.
  148. // Used to store its result in |meta| table or to compare with another
  149. // keyword. Returns true on success, false otherwise.
  150. bool GetKeywordAsString(TemplateURLID id,
  151. const std::string& table_name,
  152. std::string* result);
  153. };
  154. #endif // COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_