Upload upstream chromium 114.0.5735.31
[platform/framework/web/chromium-efl.git] / components / search_engines / keyword_table.h
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.
4
5 #ifndef COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_
6 #define COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_
7
8 #include <stdint.h>
9
10 #include <string>
11 #include <utility>
12 #include <vector>
13
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"
18
19 struct TemplateURLData;
20 class WebDatabase;
21
22 namespace sql {
23 class Statement;
24 }  // namespace sql
25
26 // This class manages the |keywords| MetaTable within the SQLite database
27 // passed to the constructor. It expects the following schema:
28 //
29 // Note: The database stores time in seconds, UTC.
30 //
31 // keywords                 Most of the columns mirror that of a field in
32 //                          TemplateURLData.  See that struct for more details.
33 //   id
34 //   short_name
35 //   keyword
36 //   favicon_url
37 //   url
38 //   safe_for_autoreplace   This is set to false for any entry that was manually
39 //                          added or edited by the user.
40 //   originating_url
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.
44 //   usage_count
45 //   input_encodings        Semicolon separated list of supported input
46 //                          encodings, may be empty.
47 //   suggest_url
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
52 //                          in version 38.
53 //   sync_guid              See TemplateURLData::sync_guid. This was added in
54 //                          version 39.
55 //   alternate_urls         See TemplateURLData::alternate_urls. This was added
56 //                          in version 47.
57 //   image_url              See TemplateURLData::image_url. This was added in
58 //                          version 52.
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
66 //                          version 53.
67 //   last_visited           See TemplateURLData::last_visited. This was added in
68 //                          version 69.
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
72 //                          in version 97.
73 //   starter_pack_id        See TemplateURLData::starter_pack_id. This was added
74 //                          in version 103.
75 //   enforced_by_policy     See TemplateURLData::enforced_by_policy. This was
76 //                          added in version 112.
77 //
78 // This class also manages some fields in the |meta| table:
79 //
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.
83 //
84 class KeywordTable : public WebDatabaseTable {
85  public:
86   enum OperationType {
87     ADD,
88     REMOVE,
89     UPDATE,
90   };
91
92   typedef std::pair<OperationType, TemplateURLData> Operation;
93   typedef std::vector<Operation> Operations;
94   typedef std::vector<TemplateURLData> Keywords;
95
96   // Constants exposed for the benefit of test code:
97
98   static const char kDefaultSearchProviderKey[];
99
100   KeywordTable();
101
102   KeywordTable(const KeywordTable&) = delete;
103   KeywordTable& operator=(const KeywordTable&) = delete;
104
105   ~KeywordTable() override;
106
107   // Retrieves the KeywordTable* owned by |database|.
108   static KeywordTable* FromWebDatabase(WebDatabase* db);
109
110   WebDatabaseTable::TypeKey GetTypeKey() const override;
111   bool CreateTablesIfNecessary() override;
112   bool MigrateToVersion(int version, bool* update_compatible_version) override;
113
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);
119
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);
124
125   // ID (TemplateURLData->id) of the default search provider.
126   bool SetDefaultSearchProviderID(int64_t id);
127   int64_t GetDefaultSearchProviderID();
128
129   // Version of the built-in keywords.
130   bool SetBuiltinKeywordVersion(int version);
131   int GetBuiltinKeywordVersion();
132
133   // Version of built-in starter pack keywords (@bookmarks, @settings, etc.).
134   bool SetStarterPackKeywordVersion(int version);
135   int GetStarterPackKeywordVersion();
136
137   // Returns a comma-separated list of the keyword columns for the current
138   // version of the table.
139   static std::string GetKeywordColumns();
140
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();
152
153  private:
154   friend class KeywordTableTest;
155
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.
159
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
162   // illegal value.
163   static bool GetKeywordDataFromStatement(sql::Statement& s,
164                                           TemplateURLData* data);
165
166   // Adds a new keyword, updating the id field on success.
167   // Returns true if successful.
168   bool AddKeyword(const TemplateURLData& data);
169
170   // Removes the specified keyword.
171   // Returns true if successful.
172   bool RemoveKeyword(TemplateURLID id);
173
174   // Updates the database values for the specified url.
175   // Returns true on success.
176   bool UpdateKeyword(const TemplateURLData& data);
177
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);
184 };
185
186 #endif  // COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_