| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_ |
| 6 #define CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_ | 6 #define CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "apps/app_lifetime_monitor.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 11 #include "base/scoped_observer.h" | 13 #include "base/scoped_observer.h" |
| 12 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
| 13 #include "components/keyed_service/core/keyed_service.h" | 15 #include "components/keyed_service/core/keyed_service.h" |
| 14 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
| 15 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
| 16 #include "extensions/browser/extension_registry_observer.h" | 18 #include "extensions/browser/extension_registry_observer.h" |
| 17 | 19 |
| 18 class Profile; | 20 class Profile; |
| 19 | 21 |
| 20 namespace extensions { | 22 namespace extensions { |
| 21 class Extension; | 23 class Extension; |
| 22 class ExtensionRegistry; | 24 class ExtensionRegistry; |
| 23 } // namespace extensions | 25 } // namespace extensions |
| 24 | 26 |
| 25 // Performs the background garbage collection of ephemeral apps. | 27 // Performs the background garbage collection of ephemeral apps. |
| 26 class EphemeralAppService : public KeyedService, | 28 class EphemeralAppService : public KeyedService, |
| 27 public content::NotificationObserver, | 29 public content::NotificationObserver, |
| 28 public extensions::ExtensionRegistryObserver { | 30 public extensions::ExtensionRegistryObserver, |
| 31 public apps::AppLifetimeMonitor::Observer { |
| 29 public: | 32 public: |
| 30 // Returns the instance for the given profile. This is a convenience wrapper | 33 // Returns the instance for the given profile. This is a convenience wrapper |
| 31 // around EphemeralAppServiceFactory::GetForProfile. | 34 // around EphemeralAppServiceFactory::GetForProfile. |
| 32 static EphemeralAppService* Get(Profile* profile); | 35 static EphemeralAppService* Get(Profile* profile); |
| 33 | 36 |
| 34 explicit EphemeralAppService(Profile* profile); | 37 explicit EphemeralAppService(Profile* profile); |
| 35 virtual ~EphemeralAppService(); | 38 virtual ~EphemeralAppService(); |
| 36 | 39 |
| 37 // Clears the ephemeral app cache. Removes all idle ephemeral apps. | 40 // Clears the ephemeral app cache. Removes all idle ephemeral apps. |
| 38 void ClearCachedApps(); | 41 void ClearCachedApps(); |
| 39 | 42 |
| 40 int ephemeral_app_count() const { return ephemeral_app_count_; } | 43 int ephemeral_app_count() const { return ephemeral_app_count_; } |
| 41 | 44 |
| 45 void set_disable_delay_for_test(int delay) { |
| 46 disable_idle_app_delay_ = delay; |
| 47 } |
| 48 |
| 42 // Constants exposed for testing purposes: | 49 // Constants exposed for testing purposes: |
| 43 | 50 |
| 44 // The number of days of inactivity before an ephemeral app will be removed. | 51 // The number of days of inactivity before an ephemeral app will be removed. |
| 45 static const int kAppInactiveThreshold; | 52 static const int kAppInactiveThreshold; |
| 46 // If the ephemeral app has been launched within this number of days, it will | 53 // If the ephemeral app has been launched within this number of days, it will |
| 47 // definitely not be garbage collected. | 54 // definitely not be garbage collected. |
| 48 static const int kAppKeepThreshold; | 55 static const int kAppKeepThreshold; |
| 49 // The maximum number of ephemeral apps to keep cached. Excess may be removed. | 56 // The maximum number of ephemeral apps to keep cached. Excess may be removed. |
| 50 static const int kMaxEphemeralAppsCount; | 57 static const int kMaxEphemeralAppsCount; |
| 51 | 58 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 63 content::BrowserContext* browser_context, | 70 content::BrowserContext* browser_context, |
| 64 const extensions::Extension* extension, | 71 const extensions::Extension* extension, |
| 65 bool is_update, | 72 bool is_update, |
| 66 bool from_ephemeral, | 73 bool from_ephemeral, |
| 67 const std::string& old_name) OVERRIDE; | 74 const std::string& old_name) OVERRIDE; |
| 68 virtual void OnExtensionUninstalled( | 75 virtual void OnExtensionUninstalled( |
| 69 content::BrowserContext* browser_context, | 76 content::BrowserContext* browser_context, |
| 70 const extensions::Extension* extension, | 77 const extensions::Extension* extension, |
| 71 extensions::UninstallReason reason) OVERRIDE; | 78 extensions::UninstallReason reason) OVERRIDE; |
| 72 | 79 |
| 80 // apps::AppLifetimeMonitor::Observer implementation. |
| 81 virtual void OnAppStop(Profile* profile, const std::string& app_id) OVERRIDE; |
| 82 virtual void OnChromeTerminating() OVERRIDE; |
| 83 |
| 73 void Init(); | 84 void Init(); |
| 74 void InitEphemeralAppCount(); | 85 void InitEphemeralAppCount(); |
| 75 | 86 |
| 87 void DisableEphemeralApp(const std::string& app_id); |
| 88 void DisableEphemeralAppsOnStartup(); |
| 89 |
| 90 void HandleEphemeralAppPromoted(const extensions::Extension* app); |
| 91 |
| 76 // Garbage collect ephemeral apps. | 92 // Garbage collect ephemeral apps. |
| 77 void TriggerGarbageCollect(const base::TimeDelta& delay); | 93 void TriggerGarbageCollect(const base::TimeDelta& delay); |
| 78 void GarbageCollectApps(); | 94 void GarbageCollectApps(); |
| 79 static void GetAppsToRemove(int app_count, | 95 static void GetAppsToRemove(int app_count, |
| 80 const LaunchTimeAppMap& app_launch_times, | 96 const LaunchTimeAppMap& app_launch_times, |
| 81 std::set<std::string>* remove_app_ids); | 97 std::set<std::string>* remove_app_ids); |
| 82 | 98 |
| 83 Profile* profile_; | 99 Profile* profile_; |
| 84 | 100 |
| 85 content::NotificationRegistrar registrar_; | 101 content::NotificationRegistrar registrar_; |
| 86 ScopedObserver<extensions::ExtensionRegistry, | 102 ScopedObserver<extensions::ExtensionRegistry, |
| 87 extensions::ExtensionRegistryObserver> | 103 extensions::ExtensionRegistryObserver> |
| 88 extension_registry_observer_; | 104 extension_registry_observer_; |
| 105 ScopedObserver<apps::AppLifetimeMonitor, apps::AppLifetimeMonitor::Observer> |
| 106 app_lifetime_monitor_observer_; |
| 89 | 107 |
| 90 base::OneShotTimer<EphemeralAppService> garbage_collect_apps_timer_; | 108 base::OneShotTimer<EphemeralAppService> garbage_collect_apps_timer_; |
| 91 | 109 |
| 92 // The count of cached ephemeral apps. | 110 // The count of cached ephemeral apps. |
| 93 int ephemeral_app_count_; | 111 int ephemeral_app_count_; |
| 94 | 112 |
| 113 // Number of seconds before disabling idle ephemeral apps. |
| 114 // Overridden in tests. |
| 115 int disable_idle_app_delay_; |
| 116 |
| 117 base::WeakPtrFactory<EphemeralAppService> weak_ptr_factory_; |
| 118 |
| 95 friend class EphemeralAppServiceTest; | 119 friend class EphemeralAppServiceTest; |
| 96 friend class EphemeralAppServiceBrowserTest; | 120 friend class EphemeralAppServiceBrowserTest; |
| 97 | 121 |
| 98 DISALLOW_COPY_AND_ASSIGN(EphemeralAppService); | 122 DISALLOW_COPY_AND_ASSIGN(EphemeralAppService); |
| 99 }; | 123 }; |
| 100 | 124 |
| 101 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_ | 125 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_ |
| OLD | NEW |