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/browser_context_keyed_service/browser_context_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 BrowserContextKeyedService
,
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.
34 static const int kAppInactiveThreshold
;
35 static const int kAppKeepThreshold
;
36 static const int kMaxEphemeralAppsCount
;
39 // A map used to order the ephemeral apps by their last launch time.
40 typedef std::multimap
<base::Time
, std::string
> LaunchTimeAppMap
;
42 // content::NotificationObserver implementation.
43 virtual void Observe(int type
,
44 const content::NotificationSource
& source
,
45 const content::NotificationDetails
& details
) OVERRIDE
;
48 void InitEphemeralAppCount();
50 // Garbage collect ephemeral apps.
51 void TriggerGarbageCollect(const base::TimeDelta
& delay
);
52 void GarbageCollectApps();
53 static void GetAppsToRemove(int app_count
,
54 const LaunchTimeAppMap
& app_launch_times
,
55 std::set
<std::string
>* remove_app_ids
);
59 content::NotificationRegistrar registrar_
;
61 base::OneShotTimer
<EphemeralAppService
> garbage_collect_timer_
;
63 // The count of cached ephemeral apps.
64 int ephemeral_app_count_
;
66 friend class EphemeralAppServiceTest
;
67 friend class EphemeralAppServiceBrowserTest
;
69 DISALLOW_COPY_AND_ASSIGN(EphemeralAppService
);
72 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_