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. 4 5 #ifndef CHROME_BROWSER_WEBDATA_KEYWORD_TABLE_H_ 6 #define CHROME_BROWSER_WEBDATA_KEYWORD_TABLE_H_ 7 8 #include <string> 9 #include <vector> 10 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" 16 17 struct TemplateURLData; 18 class WebDatabase; 19 20 namespace sql { 21 class Statement; 22 } // namespace sql 23 24 // This class manages the |keywords| MetaTable within the SQLite database 25 // passed to the constructor. It expects the following schema: 26 // 27 // Note: The database stores time in seconds, UTC. 28 // 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. 72 // 73 // This class also manages some fields in the |meta| table: 74 // 75 // Default Search Provider ID The id of the default search provider. 76 // Builtin Keyword Version The version of builtin keywords data. 77 // 78 class KeywordTable : public WebDatabaseTable { 79 public: 80 typedef std::vector<TemplateURLData> Keywords; 81 82 // Constants exposed for the benefit of test code: 83 84 static const char kDefaultSearchProviderKey[]; 85 86 KeywordTable(); 87 virtual ~KeywordTable(); 88 89 // Retrieves the KeywordTable* owned by |database|. 90 static KeywordTable* FromWebDatabase(WebDatabase* db); 91 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; 97 98 // Adds a new keyword, updating the id field on success. 99 // Returns true if successful. 100 bool AddKeyword(const TemplateURLData& data); 101 102 // Removes the specified keyword. 103 // Returns true if successful. 104 bool RemoveKeyword(TemplateURLID id); 105 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); 110 111 // Updates the database values for the specified url. 112 // Returns true on success. 113 bool UpdateKeyword(const TemplateURLData& data); 114 115 // ID (TemplateURLData->id) of the default search provider. 116 bool SetDefaultSearchProviderID(int64 id); 117 int64 GetDefaultSearchProviderID(); 118 119 // Version of the built-in keywords. 120 bool SetBuiltinKeywordVersion(int version); 121 int GetBuiltinKeywordVersion(); 122 123 // Returns a comma-separated list of the keyword columns for the current 124 // version of the table. 125 static std::string GetKeywordColumns(); 126 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(); 142 143 private: 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); 148 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. 152 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 155 // illegal value. 156 static bool GetKeywordDataFromStatement(const sql::Statement& s, 157 TemplateURLData* data); 158 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, 162 int table_version, 163 std::string* contents); 164 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); 171 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); 175 176 DISALLOW_COPY_AND_ASSIGN(KeywordTable); 177 }; 178 179 #endif // CHROME_BROWSER_WEBDATA_KEYWORD_TABLE_H_ 180