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