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