| 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/activity_log.h" | 5 #include "chrome/browser/extensions/activity_log/activity_log.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
| 17 #include "chrome/browser/extensions/activity_log/activity_action_constants.h" | 17 #include "chrome/browser/extensions/activity_log/activity_action_constants.h" |
| 18 #include "chrome/browser/extensions/activity_log/counting_policy.h" | 18 #include "chrome/browser/extensions/activity_log/counting_policy.h" |
| 19 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" | 19 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" |
| 20 #include "chrome/browser/extensions/api/activity_log_private/activity_log_privat
e_api.h" | 20 #include "chrome/browser/extensions/api/activity_log_private/activity_log_privat
e_api.h" |
| 21 #include "chrome/browser/extensions/extension_tab_util.h" | 21 #include "chrome/browser/extensions/extension_tab_util.h" |
| 22 #include "chrome/browser/extensions/install_tracker.h" | |
| 23 #include "chrome/browser/extensions/install_tracker_factory.h" | |
| 24 #include "chrome/browser/prefs/pref_service_syncable.h" | 22 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 25 #include "chrome/browser/prerender/prerender_manager.h" | 23 #include "chrome/browser/prerender/prerender_manager.h" |
| 26 #include "chrome/browser/prerender/prerender_manager_factory.h" | 24 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 27 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
| 29 #include "chrome/common/chrome_constants.h" | 27 #include "chrome/common/chrome_constants.h" |
| 30 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
| 31 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
| 32 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 34 #include "extensions/browser/extension_registry.h" | 32 #include "extensions/browser/extension_registry.h" |
| 33 #include "extensions/browser/extension_registry_factory.h" |
| 35 #include "extensions/browser/extension_system.h" | 34 #include "extensions/browser/extension_system.h" |
| 36 #include "extensions/browser/extension_system_provider.h" | 35 #include "extensions/browser/extension_system_provider.h" |
| 37 #include "extensions/browser/extensions_browser_client.h" | 36 #include "extensions/browser/extensions_browser_client.h" |
| 38 #include "extensions/common/extension.h" | 37 #include "extensions/common/extension.h" |
| 39 #include "extensions/common/one_shot_event.h" | 38 #include "extensions/common/one_shot_event.h" |
| 40 #include "third_party/re2/re2/re2.h" | 39 #include "third_party/re2/re2/re2.h" |
| 41 #include "url/gurl.h" | 40 #include "url/gurl.h" |
| 42 | 41 |
| 43 #if !defined(OS_ANDROID) | 42 #if !defined(OS_ANDROID) |
| 44 #include "chrome/browser/extensions/activity_log/uma_policy.h" | 43 #include "chrome/browser/extensions/activity_log/uma_policy.h" |
| (...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 | 353 |
| 355 // Use GetInstance instead of directly creating an ActivityLog. | 354 // Use GetInstance instead of directly creating an ActivityLog. |
| 356 ActivityLog::ActivityLog(content::BrowserContext* context) | 355 ActivityLog::ActivityLog(content::BrowserContext* context) |
| 357 : database_policy_(NULL), | 356 : database_policy_(NULL), |
| 358 database_policy_type_(ActivityLogPolicy::POLICY_INVALID), | 357 database_policy_type_(ActivityLogPolicy::POLICY_INVALID), |
| 359 uma_policy_(NULL), | 358 uma_policy_(NULL), |
| 360 profile_(Profile::FromBrowserContext(context)), | 359 profile_(Profile::FromBrowserContext(context)), |
| 361 db_enabled_(false), | 360 db_enabled_(false), |
| 362 testing_mode_(false), | 361 testing_mode_(false), |
| 363 has_threads_(true), | 362 has_threads_(true), |
| 364 tracker_(NULL), | 363 extension_registry_observer_(this), |
| 365 watchdog_apps_active_(0) { | 364 watchdog_apps_active_(0) { |
| 366 // This controls whether logging statements are printed & which policy is set. | 365 // This controls whether logging statements are printed & which policy is set. |
| 367 testing_mode_ = CommandLine::ForCurrentProcess()->HasSwitch( | 366 testing_mode_ = CommandLine::ForCurrentProcess()->HasSwitch( |
| 368 switches::kEnableExtensionActivityLogTesting); | 367 switches::kEnableExtensionActivityLogTesting); |
| 369 | 368 |
| 370 // Check if the watchdog extension is previously installed and active. | 369 // Check if the watchdog extension is previously installed and active. |
| 371 // It was originally a boolean, but we've had to move to an integer. Handle | 370 // It was originally a boolean, but we've had to move to an integer. Handle |
| 372 // the legacy case. | 371 // the legacy case. |
| 373 // TODO(felt): In M34, remove the legacy code & old pref. | 372 // TODO(felt): In M34, remove the legacy code & old pref. |
| 374 if (profile_->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActiveOld)) | 373 if (profile_->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActiveOld)) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 386 has_threads_ = false; | 385 has_threads_ = false; |
| 387 } | 386 } |
| 388 | 387 |
| 389 db_enabled_ = has_threads_ | 388 db_enabled_ = has_threads_ |
| 390 && (CommandLine::ForCurrentProcess()-> | 389 && (CommandLine::ForCurrentProcess()-> |
| 391 HasSwitch(switches::kEnableExtensionActivityLogging) | 390 HasSwitch(switches::kEnableExtensionActivityLogging) |
| 392 || watchdog_apps_active_); | 391 || watchdog_apps_active_); |
| 393 | 392 |
| 394 ExtensionSystem::Get(profile_)->ready().Post( | 393 ExtensionSystem::Get(profile_)->ready().Post( |
| 395 FROM_HERE, | 394 FROM_HERE, |
| 396 base::Bind(&ActivityLog::InitInstallTracker, base::Unretained(this))); | 395 base::Bind(&ActivityLog::StartObserving, base::Unretained(this))); |
| 397 | 396 |
| 398 // None of this should run on Android since the AL is behind ENABLE_EXTENSION | 397 // None of this should run on Android since the AL is behind ENABLE_EXTENSION |
| 399 // checks. However, UmaPolicy can't even compile on Android because it uses | 398 // checks. However, UmaPolicy can't even compile on Android because it uses |
| 400 // BrowserList and related classes that aren't compiled for Android. | 399 // BrowserList and related classes that aren't compiled for Android. |
| 401 #if !defined(OS_ANDROID) | 400 #if !defined(OS_ANDROID) |
| 402 if (!profile_->IsOffTheRecord()) | 401 if (!profile_->IsOffTheRecord()) |
| 403 uma_policy_ = new UmaPolicy(profile_); | 402 uma_policy_ = new UmaPolicy(profile_); |
| 404 #endif | 403 #endif |
| 405 | 404 |
| 406 ChooseDatabasePolicy(); | 405 ChooseDatabasePolicy(); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 431 case ActivityLogPolicy::POLICY_COUNTS: | 430 case ActivityLogPolicy::POLICY_COUNTS: |
| 432 database_policy_ = new CountingPolicy(profile_); | 431 database_policy_ = new CountingPolicy(profile_); |
| 433 break; | 432 break; |
| 434 default: | 433 default: |
| 435 NOTREACHED(); | 434 NOTREACHED(); |
| 436 } | 435 } |
| 437 database_policy_->Init(); | 436 database_policy_->Init(); |
| 438 database_policy_type_ = policy_type; | 437 database_policy_type_ = policy_type; |
| 439 } | 438 } |
| 440 | 439 |
| 441 // SHUT DOWN. ------------------------------------------------------------------ | |
| 442 | |
| 443 void ActivityLog::Shutdown() { | |
| 444 if (tracker_) tracker_->RemoveObserver(this); | |
| 445 } | |
| 446 | |
| 447 ActivityLog::~ActivityLog() { | 440 ActivityLog::~ActivityLog() { |
| 448 if (uma_policy_) | 441 if (uma_policy_) |
| 449 uma_policy_->Close(); | 442 uma_policy_->Close(); |
| 450 if (database_policy_) | 443 if (database_policy_) |
| 451 database_policy_->Close(); | 444 database_policy_->Close(); |
| 452 } | 445 } |
| 453 | 446 |
| 454 // MAINTAIN STATUS. ------------------------------------------------------------ | 447 // MAINTAIN STATUS. ------------------------------------------------------------ |
| 455 | 448 |
| 456 void ActivityLog::InitInstallTracker() { | 449 void ActivityLog::StartObserving() { |
| 457 tracker_ = InstallTrackerFactory::GetForProfile(profile_); | 450 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
| 458 tracker_->AddObserver(this); | |
| 459 } | 451 } |
| 460 | 452 |
| 461 void ActivityLog::ChooseDatabasePolicy() { | 453 void ActivityLog::ChooseDatabasePolicy() { |
| 462 if (!(IsDatabaseEnabled() || IsWatchdogAppActive())) | 454 if (!(IsDatabaseEnabled() || IsWatchdogAppActive())) |
| 463 return; | 455 return; |
| 464 if (testing_mode_) | 456 if (testing_mode_) |
| 465 SetDatabasePolicy(ActivityLogPolicy::POLICY_FULLSTREAM); | 457 SetDatabasePolicy(ActivityLogPolicy::POLICY_FULLSTREAM); |
| 466 else | 458 else |
| 467 SetDatabasePolicy(ActivityLogPolicy::POLICY_COUNTS); | 459 SetDatabasePolicy(ActivityLogPolicy::POLICY_COUNTS); |
| 468 } | 460 } |
| 469 | 461 |
| 470 bool ActivityLog::IsDatabaseEnabled() { | 462 bool ActivityLog::IsDatabaseEnabled() { |
| 471 // Make sure we are not enabled when there are no threads. | 463 // Make sure we are not enabled when there are no threads. |
| 472 DCHECK(has_threads_ || !db_enabled_); | 464 DCHECK(has_threads_ || !db_enabled_); |
| 473 return db_enabled_; | 465 return db_enabled_; |
| 474 } | 466 } |
| 475 | 467 |
| 476 bool ActivityLog::IsWatchdogAppActive() { | 468 bool ActivityLog::IsWatchdogAppActive() { |
| 477 return (watchdog_apps_active_ > 0); | 469 return (watchdog_apps_active_ > 0); |
| 478 } | 470 } |
| 479 | 471 |
| 480 void ActivityLog::SetWatchdogAppActiveForTesting(bool active) { | 472 void ActivityLog::SetWatchdogAppActiveForTesting(bool active) { |
| 481 watchdog_apps_active_ = active ? 1 : 0; | 473 watchdog_apps_active_ = active ? 1 : 0; |
| 482 } | 474 } |
| 483 | 475 |
| 484 void ActivityLog::OnExtensionLoaded(const Extension* extension) { | 476 void ActivityLog::OnExtensionLoaded(content::BrowserContext* browser_context, |
| 477 const Extension* extension) { |
| 485 if (!ActivityLogAPI::IsExtensionWhitelisted(extension->id())) return; | 478 if (!ActivityLogAPI::IsExtensionWhitelisted(extension->id())) return; |
| 486 if (has_threads_) | 479 if (has_threads_) |
| 487 db_enabled_ = true; | 480 db_enabled_ = true; |
| 488 watchdog_apps_active_++; | 481 watchdog_apps_active_++; |
| 489 profile_->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive, | 482 profile_->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive, |
| 490 watchdog_apps_active_); | 483 watchdog_apps_active_); |
| 491 if (watchdog_apps_active_ == 1) | 484 if (watchdog_apps_active_ == 1) |
| 492 ChooseDatabasePolicy(); | 485 ChooseDatabasePolicy(); |
| 493 } | 486 } |
| 494 | 487 |
| 495 void ActivityLog::OnExtensionUnloaded(const Extension* extension) { | 488 void ActivityLog::OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 489 const Extension* extension, |
| 490 UnloadedExtensionInfo::Reason reason) { |
| 496 if (!ActivityLogAPI::IsExtensionWhitelisted(extension->id())) return; | 491 if (!ActivityLogAPI::IsExtensionWhitelisted(extension->id())) return; |
| 497 watchdog_apps_active_--; | 492 watchdog_apps_active_--; |
| 498 profile_->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive, | 493 profile_->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive, |
| 499 watchdog_apps_active_); | 494 watchdog_apps_active_); |
| 500 if (watchdog_apps_active_ == 0 && | 495 if (watchdog_apps_active_ == 0 && |
| 501 !CommandLine::ForCurrentProcess()->HasSwitch( | 496 !CommandLine::ForCurrentProcess()->HasSwitch( |
| 502 switches::kEnableExtensionActivityLogging)) { | 497 switches::kEnableExtensionActivityLogging)) { |
| 503 db_enabled_ = false; | 498 db_enabled_ = false; |
| 504 } | 499 } |
| 505 } | 500 } |
| 506 | 501 |
| 507 // OnExtensionUnloaded will also be called right before this. | 502 // OnExtensionUnloaded will also be called right before this. |
| 508 void ActivityLog::OnExtensionUninstalled(const Extension* extension) { | 503 void ActivityLog::OnExtensionUninstalled( |
| 504 content::BrowserContext* browser_context, |
| 505 const Extension* extension) { |
| 509 if (ActivityLogAPI::IsExtensionWhitelisted(extension->id()) && | 506 if (ActivityLogAPI::IsExtensionWhitelisted(extension->id()) && |
| 510 !CommandLine::ForCurrentProcess()->HasSwitch( | 507 !CommandLine::ForCurrentProcess()->HasSwitch( |
| 511 switches::kEnableExtensionActivityLogging) && | 508 switches::kEnableExtensionActivityLogging) && |
| 512 watchdog_apps_active_ == 0) { | 509 watchdog_apps_active_ == 0) { |
| 513 DeleteDatabase(); | 510 DeleteDatabase(); |
| 514 } else if (database_policy_) { | 511 } else if (database_policy_) { |
| 515 database_policy_->RemoveExtensionData(extension->id()); | 512 database_policy_->RemoveExtensionData(extension->id()); |
| 516 } | 513 } |
| 517 } | 514 } |
| 518 | 515 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 690 | 687 |
| 691 void ActivityLog::DeleteDatabase() { | 688 void ActivityLog::DeleteDatabase() { |
| 692 if (!database_policy_) | 689 if (!database_policy_) |
| 693 return; | 690 return; |
| 694 database_policy_->DeleteDatabase(); | 691 database_policy_->DeleteDatabase(); |
| 695 } | 692 } |
| 696 | 693 |
| 697 template <> | 694 template <> |
| 698 void BrowserContextKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies() { | 695 void BrowserContextKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies() { |
| 699 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 696 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 700 DependsOn(InstallTrackerFactory::GetInstance()); | 697 DependsOn(ExtensionRegistryFactory::GetInstance()); |
| 701 } | 698 } |
| 702 | 699 |
| 703 } // namespace extensions | 700 } // namespace extensions |
| OLD | NEW |