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/json/json_string_value_serializer.h" | |
| 5 #include "base/logging.h" | 6 #include "base/logging.h" |
| 6 #include "base/memory/singleton.h" | 7 #include "base/memory/singleton.h" |
| 7 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 9 #include "chrome/browser/extensions/activity_log/api_actions.h" | 10 #include "chrome/browser/extensions/activity_log/api_actions.h" |
| 10 #include "chrome/browser/extensions/activity_log/api_name_constants.h" | 11 #include "chrome/browser/extensions/activity_log/api_name_constants.h" |
| 12 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" | |
| 11 #include "chrome/browser/extensions/extension_tab_util.h" | 13 #include "chrome/browser/extensions/extension_tab_util.h" |
| 12 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 13 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 14 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 15 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 16 | 18 |
| 17 using content::BrowserThread; | 19 using content::BrowserThread; |
| 18 | 20 |
| 19 namespace { | 21 namespace { |
| 20 | 22 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 93 return shortname; | 95 return shortname; |
| 94 else | 96 else |
| 95 return it->second; | 97 return it->second; |
| 96 } | 98 } |
| 97 | 99 |
| 98 private: | 100 private: |
| 99 std::map<std::string, std::string> names_to_nums_; // <name, number label> | 101 std::map<std::string, std::string> names_to_nums_; // <name, number label> |
| 100 std::map<std::string, std::string> nums_to_names_; // <number label, name> | 102 std::map<std::string, std::string> nums_to_names_; // <number label, name> |
| 101 }; | 103 }; |
| 102 | 104 |
| 105 std::string Serialize(const base::Value& value) { | |
|
felt
2013/07/18 17:09:59
I notice there is also a new Serialize in activity
mvrable
2013/07/18 18:20:34
I wanted the function in activity_actions.cc to be
| |
| 106 std::string value_as_text; | |
| 107 JSONStringValueSerializer serializer(&value_as_text); | |
| 108 serializer.SerializeAndOmitBinaryValues(value); | |
| 109 return value_as_text; | |
| 110 } | |
| 111 | |
| 103 } // namespace | 112 } // namespace |
| 104 | 113 |
| 105 namespace extensions { | 114 namespace extensions { |
| 106 | 115 |
| 107 using api::activity_log_private::ExtensionActivity; | 116 using api::activity_log_private::ExtensionActivity; |
| 108 using api::activity_log_private::DomActivityDetail; | 117 using api::activity_log_private::DomActivityDetail; |
| 109 using api::activity_log_private::ChromeActivityDetail; | 118 using api::activity_log_private::ChromeActivityDetail; |
| 110 using api::activity_log_private::BlockedChromeActivityDetail; | 119 using api::activity_log_private::BlockedChromeActivityDetail; |
| 111 | 120 |
| 112 const char* APIAction::kTableName = "activitylog_apis"; | |
| 113 const char* APIAction::kTableContentFields[] = | |
| 114 {"api_type", "api_call", "args", "extra"}; | |
| 115 const char* APIAction::kTableFieldTypes[] = | |
| 116 {"INTEGER", "LONGVARCHAR", "LONGVARCHAR", "LONGVARCHAR"}; | |
| 117 | |
| 118 // We should log the arguments to these API calls, even if argument logging is | 121 // We should log the arguments to these API calls, even if argument logging is |
| 119 // disabled by default. | 122 // disabled by default. |
| 120 const char* APIAction::kAlwaysLog[] = | 123 const char* APIAction::kAlwaysLog[] = |
| 121 {"extension.connect", "extension.sendMessage", | 124 {"extension.connect", "extension.sendMessage", |
| 122 "tabs.executeScript", "tabs.insertCSS" }; | 125 "tabs.executeScript", "tabs.insertCSS" }; |
| 123 const int APIAction::kSizeAlwaysLog = arraysize(kAlwaysLog); | 126 const int APIAction::kSizeAlwaysLog = arraysize(kAlwaysLog); |
| 124 | 127 |
| 125 // A string used in place of the real URL when the URL is hidden because it is | 128 // A string used in place of the real URL when the URL is hidden because it is |
| 126 // in an incognito window. Extension activity logs mentioning kIncognitoUrl | 129 // in an incognito window. Extension activity logs mentioning kIncognitoUrl |
| 127 // let the user know that an extension is manipulating incognito tabs without | 130 // let the user know that an extension is manipulating incognito tabs without |
| 128 // recording specific data about the pages. | 131 // recording specific data about the pages. |
| 129 const char* APIAction::kIncognitoUrl = "http://incognito/"; | 132 const char* APIAction::kIncognitoUrl = "http://incognito/"; |
| 130 | 133 |
| 131 APIAction::APIAction(const std::string& extension_id, | 134 APIAction::APIAction(const std::string& extension_id, |
| 132 const base::Time& time, | 135 const base::Time& time, |
| 133 const Type type, | 136 const Type type, |
| 134 const std::string& api_call, | 137 const std::string& api_call, |
| 135 const std::string& args, | 138 const std::string& args, |
| 139 const base::ListValue& args_list, | |
| 136 const std::string& extra) | 140 const std::string& extra) |
| 137 : Action(extension_id, time, ExtensionActivity::ACTIVITY_TYPE_CHROME), | 141 : Action(extension_id, time, ExtensionActivity::ACTIVITY_TYPE_CHROME), |
| 138 type_(type), | 142 type_(type), |
| 139 api_call_(api_call), | 143 api_call_(api_call), |
| 140 args_(args), | 144 args_(args), |
| 145 args_list_(args_list.DeepCopy()), | |
| 141 extra_(extra) { } | 146 extra_(extra) { } |
| 142 | 147 |
| 143 APIAction::APIAction(const sql::Statement& s) | |
| 144 : Action(s.ColumnString(0), | |
| 145 base::Time::FromInternalValue(s.ColumnInt64(1)), | |
| 146 ExtensionActivity::ACTIVITY_TYPE_CHROME), | |
| 147 type_(static_cast<Type>(s.ColumnInt(2))), | |
| 148 api_call_(APINameMap::GetInstance()->ShortnameToApi(s.ColumnString(3))), | |
| 149 args_(s.ColumnString(4)), | |
| 150 extra_(s.ColumnString(5)) { } | |
| 151 | |
| 152 APIAction::~APIAction() { | 148 APIAction::~APIAction() { |
| 153 } | 149 } |
| 154 | 150 |
| 155 scoped_ptr<ExtensionActivity> APIAction::ConvertToExtensionActivity() { | 151 scoped_ptr<ExtensionActivity> APIAction::ConvertToExtensionActivity() { |
| 156 scoped_ptr<ExtensionActivity> formatted_activity; | 152 scoped_ptr<ExtensionActivity> formatted_activity; |
| 157 formatted_activity.reset(new ExtensionActivity); | 153 formatted_activity.reset(new ExtensionActivity); |
| 158 formatted_activity->extension_id.reset( | 154 formatted_activity->extension_id.reset( |
| 159 new std::string(extension_id())); | 155 new std::string(extension_id())); |
| 160 formatted_activity->activity_type = activity_type(); | 156 formatted_activity->activity_type = activity_type(); |
| 161 formatted_activity->time.reset(new double(time().ToJsTime())); | 157 formatted_activity->time.reset(new double(time().ToJsTime())); |
| 162 ChromeActivityDetail* details = new ChromeActivityDetail; | 158 ChromeActivityDetail* details = new ChromeActivityDetail; |
| 163 details->api_activity_type = ChromeActivityDetail::ParseApiActivityType( | 159 details->api_activity_type = ChromeActivityDetail::ParseApiActivityType( |
| 164 TypeAsString()); | 160 TypeAsString()); |
| 165 details->api_call.reset(new std::string(api_call_)); | 161 details->api_call.reset(new std::string(api_call_)); |
| 166 details->args.reset(new std::string(args_)); | 162 details->args.reset(new std::string(args_)); |
| 167 details->extra.reset(new std::string(extra_)); | 163 details->extra.reset(new std::string(extra_)); |
| 168 formatted_activity->chrome_activity_detail.reset(details); | 164 formatted_activity->chrome_activity_detail.reset(details); |
| 169 return formatted_activity.Pass(); | 165 return formatted_activity.Pass(); |
| 170 } | 166 } |
| 171 | 167 |
| 172 // static | |
| 173 bool APIAction::InitializeTable(sql::Connection* db) { | |
| 174 // The original table schema was different than the existing one. | |
| 175 // We no longer want the api_action_type or target_type columns. | |
| 176 // Sqlite doesn't let you delete or modify existing columns, so we drop it. | |
| 177 // Any data loss incurred here doesn't matter since these fields existed | |
| 178 // before we started using the AL for anything. | |
| 179 if (db->DoesColumnExist(kTableName, "api_action_type")) { | |
| 180 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); | |
| 181 if (!db->Execute(drop_table.c_str())) | |
| 182 return false; | |
| 183 } | |
| 184 // We also now use INTEGER instead of VARCHAR for api_type. | |
| 185 if (db->DoesColumnExist(kTableName, "api_type")) { | |
| 186 std::string select = base::StringPrintf( | |
| 187 "SELECT api_type FROM %s ORDER BY rowid LIMIT 1", kTableName); | |
| 188 sql::Statement statement(db->GetUniqueStatement(select.c_str())); | |
| 189 if (statement.DeclaredColumnType(0) != sql::COLUMN_TYPE_INTEGER) { | |
| 190 std::string drop_table = base::StringPrintf("DROP TABLE %s", kTableName); | |
| 191 if (!db->Execute(drop_table.c_str())) | |
| 192 return false; | |
| 193 } | |
| 194 } | |
| 195 // Now initialize the table. | |
| 196 return InitializeTableInternal(db, | |
| 197 kTableName, | |
| 198 kTableContentFields, | |
| 199 kTableFieldTypes, | |
| 200 arraysize(kTableContentFields)); | |
| 201 } | |
| 202 | |
| 203 bool APIAction::Record(sql::Connection* db) { | 168 bool APIAction::Record(sql::Connection* db) { |
| 204 std::string sql_str = "INSERT INTO " + std::string(kTableName) | 169 std::string sql_str = |
| 205 + " (extension_id, time, api_type, api_call, args, extra) VALUES" | 170 "INSERT INTO " + std::string(FullStreamUIPolicy::kTableName) + |
| 206 " (?,?,?,?,?,?)"; | 171 " (extension_id, time, action_type, api_name, args) VALUES" |
| 172 " (?,?,?,?,?)"; | |
| 207 sql::Statement statement(db->GetCachedStatement( | 173 sql::Statement statement(db->GetCachedStatement( |
| 208 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | 174 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 209 statement.BindString(0, extension_id()); | 175 statement.BindString(0, extension_id()); |
| 210 statement.BindInt64(1, time().ToInternalValue()); | 176 statement.BindInt64(1, time().ToInternalValue()); |
| 211 statement.BindInt(2, static_cast<int>(type_)); | 177 switch (type_) { |
| 212 statement.BindString(3, APINameMap::GetInstance()->ApiToShortname(api_call_)); | 178 case CALL: |
| 213 statement.BindString(4, args_); | 179 statement.BindInt(2, static_cast<int>(Action::ACTION_API_CALL)); |
| 214 statement.BindString(5, extra_); | 180 break; |
| 181 case EVENT_CALLBACK: | |
| 182 statement.BindInt(2, static_cast<int>(Action::ACTION_API_EVENT)); | |
| 183 break; | |
| 184 default: | |
| 185 LOG(ERROR) << "Invalid action type: " << type_; | |
| 186 return false; | |
| 187 } | |
| 188 statement.BindString(3, api_call_); | |
| 189 statement.BindString(4, Serialize(*args_list_)); | |
| 215 if (!statement.Run()) { | 190 if (!statement.Run()) { |
| 216 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; | 191 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; |
| 217 statement.Clear(); | 192 statement.Clear(); |
| 218 return false; | 193 return false; |
| 219 } | 194 } |
| 220 return true; | 195 return true; |
| 221 } | 196 } |
| 222 | 197 |
| 223 // static | 198 // static |
| 224 void APIAction::LookupTabId(const std::string& api_call, | 199 void APIAction::LookupTabId(const std::string& api_call, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 return "call"; | 248 return "call"; |
| 274 case EVENT_CALLBACK: | 249 case EVENT_CALLBACK: |
| 275 return "event_callback"; | 250 return "event_callback"; |
| 276 default: | 251 default: |
| 277 return "unknown_type"; | 252 return "unknown_type"; |
| 278 } | 253 } |
| 279 } | 254 } |
| 280 | 255 |
| 281 } // namespace extensions | 256 } // namespace extensions |
| 282 | 257 |
| OLD | NEW |