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. | |
Peter Kasting
2014/03/21 18:20:13
You might want to comment here and/or at the Init(
Ilya Sherman
2014/03/21 23:12:47
Done.
| |
116 for (TableMap::iterator it = tables_.begin(); it != tables_.end(); ++it) { | |
117 if (!it->second->CreateTablesIfNecessary()) { | |
118 LOG(WARNING) << "Unable to initialize the web database."; | |
119 return sql::INIT_FAILURE; | |
120 } | |
121 } | |
122 | |
120 return transaction.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; | 123 return transaction.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; |
121 } | 124 } |
122 | 125 |
123 sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded() { | 126 sql::InitStatus WebDatabase::MigrateOldVersionsAsNeeded() { |
124 // Some malware used to lower the version number, causing migration to | 127 // 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 | 128 // fail. Ensure the version number is at least as high as the compatible |
126 // version number. | 129 // version number. |
127 int current_version = std::max(meta_table_.GetVersionNumber(), | 130 int current_version = std::max(meta_table_.GetVersionNumber(), |
128 meta_table_.GetCompatibleVersionNumber()); | 131 meta_table_.GetCompatibleVersionNumber()); |
129 if (current_version > meta_table_.GetVersionNumber()) | 132 if (current_version > meta_table_.GetVersionNumber()) |
130 ChangeVersion(&meta_table_, current_version, false); | 133 ChangeVersion(&meta_table_, current_version, false); |
131 | 134 |
132 if (current_version < 20) { | 135 if (current_version < 20) { |
133 // Versions 1 - 19 are unhandled. Version numbers greater than | 136 // Versions 1 - 19 are unhandled. Version numbers greater than |
134 // kCurrentVersionNumber should have already been weeded out by the caller. | 137 // kCurrentVersionNumber should have already been weeded out by the caller. |
135 // | 138 // |
136 // When the version is too old, we return failure error code. The schema | 139 // When the version is too old, we return failure error code. The schema |
137 // is too out of date to migrate. | 140 // is too out of date to migrate. |
138 // | 141 // |
139 // There should not be a released product that makes a database too old to | 142 // 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 | 143 // 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, | 144 // better solution to handle it (i.e., pop up a dialog to tell the user, |
142 // erase all their prefs and start over, etc.). | 145 // erase all their prefs and start over, etc.). |
143 LOG(WARNING) << "Web database version " << current_version << | 146 LOG(WARNING) << "Web database version " << current_version |
144 " is too old to handle."; | 147 << " is too old to handle."; |
145 NOTREACHED(); | 148 NOTREACHED(); |
146 return sql::INIT_FAILURE; | 149 return sql::INIT_FAILURE; |
147 } | 150 } |
148 | 151 |
149 for (int next_version = current_version + 1; | 152 for (int next_version = current_version + 1; |
150 next_version <= kCurrentVersionNumber; | 153 next_version <= kCurrentVersionNumber; |
151 ++next_version) { | 154 ++next_version) { |
152 // Give each table a chance to migrate to this version. | 155 // Give each table a chance to migrate to this version. |
153 for (TableMap::iterator it = tables_.begin(); | 156 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. | 157 // Any of the tables may set this to true, but by default it is false. |
157 bool update_compatible_version = false; | 158 bool update_compatible_version = false; |
158 if (!it->second->MigrateToVersion(next_version, | 159 if (!it->second->MigrateToVersion(next_version, |
159 &update_compatible_version)) { | 160 &update_compatible_version)) { |
160 return FailedMigrationTo(next_version); | 161 return FailedMigrationTo(next_version); |
161 } | 162 } |
162 | 163 |
163 ChangeVersion(&meta_table_, next_version, update_compatible_version); | 164 ChangeVersion(&meta_table_, next_version, update_compatible_version); |
164 } | 165 } |
165 } | 166 } |
166 return sql::INIT_OK; | 167 return sql::INIT_OK; |
167 } | 168 } |
OLD | NEW |