Chromium Code Reviews| Index: chrome/browser/extensions/activity_database.cc |
| =================================================================== |
| --- chrome/browser/extensions/activity_database.cc (revision 0) |
| +++ chrome/browser/extensions/activity_database.cc (revision 0) |
| @@ -0,0 +1,180 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <string> |
| +#include "base/logging.h" |
| +#include "chrome/browser/extensions/activity_database.h" |
| +#include "chrome/browser/history/url_database.h" |
| +#include "sql/transaction.h" |
| + |
| +#if defined(OS_MACOSX) |
| +#include "base/mac/mac_util.h" |
| +#endif |
| + |
| +namespace { |
| + |
| +void LogStatementFailure(const std::string& sql_str) { |
| + LOG(ERROR) << "Activity log database I/O failed: " << sql_str; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace extensions { |
| + |
| +ActivityDatabase::ActivityDatabase() { |
| + initialized_ = false; |
|
Eric Dingle
2012/12/13 20:37:34
You should initialize this in the initialization l
felt
2012/12/15 02:51:52
Done.
|
| +} |
| + |
| +ActivityDatabase::~ActivityDatabase() { |
| + Close(); // Safe to call Close() even if Open() never happened. |
| +} |
| + |
| +sql::InitStatus ActivityDatabase::Init( |
| + const FilePath& db_name, |
| + sql::ErrorDelegate* error_delegate) { |
| + db_.set_error_delegate(error_delegate); |
| + db_.set_page_size(4096); |
| + db_.set_cache_size(32); |
| + |
| + if (!db_.Open(db_name)) { |
| + LOG(ERROR) << db_.GetErrorMessage(); |
| + return MarkInitFailure(); |
| + } |
| + |
| + // Wrap the initialization in a transaction so that the db doesn't |
| + // get corrupted if init fails/crashes. |
| + sql::Transaction committer(&db_); |
| + if (!committer.Begin()) |
| + return MarkInitFailure(); |
| + |
| +#if defined(OS_MACOSX) |
| + // Exclude the database from backups. |
| + base::mac::SetFileBackupExclusion(db_name); |
| +#endif |
| + |
| + db_.Preload(); |
| + |
| + // Create the UrlAction database. |
| + if (!db_.DoesTableExist(url_table_name_)) { |
|
Eric Dingle
2012/12/13 20:37:34
This logic might be worth extracting to a helper m
felt
2012/12/15 02:51:52
Done.
|
| + std::string url_table_creator = "CREATE TABLE " + |
|
Eric Dingle
2012/12/13 20:37:34
Replace these concatenations with StrCat(...). Her
felt
2012/12/15 02:51:52
Done.
|
| + std::string(url_table_name_) + " " + |
| + std::string(url_table_structure_); |
| + if (!db_.Execute(url_table_creator.c_str())) |
| + return MarkInitFailure(); |
| + } |
| + |
| + // Create the ManagerAction database. |
| + if (!db_.DoesTableExist(manager_table_name_)) { |
| + std::string manager_table_creator = "CREATE TABLE " + |
| + std::string(manager_table_name_) + " " + |
| + std::string(manager_table_structure_); |
| + if (!db_.Execute(manager_table_creator.c_str())) |
| + return MarkInitFailure(); |
| + } |
| + |
| + // Create the BlockedAction database. |
| + if (!db_.DoesTableExist(blocked_table_name_)) { |
| + std::string blocked_table_creator = "CREATE TABLE " + |
| + std::string(blocked_table_name_) + " " + |
| + std::string(blocked_table_structure_); |
| + if (!db_.Execute(blocked_table_creator.c_str())) |
| + return MarkInitFailure(); |
| + } |
| + |
| + sql::InitStatus stat = committer.Commit() ? sql::INIT_OK : MarkInitFailure(); |
| + if (stat == sql::INIT_OK) |
| + initialized_ = true; |
| + return stat; |
| +} |
| + |
| +sql::InitStatus ActivityDatabase::MarkInitFailure() { |
|
Eric Dingle
2012/12/13 20:37:34
Not sure that this method is needed. initialized_
felt
2012/12/15 02:51:52
Done.
|
| + initialized_ = false; |
| + return sql::INIT_FAILURE; |
| +} |
| + |
| +// The table structure is defined in url_actions.h. |
| +void ActivityDatabase::RecordUrlAction(scoped_refptr<UrlAction> action) { |
| + if (!initialized_) |
| + return; |
| + std::string sql_str = "INSERT INTO " + std::string(url_table_name_) + |
| + " (extension_id, time, url_action_type, url, url_title, api_call)" |
| + " VALUES (?,?,?,?,?,?)"; |
| + sql::Statement statement(db_.GetCachedStatement( |
| + sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| + statement.BindString(0, action->extension_id()); |
| + statement.BindInt64(1, action->time().ToInternalValue()); |
| + statement.BindString(2, action->VerbAsString()); |
| + statement.BindString(3, |
| + history::URLDatabase::GURLToDatabaseURL(action->url())); |
| + statement.BindString16(4, action->url_title()); |
| + statement.BindString(5, action->script()); |
| + if (!statement.Run()) |
| + LogStatementFailure(sql_str); |
| +} |
| + |
| +// The table structure is defined in manager_actions.h. |
| +void ActivityDatabase::RecordManagerAction( |
| + scoped_refptr<ManagerAction> action) { |
| + if (!initialized_) |
| + return; |
| + std::string sql_str = "INSERT INTO " + std::string(manager_table_name_) + |
| + " (extension_id, time, manager_action_type, target_type, api_call)" |
| + " VALUES (?,?,?,?,?)"; |
| + sql::Statement statement(db_.GetCachedStatement( |
| + sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| + statement.BindString(0, action->extension_id()); |
| + statement.BindInt64(1, action->time().ToInternalValue()); |
| + statement.BindString(2, action->VerbAsString()); |
| + statement.BindString(3, action->TargetAsString()); |
| + statement.BindString(4, action->api_call()); |
| + if (!statement.Run()) |
| + LogStatementFailure(sql_str); |
| +} |
| + |
| +// The table structure is defined in blocked_actions.h |
| +void ActivityDatabase::RecordBlockedAction( |
| + scoped_refptr<BlockedAction> action) { |
| + if (!initialized_) |
| + return; |
| + std::string sql_str = "INSERT INTO " + std::string(blocked_table_name_) + |
| + " (extension_id, time, blocked_action, reason)" |
| + " VALUES (?,?,?,?)"; |
| + sql::Statement statement(db_.GetCachedStatement( |
| + sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| + statement.BindString(0, action->extension_id()); |
| + statement.BindInt64(1, action->time().ToInternalValue()); |
| + statement.BindString(2, action->blocked_action()); |
| + statement.BindString(3, action->reason()); |
| + if (!statement.Run()) |
| + LogStatementFailure(sql_str); |
| +} |
| + |
| +void ActivityDatabase::BeginTransaction() { |
| + db_.BeginTransaction(); |
| +} |
| + |
| +void ActivityDatabase::CommitTransaction() { |
| + db_.CommitTransaction(); |
| +} |
| + |
| +void ActivityDatabase::RollbackTransaction() { |
| + db_.RollbackTransaction(); |
| +} |
| + |
| +bool ActivityDatabase::Raze() { |
| + return db_.Raze(); |
| +} |
| + |
| +void ActivityDatabase::Close() { |
| + db_.Close(); |
| +} |
| + |
| +void ActivityDatabase::KillDatabase() { |
| + db_.RollbackTransaction(); |
| + db_.Raze(); |
| + db_.Close(); |
| +} |
| + |
| +} // namespace |
| + |