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 "sql/transaction.h" |
| 11 |
| 12 #if defined(OS_MACOSX) |
| 13 #include "base/mac/mac_util.h" |
| 14 #endif |
| 15 |
| 16 namespace { |
| 17 |
| 18 void LogStatementFailure(const std::string& sql_str) { |
| 19 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; |
| 20 } |
| 21 |
| 22 } // namespace |
| 23 |
| 24 namespace extensions { |
| 25 |
| 26 ActivityDatabase::ActivityDatabase() : initialized_(false) {} |
| 27 |
| 28 ActivityDatabase::~ActivityDatabase() { |
| 29 Close(); // Safe to call Close() even if Open() never happened. |
| 30 } |
| 31 |
| 32 sql::InitStatus ActivityDatabase::Init( |
| 33 const FilePath& db_name, |
| 34 sql::ErrorDelegate* error_delegate) { |
| 35 db_.set_error_delegate(error_delegate); |
| 36 db_.set_page_size(4096); |
| 37 db_.set_cache_size(32); |
| 38 |
| 39 if (!db_.Open(db_name)) { |
| 40 LOG(ERROR) << db_.GetErrorMessage(); |
| 41 return sql::INIT_FAILURE; |
| 42 } |
| 43 |
| 44 // Wrap the initialization in a transaction so that the db doesn't |
| 45 // get corrupted if init fails/crashes. |
| 46 sql::Transaction committer(&db_); |
| 47 if (!committer.Begin()) |
| 48 return sql::INIT_FAILURE; |
| 49 |
| 50 #if defined(OS_MACOSX) |
| 51 // Exclude the database from backups. |
| 52 base::mac::SetFileBackupExclusion(db_name); |
| 53 #endif |
| 54 |
| 55 db_.Preload(); |
| 56 |
| 57 // Create the UrlAction database. |
| 58 if (InitializeTable(UrlAction::kTableName, UrlAction::kTableStructure) != |
| 59 sql::INIT_OK) |
| 60 return sql::INIT_FAILURE; |
| 61 |
| 62 // Create the ManagerAction database. |
| 63 if (InitializeTable(ManagerAction::kTableName, ManagerAction::kTableStructure) |
| 64 != sql::INIT_OK) |
| 65 return sql::INIT_FAILURE; |
| 66 |
| 67 // Create the BlockedAction database. |
| 68 if (InitializeTable(BlockedAction::kTableName, BlockedAction::kTableStructure) |
| 69 != sql::INIT_OK) |
| 70 return sql::INIT_FAILURE; |
| 71 |
| 72 sql::InitStatus stat = committer.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; |
| 73 if (stat == sql::INIT_OK) |
| 74 initialized_ = true; |
| 75 return stat; |
| 76 } |
| 77 |
| 78 sql::InitStatus ActivityDatabase::InitializeTable(const char* table_name, |
| 79 const char* table_structure) { |
| 80 if (!db_.DoesTableExist(table_name)) { |
| 81 char table_creator[1000]; |
| 82 base::snprintf(table_creator, |
| 83 arraysize(table_creator), |
| 84 "CREATE TABLE %s %s", table_name, table_structure); |
| 85 if (!db_.Execute(table_creator)) |
| 86 return sql::INIT_FAILURE; |
| 87 } |
| 88 return sql::INIT_OK; |
| 89 } |
| 90 |
| 91 // The table structure is defined in url_actions.h. |
| 92 void ActivityDatabase::RecordUrlAction(scoped_refptr<UrlAction> action) { |
| 93 if (!initialized_) |
| 94 return; |
| 95 std::string sql_str = "INSERT INTO " + std::string(UrlAction::kTableName) + |
| 96 " (extension_id, time, url_action_type, url, url_title, api_call)" |
| 97 " VALUES (?,?,?,?,?,?)"; |
| 98 sql::Statement statement(db_.GetCachedStatement( |
| 99 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 100 statement.BindString(0, action->extension_id()); |
| 101 statement.BindInt64(1, action->time().ToInternalValue()); |
| 102 statement.BindString(2, action->VerbAsString()); |
| 103 statement.BindString(3, |
| 104 history::URLDatabase::GURLToDatabaseURL(action->url())); |
| 105 statement.BindString16(4, action->url_title()); |
| 106 statement.BindString(5, action->script()); |
| 107 if (!statement.Run()) |
| 108 LogStatementFailure(sql_str); |
| 109 } |
| 110 |
| 111 // The table structure is defined in manager_actions.h. |
| 112 void ActivityDatabase::RecordManagerAction( |
| 113 scoped_refptr<ManagerAction> action) { |
| 114 if (!initialized_) |
| 115 return; |
| 116 std::string sql_str = "INSERT INTO " + std::string(ManagerAction::kTableName) |
| 117 + " (extension_id, time, manager_action_type, target_type, api_call)" |
| 118 " VALUES (?,?,?,?,?)"; |
| 119 sql::Statement statement(db_.GetCachedStatement( |
| 120 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 121 statement.BindString(0, action->extension_id()); |
| 122 statement.BindInt64(1, action->time().ToInternalValue()); |
| 123 statement.BindString(2, action->VerbAsString()); |
| 124 statement.BindString(3, action->TargetAsString()); |
| 125 statement.BindString(4, action->api_call()); |
| 126 if (!statement.Run()) |
| 127 LogStatementFailure(sql_str); |
| 128 } |
| 129 |
| 130 // The table structure is defined in blocked_actions.h |
| 131 void ActivityDatabase::RecordBlockedAction( |
| 132 scoped_refptr<BlockedAction> action) { |
| 133 if (!initialized_) |
| 134 return; |
| 135 std::string sql_str = "INSERT INTO " + std::string(BlockedAction::kTableName) |
| 136 + " (extension_id, time, blocked_action, reason) VALUES (?,?,?,?)"; |
| 137 sql::Statement statement(db_.GetCachedStatement( |
| 138 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 139 statement.BindString(0, action->extension_id()); |
| 140 statement.BindInt64(1, action->time().ToInternalValue()); |
| 141 statement.BindString(2, action->blocked_action()); |
| 142 statement.BindString(3, action->reason()); |
| 143 if (!statement.Run()) |
| 144 LogStatementFailure(sql_str); |
| 145 } |
| 146 |
| 147 void ActivityDatabase::BeginTransaction() { |
| 148 db_.BeginTransaction(); |
| 149 } |
| 150 |
| 151 void ActivityDatabase::CommitTransaction() { |
| 152 db_.CommitTransaction(); |
| 153 } |
| 154 |
| 155 void ActivityDatabase::RollbackTransaction() { |
| 156 db_.RollbackTransaction(); |
| 157 } |
| 158 |
| 159 bool ActivityDatabase::Raze() { |
| 160 return db_.Raze(); |
| 161 } |
| 162 |
| 163 void ActivityDatabase::Close() { |
| 164 db_.Close(); |
| 165 } |
| 166 |
| 167 void ActivityDatabase::KillDatabase() { |
| 168 db_.RollbackTransaction(); |
| 169 db_.Raze(); |
| 170 db_.Close(); |
| 171 } |
| 172 |
| 173 } // namespace extensions |
| 174 |
OLD | NEW |