Revert "Enable by default drive search on all channels and LSP API on dev."
[chromium-blink-merge.git] / components / webdata / common / web_database.cc
blobf294dfa6616d0389da6d3bf6d09c89a0f397c5be
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 #include "components/webdata/common/web_database.h"
7 #include <algorithm>
9 #include "base/stl_util.h"
10 #include "sql/transaction.h"
12 // Current version number. Note: when changing the current version number,
13 // corresponding changes must happen in the unit tests, and new migration test
14 // added. See |WebDatabaseMigrationTest::kCurrentTestedVersionNumber|.
15 // static
16 const int WebDatabase::kCurrentVersionNumber = 65;
18 const int WebDatabase::kDeprecatedVersionNumber = 51;
20 namespace {
22 const int kCompatibleVersionNumber = 61;
24 // Change the version number and possibly the compatibility version of
25 // |meta_table_|.
26 void ChangeVersion(sql::MetaTable* meta_table,
27 int version_num,
28 bool update_compatible_version_num) {
29 meta_table->SetVersionNumber(version_num);
30 if (update_compatible_version_num) {
31 meta_table->SetCompatibleVersionNumber(
32 std::min(version_num, kCompatibleVersionNumber));
36 // Outputs the failed version number as a warning and always returns
37 // |sql::INIT_FAILURE|.
38 sql::InitStatus FailedMigrationTo(int version_num) {
39 LOG(WARNING) << "Unable to update web database to version "
40 << version_num << ".";
41 NOTREACHED();
42 return sql::INIT_FAILURE;
45 } // namespace
47 WebDatabase::WebDatabase() {}
49 WebDatabase::~WebDatabase() {
52 void WebDatabase::AddTable(WebDatabaseTable* table) {
53 tables_[table->GetTypeKey()] = table;
56 WebDatabaseTable* WebDatabase::GetTable(WebDatabaseTable::TypeKey key) {
57 return tables_[key];
60 void WebDatabase::BeginTransaction() {
61 db_.BeginTransaction();
64 void WebDatabase::CommitTransaction() {
65 db_.CommitTransaction();
68 sql::Connection* WebDatabase::GetSQLConnection() {
69 return &db_;
72 sql::InitStatus WebDatabase::Init(const base::FilePath& db_name) {
73 db_.set_histogram_tag("Web");
75 // We don't store that much data in the tables so use a small page size.
76 // This provides a large benefit for empty tables (which is very likely with
77 // the tables we create).
78 db_.set_page_size(2048);
80 // We shouldn't have much data and what access we currently have is quite
81 // infrequent. So we go with a small cache size.
82 db_.set_cache_size(32);
84 // Run the database in exclusive mode. Nobody else should be accessing the
85 // database while we're running, and this will give somewhat improved perf.
86 db_.set_exclusive_locking();
88 if (!db_.Open(db_name))
89 return sql::INIT_FAILURE;
91 // Clobber really old databases.
92 static_assert(kDeprecatedVersionNumber < kCurrentVersionNumber,
93 "Deprecation version must be less than current");
94 sql::MetaTable::RazeIfDeprecated(&db_, kDeprecatedVersionNumber);
96 // Scope initialization in a transaction so we can't be partially
97 // initialized.
98 sql::Transaction transaction(&db_);
99 if (!transaction.Begin())
100 return sql::INIT_FAILURE;
102 // Version check.
103 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber))
104 return sql::INIT_FAILURE;
105 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) {
106 LOG(WARNING) << "Web database is too new.";
107 return sql::INIT_TOO_NEW;
110 // Initialize the tables.
111 for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) {
112 it->second->Init(&db_, &meta_table_);
115 // If the file on disk is an older database version, bring it up to date.
116 // If the migration fails we return an error to caller and do not commit
117 // the migration.
118 sql::InitStatus migration_status = MigrateOldVersionsAsNeeded();
119 if (migration_status != sql::INIT_OK)
120 return migration_status;
122 // Create the desired SQL tables if they do not already exist.
123 // It's important that this happen *after* the migration code runs.
124 // Otherwise, the migration code would have to explicitly check for empty
125 // tables created in the new format, and skip the migration in that case.
126 for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) {
127 if (!it->second->CreateTablesIfNecessary()) {
128 LOG(WARNING) << "Unable to initialize the web database.";
129 return sql::INIT_FAILURE;
133 return transaction.Commit() ? sql::INIT_OK : sql::INIT_FAILURE;
136 sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded() {
137 // Some malware used to lower the version number, causing migration to
138 // fail. Ensure the version number is at least as high as the compatible
139 // version number.
140 int current_version = std::max(meta_table_.GetVersionNumber(),
141 meta_table_.GetCompatibleVersionNumber());
142 if (current_version > meta_table_.GetVersionNumber())
143 ChangeVersion(&meta_table_, current_version, false);
145 DCHECK_GT(current_version, kDeprecatedVersionNumber);
147 for (int next_version = current_version + 1;
148 next_version <= kCurrentVersionNumber;
149 ++next_version) {
151 // Do any database-wide migrations.
152 bool update_compatible_version = false;
153 if (!MigrateToVersion(next_version, &update_compatible_version))
154 return FailedMigrationTo(next_version);
156 ChangeVersion(&meta_table_, next_version, update_compatible_version);
158 // Give each table a chance to migrate to this version.
159 for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) {
160 // Any of the tables may set this to true, but by default it is false.
161 update_compatible_version = false;
162 if (!it->second->MigrateToVersion(next_version,
163 &update_compatible_version)) {
164 return FailedMigrationTo(next_version);
167 ChangeVersion(&meta_table_, next_version, update_compatible_version);
170 return sql::INIT_OK;
173 bool WebDatabase::MigrateToVersion(int version,
174 bool* update_compatible_version) {
175 // Migrate if necessary.
176 switch (version) {
177 case 58:
178 *update_compatible_version = true;
179 return MigrateToVersion58DropWebAppsAndIntents();
182 return true;
185 bool WebDatabase::MigrateToVersion58DropWebAppsAndIntents() {
186 sql::Transaction transaction(&db_);
187 return transaction.Begin() &&
188 db_.Execute("DROP TABLE IF EXISTS web_apps") &&
189 db_.Execute("DROP TABLE IF EXISTS web_app_icons") &&
190 db_.Execute("DROP TABLE IF EXISTS web_intents") &&
191 db_.Execute("DROP TABLE IF EXISTS web_intents_defaults") &&
192 transaction.Commit();