Chromium Code Reviews| 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 <set> | 5 #include <set> |
| 6 #include <vector> | 6 #include <vector> |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/threading/thread_checker.h" | 11 #include "base/threading/thread_checker.h" |
| 12 #include "chrome/browser/extensions/activity_log/activity_log.h" | 12 #include "chrome/browser/extensions/activity_log/activity_log.h" |
| 13 #include "chrome/browser/extensions/activity_log/api_actions.h" | 13 #include "chrome/browser/extensions/activity_log/api_actions.h" |
| 14 #include "chrome/browser/extensions/activity_log/blocked_actions.h" | 14 #include "chrome/browser/extensions/activity_log/blocked_actions.h" |
| 15 #include "chrome/browser/extensions/activity_log/stream_noargs_ui_policy.h" | 15 #include "chrome/browser/extensions/activity_log/stream_noargs_ui_policy.h" |
| 16 #include "chrome/browser/extensions/api/activity_log_private/activity_log_privat e_api.h" | 16 #include "chrome/browser/extensions/api/activity_log_private/activity_log_privat e_api.h" |
| 17 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 18 #include "chrome/browser/extensions/extension_system.h" | 18 #include "chrome/browser/extensions/extension_system.h" |
| 19 #include "chrome/browser/extensions/extension_system_factory.h" | 19 #include "chrome/browser/extensions/extension_system_factory.h" |
| 20 #include "chrome/browser/extensions/install_tracker_factory.h" | 20 #include "chrome/browser/extensions/install_tracker_factory.h" |
| 21 #include "chrome/browser/prefs/pref_service_syncable.h" | |
| 21 #include "chrome/browser/prerender/prerender_manager.h" | 22 #include "chrome/browser/prerender/prerender_manager.h" |
| 22 #include "chrome/browser/prerender/prerender_manager_factory.h" | 23 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 23 #include "chrome/browser/profiles/incognito_helpers.h" | 24 #include "chrome/browser/profiles/incognito_helpers.h" |
| 24 #include "chrome/common/chrome_constants.h" | 25 #include "chrome/common/chrome_constants.h" |
| 25 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
| 28 #include "chrome/common/pref_names.h" | |
| 27 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" | 29 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" |
| 28 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 29 #include "third_party/re2/re2/re2.h" | 31 #include "third_party/re2/re2/re2.h" |
| 30 #include "url/gurl.h" | 32 #include "url/gurl.h" |
| 31 | 33 |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 34 // Concatenate arguments. | 36 // Concatenate arguments. |
| 35 std::string MakeArgList(const base::ListValue* args) { | 37 std::string MakeArgList(const base::ListValue* args) { |
| 36 std::string call_signature; | 38 std::string call_signature; |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 156 policy_type_ = policy_type; | 158 policy_type_ = policy_type; |
| 157 } | 159 } |
| 158 } | 160 } |
| 159 | 161 |
| 160 // Use GetInstance instead of directly creating an ActivityLog. | 162 // Use GetInstance instead of directly creating an ActivityLog. |
| 161 ActivityLog::ActivityLog(Profile* profile) | 163 ActivityLog::ActivityLog(Profile* profile) |
| 162 : policy_(NULL), | 164 : policy_(NULL), |
| 163 policy_type_(ActivityLogPolicy::POLICY_INVALID), | 165 policy_type_(ActivityLogPolicy::POLICY_INVALID), |
| 164 profile_(profile), | 166 profile_(profile), |
| 165 enabled_(false), | 167 enabled_(false), |
| 166 initialized_(false), | |
| 167 policy_chosen_(false), | 168 policy_chosen_(false), |
| 168 testing_mode_(false), | 169 testing_mode_(false), |
| 169 has_threads_(true), | 170 has_threads_(true), |
| 170 tracker_(NULL) { | 171 tracker_(NULL), |
| 172 activity_log_extension_active_(false) { | |
|
felt
2013/07/18 22:11:37
nit: can you also replace "activity_log" with "wat
pmarch
2013/07/19 02:41:54
Done.
| |
| 171 // This controls whether logging statements are printed, which policy is set, | 173 // This controls whether logging statements are printed, which policy is set, |
| 172 // etc. | 174 // etc. |
| 173 testing_mode_ = CommandLine::ForCurrentProcess()->HasSwitch( | 175 testing_mode_ = CommandLine::ForCurrentProcess()->HasSwitch( |
| 174 switches::kEnableExtensionActivityLogTesting); | 176 switches::kEnableExtensionActivityLogTesting); |
| 175 | 177 |
| 178 // Check if the activity log extension is previously installed and active. | |
| 179 activity_log_extension_active_ = | |
| 180 profile_->GetPrefs()->GetBoolean(prefs::kActivityLogExtensionActive); | |
| 181 | |
| 176 // Check that the right threads exist. If not, we shouldn't try to do things | 182 // Check that the right threads exist. If not, we shouldn't try to do things |
| 177 // that require them. | 183 // that require them. |
| 178 if (!BrowserThread::IsMessageLoopValid(BrowserThread::DB) || | 184 if (!BrowserThread::IsMessageLoopValid(BrowserThread::DB) || |
| 179 !BrowserThread::IsMessageLoopValid(BrowserThread::FILE) || | 185 !BrowserThread::IsMessageLoopValid(BrowserThread::FILE) || |
| 180 !BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { | 186 !BrowserThread::IsMessageLoopValid(BrowserThread::IO)) { |
| 181 LOG(ERROR) << "Missing threads, disabling Activity Logging!"; | 187 LOG(ERROR) << "Missing threads, disabling Activity Logging!"; |
| 182 has_threads_ = false; | 188 has_threads_ = false; |
| 183 } else { | 189 return; |
| 184 enabled_ = IsLogEnabledOnAnyProfile(); | |
| 185 ExtensionSystem::Get(profile_)->ready().Post( | |
| 186 FROM_HERE, base::Bind(&ActivityLog::Init, base::Unretained(this))); | |
| 187 } | 190 } |
| 188 | 191 |
| 192 // Check if we have the --enable-extension-activity-logging flag, or the | |
| 193 // activity logger is enabled on any profile. | |
| 194 enabled_ = IsLogEnabledOnAnyProfile(); | |
| 189 observers_ = new ObserverListThreadSafe<Observer>; | 195 observers_ = new ObserverListThreadSafe<Observer>; |
| 190 } | |
| 191 | 196 |
| 192 void ActivityLog::Init() { | 197 if (activity_log_extension_active_) { |
| 193 DCHECK(has_threads_); | |
| 194 DCHECK(!initialized_); | |
| 195 const Extension* whitelisted_extension = ExtensionSystem::Get(profile_)-> | |
| 196 extension_service()->GetExtensionById(kActivityLogExtensionId, false); | |
| 197 if (whitelisted_extension) { | |
| 198 enabled_ = true; | 198 enabled_ = true; |
| 199 LogIsEnabled::GetInstance()->SetProfileEnabled(true); | 199 LogIsEnabled::GetInstance()->SetProfileEnabled(true); |
| 200 } | 200 } |
| 201 ExtensionSystem::Get(profile_)->ready().Post( | |
| 202 FROM_HERE, | |
| 203 base::Bind(&ActivityLog::InitInstallTracker, base::Unretained(this))); | |
| 204 ChooseDefaultPolicy(); | |
| 205 } | |
| 206 | |
| 207 void ActivityLog::InitInstallTracker() { | |
| 201 tracker_ = InstallTrackerFactory::GetForProfile(profile_); | 208 tracker_ = InstallTrackerFactory::GetForProfile(profile_); |
| 202 tracker_->AddObserver(this); | 209 tracker_->AddObserver(this); |
| 203 ChooseDefaultPolicy(); | |
| 204 initialized_ = true; | |
| 205 } | 210 } |
| 206 | 211 |
| 207 void ActivityLog::ChooseDefaultPolicy() { | 212 void ActivityLog::ChooseDefaultPolicy() { |
| 208 if (policy_chosen_ || !enabled_) return; | 213 if (policy_chosen_ || !enabled_) return; |
| 209 if (testing_mode_) | 214 if (testing_mode_) |
| 210 SetDefaultPolicy(ActivityLogPolicy::POLICY_FULLSTREAM); | 215 SetDefaultPolicy(ActivityLogPolicy::POLICY_FULLSTREAM); |
| 211 else | 216 else |
| 212 SetDefaultPolicy(ActivityLogPolicy::POLICY_NOARGS); | 217 SetDefaultPolicy(ActivityLogPolicy::POLICY_NOARGS); |
| 213 } | 218 } |
| 214 | 219 |
| 215 void ActivityLog::Shutdown() { | 220 void ActivityLog::Shutdown() { |
| 216 if (tracker_) tracker_->RemoveObserver(this); | 221 if (tracker_) tracker_->RemoveObserver(this); |
| 217 } | 222 } |
| 218 | 223 |
| 219 ActivityLog::~ActivityLog() { | 224 ActivityLog::~ActivityLog() { |
| 220 if (policy_) | 225 if (policy_) |
| 221 policy_->Close(); | 226 policy_->Close(); |
| 222 } | 227 } |
| 223 | 228 |
| 224 bool ActivityLog::IsLogEnabled() { | 229 bool ActivityLog::IsLogEnabled() { |
| 225 if (!has_threads_ || !initialized_) return false; | 230 // Make sure we are not enabled when there are no threads. |
| 231 DCHECK(has_threads_ || !enabled_); | |
| 226 return enabled_; | 232 return enabled_; |
| 227 } | 233 } |
| 228 | 234 |
| 229 void ActivityLog::OnExtensionLoaded(const Extension* extension) { | 235 void ActivityLog::OnExtensionLoaded(const Extension* extension) { |
| 230 if (extension->id() != kActivityLogExtensionId) return; | 236 if (extension->id() != kActivityLogExtensionId) return; |
| 231 enabled_ = true; | 237 if (has_threads_) |
| 238 enabled_ = true; | |
| 239 if (!activity_log_extension_active_) { | |
| 240 activity_log_extension_active_ = true; | |
| 241 profile_->GetPrefs()->SetBoolean(prefs::kActivityLogExtensionActive, true); | |
| 242 } | |
| 232 LogIsEnabled::GetInstance()->SetProfileEnabled(true); | 243 LogIsEnabled::GetInstance()->SetProfileEnabled(true); |
| 233 ChooseDefaultPolicy(); | 244 ChooseDefaultPolicy(); |
| 234 } | 245 } |
| 235 | 246 |
| 236 void ActivityLog::OnExtensionUnloaded(const Extension* extension) { | 247 void ActivityLog::OnExtensionUnloaded(const Extension* extension) { |
| 237 if (extension->id() != kActivityLogExtensionId) return; | 248 if (extension->id() != kActivityLogExtensionId) return; |
| 249 // Make sure we are not enabled when there are no threads. | |
| 250 DCHECK(has_threads_ || !enabled_); | |
| 238 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 251 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 239 switches::kEnableExtensionActivityLogging)) | 252 switches::kEnableExtensionActivityLogging)) |
| 240 enabled_ = false; | 253 enabled_ = false; |
| 254 if (activity_log_extension_active_) { | |
| 255 activity_log_extension_active_ = false; | |
| 256 profile_->GetPrefs()->SetBoolean(prefs::kActivityLogExtensionActive, | |
| 257 false); | |
| 258 } | |
| 241 } | 259 } |
| 242 | 260 |
| 243 // static | 261 // static |
| 244 ActivityLog* ActivityLog::GetInstance(Profile* profile) { | 262 ActivityLog* ActivityLog::GetInstance(Profile* profile) { |
| 245 return ActivityLogFactory::GetForProfile(profile); | 263 return ActivityLogFactory::GetForProfile(profile); |
| 246 } | 264 } |
| 247 | 265 |
| 248 void ActivityLog::AddObserver(ActivityLog::Observer* observer) { | 266 void ActivityLog::AddObserver(ActivityLog::Observer* observer) { |
| 249 observers_->AddObserver(observer); | 267 observers_->AddObserver(observer); |
| 250 } | 268 } |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 504 on_url, | 522 on_url, |
| 505 web_contents->GetTitle(), | 523 web_contents->GetTitle(), |
| 506 std::string(), // no api call here | 524 std::string(), // no api call here |
| 507 script_names.get(), | 525 script_names.get(), |
| 508 DomActionType::INSERTED, | 526 DomActionType::INSERTED, |
| 509 extra); | 527 extra); |
| 510 } | 528 } |
| 511 } | 529 } |
| 512 } | 530 } |
| 513 | 531 |
| 532 // static | |
| 533 void ActivityLog::RegisterProfilePrefs( | |
| 534 user_prefs::PrefRegistrySyncable* registry) { | |
| 535 registry->RegisterBooleanPref( | |
| 536 prefs::kActivityLogExtensionActive, | |
| 537 false, | |
| 538 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | |
| 539 } | |
| 540 | |
| 514 } // namespace extensions | 541 } // namespace extensions |
| OLD | NEW |