1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_
6 #define COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_
14 #include "base/compiler_specific.h"
15 #include "base/gtest_prod_util.h"
16 #include "components/search_engines/template_url_id.h"
17 #include "components/webdata/common/web_database_table.h"
19 struct TemplateURLData;
26 // This class manages the |keywords| MetaTable within the SQLite database
27 // passed to the constructor. It expects the following schema:
29 // Note: The database stores time in seconds, UTC.
31 // keywords Most of the columns mirror that of a field in
32 // TemplateURLData. See that struct for more details.
38 // safe_for_autoreplace This is set to false for any entry that was manually
39 // added or edited by the user.
41 // date_created This column was added after we allowed keywords.
42 // Keywords created before we started tracking
43 // creation date have a value of 0 for this.
45 // input_encodings Semicolon separated list of supported input
46 // encodings, may be empty.
48 // prepopulate_id See TemplateURLData::prepopulate_id.
49 // created_by_policy See TemplateURLData::created_by_policy. This was
50 // added in version 26.
51 // last_modified See TemplateURLData::last_modified. This was added
53 // sync_guid See TemplateURLData::sync_guid. This was added in
55 // alternate_urls See TemplateURLData::alternate_urls. This was added
57 // image_url See TemplateURLData::image_url. This was added in
59 // search_url_post_params See TemplateURLData::search_url_post_params. This
60 // was added in version 52.
61 // suggest_url_post_params See TemplateURLData::suggestions_url_post_params.
62 // This was added in version 52.
63 // image_url_post_params See TemplateURLData::image_url_post_params. This
64 // was added in version 52.
65 // new_tab_url See TemplateURLData::new_tab_url. This was added in
67 // last_visited See TemplateURLData::last_visited. This was added in
69 // created_from_play_api See TemplateURLData::created_from_play_api. This was
70 // added in version 82.
71 // is_active See TemplateURLData::is_active. This was added
73 // starter_pack_id See TemplateURLData::starter_pack_id. This was added
75 // enforced_by_policy See TemplateURLData::enforced_by_policy. This was
76 // added in version 112.
78 // This class also manages some fields in the |meta| table:
80 // Default Search Provider ID The id of the default search provider.
81 // Builtin Keyword Version The version of builtin keywords data.
82 // Starter Pack Keyword Version The version of starter pack data.
84 class KeywordTable : public WebDatabaseTable {
92 typedef std::pair<OperationType, TemplateURLData> Operation;
93 typedef std::vector<Operation> Operations;
94 typedef std::vector<TemplateURLData> Keywords;
96 // Constants exposed for the benefit of test code:
98 static const char kDefaultSearchProviderKey[];
102 KeywordTable(const KeywordTable&) = delete;
103 KeywordTable& operator=(const KeywordTable&) = delete;
105 ~KeywordTable() override;
107 // Retrieves the KeywordTable* owned by |database|.
108 static KeywordTable* FromWebDatabase(WebDatabase* db);
110 WebDatabaseTable::TypeKey GetTypeKey() const override;
111 bool CreateTablesIfNecessary() override;
112 bool MigrateToVersion(int version, bool* update_compatible_version) override;
114 // Performs an arbitrary number of Add/Remove/Update operations as a single
115 // transaction. This is provided for efficiency reasons: if the caller needs
116 // to perform a large number of operations, doing them in a single transaction
117 // instead of one-per-transaction can be dramatically more efficient.
118 bool PerformOperations(const Operations& operations);
120 // Loads the keywords into the specified vector. It's up to the caller to
121 // delete the returned objects.
122 // Returns true on success.
123 bool GetKeywords(Keywords* keywords);
125 // ID (TemplateURLData->id) of the default search provider.
126 bool SetDefaultSearchProviderID(int64_t id);
127 int64_t GetDefaultSearchProviderID();
129 // Version of the built-in keywords.
130 bool SetBuiltinKeywordVersion(int version);
131 int GetBuiltinKeywordVersion();
133 // Version of built-in starter pack keywords (@bookmarks, @settings, etc.).
134 bool SetStarterPackKeywordVersion(int version);
135 int GetStarterPackKeywordVersion();
137 // Returns a comma-separated list of the keyword columns for the current
138 // version of the table.
139 static std::string GetKeywordColumns();
141 // Table migration functions.
142 bool MigrateToVersion53AddNewTabURLColumn();
143 bool MigrateToVersion59RemoveExtensionKeywords();
144 bool MigrateToVersion68RemoveShowInDefaultListColumn();
145 bool MigrateToVersion69AddLastVisitedColumn();
146 bool MigrateToVersion76RemoveInstantColumns();
147 bool MigrateToVersion77IncreaseTimePrecision();
148 bool MigrateToVersion82AddCreatedFromPlayApiColumn();
149 bool MigrateToVersion97AddIsActiveColumn();
150 bool MigrateToVersion103AddStarterPackIdColumn();
151 bool MigrateToVersion112AddEnforcedByPolicyColumn();
154 friend class KeywordTableTest;
156 // NOTE: Since the table columns have changed in different versions, many
157 // functions below take a |table_version| argument which dictates which
158 // version number's column set to use.
160 // Fills |data| with the data in |s|. Returns false if we couldn't fill
161 // |data| for some reason, e.g. |s| tried to set one of the fields to an
163 static bool GetKeywordDataFromStatement(sql::Statement& s,
164 TemplateURLData* data);
166 // Adds a new keyword, updating the id field on success.
167 // Returns true if successful.
168 bool AddKeyword(const TemplateURLData& data);
170 // Removes the specified keyword.
171 // Returns true if successful.
172 bool RemoveKeyword(TemplateURLID id);
174 // Updates the database values for the specified url.
175 // Returns true on success.
176 bool UpdateKeyword(const TemplateURLData& data);
178 // Gets a string representation for keyword with id specified.
179 // Used to store its result in |meta| table or to compare with another
180 // keyword. Returns true on success, false otherwise.
181 bool GetKeywordAsString(TemplateURLID id,
182 const std::string& table_name,
183 std::string* result);
186 #endif // COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_