| 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 "chrome/browser/webdata/web_database.h" | 5 #include "chrome/browser/webdata/web_database.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "chrome/browser/webdata/autofill_table.h" | 9 #include "base/stl_util.h" |
| 10 #include "chrome/browser/webdata/keyword_table.h" | |
| 11 #include "chrome/browser/webdata/logins_table.h" | |
| 12 #include "chrome/browser/webdata/token_service_table.h" | |
| 13 #include "chrome/browser/webdata/web_apps_table.h" | |
| 14 #include "chrome/browser/webdata/web_intents_table.h" | |
| 15 #include "content/public/browser/notification_service.h" | 10 #include "content/public/browser/notification_service.h" |
| 16 #include "sql/statement.h" | 11 #include "sql/statement.h" |
| 17 #include "sql/transaction.h" | 12 #include "sql/transaction.h" |
| 18 | 13 |
| 19 // Current version number. Note: when changing the current version number, | 14 // Current version number. Note: when changing the current version number, |
| 20 // corresponding changes must happen in the unit tests, and new migration test | 15 // corresponding changes must happen in the unit tests, and new migration test |
| 21 // added. See |WebDatabaseMigrationTest::kCurrentTestedVersionNumber|. | 16 // added. See |WebDatabaseMigrationTest::kCurrentTestedVersionNumber|. |
| 22 // static | 17 // static |
| 23 const int WebDatabase::kCurrentVersionNumber = 49; | 18 const int WebDatabase::kCurrentVersionNumber = 49; |
| 24 | 19 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 44 LOG(WARNING) << "Unable to update web database to version " | 39 LOG(WARNING) << "Unable to update web database to version " |
| 45 << version_num << "."; | 40 << version_num << "."; |
| 46 NOTREACHED(); | 41 NOTREACHED(); |
| 47 return sql::INIT_FAILURE; | 42 return sql::INIT_FAILURE; |
| 48 } | 43 } |
| 49 | 44 |
| 50 } // namespace | 45 } // namespace |
| 51 | 46 |
| 52 WebDatabase::WebDatabase() {} | 47 WebDatabase::WebDatabase() {} |
| 53 | 48 |
| 54 WebDatabase::~WebDatabase() {} | 49 WebDatabase::~WebDatabase() { |
| 50 } |
| 51 |
| 52 void WebDatabase::AddTable(WebDatabaseTable* table) { |
| 53 tables_[table->GetTypeKey()] = table; |
| 54 } |
| 55 |
| 56 WebDatabaseTable* WebDatabase::GetTable(WebDatabaseTable::TypeKey key) { |
| 57 return tables_[key]; |
| 58 } |
| 55 | 59 |
| 56 void WebDatabase::BeginTransaction() { | 60 void WebDatabase::BeginTransaction() { |
| 57 db_.BeginTransaction(); | 61 db_.BeginTransaction(); |
| 58 } | 62 } |
| 59 | 63 |
| 60 void WebDatabase::CommitTransaction() { | 64 void WebDatabase::CommitTransaction() { |
| 61 db_.CommitTransaction(); | 65 db_.CommitTransaction(); |
| 62 } | 66 } |
| 63 | 67 |
| 64 AutofillTable* WebDatabase::GetAutofillTable() { | |
| 65 return autofill_table_; | |
| 66 } | |
| 67 | |
| 68 KeywordTable* WebDatabase::GetKeywordTable() { | |
| 69 return keyword_table_; | |
| 70 } | |
| 71 | |
| 72 LoginsTable* WebDatabase::GetLoginsTable() { | |
| 73 return logins_table_; | |
| 74 } | |
| 75 | |
| 76 TokenServiceTable* WebDatabase::GetTokenServiceTable() { | |
| 77 return token_service_table_; | |
| 78 } | |
| 79 | |
| 80 WebAppsTable* WebDatabase::GetWebAppsTable() { | |
| 81 return web_apps_table_; | |
| 82 } | |
| 83 | |
| 84 sql::Connection* WebDatabase::GetSQLConnection() { | 68 sql::Connection* WebDatabase::GetSQLConnection() { |
| 85 return &db_; | 69 return &db_; |
| 86 } | 70 } |
| 87 | 71 |
| 88 sql::InitStatus WebDatabase::Init(const base::FilePath& db_name, | 72 sql::InitStatus WebDatabase::Init(const base::FilePath& db_name, |
| 89 const std::string& app_locale) { | 73 const std::string& app_locale) { |
| 90 // When running in unit tests, there is already a NotificationService object. | 74 // When running in unit tests, there is already a NotificationService object. |
| 91 // Since only one can exist at a time per thread, check first. | 75 // Since only one can exist at a time per thread, check first. |
| 92 if (!content::NotificationService::current()) | 76 if (!content::NotificationService::current()) |
| 93 notification_service_.reset(content::NotificationService::Create()); | 77 notification_service_.reset(content::NotificationService::Create()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 116 return sql::INIT_FAILURE; | 100 return sql::INIT_FAILURE; |
| 117 | 101 |
| 118 // Version check. | 102 // Version check. |
| 119 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber)) | 103 if (!meta_table_.Init(&db_, kCurrentVersionNumber, kCompatibleVersionNumber)) |
| 120 return sql::INIT_FAILURE; | 104 return sql::INIT_FAILURE; |
| 121 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { | 105 if (meta_table_.GetCompatibleVersionNumber() > kCurrentVersionNumber) { |
| 122 LOG(WARNING) << "Web database is too new."; | 106 LOG(WARNING) << "Web database is too new."; |
| 123 return sql::INIT_TOO_NEW; | 107 return sql::INIT_TOO_NEW; |
| 124 } | 108 } |
| 125 | 109 |
| 126 // TODO(joi): Table creation should move out of this class; switch | |
| 127 // to a two-phase init to accomplish this. | |
| 128 | |
| 129 // Create the tables. | |
| 130 autofill_table_ = new AutofillTable(&db_, &meta_table_); | |
| 131 tables_.push_back(autofill_table_); | |
| 132 | |
| 133 keyword_table_ = new KeywordTable(&db_, &meta_table_); | |
| 134 tables_.push_back(keyword_table_); | |
| 135 | |
| 136 // TODO(mdm): We only really need the LoginsTable on Windows for IE7 password | |
| 137 // access, but for now, we still create it on all platforms since it deletes | |
| 138 // the old logins table. We can remove this after a while, e.g. in M22 or so. | |
| 139 logins_table_ = new LoginsTable(&db_, &meta_table_); | |
| 140 tables_.push_back(logins_table_); | |
| 141 | |
| 142 token_service_table_ = new TokenServiceTable(&db_, &meta_table_); | |
| 143 tables_.push_back(token_service_table_); | |
| 144 | |
| 145 web_apps_table_ = new WebAppsTable(&db_, &meta_table_); | |
| 146 tables_.push_back(web_apps_table_); | |
| 147 | |
| 148 web_intents_table_ = new WebIntentsTable(&db_, &meta_table_); | |
| 149 tables_.push_back(web_intents_table_); | |
| 150 | |
| 151 // Initialize the tables. | 110 // Initialize the tables. |
| 152 for (ScopedVector<WebDatabaseTable>::iterator it = tables_.begin(); | 111 for (TableMap::iterator it = tables_.begin(); |
| 153 it != tables_.end(); | 112 it != tables_.end(); |
| 154 ++it) { | 113 ++it) { |
| 155 if (!(*it)->Init()) { | 114 if (!it->second->Init(&db_, &meta_table_)) { |
| 156 LOG(WARNING) << "Unable to initialize the web database."; | 115 LOG(WARNING) << "Unable to initialize the web database."; |
| 157 return sql::INIT_FAILURE; | 116 return sql::INIT_FAILURE; |
| 158 } | 117 } |
| 159 } | 118 } |
| 160 | 119 |
| 161 // If the file on disk is an older database version, bring it up to date. | 120 // If the file on disk is an older database version, bring it up to date. |
| 162 // If the migration fails we return an error to caller and do not commit | 121 // If the migration fails we return an error to caller and do not commit |
| 163 // the migration. | 122 // the migration. |
| 164 sql::InitStatus migration_status = MigrateOldVersionsAsNeeded(app_locale); | 123 sql::InitStatus migration_status = MigrateOldVersionsAsNeeded(app_locale); |
| 165 if (migration_status != sql::INIT_OK) | 124 if (migration_status != sql::INIT_OK) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 192 LOG(WARNING) << "Web database version " << current_version << | 151 LOG(WARNING) << "Web database version " << current_version << |
| 193 " is too old to handle."; | 152 " is too old to handle."; |
| 194 NOTREACHED(); | 153 NOTREACHED(); |
| 195 return sql::INIT_FAILURE; | 154 return sql::INIT_FAILURE; |
| 196 } | 155 } |
| 197 | 156 |
| 198 for (int next_version = current_version + 1; | 157 for (int next_version = current_version + 1; |
| 199 next_version <= kCurrentVersionNumber; | 158 next_version <= kCurrentVersionNumber; |
| 200 ++next_version) { | 159 ++next_version) { |
| 201 // Give each table a chance to migrate to this version. | 160 // Give each table a chance to migrate to this version. |
| 202 for (ScopedVector<WebDatabaseTable>::iterator it = tables_.begin(); | 161 for (TableMap::iterator it = tables_.begin(); |
| 203 it != tables_.end(); | 162 it != tables_.end(); |
| 204 ++it) { | 163 ++it) { |
| 205 // Any of the tables may set this to true, but by default it is false. | 164 // Any of the tables may set this to true, but by default it is false. |
| 206 bool update_compatible_version = false; | 165 bool update_compatible_version = false; |
| 207 if (!(*it)->MigrateToVersion(next_version, | 166 if (!it->second->MigrateToVersion(next_version, |
| 208 app_locale, | 167 app_locale, |
| 209 &update_compatible_version)) { | 168 &update_compatible_version)) { |
| 210 return FailedMigrationTo(next_version); | 169 return FailedMigrationTo(next_version); |
| 211 } | 170 } |
| 212 | 171 |
| 213 ChangeVersion(&meta_table_, next_version, update_compatible_version); | 172 ChangeVersion(&meta_table_, next_version, update_compatible_version); |
| 214 } | 173 } |
| 215 } | 174 } |
| 216 return sql::INIT_OK; | 175 return sql::INIT_OK; |
| 217 } | 176 } |
| OLD | NEW |