OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/webdata/common/web_database.h" | 5 #include "components/webdata/common/web_database.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "sql/statement.h" | 10 #include "sql/statement.h" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 | 94 |
95 // Version check. | 95 // Version check. |
96 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber)) | 96 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber)) |
97 return sql::INIT_FAILURE; | 97 return sql::INIT_FAILURE; |
98 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { | 98 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { |
99 LOG(WARNING) << "Web database is too new."; | 99 LOG(WARNING) << "Web database is too new."; |
100 return sql::INIT_TOO_NEW; | 100 return sql::INIT_TOO_NEW; |
101 } | 101 } |
102 | 102 |
103 // Initialize the tables. | 103 // Initialize the tables. |
104 for (TableMap::iterator it = tables_.begin(); | 104 for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) { |
105 it != tables_.end(); | 105 it->second->Init(&db_, &meta_table_); |
106 ++it) { | |
107 if (!it->second->Init(&db_, &meta_table_)) { | |
108 LOG(WARNING) << "Unable to initialize the web database."; | |
109 return sql::INIT_FAILURE; | |
110 } | |
111 } | 106 } |
112 | 107 |
113 // If the file on disk is an older database version, bring it up to date. | 108 // If the file on disk is an older database version, bring it up to date. |
114 // If the migration fails we return an error to caller and do not commit | 109 // If the migration fails we return an error to caller and do not commit |
115 // the migration. | 110 // the migration. |
116 sql::InitStatus migration_status = MigrateOldVersionsAsNeeded(); | 111 sql::InitStatus migration_status = MigrateOldVersionsAsNeeded(); |
117 if (migration_status != sql::INIT_OK) | 112 if (migration_status != sql::INIT_OK) |
118 return migration_status; | 113 return migration_status; |
119 | 114 |
| 115 // Create the desired SQL tables if they do not already exist. |
| 116 // It's important that this happen *after* the migration code runs. |
| 117 // Otherwise, the migration code would have to explicitly check for empty |
| 118 // tables created in the new format, and skip the migration in that case. |
| 119 for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) { |
| 120 if (!it->second->CreateTablesIfNecessary()) { |
| 121 LOG(WARNING) << "Unable to initialize the web database."; |
| 122 return sql::INIT_FAILURE; |
| 123 } |
| 124 } |
| 125 |
120 return transaction.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; | 126 return transaction.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; |
121 } | 127 } |
122 | 128 |
123 sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded() { | 129 sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded() { |
124 // Some malware used to lower the version number, causing migration to | 130 // Some malware used to lower the version number, causing migration to |
125 // fail. Ensure the version number is at least as high as the compatible | 131 // fail. Ensure the version number is at least as high as the compatible |
126 // version number. | 132 // version number. |
127 int current_version = std::max(meta_table_.GetVersionNumber(), | 133 int current_version = std::max(meta_table_.GetVersionNumber(), |
128 meta_table_.GetCompatibleVersionNumber()); | 134 meta_table_.GetCompatibleVersionNumber()); |
129 if (current_version > meta_table_.GetVersionNumber()) | 135 if (current_version > meta_table_.GetVersionNumber()) |
130 ChangeVersion(&meta_table_, current_version, false); | 136 ChangeVersion(&meta_table_, current_version, false); |
131 | 137 |
132 if (current_version < 20) { | 138 if (current_version < 20) { |
133 // Versions 1 - 19 are unhandled. Version numbers greater than | 139 // Versions 1 - 19 are unhandled. Version numbers greater than |
134 // kCurrentVersionNumber should have already been weeded out by the caller. | 140 // kCurrentVersionNumber should have already been weeded out by the caller. |
135 // | 141 // |
136 // When the version is too old, we return failure error code. The schema | 142 // When the version is too old, we return failure error code. The schema |
137 // is too out of date to migrate. | 143 // is too out of date to migrate. |
138 // | 144 // |
139 // There should not be a released product that makes a database too old to | 145 // There should not be a released product that makes a database too old to |
140 // migrate. If we do encounter such a legacy database, we will need a | 146 // migrate. If we do encounter such a legacy database, we will need a |
141 // better solution to handle it (i.e., pop up a dialog to tell the user, | 147 // better solution to handle it (i.e., pop up a dialog to tell the user, |
142 // erase all their prefs and start over, etc.). | 148 // erase all their prefs and start over, etc.). |
143 LOG(WARNING) << "Web database version " << current_version << | 149 LOG(WARNING) << "Web database version " << current_version |
144 " is too old to handle."; | 150 << " is too old to handle."; |
145 NOTREACHED(); | 151 NOTREACHED(); |
146 return sql::INIT_FAILURE; | 152 return sql::INIT_FAILURE; |
147 } | 153 } |
148 | 154 |
149 for (int next_version = current_version + 1; | 155 for (int next_version = current_version + 1; |
150 next_version <= kCurrentVersionNumber; | 156 next_version <= kCurrentVersionNumber; |
151 ++next_version) { | 157 ++next_version) { |
152 // Give each table a chance to migrate to this version. | 158 // Give each table a chance to migrate to this version. |
153 for (TableMap::iterator it = tables_.begin(); | 159 for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) { |
154 it != tables_.end(); | |
155 ++it) { | |
156 // Any of the tables may set this to true, but by default it is false. | 160 // Any of the tables may set this to true, but by default it is false. |
157 bool update_compatible_version = false; | 161 bool update_compatible_version = false; |
158 if (!it->second->MigrateToVersion(next_version, | 162 if (!it->second->MigrateToVersion(next_version, |
159 &update_compatible_version)) { | 163 &update_compatible_version)) { |
160 return FailedMigrationTo(next_version); | 164 return FailedMigrationTo(next_version); |
161 } | 165 } |
162 | 166 |
163 ChangeVersion(&meta_table_, next_version, update_compatible_version); | 167 ChangeVersion(&meta_table_, next_version, update_compatible_version); |
164 } | 168 } |
165 } | 169 } |
166 return sql::INIT_OK; | 170 return sql::INIT_OK; |
167 } | 171 } |
OLD | NEW |