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/timer/timer.h"
12 #include "components/keyed_service/core/keyed_service.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
18 namespace extensions
{
20 } // namespace extensions
22 // Performs the background garbage collection of ephemeral apps.
23 class EphemeralAppService
: public KeyedService
,
24 public content::NotificationObserver
{
26 // Returns the instance for the given profile. This is a convenience wrapper
27 // around EphemeralAppServiceFactory::GetForProfile.
28 static EphemeralAppService
* Get(Profile
* profile
);
30 explicit EphemeralAppService(Profile
* profile
);
31 virtual ~EphemeralAppService();
33 // Constants exposed for testing purposes:
35 // The number of days of inactivity before an ephemeral app will be removed.
36 static const int kAppInactiveThreshold
;
37 // If the ephemeral app has been launched within this number of days, it will
38 // definitely not be garbage collected.
39 static const int kAppKeepThreshold
;
40 // The maximum number of ephemeral apps to keep cached. Excess may be removed.
41 static const int kMaxEphemeralAppsCount
;
42 // The number of days of inactivity before the data of an already evicted
43 // ephemeral app will be removed.
44 static const int kDataInactiveThreshold
;
47 // A map used to order the ephemeral apps by their last launch time.
48 typedef std::multimap
<base::Time
, std::string
> LaunchTimeAppMap
;
50 // content::NotificationObserver implementation.
51 virtual void Observe(int type
,
52 const content::NotificationSource
& source
,
53 const content::NotificationDetails
& details
) OVERRIDE
;
56 void InitEphemeralAppCount();
58 // Garbage collect ephemeral apps.
59 void TriggerGarbageCollect(const base::TimeDelta
& delay
);
60 void GarbageCollectApps();
61 static void GetAppsToRemove(int app_count
,
62 const LaunchTimeAppMap
& app_launch_times
,
63 std::set
<std::string
>* remove_app_ids
);
65 // Garbage collect the data of ephemeral apps that have been evicted and
66 // inactive for a long period of time.
67 void GarbageCollectData();
71 content::NotificationRegistrar registrar_
;
73 base::OneShotTimer
<EphemeralAppService
> garbage_collect_apps_timer_
;
74 base::OneShotTimer
<EphemeralAppService
> garbage_collect_data_timer_
;
76 // The count of cached ephemeral apps.
77 int ephemeral_app_count_
;
79 friend class EphemeralAppBrowserTest
;
80 friend class EphemeralAppServiceTest
;
81 friend class EphemeralAppServiceBrowserTest
;
83 DISALLOW_COPY_AND_ASSIGN(EphemeralAppService
);
86 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_