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" |
(...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
354 | 354 |
355 // Use GetInstance instead of directly creating an ActivityLog. | 355 // Use GetInstance instead of directly creating an ActivityLog. |
356 ActivityLog::ActivityLog(content::BrowserContext* context) | 356 ActivityLog::ActivityLog(content::BrowserContext* context) |
357 : database_policy_(NULL), | 357 : database_policy_(NULL), |
358 database_policy_type_(ActivityLogPolicy::POLICY_INVALID), | 358 database_policy_type_(ActivityLogPolicy::POLICY_INVALID), |
359 uma_policy_(NULL), | 359 uma_policy_(NULL), |
360 profile_(Profile::FromBrowserContext(context)), | 360 profile_(Profile::FromBrowserContext(context)), |
361 db_enabled_(false), | 361 db_enabled_(false), |
362 testing_mode_(false), | 362 testing_mode_(false), |
363 has_threads_(true), | 363 has_threads_(true), |
364 tracker_(NULL), | 364 extension_registry_observer_(this), |
365 watchdog_apps_active_(0) { | 365 watchdog_apps_active_(0) { |
366 // This controls whether logging statements are printed & which policy is set. | 366 // This controls whether logging statements are printed & which policy is set. |
367 testing_mode_ = CommandLine::ForCurrentProcess()->HasSwitch( | 367 testing_mode_ = CommandLine::ForCurrentProcess()->HasSwitch( |
368 switches::kEnableExtensionActivityLogTesting); | 368 switches::kEnableExtensionActivityLogTesting); |
369 | 369 |
370 // Check if the watchdog extension is previously installed and active. | 370 // 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 | 371 // It was originally a boolean, but we've had to move to an integer. Handle |
372 // the legacy case. | 372 // the legacy case. |
373 // TODO(felt): In M34, remove the legacy code & old pref. | 373 // TODO(felt): In M34, remove the legacy code & old pref. |
374 if (profile_->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActiveOld)) | 374 if (profile_->GetPrefs()->GetBoolean(prefs::kWatchdogExtensionActiveOld)) |
(...skipping 11 matching lines...) Expand all Loading... | |
386 has_threads_ = false; | 386 has_threads_ = false; |
387 } | 387 } |
388 | 388 |
389 db_enabled_ = has_threads_ | 389 db_enabled_ = has_threads_ |
390 && (CommandLine::ForCurrentProcess()-> | 390 && (CommandLine::ForCurrentProcess()-> |
391 HasSwitch(switches::kEnableExtensionActivityLogging) | 391 HasSwitch(switches::kEnableExtensionActivityLogging) |
392 || watchdog_apps_active_); | 392 || watchdog_apps_active_); |
393 | 393 |
394 ExtensionSystem::Get(profile_)->ready().Post( | 394 ExtensionSystem::Get(profile_)->ready().Post( |
395 FROM_HERE, | 395 FROM_HERE, |
396 base::Bind(&ActivityLog::InitInstallTracker, base::Unretained(this))); | 396 base::Bind(&ActivityLog::StartObserving, base::Unretained(this))); |
397 | 397 |
398 // None of this should run on Android since the AL is behind ENABLE_EXTENSION | 398 // 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 | 399 // checks. However, UmaPolicy can't even compile on Android because it uses |
400 // BrowserList and related classes that aren't compiled for Android. | 400 // BrowserList and related classes that aren't compiled for Android. |
401 #if !defined(OS_ANDROID) | 401 #if !defined(OS_ANDROID) |
402 if (!profile_->IsOffTheRecord()) | 402 if (!profile_->IsOffTheRecord()) |
403 uma_policy_ = new UmaPolicy(profile_); | 403 uma_policy_ = new UmaPolicy(profile_); |
404 #endif | 404 #endif |
405 | 405 |
406 ChooseDatabasePolicy(); | 406 ChooseDatabasePolicy(); |
(...skipping 24 matching lines...) Expand all Loading... | |
431 case ActivityLogPolicy::POLICY_COUNTS: | 431 case ActivityLogPolicy::POLICY_COUNTS: |
432 database_policy_ = new CountingPolicy(profile_); | 432 database_policy_ = new CountingPolicy(profile_); |
433 break; | 433 break; |
434 default: | 434 default: |
435 NOTREACHED(); | 435 NOTREACHED(); |
436 } | 436 } |
437 database_policy_->Init(); | 437 database_policy_->Init(); |
438 database_policy_type_ = policy_type; | 438 database_policy_type_ = policy_type; |
439 } | 439 } |
440 | 440 |
441 // SHUT DOWN. ------------------------------------------------------------------ | |
442 | |
443 void ActivityLog::Shutdown() { | |
444 if (tracker_) tracker_->RemoveObserver(this); | |
445 } | |
446 | |
447 ActivityLog::~ActivityLog() { | 441 ActivityLog::~ActivityLog() { |
448 if (uma_policy_) | 442 if (uma_policy_) |
449 uma_policy_->Close(); | 443 uma_policy_->Close(); |
450 if (database_policy_) | 444 if (database_policy_) |
451 database_policy_->Close(); | 445 database_policy_->Close(); |
452 } | 446 } |
453 | 447 |
454 // MAINTAIN STATUS. ------------------------------------------------------------ | 448 // MAINTAIN STATUS. ------------------------------------------------------------ |
455 | 449 |
456 void ActivityLog::InitInstallTracker() { | 450 void ActivityLog::StartObserving() { |
457 tracker_ = InstallTrackerFactory::GetForProfile(profile_); | 451 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
458 tracker_->AddObserver(this); | |
459 } | 452 } |
460 | 453 |
461 void ActivityLog::ChooseDatabasePolicy() { | 454 void ActivityLog::ChooseDatabasePolicy() { |
462 if (!(IsDatabaseEnabled() || IsWatchdogAppActive())) | 455 if (!(IsDatabaseEnabled() || IsWatchdogAppActive())) |
463 return; | 456 return; |
464 if (testing_mode_) | 457 if (testing_mode_) |
465 SetDatabasePolicy(ActivityLogPolicy::POLICY_FULLSTREAM); | 458 SetDatabasePolicy(ActivityLogPolicy::POLICY_FULLSTREAM); |
466 else | 459 else |
467 SetDatabasePolicy(ActivityLogPolicy::POLICY_COUNTS); | 460 SetDatabasePolicy(ActivityLogPolicy::POLICY_COUNTS); |
468 } | 461 } |
469 | 462 |
470 bool ActivityLog::IsDatabaseEnabled() { | 463 bool ActivityLog::IsDatabaseEnabled() { |
471 // Make sure we are not enabled when there are no threads. | 464 // Make sure we are not enabled when there are no threads. |
472 DCHECK(has_threads_ || !db_enabled_); | 465 DCHECK(has_threads_ || !db_enabled_); |
473 return db_enabled_; | 466 return db_enabled_; |
474 } | 467 } |
475 | 468 |
476 bool ActivityLog::IsWatchdogAppActive() { | 469 bool ActivityLog::IsWatchdogAppActive() { |
477 return (watchdog_apps_active_ > 0); | 470 return (watchdog_apps_active_ > 0); |
478 } | 471 } |
479 | 472 |
480 void ActivityLog::SetWatchdogAppActiveForTesting(bool active) { | 473 void ActivityLog::SetWatchdogAppActiveForTesting(bool active) { |
481 watchdog_apps_active_ = active ? 1 : 0; | 474 watchdog_apps_active_ = active ? 1 : 0; |
482 } | 475 } |
483 | 476 |
484 void ActivityLog::OnExtensionLoaded(const Extension* extension) { | 477 void ActivityLog::OnExtensionLoaded(content::BrowserContext* browser_context, |
478 const Extension* extension) { | |
485 if (!ActivityLogAPI::IsExtensionWhitelisted(extension->id())) return; | 479 if (!ActivityLogAPI::IsExtensionWhitelisted(extension->id())) return; |
486 if (has_threads_) | 480 if (has_threads_) |
487 db_enabled_ = true; | 481 db_enabled_ = true; |
488 watchdog_apps_active_++; | 482 watchdog_apps_active_++; |
489 profile_->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive, | 483 profile_->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive, |
490 watchdog_apps_active_); | 484 watchdog_apps_active_); |
491 if (watchdog_apps_active_ == 1) | 485 if (watchdog_apps_active_ == 1) |
492 ChooseDatabasePolicy(); | 486 ChooseDatabasePolicy(); |
493 } | 487 } |
494 | 488 |
495 void ActivityLog::OnExtensionUnloaded(const Extension* extension) { | 489 void ActivityLog::OnExtensionUnloaded(content::BrowserContext* browser_context, |
490 const Extension* extension, | |
491 UnloadedExtensionInfo::Reason reason) { | |
496 if (!ActivityLogAPI::IsExtensionWhitelisted(extension->id())) return; | 492 if (!ActivityLogAPI::IsExtensionWhitelisted(extension->id())) return; |
497 watchdog_apps_active_--; | 493 watchdog_apps_active_--; |
498 profile_->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive, | 494 profile_->GetPrefs()->SetInteger(prefs::kWatchdogExtensionActive, |
499 watchdog_apps_active_); | 495 watchdog_apps_active_); |
500 if (watchdog_apps_active_ == 0 && | 496 if (watchdog_apps_active_ == 0 && |
501 !CommandLine::ForCurrentProcess()->HasSwitch( | 497 !CommandLine::ForCurrentProcess()->HasSwitch( |
502 switches::kEnableExtensionActivityLogging)) { | 498 switches::kEnableExtensionActivityLogging)) { |
503 db_enabled_ = false; | 499 db_enabled_ = false; |
504 } | 500 } |
505 } | 501 } |
506 | 502 |
507 // OnExtensionUnloaded will also be called right before this. | 503 // OnExtensionUnloaded will also be called right before this. |
508 void ActivityLog::OnExtensionUninstalled(const Extension* extension) { | 504 void ActivityLog::OnExtensionUninstalled( |
505 content::BrowserContext* browser_context, | |
506 const Extension* extension) { | |
509 if (ActivityLogAPI::IsExtensionWhitelisted(extension->id()) && | 507 if (ActivityLogAPI::IsExtensionWhitelisted(extension->id()) && |
510 !CommandLine::ForCurrentProcess()->HasSwitch( | 508 !CommandLine::ForCurrentProcess()->HasSwitch( |
511 switches::kEnableExtensionActivityLogging) && | 509 switches::kEnableExtensionActivityLogging) && |
512 watchdog_apps_active_ == 0) { | 510 watchdog_apps_active_ == 0) { |
513 DeleteDatabase(); | 511 DeleteDatabase(); |
514 } else if (database_policy_) { | 512 } else if (database_policy_) { |
515 database_policy_->RemoveExtensionData(extension->id()); | 513 database_policy_->RemoveExtensionData(extension->id()); |
516 } | 514 } |
517 } | 515 } |
518 | 516 |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
690 | 688 |
691 void ActivityLog::DeleteDatabase() { | 689 void ActivityLog::DeleteDatabase() { |
692 if (!database_policy_) | 690 if (!database_policy_) |
693 return; | 691 return; |
694 database_policy_->DeleteDatabase(); | 692 database_policy_->DeleteDatabase(); |
695 } | 693 } |
696 | 694 |
697 template <> | 695 template <> |
698 void BrowserContextKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies() { | 696 void BrowserContextKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies() { |
699 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 697 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
700 DependsOn(InstallTrackerFactory::GetInstance()); | |
not at google - send to devlin
2014/05/29 18:09:29
strictly speaking you should add ExtensionRegistry
limasdf
2014/05/30 21:00:49
Done.
| |
701 } | 698 } |
702 | 699 |
703 } // namespace extensions | 700 } // namespace extensions |
OLD | NEW |