| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/extensions/activity_log.h" | 5 #include "chrome/browser/extensions/activity_log.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <vector> | 8 #include <vector> |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 // ActivityLog | 141 // ActivityLog |
| 142 | 142 |
| 143 // Use GetInstance instead of directly creating an ActivityLog. | 143 // Use GetInstance instead of directly creating an ActivityLog. |
| 144 ActivityLog::ActivityLog(Profile* profile) { | 144 ActivityLog::ActivityLog(Profile* profile) { |
| 145 // enable-extension-activity-logging and enable-extension-activity-ui | 145 // enable-extension-activity-logging and enable-extension-activity-ui |
| 146 log_activity_to_stdout_ = CommandLine::ForCurrentProcess()-> | 146 log_activity_to_stdout_ = CommandLine::ForCurrentProcess()-> |
| 147 HasSwitch(switches::kEnableExtensionActivityLogging); | 147 HasSwitch(switches::kEnableExtensionActivityLogging); |
| 148 log_activity_to_ui_ = CommandLine::ForCurrentProcess()-> | 148 log_activity_to_ui_ = CommandLine::ForCurrentProcess()-> |
| 149 HasSwitch(switches::kEnableExtensionActivityUI); | 149 HasSwitch(switches::kEnableExtensionActivityUI); |
| 150 | 150 |
| 151 // enable-extension-activity-log-testing | |
| 152 // Currently, this just controls whether arguments are collected. In the | |
| 153 // future, it may also control other optional activity log features. | |
| 154 log_arguments_ = CommandLine::ForCurrentProcess()-> | |
| 155 HasSwitch(switches::kEnableExtensionActivityLogTesting); | |
| 156 if (!log_arguments_) { | |
| 157 for (int i = 0; i < APIAction::kSizeAlwaysLog; i++) { | |
| 158 arg_whitelist_api_.insert(std::string(APIAction::kAlwaysLog[i])); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 // If the database cannot be initialized for some reason, we keep | 151 // If the database cannot be initialized for some reason, we keep |
| 163 // chugging along but nothing will get recorded. If the UI is | 152 // chugging along but nothing will get recorded. If the UI is |
| 164 // available, things will still get sent to the UI even if nothing | 153 // available, things will still get sent to the UI even if nothing |
| 165 // is being written to the database. | 154 // is being written to the database. |
| 166 db_ = new ActivityDatabase(); | 155 db_ = new ActivityDatabase(); |
| 167 if (!IsLogEnabled()) return; | 156 if (!IsLogEnabled()) return; |
| 168 base::FilePath base_dir = profile->GetPath(); | 157 base::FilePath base_dir = profile->GetPath(); |
| 169 base::FilePath database_name = base_dir.Append( | 158 base::FilePath database_name = base_dir.Append( |
| 170 chrome::kExtensionActivityLogFilename); | 159 chrome::kExtensionActivityLogFilename); |
| 171 KillActivityDatabaseErrorDelegate* error_delegate = | 160 KillActivityDatabaseErrorDelegate* error_delegate = |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 LOG(ERROR) << "Unknown API call! " << api_call; | 226 LOG(ERROR) << "Unknown API call! " << api_call; |
| 238 } | 227 } |
| 239 } | 228 } |
| 240 | 229 |
| 241 // A wrapper around LogAPIActionInternal, but we know it's an API call. | 230 // A wrapper around LogAPIActionInternal, but we know it's an API call. |
| 242 void ActivityLog::LogAPIAction(const Extension* extension, | 231 void ActivityLog::LogAPIAction(const Extension* extension, |
| 243 const std::string& api_call, | 232 const std::string& api_call, |
| 244 const ListValue* args, | 233 const ListValue* args, |
| 245 const std::string& extra) { | 234 const std::string& extra) { |
| 246 if (!IsLogEnabled()) return; | 235 if (!IsLogEnabled()) return; |
| 247 bool log_args = log_arguments_ || | 236 LogAPIActionInternal(extension, api_call, args, extra, APIAction::CALL); |
| 248 arg_whitelist_api_.find(api_call) != arg_whitelist_api_.end(); | |
| 249 LogAPIActionInternal(extension, | |
| 250 api_call, | |
| 251 log_args ? args : new ListValue(), | |
| 252 extra, | |
| 253 APIAction::CALL); | |
| 254 } | 237 } |
| 255 | 238 |
| 256 // A wrapper around LogAPIActionInternal, but we know it's actually an event | 239 // A wrapper around LogAPIActionInternal, but we know it's actually an event |
| 257 // being fired and triggering extension code. Having the two separate methods | 240 // being fired and triggering extension code. Having the two separate methods |
| 258 // (LogAPIAction vs LogEventAction) lets us hide how we actually choose to | 241 // (LogAPIAction vs LogEventAction) lets us hide how we actually choose to |
| 259 // handle them. Right now they're being handled almost the same. | 242 // handle them. Right now they're being handled almost the same. |
| 260 void ActivityLog::LogEventAction(const Extension* extension, | 243 void ActivityLog::LogEventAction(const Extension* extension, |
| 261 const std::string& api_call, | 244 const std::string& api_call, |
| 262 const ListValue* args, | 245 const ListValue* args, |
| 263 const std::string& extra) { | 246 const std::string& extra) { |
| 264 if (!IsLogEnabled()) return; | 247 if (!IsLogEnabled()) return; |
| 265 LogAPIActionInternal(extension, | 248 LogAPIActionInternal(extension, |
| 266 api_call, | 249 api_call, |
| 267 log_arguments_ ? args : new ListValue(), | 250 args, |
| 268 extra, | 251 extra, |
| 269 APIAction::EVENT_CALLBACK); | 252 APIAction::EVENT_CALLBACK); |
| 270 } | 253 } |
| 271 | 254 |
| 272 void ActivityLog::LogBlockedAction(const Extension* extension, | 255 void ActivityLog::LogBlockedAction(const Extension* extension, |
| 273 const std::string& blocked_call, | 256 const std::string& blocked_call, |
| 274 const ListValue* args, | 257 const ListValue* args, |
| 275 const char* reason, | 258 const char* reason, |
| 276 const std::string& extra) { | 259 const std::string& extra) { |
| 277 if (!IsLogEnabled()) return; | 260 if (!IsLogEnabled()) return; |
| 278 bool log_args = log_arguments_ || | |
| 279 arg_whitelist_api_.find(blocked_call) != arg_whitelist_api_.end(); | |
| 280 std::string altered_args = | |
| 281 log_args ? MakeArgList(args) : MakeArgList(new ListValue()); | |
| 282 scoped_refptr<BlockedAction> action = new BlockedAction(extension->id(), | 261 scoped_refptr<BlockedAction> action = new BlockedAction(extension->id(), |
| 283 base::Time::Now(), | 262 base::Time::Now(), |
| 284 blocked_call, | 263 blocked_call, |
| 285 altered_args, | 264 MakeArgList(args), |
| 286 std::string(reason), | 265 std::string(reason), |
| 287 extra); | 266 extra); |
| 288 ScheduleAndForget(&ActivityDatabase::RecordAction, action); | 267 ScheduleAndForget(&ActivityDatabase::RecordAction, action); |
| 289 // Display the action. | 268 // Display the action. |
| 290 ObserverMap::const_iterator iter = observers_.find(extension); | 269 ObserverMap::const_iterator iter = observers_.find(extension); |
| 291 if (iter != observers_.end()) { | 270 if (iter != observers_.end()) { |
| 292 std::string blocked_str = MakeCallSignature(blocked_call, | 271 std::string blocked_str = MakeCallSignature(blocked_call, args); |
| 293 log_args ? args : new ListValue()); | |
| 294 iter->second->Notify(&Observer::OnExtensionActivity, | 272 iter->second->Notify(&Observer::OnExtensionActivity, |
| 295 extension, | 273 extension, |
| 296 ActivityLog::ACTIVITY_EXTENSION_API_BLOCK, | 274 ActivityLog::ACTIVITY_EXTENSION_API_BLOCK, |
| 297 blocked_str); | 275 blocked_str); |
| 298 } | 276 } |
| 299 if (log_activity_to_stdout_) | 277 if (log_activity_to_stdout_) |
| 300 LOG(INFO) << action->PrettyPrintForDebug(); | 278 LOG(INFO) << action->PrettyPrintForDebug(); |
| 301 } | 279 } |
| 302 | 280 |
| 303 void ActivityLog::LogDOMActionInternal(const Extension* extension, | 281 void ActivityLog::LogDOMActionInternal(const Extension* extension, |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 return "content_script"; | 407 return "content_script"; |
| 430 case ActivityLog::ACTIVITY_EVENT_DISPATCH: | 408 case ActivityLog::ACTIVITY_EVENT_DISPATCH: |
| 431 return "event_dispatch"; | 409 return "event_dispatch"; |
| 432 default: | 410 default: |
| 433 NOTREACHED(); | 411 NOTREACHED(); |
| 434 return ""; | 412 return ""; |
| 435 } | 413 } |
| 436 } | 414 } |
| 437 | 415 |
| 438 } // namespace extensions | 416 } // namespace extensions |
| OLD | NEW |