OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <string> |
| 6 #include "base/logging.h" |
| 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/extensions/activity_database.h" |
| 9 #include "chrome/browser/history/url_database.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "sql/transaction.h" |
| 12 |
| 13 #if defined(OS_MACOSX) |
| 14 #include "base/mac/mac_util.h" |
| 15 #endif |
| 16 |
| 17 using content::BrowserThread; |
| 18 |
| 19 namespace extensions { |
| 20 |
| 21 ActivityDatabase::ActivityDatabase() : initialized_(false) {} |
| 22 |
| 23 ActivityDatabase::~ActivityDatabase() { |
| 24 Close(); // Safe to call Close() even if Open() never happened. |
| 25 } |
| 26 |
| 27 void ActivityDatabase::SetErrorDelegate(sql::ErrorDelegate* error_delegate) { |
| 28 db_.set_error_delegate(error_delegate); |
| 29 } |
| 30 |
| 31 void ActivityDatabase::Init(const FilePath& db_name) { |
| 32 db_.set_page_size(4096); |
| 33 db_.set_cache_size(32); |
| 34 |
| 35 if (!db_.Open(db_name)) { |
| 36 LOG(ERROR) << db_.GetErrorMessage(); |
| 37 return LogInitFailure(); |
| 38 } |
| 39 |
| 40 // Wrap the initialization in a transaction so that the db doesn't |
| 41 // get corrupted if init fails/crashes. |
| 42 sql::Transaction committer(&db_); |
| 43 if (!committer.Begin()) |
| 44 return LogInitFailure(); |
| 45 |
| 46 #if defined(OS_MACOSX) |
| 47 // Exclude the database from backups. |
| 48 base::mac::SetFileBackupExclusion(db_name); |
| 49 #endif |
| 50 |
| 51 db_.Preload(); |
| 52 |
| 53 // Create the UrlAction database. |
| 54 if (InitializeTable(UrlAction::kTableName, UrlAction::kTableStructure) != |
| 55 sql::INIT_OK) |
| 56 return LogInitFailure(); |
| 57 |
| 58 // Create the APIAction database. |
| 59 if (InitializeTable(APIAction::kTableName, APIAction::kTableStructure) |
| 60 != sql::INIT_OK) |
| 61 return LogInitFailure(); |
| 62 |
| 63 // Create the BlockedAction database. |
| 64 if (InitializeTable(BlockedAction::kTableName, BlockedAction::kTableStructure) |
| 65 != sql::INIT_OK) |
| 66 return LogInitFailure(); |
| 67 |
| 68 sql::InitStatus stat = committer.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; |
| 69 if (stat == sql::INIT_OK) |
| 70 initialized_ = true; |
| 71 else |
| 72 return LogInitFailure(); |
| 73 } |
| 74 |
| 75 void ActivityDatabase::LogInitFailure() { |
| 76 LOG(ERROR) << "Couldn't initialize the activity log database."; |
| 77 } |
| 78 |
| 79 sql::InitStatus ActivityDatabase::InitializeTable(const char* table_name, |
| 80 const char* table_structure) { |
| 81 if (!db_.DoesTableExist(table_name)) { |
| 82 char table_creator[1000]; |
| 83 base::snprintf(table_creator, |
| 84 arraysize(table_creator), |
| 85 "CREATE TABLE %s %s", table_name, table_structure); |
| 86 if (!db_.Execute(table_creator)) |
| 87 return sql::INIT_FAILURE; |
| 88 } |
| 89 return sql::INIT_OK; |
| 90 } |
| 91 |
| 92 void ActivityDatabase::RecordAction(scoped_refptr<Action> action) { |
| 93 if (initialized_) |
| 94 action->Record(&db_); |
| 95 } |
| 96 |
| 97 void ActivityDatabase::BeginTransaction() { |
| 98 db_.BeginTransaction(); |
| 99 } |
| 100 |
| 101 void ActivityDatabase::CommitTransaction() { |
| 102 db_.CommitTransaction(); |
| 103 } |
| 104 |
| 105 void ActivityDatabase::RollbackTransaction() { |
| 106 db_.RollbackTransaction(); |
| 107 } |
| 108 |
| 109 bool ActivityDatabase::Raze() { |
| 110 return db_.Raze(); |
| 111 } |
| 112 |
| 113 void ActivityDatabase::Close() { |
| 114 db_.Close(); |
| 115 } |
| 116 |
| 117 void ActivityDatabase::KillDatabase() { |
| 118 db_.RollbackTransaction(); |
| 119 db_.Raze(); |
| 120 db_.Close(); |
| 121 } |
| 122 |
| 123 } // namespace extensions |
| 124 |
OLD | NEW |