1 // Copyright (c) 2012 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 CHROME_BROWSER_WEBDATA_KEYWORD_TABLE_H_
6 #define CHROME_BROWSER_WEBDATA_KEYWORD_TABLE_H_
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/strings/string16.h"
14 #include "chrome/browser/search_engines/template_url_id.h"
15 #include "components/webdata/common/web_database_table.h"
17 struct TemplateURLData
;
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.
36 // show_in_default_list
37 // safe_for_autoreplace
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.
43 // input_encodings Semicolon separated list of supported input
44 // encodings, may be empty.
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
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 // 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
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
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
{
80 typedef std::vector
<TemplateURLData
> Keywords
;
82 // Constants exposed for the benefit of test code:
84 static const char kDefaultSearchProviderKey
[];
87 virtual ~KeywordTable();
89 // Retrieves the KeywordTable* owned by |database|.
90 static KeywordTable
* FromWebDatabase(WebDatabase
* db
);
92 virtual WebDatabaseTable::TypeKey
GetTypeKey() const OVERRIDE
;
93 virtual bool Init(sql::Connection
* db
, sql::MetaTable
* meta_table
) OVERRIDE
;
94 virtual bool IsSyncable() OVERRIDE
;
95 virtual bool MigrateToVersion(int version
,
96 bool* update_compatible_version
) OVERRIDE
;
98 // Adds a new keyword, updating the id field on success.
99 // Returns true if successful.
100 bool AddKeyword(const TemplateURLData
& data
);
102 // Removes the specified keyword.
103 // Returns true if successful.
104 bool RemoveKeyword(TemplateURLID id
);
106 // Loads the keywords into the specified vector. It's up to the caller to
107 // delete the returned objects.
108 // Returns true on success.
109 bool GetKeywords(Keywords
* keywords
);
111 // Updates the database values for the specified url.
112 // Returns true on success.
113 bool UpdateKeyword(const TemplateURLData
& data
);
115 // ID (TemplateURLData->id) of the default search provider.
116 bool SetDefaultSearchProviderID(int64 id
);
117 int64
GetDefaultSearchProviderID();
119 // Version of the built-in keywords.
120 bool SetBuiltinKeywordVersion(int version
);
121 int GetBuiltinKeywordVersion();
123 // Returns a comma-separated list of the keyword columns for the current
124 // version of the table.
125 static std::string
GetKeywordColumns();
127 // Table migration functions.
128 bool MigrateToVersion21AutoGenerateKeywordColumn();
129 bool MigrateToVersion25AddLogoIDColumn();
130 bool MigrateToVersion26AddCreatedByPolicyColumn();
131 bool MigrateToVersion28SupportsInstantColumn();
132 bool MigrateToVersion29InstantURLToSupportsInstant();
133 bool MigrateToVersion38AddLastModifiedColumn();
134 bool MigrateToVersion39AddSyncGUIDColumn();
135 bool MigrateToVersion44AddDefaultSearchProviderBackup();
136 bool MigrateToVersion45RemoveLogoIDAndAutogenerateColumns();
137 bool MigrateToVersion47AddAlternateURLsColumn();
138 bool MigrateToVersion48RemoveKeywordsBackup();
139 bool MigrateToVersion49AddSearchTermsReplacementKeyColumn();
140 bool MigrateToVersion52AddImageSearchAndPOSTSupport();
141 bool MigrateToVersion53AddNewTabURLColumn();
144 FRIEND_TEST_ALL_PREFIXES(KeywordTableTest
, GetTableContents
);
145 FRIEND_TEST_ALL_PREFIXES(KeywordTableTest
, GetTableContentsOrdering
);
146 FRIEND_TEST_ALL_PREFIXES(KeywordTableTest
, SanitizeURLs
);
147 FRIEND_TEST_ALL_PREFIXES(WebDatabaseMigrationTest
, MigrateVersion44ToCurrent
);
149 // NOTE: Since the table columns have changed in different versions, many
150 // functions below take a |table_version| argument which dictates which
151 // version number's column set to use.
153 // Fills |data| with the data in |s|. Returns false if we couldn't fill
154 // |data| for some reason, e.g. |s| tried to set one of the fields to an
156 static bool GetKeywordDataFromStatement(const sql::Statement
& s
,
157 TemplateURLData
* data
);
159 // Returns contents of selected table as a string in |contents| parameter.
160 // Returns true on success, false otherwise.
161 bool GetTableContents(const char* table_name
,
163 std::string
* contents
);
165 // Gets a string representation for keyword with id specified.
166 // Used to store its result in |meta| table or to compare with another
167 // keyword. Returns true on success, false otherwise.
168 bool GetKeywordAsString(TemplateURLID id
,
169 const std::string
& table_name
,
170 std::string
* result
);
172 // Migrates table |name| (which should be either "keywords" or
173 // "keywords_backup") from version 44 to version 45.
174 bool MigrateKeywordsTableForVersion45(const std::string
& name
);
176 DISALLOW_COPY_AND_ASSIGN(KeywordTable
);
179 #endif // CHROME_BROWSER_WEBDATA_KEYWORD_TABLE_H_