#include <map>
#include <set>
+#include "apps/app_lifetime_monitor.h"
+#include "base/memory/weak_ptr.h"
#include "base/scoped_observer.h"
#include "base/timer/timer.h"
#include "components/keyed_service/core/keyed_service.h"
// Performs the background garbage collection of ephemeral apps.
class EphemeralAppService : public KeyedService,
public content::NotificationObserver,
- public extensions::ExtensionRegistryObserver {
+ public extensions::ExtensionRegistryObserver,
+ public apps::AppLifetimeMonitor::Observer {
public:
// Returns the instance for the given profile. This is a convenience wrapper
// around EphemeralAppServiceFactory::GetForProfile.
explicit EphemeralAppService(Profile* profile);
virtual ~EphemeralAppService();
+ // Clears the ephemeral app cache. Removes all idle ephemeral apps.
+ void ClearCachedApps();
+
int ephemeral_app_count() const { return ephemeral_app_count_; }
+ void set_disable_delay_for_test(int delay) {
+ disable_idle_app_delay_ = delay;
+ }
+
// Constants exposed for testing purposes:
// The number of days of inactivity before an ephemeral app will be removed.
const std::string& old_name) OVERRIDE;
virtual void OnExtensionUninstalled(
content::BrowserContext* browser_context,
- const extensions::Extension* extension) OVERRIDE;
+ const extensions::Extension* extension,
+ extensions::UninstallReason reason) OVERRIDE;
+
+ // apps::AppLifetimeMonitor::Observer implementation.
+ virtual void OnAppStop(Profile* profile, const std::string& app_id) OVERRIDE;
+ virtual void OnChromeTerminating() OVERRIDE;
void Init();
void InitEphemeralAppCount();
+ void DisableEphemeralApp(const std::string& app_id);
+ void DisableEphemeralAppsOnStartup();
+
+ void HandleEphemeralAppPromoted(const extensions::Extension* app);
+
// Garbage collect ephemeral apps.
void TriggerGarbageCollect(const base::TimeDelta& delay);
void GarbageCollectApps();
ScopedObserver<extensions::ExtensionRegistry,
extensions::ExtensionRegistryObserver>
extension_registry_observer_;
+ ScopedObserver<apps::AppLifetimeMonitor, apps::AppLifetimeMonitor::Observer>
+ app_lifetime_monitor_observer_;
base::OneShotTimer<EphemeralAppService> garbage_collect_apps_timer_;
// The count of cached ephemeral apps.
int ephemeral_app_count_;
+ // Number of seconds before disabling idle ephemeral apps.
+ // Overridden in tests.
+ int disable_idle_app_delay_;
+
+ base::WeakPtrFactory<EphemeralAppService> weak_ptr_factory_;
+
friend class EphemeralAppServiceTest;
friend class EphemeralAppServiceBrowserTest;