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_unload_delay_for_test(int delay) { unload_idle_app_delay_ = delay; } |
| 46 |
42 // Constants exposed for testing purposes: | 47 // Constants exposed for testing purposes: |
43 | 48 |
44 // The number of days of inactivity before an ephemeral app will be removed. | 49 // The number of days of inactivity before an ephemeral app will be removed. |
45 static const int kAppInactiveThreshold; | 50 static const int kAppInactiveThreshold; |
46 // If the ephemeral app has been launched within this number of days, it will | 51 // If the ephemeral app has been launched within this number of days, it will |
47 // definitely not be garbage collected. | 52 // definitely not be garbage collected. |
48 static const int kAppKeepThreshold; | 53 static const int kAppKeepThreshold; |
49 // The maximum number of ephemeral apps to keep cached. Excess may be removed. | 54 // The maximum number of ephemeral apps to keep cached. Excess may be removed. |
50 static const int kMaxEphemeralAppsCount; | 55 static const int kMaxEphemeralAppsCount; |
51 | 56 |
(...skipping 11 matching lines...) Expand all Loading... |
63 content::BrowserContext* browser_context, | 68 content::BrowserContext* browser_context, |
64 const extensions::Extension* extension, | 69 const extensions::Extension* extension, |
65 bool is_update, | 70 bool is_update, |
66 bool from_ephemeral, | 71 bool from_ephemeral, |
67 const std::string& old_name) OVERRIDE; | 72 const std::string& old_name) OVERRIDE; |
68 virtual void OnExtensionUninstalled( | 73 virtual void OnExtensionUninstalled( |
69 content::BrowserContext* browser_context, | 74 content::BrowserContext* browser_context, |
70 const extensions::Extension* extension, | 75 const extensions::Extension* extension, |
71 extensions::UninstallReason reason) OVERRIDE; | 76 extensions::UninstallReason reason) OVERRIDE; |
72 | 77 |
| 78 // apps::AppLifetimeMonitor::Observer implementation. |
| 79 virtual void OnAppStop(Profile* profile, const std::string& app_id) OVERRIDE; |
| 80 virtual void OnChromeTerminating() OVERRIDE; |
| 81 |
73 void Init(); | 82 void Init(); |
74 void InitEphemeralAppCount(); | 83 void InitEphemeralAppCount(); |
75 | 84 |
| 85 void UnloadEphemeralApp(const std::string& app_id); |
| 86 void UnloadEphemeralAppsOnStartup(); |
| 87 |
| 88 void HandleEphemeralAppPromoted(const extensions::Extension* app); |
| 89 |
76 // Garbage collect ephemeral apps. | 90 // Garbage collect ephemeral apps. |
77 void TriggerGarbageCollect(const base::TimeDelta& delay); | 91 void TriggerGarbageCollect(const base::TimeDelta& delay); |
78 void GarbageCollectApps(); | 92 void GarbageCollectApps(); |
79 static void GetAppsToRemove(int app_count, | 93 static void GetAppsToRemove(int app_count, |
80 const LaunchTimeAppMap& app_launch_times, | 94 const LaunchTimeAppMap& app_launch_times, |
81 std::set<std::string>* remove_app_ids); | 95 std::set<std::string>* remove_app_ids); |
82 | 96 |
83 Profile* profile_; | 97 Profile* profile_; |
84 | 98 |
85 content::NotificationRegistrar registrar_; | 99 content::NotificationRegistrar registrar_; |
86 ScopedObserver<extensions::ExtensionRegistry, | 100 ScopedObserver<extensions::ExtensionRegistry, |
87 extensions::ExtensionRegistryObserver> | 101 extensions::ExtensionRegistryObserver> |
88 extension_registry_observer_; | 102 extension_registry_observer_; |
89 | 103 |
90 base::OneShotTimer<EphemeralAppService> garbage_collect_apps_timer_; | 104 base::OneShotTimer<EphemeralAppService> garbage_collect_apps_timer_; |
91 | 105 |
92 // The count of cached ephemeral apps. | 106 // The count of cached ephemeral apps. |
93 int ephemeral_app_count_; | 107 int ephemeral_app_count_; |
94 | 108 |
| 109 // Number of seconds before unloading idle ephemeral apps. |
| 110 // Overridden in tests. |
| 111 int unload_idle_app_delay_; |
| 112 |
| 113 base::WeakPtrFactory<EphemeralAppService> weak_ptr_factory_; |
| 114 |
95 friend class EphemeralAppServiceTest; | 115 friend class EphemeralAppServiceTest; |
96 friend class EphemeralAppServiceBrowserTest; | 116 friend class EphemeralAppServiceBrowserTest; |
97 | 117 |
98 DISALLOW_COPY_AND_ASSIGN(EphemeralAppService); | 118 DISALLOW_COPY_AND_ASSIGN(EphemeralAppService); |
99 }; | 119 }; |
100 | 120 |
101 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_ | 121 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_ |
OLD | NEW |