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,84 @@ |
+// 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 "chrome/browser/extensions/activity_database.h" |
+#include "chrome/browser/history/url_database.h" |
+ |
+namespace activity { |
+ |
+ActivityDatabase::ActivityDatabase() { |
+} |
+ |
+ActivityDatabase::~ActivityDatabase() { |
+} |
+ |
+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 sql::INIT_FAILURE; |
+ } |
+ |
+ // 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 sql::INIT_FAILURE; |
+ |
+ db_.Preload(); |
+ |
+ // Create the UrlAction database. |
+ if (!db_.DoesTableExist(activity::url_table_name_.c_str())) |
+ if (!db_.Execute(url_table_creator_.c_str())) |
+ return sql::INIT_FAILURE; |
+ |
+ // Create the ManagerAction database. |
+ if (!db_.DoesTableExist(activity::manager_table_name_.c_str())) |
+ if (!db_.Execute(activity::manager_table_creator_.c_str())) |
+ return sql::INIT_FAILURE; |
+ |
+ // TODO(felt): do we need to do a version check? |
+ |
+ return committer.Commit() ? sql::INIT_OK : sql::INIT_FAILURE; |
+} |
+ |
+// The table structure is defined by activity::url_table_creator |
+// in url_actions.h. |
+bool ActivityDatabase::RecordAction(const UrlAction& action) { |
+ std::string sql_str = "INSERT INTO " + activity::url_table_name_ + |
+ " (extension_id, time, url_action_type, url, url_title, api_call)" |
+ " VALUES (?,?,?,?,?,?)"; |
+ sql::Statement statement(db_.GetCachedStatement( |
+ sql::StatementID("url_action_insertion"), 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.api_call()); |
+ return statement.Run(); |
+} |
+ |
+// The table structure is defined by activity::manager_table_creator |
+// in manager_actions.h. |
+bool ActivityDatabase::RecordAction(const ManagerAction& action) { |
+ std::string sql_str = "INSERT INTO " + activity::manager_table_name_ + |
+ " (extension_id, time, manager_action_type, target_type, api_call)" |
+ " VALUES (?,?,?,?,?)"; |
+ sql::Statement statement(db_.GetCachedStatement( |
+ sql::StatementID("manager_action_insertion"), sql_str.c_str())); |
+ statement.BindString(0, action.extension_id()); |
+ statement.BindInt64(2, action.time().ToInternalValue()); |
+ statement.BindString(3, action.VerbAsString()); |
+ statement.BindString(4, action.TargetAsString()); |
+ statement.BindString(5, action.api_call()); |
+ return statement.Run(); |
+} |
+} // namespace |