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;
50 // A map used to order the ephemeral apps by their last launch time.
51 typedef std::multimap<base::Time, std::string> LaunchTimeAppMap;
53 // content::NotificationObserver implementation.
54 virtual void Observe(int type,
55 const content::NotificationSource& source,
56 const content::NotificationDetails& details) OVERRIDE;
58 // extensions::ExtensionRegistryObserver.
59 virtual void OnExtensionWillBeInstalled(
60 content::BrowserContext* browser_context,
61 const extensions::Extension* extension,
64 const std::string& old_name) OVERRIDE;
65 virtual void OnExtensionUninstalled(
66 content::BrowserContext* browser_context,
67 const extensions::Extension* extension) OVERRIDE;
70 void InitEphemeralAppCount();
72 // Garbage collect ephemeral apps.
73 void TriggerGarbageCollect(const base::TimeDelta& delay);
74 void GarbageCollectApps();
75 static void GetAppsToRemove(int app_count,
76 const LaunchTimeAppMap& app_launch_times,
77 std::set<std::string>* remove_app_ids);
81 content::NotificationRegistrar registrar_;
82 ScopedObserver<extensions::ExtensionRegistry,
83 extensions::ExtensionRegistryObserver>
84 extension_registry_observer_;
86 base::OneShotTimer<EphemeralAppService> garbage_collect_apps_timer_;
88 // The count of cached ephemeral apps.
89 int ephemeral_app_count_;
91 friend class EphemeralAppServiceTest;
92 friend class EphemeralAppServiceBrowserTest;
94 DISALLOW_COPY_AND_ASSIGN(EphemeralAppService);
97 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_