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/diagnostics/sqlite_diagnostics.h" | 5 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 #if defined(OS_CHROMEOS) | 40 #if defined(OS_CHROMEOS) |
41 const char kSQLiteIntegrityNSSCertTest[] = "SQLiteIntegrityNSSCert"; | 41 const char kSQLiteIntegrityNSSCertTest[] = "SQLiteIntegrityNSSCert"; |
42 const char kSQLiteIntegrityNSSKeyTest[] = "SQLiteIntegrityNSSKey"; | 42 const char kSQLiteIntegrityNSSKeyTest[] = "SQLiteIntegrityNSSKey"; |
43 #endif | 43 #endif |
44 | 44 |
45 namespace { | 45 namespace { |
46 | 46 |
47 // Generic diagnostic test class for checking SQLite database integrity. | 47 // Generic diagnostic test class for checking SQLite database integrity. |
48 class SqliteIntegrityTest : public DiagnosticsTest { | 48 class SqliteIntegrityTest : public DiagnosticsTest { |
49 public: | 49 public: |
| 50 // These are bit flags, so each value should be a power of two. |
| 51 enum Flags { |
| 52 NO_FLAGS_SET = 0, |
| 53 CRITICAL = 0x01, |
| 54 REMOVE_IF_CORRUPT = 0x02, |
| 55 }; |
50 | 56 |
51 SqliteIntegrityTest(bool critical, | 57 SqliteIntegrityTest(uint32 flags, |
52 const std::string& id, | 58 const std::string& id, |
53 const std::string& title, | 59 const std::string& title, |
54 const base::FilePath& db_path) | 60 const base::FilePath& db_path) |
55 : DiagnosticsTest(id, title), critical_(critical), db_path_(db_path) {} | 61 : DiagnosticsTest(id, title), |
| 62 flags_(flags), |
| 63 db_path_(db_path) {} |
| 64 |
| 65 virtual bool RecoveryImpl(DiagnosticsModel::Observer* observer) OVERRIDE { |
| 66 int outcome_code = GetOutcomeCode(); |
| 67 if (flags_ & REMOVE_IF_CORRUPT) { |
| 68 switch (outcome_code) { |
| 69 case DIAG_SQLITE_ERROR_HANDLER_CALLED: |
| 70 case DIAG_SQLITE_CANNOT_OPEN_DB: |
| 71 case DIAG_SQLITE_DB_LOCKED: |
| 72 case DIAG_SQLITE_PRAGMA_FAILED: |
| 73 case DIAG_SQLITE_DB_CORRUPTED: |
| 74 LOG(WARNING) << "Removing broken SQLite database: " |
| 75 << db_path_.value(); |
| 76 base::DeleteFile(db_path_, false); |
| 77 break; |
| 78 case DIAG_SQLITE_SUCCESS: |
| 79 case DIAG_SQLITE_FILE_NOT_FOUND_OK: |
| 80 case DIAG_SQLITE_FILE_NOT_FOUND: |
| 81 break; |
| 82 default: |
| 83 DCHECK(false) << "Invalid outcome code: " << outcome_code; |
| 84 break; |
| 85 } |
| 86 } |
| 87 return true; |
| 88 } |
56 | 89 |
57 virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) OVERRIDE { | 90 virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) OVERRIDE { |
58 // If we're given an absolute path, use it. If not, then assume it's under | 91 // If we're given an absolute path, use it. If not, then assume it's under |
59 // the profile directory. | 92 // the profile directory. |
60 base::FilePath path; | 93 base::FilePath path; |
61 if (!db_path_.IsAbsolute()) | 94 if (!db_path_.IsAbsolute()) |
62 path = GetUserDefaultProfileDir().Append(db_path_); | 95 path = GetUserDefaultProfileDir().Append(db_path_); |
63 else | 96 else |
64 path = db_path_; | 97 path = db_path_; |
65 | 98 |
66 if (!base::PathExists(path)) { | 99 if (!base::PathExists(path)) { |
67 if (critical_) { | 100 if (flags_ & CRITICAL) { |
68 RecordOutcome(DIAG_SQLITE_FILE_NOT_FOUND, | 101 RecordOutcome(DIAG_SQLITE_FILE_NOT_FOUND, |
69 "File not found", | 102 "File not found", |
70 DiagnosticsModel::TEST_FAIL_CONTINUE); | 103 DiagnosticsModel::TEST_FAIL_CONTINUE); |
71 } else { | 104 } else { |
72 RecordOutcome(DIAG_SQLITE_FILE_NOT_FOUND_OK, | 105 RecordOutcome(DIAG_SQLITE_FILE_NOT_FOUND_OK, |
73 "File not found (but that is OK)", | 106 "File not found (but that is OK)", |
74 DiagnosticsModel::TEST_OK); | 107 DiagnosticsModel::TEST_OK); |
75 } | 108 } |
76 return true; | 109 return true; |
77 } | 110 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 ~ErrorRecorder() {} | 204 ~ErrorRecorder() {} |
172 | 205 |
173 bool has_error_; | 206 bool has_error_; |
174 int sqlite_error_; | 207 int sqlite_error_; |
175 int last_errno_; | 208 int last_errno_; |
176 std::string message_; | 209 std::string message_; |
177 | 210 |
178 DISALLOW_COPY_AND_ASSIGN(ErrorRecorder); | 211 DISALLOW_COPY_AND_ASSIGN(ErrorRecorder); |
179 }; | 212 }; |
180 | 213 |
181 bool critical_; | 214 uint32 flags_; |
182 base::FilePath db_path_; | 215 base::FilePath db_path_; |
183 DISALLOW_COPY_AND_ASSIGN(SqliteIntegrityTest); | 216 DISALLOW_COPY_AND_ASSIGN(SqliteIntegrityTest); |
184 }; | 217 }; |
185 | 218 |
186 } // namespace | 219 } // namespace |
187 | 220 |
188 DiagnosticsTest* MakeSqliteWebDbTest() { | 221 DiagnosticsTest* MakeSqliteWebDbTest() { |
189 return new SqliteIntegrityTest(true, | 222 return new SqliteIntegrityTest(SqliteIntegrityTest::CRITICAL, |
190 kSQLiteIntegrityWebTest, | 223 kSQLiteIntegrityWebTest, |
191 "Web Database", | 224 "Web Database", |
192 base::FilePath(kWebDataFilename)); | 225 base::FilePath(kWebDataFilename)); |
193 } | 226 } |
194 | 227 |
195 DiagnosticsTest* MakeSqliteCookiesDbTest() { | 228 DiagnosticsTest* MakeSqliteCookiesDbTest() { |
196 return new SqliteIntegrityTest(true, | 229 return new SqliteIntegrityTest(SqliteIntegrityTest::CRITICAL, |
197 kSQLiteIntegrityCookieTest, | 230 kSQLiteIntegrityCookieTest, |
198 "Cookies Database", | 231 "Cookies Database", |
199 base::FilePath(chrome::kCookieFilename)); | 232 base::FilePath(chrome::kCookieFilename)); |
200 } | 233 } |
201 | 234 |
202 DiagnosticsTest* MakeSqliteHistoryDbTest() { | 235 DiagnosticsTest* MakeSqliteHistoryDbTest() { |
203 return new SqliteIntegrityTest(true, | 236 return new SqliteIntegrityTest(SqliteIntegrityTest::CRITICAL, |
204 kSQLiteIntegrityHistoryTest, | 237 kSQLiteIntegrityHistoryTest, |
205 "History Database", | 238 "History Database", |
206 base::FilePath(chrome::kHistoryFilename)); | 239 base::FilePath(chrome::kHistoryFilename)); |
207 } | 240 } |
208 | 241 |
209 DiagnosticsTest* MakeSqliteArchivedHistoryDbTest() { | 242 DiagnosticsTest* MakeSqliteArchivedHistoryDbTest() { |
210 return new SqliteIntegrityTest( | 243 return new SqliteIntegrityTest( |
211 false, | 244 SqliteIntegrityTest::NO_FLAGS_SET, |
212 kSQLiteIntegrityArchivedHistoryTest, | 245 kSQLiteIntegrityArchivedHistoryTest, |
213 "Archived History Database", | 246 "Archived History Database", |
214 base::FilePath(chrome::kArchivedHistoryFilename)); | 247 base::FilePath(chrome::kArchivedHistoryFilename)); |
215 } | 248 } |
216 | 249 |
217 DiagnosticsTest* MakeSqliteThumbnailsDbTest() { | 250 DiagnosticsTest* MakeSqliteThumbnailsDbTest() { |
218 return new SqliteIntegrityTest(false, | 251 return new SqliteIntegrityTest(SqliteIntegrityTest::NO_FLAGS_SET, |
219 kSQLiteIntegrityThumbnailsTest, | 252 kSQLiteIntegrityThumbnailsTest, |
220 "Thumbnails Database", | 253 "Thumbnails Database", |
221 base::FilePath(chrome::kThumbnailsFilename)); | 254 base::FilePath(chrome::kThumbnailsFilename)); |
222 } | 255 } |
223 | 256 |
224 DiagnosticsTest* MakeSqliteAppCacheDbTest() { | 257 DiagnosticsTest* MakeSqliteAppCacheDbTest() { |
225 base::FilePath appcache_dir(content::kAppCacheDirname); | 258 base::FilePath appcache_dir(content::kAppCacheDirname); |
226 base::FilePath appcache_db = | 259 base::FilePath appcache_db = |
227 appcache_dir.Append(appcache::kAppCacheDatabaseName); | 260 appcache_dir.Append(appcache::kAppCacheDatabaseName); |
228 return new SqliteIntegrityTest(false, | 261 return new SqliteIntegrityTest(SqliteIntegrityTest::NO_FLAGS_SET, |
229 kSQLiteIntegrityAppCacheTest, | 262 kSQLiteIntegrityAppCacheTest, |
230 "Application Cache Database", | 263 "Application Cache Database", |
231 appcache_db); | 264 appcache_db); |
232 } | 265 } |
233 | 266 |
234 DiagnosticsTest* MakeSqliteWebDatabaseTrackerDbTest() { | 267 DiagnosticsTest* MakeSqliteWebDatabaseTrackerDbTest() { |
235 base::FilePath databases_dir(webkit_database::kDatabaseDirectoryName); | 268 base::FilePath databases_dir(webkit_database::kDatabaseDirectoryName); |
236 base::FilePath tracker_db = | 269 base::FilePath tracker_db = |
237 databases_dir.Append(webkit_database::kTrackerDatabaseFileName); | 270 databases_dir.Append(webkit_database::kTrackerDatabaseFileName); |
238 return new SqliteIntegrityTest(false, | 271 return new SqliteIntegrityTest(SqliteIntegrityTest::NO_FLAGS_SET, |
239 kSQLiteIntegrityDatabaseTrackerTest, | 272 kSQLiteIntegrityDatabaseTrackerTest, |
240 "Database Tracker Database", | 273 "Database Tracker Database", |
241 tracker_db); | 274 tracker_db); |
242 } | 275 } |
243 | 276 |
244 #if defined(OS_CHROMEOS) | 277 #if defined(OS_CHROMEOS) |
245 DiagnosticsTest* MakeSqliteNssCertDbTest() { | 278 DiagnosticsTest* MakeSqliteNssCertDbTest() { |
246 base::FilePath home_dir = file_util::GetHomeDir(); | 279 base::FilePath home_dir = file_util::GetHomeDir(); |
247 return new SqliteIntegrityTest(false, | 280 return new SqliteIntegrityTest(SqliteIntegrityTest::REMOVE_IF_CORRUPT, |
248 kSQLiteIntegrityNSSCertTest, | 281 kSQLiteIntegrityNSSCertTest, |
249 "NSS Certificate Database", | 282 "NSS Certificate Database", |
250 home_dir.Append(chromeos::kNssCertDbPath)); | 283 home_dir.Append(chromeos::kNssCertDbPath)); |
251 } | 284 } |
252 | 285 |
253 DiagnosticsTest* MakeSqliteNssKeyDbTest() { | 286 DiagnosticsTest* MakeSqliteNssKeyDbTest() { |
254 base::FilePath home_dir = file_util::GetHomeDir(); | 287 base::FilePath home_dir = file_util::GetHomeDir(); |
255 return new SqliteIntegrityTest(false, | 288 return new SqliteIntegrityTest(SqliteIntegrityTest::REMOVE_IF_CORRUPT, |
256 kSQLiteIntegrityNSSKeyTest, | 289 kSQLiteIntegrityNSSKeyTest, |
257 "NSS Key Database", | 290 "NSS Key Database", |
258 home_dir.Append(chromeos::kNssKeyDbPath)); | 291 home_dir.Append(chromeos::kNssKeyDbPath)); |
259 } | 292 } |
260 #endif // defined(OS_CHROMEOS) | 293 #endif // defined(OS_CHROMEOS) |
261 } // namespace diagnostics | 294 } // namespace diagnostics |
OLD | NEW |