[Sync] Add auto-generated ModelType in Java.
[chromium-blink-merge.git] / components / search_engines / keyword_table.h
blobef4569424fa93fdac48db574b8540357c2b6b26a
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.
5 #ifndef COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_
6 #define COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_
8 #include <string>
9 #include <vector>
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/strings/string16.h"
14 #include "components/search_engines/template_url_id.h"
15 #include "components/webdata/common/web_database_table.h"
17 struct TemplateURLData;
18 class WebDatabase;
20 namespace sql {
21 class Statement;
22 } // namespace sql
24 // This class manages the |keywords| MetaTable within the SQLite database
25 // passed to the constructor. It expects the following schema:
27 // Note: The database stores time in seconds, UTC.
29 // keywords Most of the columns mirror that of a field in
30 // TemplateURLData. See that struct for more details.
31 // id
32 // short_name
33 // keyword
34 // favicon_url
35 // url
36 // show_in_default_list
37 // safe_for_autoreplace
38 // originating_url
39 // date_created This column was added after we allowed keywords.
40 // Keywords created before we started tracking
41 // creation date have a value of 0 for this.
42 // usage_count
43 // input_encodings Semicolon separated list of supported input
44 // encodings, may be empty.
45 // suggest_url
46 // prepopulate_id See TemplateURLData::prepopulate_id.
47 // created_by_policy See TemplateURLData::created_by_policy. This was
48 // added in version 26.
49 // instant_url See TemplateURLData::instant_url. This was added in
50 // version 29.
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 // search_terms_replacement_key
58 // See TemplateURLData::search_terms_replacement_key.
59 // This was added in version 49.
60 // image_url See TemplateURLData::image_url. This was added in
61 // version 52.
62 // search_url_post_params See TemplateURLData::search_url_post_params. This
63 // was added in version 52.
64 // suggest_url_post_params See TemplateURLData::suggestions_url_post_params.
65 // This was added in version 52.
66 // instant_url_post_params See TemplateURLData::instant_url_post_params. This
67 // was added in version 52.
68 // image_url_post_params See TemplateURLData::image_url_post_params. This
69 // was added in version 52.
70 // new_tab_url See TemplateURLData::new_tab_url. This was added in
71 // version 53.
73 // This class also manages some fields in the |meta| table:
75 // Default Search Provider ID The id of the default search provider.
76 // Builtin Keyword Version The version of builtin keywords data.
78 class KeywordTable : public WebDatabaseTable {
79 public:
80 enum OperationType {
81 ADD,
82 REMOVE,
83 UPDATE,
86 typedef std::pair<OperationType, TemplateURLData> Operation;
87 typedef std::vector<Operation> Operations;
88 typedef std::vector<TemplateURLData> Keywords;
90 // Constants exposed for the benefit of test code:
92 static const char kDefaultSearchProviderKey[];
94 KeywordTable();
95 ~KeywordTable() override;
97 // Retrieves the KeywordTable* owned by |database|.
98 static KeywordTable* FromWebDatabase(WebDatabase* db);
100 WebDatabaseTable::TypeKey GetTypeKey() const override;
101 bool CreateTablesIfNecessary() override;
102 bool IsSyncable() override;
103 bool MigrateToVersion(int version, bool* update_compatible_version) override;
105 // Performs an arbitrary number of Add/Remove/Update operations as a single
106 // transaction. This is provided for efficiency reasons: if the caller needs
107 // to perform a large number of operations, doing them in a single transaction
108 // instead of one-per-transaction can be dramatically more efficient.
109 bool PerformOperations(const Operations& operations);
111 // Loads the keywords into the specified vector. It's up to the caller to
112 // delete the returned objects.
113 // Returns true on success.
114 bool GetKeywords(Keywords* keywords);
116 // ID (TemplateURLData->id) of the default search provider.
117 bool SetDefaultSearchProviderID(int64 id);
118 int64 GetDefaultSearchProviderID();
120 // Version of the built-in keywords.
121 bool SetBuiltinKeywordVersion(int version);
122 int GetBuiltinKeywordVersion();
124 // Returns a comma-separated list of the keyword columns for the current
125 // version of the table.
126 static std::string GetKeywordColumns();
128 // Table migration functions.
129 bool MigrateToVersion53AddNewTabURLColumn();
130 bool MigrateToVersion59RemoveExtensionKeywords();
132 private:
133 friend class KeywordTableTest;
134 FRIEND_TEST_ALL_PREFIXES(WebDatabaseMigrationTest, MigrateVersion44ToCurrent);
136 // NOTE: Since the table columns have changed in different versions, many
137 // functions below take a |table_version| argument which dictates which
138 // version number's column set to use.
140 // Fills |data| with the data in |s|. Returns false if we couldn't fill
141 // |data| for some reason, e.g. |s| tried to set one of the fields to an
142 // illegal value.
143 static bool GetKeywordDataFromStatement(const sql::Statement& s,
144 TemplateURLData* data);
146 // Adds a new keyword, updating the id field on success.
147 // Returns true if successful.
148 bool AddKeyword(const TemplateURLData& data);
150 // Removes the specified keyword.
151 // Returns true if successful.
152 bool RemoveKeyword(TemplateURLID id);
154 // Updates the database values for the specified url.
155 // Returns true on success.
156 bool UpdateKeyword(const TemplateURLData& data);
158 // Gets a string representation for keyword with id specified.
159 // Used to store its result in |meta| table or to compare with another
160 // keyword. Returns true on success, false otherwise.
161 bool GetKeywordAsString(TemplateURLID id,
162 const std::string& table_name,
163 std::string* result);
165 // Migrates table |name| (which should be either "keywords" or
166 // "keywords_backup") from version 44 to version 45.
167 bool MigrateKeywordsTableForVersion45(const std::string& name);
169 DISALLOW_COPY_AND_ASSIGN(KeywordTable);
172 #endif // COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_