Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "base/stringprintf.h" | 6 #include "base/stringprintf.h" |
| 7 #include "chrome/browser/extensions/activity_log/api_actions.h" | 7 #include "chrome/browser/extensions/activity_log/api_actions.h" |
| 8 #include "chrome/browser/extensions/extension_tab_util.h" | 8 #include "chrome/browser/extensions/extension_tab_util.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 return std::string(); | 31 return std::string(); |
| 32 } | 32 } |
| 33 } | 33 } |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 namespace extensions { | 37 namespace extensions { |
| 38 | 38 |
| 39 const char* APIAction::kTableName = "activitylog_apis"; | 39 const char* APIAction::kTableName = "activitylog_apis"; |
| 40 const char* APIAction::kTableContentFields[] = | 40 const char* APIAction::kTableContentFields[] = |
| 41 {"api_type", "api_call", "args", "extra"}; | 41 {"api_type", "api_call", "args", "extra"}; |
|
Matt Perry
2013/05/17 19:08:55
You could probably store the api_call as an intege
felt
2013/05/18 00:27:18
This is a good idea. I added code that does this.
| |
| 42 const char* APIAction::kTableFieldTypes[] = | |
| 43 {"INTEGER", "LONGVARCHAR", "LONGVARCHAR", "LONGVARCHAR"}; | |
| 42 | 44 |
| 43 // We should log the arguments to these API calls, even if argument logging is | 45 // We should log the arguments to these API calls, even if argument logging is |
| 44 // disabled by default. | 46 // disabled by default. |
| 45 const char* APIAction::kAlwaysLog[] = | 47 const char* APIAction::kAlwaysLog[] = |
| 46 {"extension.connect", "extension.sendMessage", | 48 {"extension.connect", "extension.sendMessage", |
| 47 "tabs.executeScript", "tabs.insertCSS" }; | 49 "tabs.executeScript", "tabs.insertCSS" }; |
| 48 const int APIAction::kSizeAlwaysLog = arraysize(kAlwaysLog); | 50 const int APIAction::kSizeAlwaysLog = arraysize(kAlwaysLog); |
| 49 | 51 |
| 50 APIAction::APIAction(const std::string& extension_id, | 52 APIAction::APIAction(const std::string& extension_id, |
| 51 const base::Time& time, | 53 const base::Time& time, |
| 52 const Type type, | 54 const Type type, |
| 53 const std::string& api_call, | 55 const std::string& api_call, |
| 54 const std::string& args, | 56 const std::string& args, |
| 55 const std::string& extra) | 57 const std::string& extra) |
| 56 : Action(extension_id, time), | 58 : Action(extension_id, time), |
| 57 type_(type), | 59 type_(type), |
| 58 api_call_(api_call), | 60 api_call_(api_call), |
| 59 args_(args), | 61 args_(args), |
| 60 extra_(extra) { } | 62 extra_(extra) { } |
| 61 | 63 |
| 62 APIAction::APIAction(const sql::Statement& s) | 64 APIAction::APIAction(const sql::Statement& s) |
| 63 : Action(s.ColumnString(0), | 65 : Action(s.ColumnString(0), |
| 64 base::Time::FromInternalValue(s.ColumnInt64(1))), | 66 base::Time::FromInternalValue(s.ColumnInt64(1))), |
| 65 type_(StringAsType(s.ColumnString(2))), | 67 type_((Type)s.ColumnInt(2)), |
|
Matt Perry
2013/05/17 19:08:55
style guide forbids c-style casts. use static_cast
felt
2013/05/18 00:27:18
whoops, I didn't remember that. Done.
On 2013/05/
| |
| 66 api_call_(s.ColumnString(3)), | 68 api_call_(s.ColumnString(3)), |
| 67 args_(s.ColumnString(4)), | 69 args_(s.ColumnString(4)), |
| 68 extra_(s.ColumnString(5)) { } | 70 extra_(s.ColumnString(5)) { } |
| 69 | 71 |
| 70 APIAction::~APIAction() { | 72 APIAction::~APIAction() { |
| 71 } | 73 } |
| 72 | 74 |
| 73 // static | 75 // static |
| 74 bool APIAction::InitializeTable(sql::Connection* db) { | 76 bool APIAction::InitializeTable(sql::Connection* db) { |
| 75 // The original table schema was different than the existing one. | 77 // The original table schema was different than the existing one. |
| 76 // We no longer want the api_action_type or target_type columns. | 78 // We no longer want the api_action_type or target_type columns. |
| 77 // Sqlite doesn't let you delete or modify existing columns, so we drop it. | 79 // Sqlite doesn't let you delete or modify existing columns, so we drop it. |
| 78 // Any data loss incurred here doesn't matter since these fields existed | 80 // Any data loss incurred here doesn't matter since these fields existed |
| 79 // before we started using the AL for anything. | 81 // before we started using the AL for anything. |
| 80 if (db->DoesColumnExist(kTableName, "api_action_type")) { | 82 if (db->DoesColumnExist(kTableName, "api_action_type")) { |
| 81 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); | 83 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); |
| 82 if (!db->Execute(drop_table.c_str())) | 84 if (!db->Execute(drop_table.c_str())) |
| 83 return false; | 85 return false; |
| 84 } | 86 } |
| 87 // We also now use INTEGER instead of VARCHAR for api_type. | |
| 88 if (db->DoesColumnExist(kTableName, "api_type")) { | |
| 89 std::string select = base::StringPrintf( | |
| 90 "SELECT api_type FROM %s ORDER BY rowid LIMIT 1", kTableName); | |
| 91 sql::Statement statement(db->GetUniqueStatement(select.c_str())); | |
| 92 if (statement.DeclaredColumnType(0) != sql::COLUMN_TYPE_INTEGER) { | |
| 93 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); | |
| 94 if (!db->Execute(drop_table.c_str())) | |
| 95 return false; | |
| 96 } | |
| 97 } | |
| 85 // Now initialize the table. | 98 // Now initialize the table. |
| 86 return InitializeTableInternal(db, | 99 return InitializeTableInternal(db, |
| 87 kTableName, | 100 kTableName, |
| 88 kTableContentFields, | 101 kTableContentFields, |
| 102 kTableFieldTypes, | |
| 89 arraysize(kTableContentFields)); | 103 arraysize(kTableContentFields)); |
| 90 } | 104 } |
| 91 | 105 |
| 92 void APIAction::Record(sql::Connection* db) { | 106 void APIAction::Record(sql::Connection* db) { |
| 93 std::string sql_str = "INSERT INTO " + std::string(kTableName) | 107 std::string sql_str = "INSERT INTO " + std::string(kTableName) |
| 94 + " (extension_id, time, api_type, api_call, args, extra) VALUES" | 108 + " (extension_id, time, api_type, api_call, args, extra) VALUES" |
| 95 " (?,?,?,?,?,?)"; | 109 " (?,?,?,?,?,?)"; |
| 96 sql::Statement statement(db->GetCachedStatement( | 110 sql::Statement statement(db->GetCachedStatement( |
| 97 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | 111 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 98 statement.BindString(0, extension_id()); | 112 statement.BindString(0, extension_id()); |
| 99 statement.BindInt64(1, time().ToInternalValue()); | 113 statement.BindInt64(1, time().ToInternalValue()); |
| 100 statement.BindString(2, TypeAsString()); | 114 statement.BindInt(2, (int)type_); |
|
Matt Perry
2013/05/17 19:08:55
cast style
felt
2013/05/18 00:27:18
Done.
| |
| 101 statement.BindString(3, api_call_); | 115 statement.BindString(3, api_call_); |
| 102 statement.BindString(4, args_); | 116 statement.BindString(4, args_); |
| 103 statement.BindString(5, extra_); | 117 statement.BindString(5, extra_); |
| 104 if (!statement.Run()) | 118 if (!statement.Run()) |
| 105 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; | 119 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; |
| 106 } | 120 } |
| 107 | 121 |
| 108 // static | 122 // static |
| 109 void APIAction::LookupTabId(const std::string& api_call, | 123 void APIAction::LookupTabId(const std::string& api_call, |
| 110 ListValue* args, | 124 ListValue* args, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 switch (type_) { | 170 switch (type_) { |
| 157 case CALL: | 171 case CALL: |
| 158 return "CALL"; | 172 return "CALL"; |
| 159 case EVENT_CALLBACK: | 173 case EVENT_CALLBACK: |
| 160 return "EVENT_CALLBACK"; | 174 return "EVENT_CALLBACK"; |
| 161 default: | 175 default: |
| 162 return "UNKNOWN_TYPE"; | 176 return "UNKNOWN_TYPE"; |
| 163 } | 177 } |
| 164 } | 178 } |
| 165 | 179 |
| 166 APIAction::Type APIAction::StringAsType( | |
| 167 const std::string& str) { | |
| 168 if (str == "CALL") { | |
| 169 return CALL; | |
| 170 } else if (str == "EVENT_CALLBACK") { | |
| 171 return EVENT_CALLBACK; | |
| 172 } else { | |
| 173 return UNKNOWN_TYPE; | |
| 174 } | |
| 175 } | |
| 176 | |
| 177 } // namespace extensions | 180 } // namespace extensions |
| 178 | 181 |
| OLD | NEW |