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 int ephemeral_app_count() const { return ephemeral_app_count_
; }
39 // Constants exposed for testing purposes:
41 // The number of days of inactivity before an ephemeral app will be removed.
42 static const int kAppInactiveThreshold
;
43 // If the ephemeral app has been launched within this number of days, it will
44 // definitely not be garbage collected.
45 static const int kAppKeepThreshold
;
46 // The maximum number of ephemeral apps to keep cached. Excess may be removed.
47 static const int kMaxEphemeralAppsCount
;
48 // The number of days of inactivity before the data of an already evicted
49 // ephemeral app will be removed.
50 static const int kDataInactiveThreshold
;
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
) OVERRIDE
;
73 void InitEphemeralAppCount();
75 // Garbage collect ephemeral apps.
76 void TriggerGarbageCollect(const base::TimeDelta
& delay
);
77 void GarbageCollectApps();
78 static void GetAppsToRemove(int app_count
,
79 const LaunchTimeAppMap
& app_launch_times
,
80 std::set
<std::string
>* remove_app_ids
);
82 // Garbage collect the data of ephemeral apps that have been evicted and
83 // inactive for a long period of time.
84 void GarbageCollectData();
88 content::NotificationRegistrar registrar_
;
90 ScopedObserver
<extensions::ExtensionRegistry
,
91 extensions::ExtensionRegistryObserver
>
92 extension_registry_observer_
;
94 base::OneShotTimer
<EphemeralAppService
> garbage_collect_apps_timer_
;
95 base::OneShotTimer
<EphemeralAppService
> garbage_collect_data_timer_
;
97 // The count of cached ephemeral apps.
98 int ephemeral_app_count_
;
100 friend class EphemeralAppBrowserTest
;
101 friend class EphemeralAppServiceTest
;
102 friend class EphemeralAppServiceBrowserTest
;
104 DISALLOW_COPY_AND_ASSIGN(EphemeralAppService
);
107 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_