Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / apps / ephemeral_app_service.h
blob76a7213f39d5bf8683c9b044ca4e440eeb98fb1c
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_
8 #include <set>
10 #include "apps/app_lifetime_monitor.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/scoped_observer.h"
13 #include "base/timer/timer.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "extensions/browser/extension_registry_observer.h"
17 class Profile;
19 namespace extensions {
20 class Extension;
21 class ExtensionRegistry;
22 } // namespace extensions
24 // Performs the background garbage collection of ephemeral apps.
25 class EphemeralAppService : public KeyedService,
26 public extensions::ExtensionRegistryObserver,
27 public apps::AppLifetimeMonitor::Observer {
28 public:
29 // Returns the instance for the given profile. This is a convenience wrapper
30 // around EphemeralAppServiceFactory::GetForProfile.
31 static EphemeralAppService* Get(Profile* profile);
33 explicit EphemeralAppService(Profile* profile);
34 ~EphemeralAppService() override;
36 // Clears the ephemeral app cache. Removes all idle ephemeral apps.
37 void ClearCachedApps();
39 int ephemeral_app_count() const { return ephemeral_app_count_; }
41 void set_disable_delay_for_test(int delay) {
42 disable_idle_app_delay_ = delay;
45 // Constants exposed for testing purposes:
47 // The number of days of inactivity before an ephemeral app will be removed.
48 static const int kAppInactiveThreshold;
49 // If the ephemeral app has been launched within this number of days, it will
50 // definitely not be garbage collected.
51 static const int kAppKeepThreshold;
52 // The maximum number of ephemeral apps to keep cached. Excess may be removed.
53 static const int kMaxEphemeralAppsCount;
55 private:
56 // A map used to order the ephemeral apps by their last launch time.
57 typedef std::multimap<base::Time, std::string> LaunchTimeAppMap;
59 // extensions::ExtensionRegistryObserver.
60 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
61 const extensions::Extension* extension,
62 bool is_update,
63 bool from_ephemeral,
64 const std::string& old_name) override;
65 void OnExtensionUninstalled(content::BrowserContext* browser_context,
66 const extensions::Extension* extension,
67 extensions::UninstallReason reason) override;
69 // apps::AppLifetimeMonitor::Observer implementation.
70 void OnAppStop(Profile* profile, const std::string& app_id) override;
71 void OnChromeTerminating() override;
73 void Init();
74 void InitEphemeralAppCount();
76 void DisableEphemeralApp(const std::string& app_id);
77 void DisableEphemeralAppsOnStartup();
79 void HandleEphemeralAppPromoted(const extensions::Extension* app);
81 // Garbage collect ephemeral apps.
82 void TriggerGarbageCollect(const base::TimeDelta& delay);
83 void GarbageCollectApps();
84 static void GetAppsToRemove(int app_count,
85 const LaunchTimeAppMap& app_launch_times,
86 std::set<std::string>* remove_app_ids);
88 Profile* profile_;
90 ScopedObserver<extensions::ExtensionRegistry,
91 extensions::ExtensionRegistryObserver>
92 extension_registry_observer_;
93 ScopedObserver<apps::AppLifetimeMonitor, apps::AppLifetimeMonitor::Observer>
94 app_lifetime_monitor_observer_;
96 base::OneShotTimer<EphemeralAppService> garbage_collect_apps_timer_;
98 // The count of cached ephemeral apps.
99 int ephemeral_app_count_;
101 // Number of seconds before disabling idle ephemeral apps.
102 // Overridden in tests.
103 int disable_idle_app_delay_;
105 base::WeakPtrFactory<EphemeralAppService> weak_ptr_factory_;
107 friend class EphemeralAppServiceTest;
108 friend class EphemeralAppServiceBrowserTest;
110 DISALLOW_COPY_AND_ASSIGN(EphemeralAppService);
113 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_