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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_
6 #define CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_
11 #include "base/scoped_observer.h"
12 #include "base/timer/timer.h"
13 #include "components/keyed_service/core/keyed_service.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "extensions/browser/extension_registry_observer.h"
20 namespace extensions
{
22 class ExtensionRegistry
;
23 } // namespace extensions
25 // Performs the background garbage collection of ephemeral apps.
26 class EphemeralAppService
: public KeyedService
,
27 public content::NotificationObserver
,
28 public extensions::ExtensionRegistryObserver
{
30 // Returns the instance for the given profile. This is a convenience wrapper
31 // around EphemeralAppServiceFactory::GetForProfile.
32 static EphemeralAppService
* Get(Profile
* profile
);
34 explicit EphemeralAppService(Profile
* profile
);
35 virtual ~EphemeralAppService();
37 // Clears the ephemeral app cache. Removes all idle ephemeral apps.
38 void ClearCachedApps();
40 int ephemeral_app_count() const { return ephemeral_app_count_
; }
42 // Constants exposed for testing purposes:
44 // The number of days of inactivity before an ephemeral app will be removed.
45 static const int kAppInactiveThreshold
;
46 // If the ephemeral app has been launched within this number of days, it will
47 // definitely not be garbage collected.
48 static const int kAppKeepThreshold
;
49 // The maximum number of ephemeral apps to keep cached. Excess may be removed.
50 static const int kMaxEphemeralAppsCount
;
53 // A map used to order the ephemeral apps by their last launch time.
54 typedef std::multimap
<base::Time
, std::string
> LaunchTimeAppMap
;
56 // content::NotificationObserver implementation.
57 virtual void Observe(int type
,
58 const content::NotificationSource
& source
,
59 const content::NotificationDetails
& details
) OVERRIDE
;
61 // extensions::ExtensionRegistryObserver.
62 virtual void OnExtensionWillBeInstalled(
63 content::BrowserContext
* browser_context
,
64 const extensions::Extension
* extension
,
67 const std::string
& old_name
) OVERRIDE
;
68 virtual void OnExtensionUninstalled(
69 content::BrowserContext
* browser_context
,
70 const extensions::Extension
* extension
,
71 extensions::UninstallReason reason
) OVERRIDE
;
74 void InitEphemeralAppCount();
76 // Garbage collect ephemeral apps.
77 void TriggerGarbageCollect(const base::TimeDelta
& delay
);
78 void GarbageCollectApps();
79 static void GetAppsToRemove(int app_count
,
80 const LaunchTimeAppMap
& app_launch_times
,
81 std::set
<std::string
>* remove_app_ids
);
85 content::NotificationRegistrar registrar_
;
86 ScopedObserver
<extensions::ExtensionRegistry
,
87 extensions::ExtensionRegistryObserver
>
88 extension_registry_observer_
;
90 base::OneShotTimer
<EphemeralAppService
> garbage_collect_apps_timer_
;
92 // The count of cached ephemeral apps.
93 int ephemeral_app_count_
;
95 friend class EphemeralAppServiceTest
;
96 friend class EphemeralAppServiceBrowserTest
;
98 DISALLOW_COPY_AND_ASSIGN(EphemeralAppService
);
101 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_