| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/json/json_string_value_serializer.h" | |
| 6 #include "base/logging.h" | |
| 7 #include "base/strings/stringprintf.h" | |
| 8 #include "chrome/browser/extensions/activity_log/blocked_actions.h" | |
| 9 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 | |
| 12 using content::BrowserThread; | |
| 13 | |
| 14 namespace extensions { | |
| 15 | |
| 16 using api::activity_log_private::ExtensionActivity; | |
| 17 using api::activity_log_private::DomActivityDetail; | |
| 18 using api::activity_log_private::ChromeActivityDetail; | |
| 19 using api::activity_log_private::BlockedChromeActivityDetail; | |
| 20 | |
| 21 BlockedAction::BlockedAction(const std::string& extension_id, | |
| 22 const base::Time& time, | |
| 23 const std::string& api_call, | |
| 24 const std::string& args, | |
| 25 const BlockedAction::Reason reason, | |
| 26 const std::string& extra) | |
| 27 : Action(extension_id, | |
| 28 time, | |
| 29 ExtensionActivity::ACTIVITY_TYPE_BLOCKED_CHROME), | |
| 30 api_call_(api_call), | |
| 31 args_(args), | |
| 32 reason_(reason), | |
| 33 extra_(extra) { } | |
| 34 | |
| 35 BlockedAction::~BlockedAction() { | |
| 36 } | |
| 37 | |
| 38 scoped_ptr<ExtensionActivity> BlockedAction::ConvertToExtensionActivity() { | |
| 39 scoped_ptr<ExtensionActivity> formatted_activity; | |
| 40 formatted_activity.reset(new ExtensionActivity); | |
| 41 formatted_activity->extension_id.reset( | |
| 42 new std::string(extension_id())); | |
| 43 formatted_activity->activity_type = activity_type(); | |
| 44 formatted_activity->time.reset(new double(time().ToJsTime())); | |
| 45 BlockedChromeActivityDetail* details = new BlockedChromeActivityDetail; | |
| 46 details->api_call.reset(new std::string(api_call_)); | |
| 47 details->args.reset(new std::string(args_)); | |
| 48 details->reason = BlockedChromeActivityDetail::ParseReason( | |
| 49 ReasonAsString()); | |
| 50 details->extra.reset(new std::string(extra_)); | |
| 51 formatted_activity->blocked_chrome_activity_detail.reset(details); | |
| 52 return formatted_activity.Pass(); | |
| 53 } | |
| 54 | |
| 55 bool BlockedAction::Record(sql::Connection* db) { | |
| 56 std::string sql_str = | |
| 57 "INSERT INTO " + std::string(FullStreamUIPolicy::kTableName) + | |
| 58 " (extension_id, time, action_type, api_name, args, other) VALUES" | |
| 59 " (?,?,?,?,?,?)"; | |
| 60 sql::Statement statement(db->GetCachedStatement( | |
| 61 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | |
| 62 statement.BindString(0, extension_id()); | |
| 63 statement.BindInt64(1, time().ToInternalValue()); | |
| 64 statement.BindInt(2, static_cast<int>(Action::ACTION_API_BLOCKED)); | |
| 65 statement.BindString(3, api_call_); | |
| 66 statement.BindString(4, args_); | |
| 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 | |
| 75 if (!statement.Run()) { | |
| 76 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; | |
| 77 statement.Clear(); | |
| 78 return false; | |
| 79 } | |
| 80 return true; | |
| 81 } | |
| 82 | |
| 83 std::string BlockedAction::PrintForDebug() { | |
| 84 return "ID: " + extension_id() + ", blocked action " + api_call_ + | |
| 85 ", reason: " + ReasonAsString(); | |
| 86 } | |
| 87 | |
| 88 std::string BlockedAction::ReasonAsString() const { | |
| 89 if (reason_ == ACCESS_DENIED) | |
| 90 return std::string("access_denied"); | |
| 91 else if (reason_ == QUOTA_EXCEEDED) | |
| 92 return std::string("quota_exceeded"); | |
| 93 else | |
| 94 return std::string("unknown_reason_type"); // To avoid Win header name. | |
| 95 } | |
| 96 | |
| 97 } // namespace extensions | |
| 98 | |
| OLD | NEW |