| 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/json/json_string_value_serializer.h" |
| 5 #include "base/logging.h" | 6 #include "base/logging.h" |
| 6 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 7 #include "chrome/browser/extensions/activity_log/blocked_actions.h" | 8 #include "chrome/browser/extensions/activity_log/blocked_actions.h" |
| 9 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" |
| 8 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 9 | 11 |
| 10 using content::BrowserThread; | 12 using content::BrowserThread; |
| 11 | 13 |
| 12 namespace extensions { | 14 namespace extensions { |
| 13 | 15 |
| 14 using api::activity_log_private::ExtensionActivity; | 16 using api::activity_log_private::ExtensionActivity; |
| 15 using api::activity_log_private::DomActivityDetail; | 17 using api::activity_log_private::DomActivityDetail; |
| 16 using api::activity_log_private::ChromeActivityDetail; | 18 using api::activity_log_private::ChromeActivityDetail; |
| 17 using api::activity_log_private::BlockedChromeActivityDetail; | 19 using api::activity_log_private::BlockedChromeActivityDetail; |
| 18 | 20 |
| 19 const char* BlockedAction::kTableName = "activitylog_blocked"; | |
| 20 const char* BlockedAction::kTableContentFields[] = | |
| 21 {"api_call", "args", "reason", "extra"}; | |
| 22 const char* BlockedAction::kTableFieldTypes[] = | |
| 23 {"LONGVARCHAR", "LONGVARCHAR", "INTEGER", "LONGVARCHAR"}; | |
| 24 | |
| 25 BlockedAction::BlockedAction(const std::string& extension_id, | 21 BlockedAction::BlockedAction(const std::string& extension_id, |
| 26 const base::Time& time, | 22 const base::Time& time, |
| 27 const std::string& api_call, | 23 const std::string& api_call, |
| 28 const std::string& args, | 24 const std::string& args, |
| 29 const BlockedAction::Reason reason, | 25 const BlockedAction::Reason reason, |
| 30 const std::string& extra) | 26 const std::string& extra) |
| 31 : Action(extension_id, | 27 : Action(extension_id, |
| 32 time, | 28 time, |
| 33 ExtensionActivity::ACTIVITY_TYPE_BLOCKED_CHROME), | 29 ExtensionActivity::ACTIVITY_TYPE_BLOCKED_CHROME), |
| 34 api_call_(api_call), | 30 api_call_(api_call), |
| 35 args_(args), | 31 args_(args), |
| 36 reason_(reason), | 32 reason_(reason), |
| 37 extra_(extra) { } | 33 extra_(extra) { } |
| 38 | 34 |
| 39 BlockedAction::BlockedAction(const sql::Statement& s) | |
| 40 : Action(s.ColumnString(0), | |
| 41 base::Time::FromInternalValue(s.ColumnInt64(1)), | |
| 42 ExtensionActivity::ACTIVITY_TYPE_BLOCKED_CHROME), | |
| 43 api_call_(s.ColumnString(2)), | |
| 44 args_(s.ColumnString(3)), | |
| 45 reason_(static_cast<Reason>(s.ColumnInt(4))), | |
| 46 extra_(s.ColumnString(5)) { } | |
| 47 | |
| 48 BlockedAction::~BlockedAction() { | 35 BlockedAction::~BlockedAction() { |
| 49 } | 36 } |
| 50 | 37 |
| 51 scoped_ptr<ExtensionActivity> BlockedAction::ConvertToExtensionActivity() { | 38 scoped_ptr<ExtensionActivity> BlockedAction::ConvertToExtensionActivity() { |
| 52 scoped_ptr<ExtensionActivity> formatted_activity; | 39 scoped_ptr<ExtensionActivity> formatted_activity; |
| 53 formatted_activity.reset(new ExtensionActivity); | 40 formatted_activity.reset(new ExtensionActivity); |
| 54 formatted_activity->extension_id.reset( | 41 formatted_activity->extension_id.reset( |
| 55 new std::string(extension_id())); | 42 new std::string(extension_id())); |
| 56 formatted_activity->activity_type = activity_type(); | 43 formatted_activity->activity_type = activity_type(); |
| 57 formatted_activity->time.reset(new double(time().ToJsTime())); | 44 formatted_activity->time.reset(new double(time().ToJsTime())); |
| 58 BlockedChromeActivityDetail* details = new BlockedChromeActivityDetail; | 45 BlockedChromeActivityDetail* details = new BlockedChromeActivityDetail; |
| 59 details->api_call.reset(new std::string(api_call_)); | 46 details->api_call.reset(new std::string(api_call_)); |
| 60 details->args.reset(new std::string(args_)); | 47 details->args.reset(new std::string(args_)); |
| 61 details->reason = BlockedChromeActivityDetail::ParseReason( | 48 details->reason = BlockedChromeActivityDetail::ParseReason( |
| 62 ReasonAsString()); | 49 ReasonAsString()); |
| 63 details->extra.reset(new std::string(extra_)); | 50 details->extra.reset(new std::string(extra_)); |
| 64 formatted_activity->blocked_chrome_activity_detail.reset(details); | 51 formatted_activity->blocked_chrome_activity_detail.reset(details); |
| 65 return formatted_activity.Pass(); | 52 return formatted_activity.Pass(); |
| 66 } | 53 } |
| 67 | 54 |
| 68 // static | |
| 69 bool BlockedAction::InitializeTable(sql::Connection* db) { | |
| 70 // The original table schema was different than the existing one. | |
| 71 // Sqlite doesn't let you delete or modify existing columns, so we drop it. | |
| 72 // The old version can be identified because it had a field named | |
| 73 // blocked_action. Any data loss incurred here doesn't matter since these | |
| 74 // fields existed before we started using the AL for anything. | |
| 75 if (db->DoesColumnExist(kTableName, "blocked_action")) { | |
| 76 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); | |
| 77 if (!db->Execute(drop_table.c_str())) | |
| 78 return false; | |
| 79 } | |
| 80 // We also now use INTEGER instead of VARCHAR for url_action_type. | |
| 81 if (db->DoesColumnExist(kTableName, "reason")) { | |
| 82 std::string select = base::StringPrintf( | |
| 83 "SELECT reason FROM %s ORDER BY rowid LIMIT 1", kTableName); | |
| 84 sql::Statement statement(db->GetUniqueStatement(select.c_str())); | |
| 85 if (statement.DeclaredColumnType(0) != sql::COLUMN_TYPE_INTEGER) { | |
| 86 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); | |
| 87 if (!db->Execute(drop_table.c_str())) | |
| 88 return false; | |
| 89 } | |
| 90 } | |
| 91 return InitializeTableInternal(db, | |
| 92 kTableName, | |
| 93 kTableContentFields, | |
| 94 kTableFieldTypes, | |
| 95 arraysize(kTableContentFields)); | |
| 96 } | |
| 97 | |
| 98 bool BlockedAction::Record(sql::Connection* db) { | 55 bool BlockedAction::Record(sql::Connection* db) { |
| 99 std::string sql_str = "INSERT INTO " + std::string(kTableName) | 56 std::string sql_str = |
| 100 + " (extension_id, time, api_call, args, reason, extra)" | 57 "INSERT INTO " + std::string(FullStreamUIPolicy::kTableName) + |
| 101 " VALUES (?,?,?,?,?,?)"; | 58 " (extension_id, time, action_type, api_name, args, other) VALUES" |
| 59 " (?,?,?,?,?,?)"; |
| 102 sql::Statement statement(db->GetCachedStatement( | 60 sql::Statement statement(db->GetCachedStatement( |
| 103 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | 61 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 104 statement.BindString(0, extension_id()); | 62 statement.BindString(0, extension_id()); |
| 105 statement.BindInt64(1, time().ToInternalValue()); | 63 statement.BindInt64(1, time().ToInternalValue()); |
| 106 statement.BindString(2, api_call_); | 64 statement.BindInt(2, static_cast<int>(Action::ACTION_API_BLOCKED)); |
| 107 statement.BindString(3, args_); | 65 statement.BindString(3, api_call_); |
| 108 statement.BindInt(4, static_cast<int>(reason_)); | 66 statement.BindString(4, args_); |
| 109 statement.BindString(5, extra_); | 67 |
| 68 DictionaryValue other; |
| 69 other.SetInteger("reason", static_cast<int>(reason_)); |
| 70 std::string other_string; |
| 71 JSONStringValueSerializer other_serializer(&other_string); |
| 72 other_serializer.SerializeAndOmitBinaryValues(other); |
| 73 statement.BindString(5, other_string); |
| 74 |
| 110 if (!statement.Run()) { | 75 if (!statement.Run()) { |
| 111 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; | 76 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; |
| 112 statement.Clear(); | 77 statement.Clear(); |
| 113 return false; | 78 return false; |
| 114 } else { | |
| 115 return true; | |
| 116 } | 79 } |
| 80 return true; |
| 117 } | 81 } |
| 118 | 82 |
| 119 std::string BlockedAction::PrintForDebug() { | 83 std::string BlockedAction::PrintForDebug() { |
| 120 return "ID: " + extension_id() + ", blocked action " + api_call_ + | 84 return "ID: " + extension_id() + ", blocked action " + api_call_ + |
| 121 ", reason: " + ReasonAsString(); | 85 ", reason: " + ReasonAsString(); |
| 122 } | 86 } |
| 123 | 87 |
| 124 std::string BlockedAction::ReasonAsString() const { | 88 std::string BlockedAction::ReasonAsString() const { |
| 125 if (reason_ == ACCESS_DENIED) | 89 if (reason_ == ACCESS_DENIED) |
| 126 return std::string("access_denied"); | 90 return std::string("access_denied"); |
| 127 else if (reason_ == QUOTA_EXCEEDED) | 91 else if (reason_ == QUOTA_EXCEEDED) |
| 128 return std::string("quota_exceeded"); | 92 return std::string("quota_exceeded"); |
| 129 else | 93 else |
| 130 return std::string("unknown_reason_type"); // To avoid Win header name. | 94 return std::string("unknown_reason_type"); // To avoid Win header name. |
| 131 } | 95 } |
| 132 | 96 |
| 133 } // namespace extensions | 97 } // namespace extensions |
| 134 | 98 |
| OLD | NEW |