Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / apps / ephemeral_app_service.h
blobcf8c879c8c81a5c6ed54f91963acf8d3e967595f
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 <map>
9 #include <set>
11 #include "apps/app_lifetime_monitor.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/scoped_observer.h"
14 #include "base/timer/timer.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "extensions/browser/extension_registry_observer.h"
20 class Profile;
22 namespace extensions {
23 class Extension;
24 class ExtensionRegistry;
25 } // namespace extensions
27 // Performs the background garbage collection of ephemeral apps.
28 class EphemeralAppService : public KeyedService,
29 public content::NotificationObserver,
30 public extensions::ExtensionRegistryObserver,
31 public apps::AppLifetimeMonitor::Observer {
32 public:
33 // Returns the instance for the given profile. This is a convenience wrapper
34 // around EphemeralAppServiceFactory::GetForProfile.
35 static EphemeralAppService* Get(Profile* profile);
37 explicit EphemeralAppService(Profile* profile);
38 ~EphemeralAppService() override;
40 // Clears the ephemeral app cache. Removes all idle ephemeral apps.
41 void ClearCachedApps();
43 int ephemeral_app_count() const { return ephemeral_app_count_; }
45 void set_disable_delay_for_test(int delay) {
46 disable_idle_app_delay_ = delay;
49 // Constants exposed for testing purposes:
51 // The number of days of inactivity before an ephemeral app will be removed.
52 static const int kAppInactiveThreshold;
53 // If the ephemeral app has been launched within this number of days, it will
54 // definitely not be garbage collected.
55 static const int kAppKeepThreshold;
56 // The maximum number of ephemeral apps to keep cached. Excess may be removed.
57 static const int kMaxEphemeralAppsCount;
59 private:
60 // A map used to order the ephemeral apps by their last launch time.
61 typedef std::multimap<base::Time, std::string> LaunchTimeAppMap;
63 // content::NotificationObserver implementation.
64 void Observe(int type,
65 const content::NotificationSource& source,
66 const content::NotificationDetails& details) override;
68 // extensions::ExtensionRegistryObserver.
69 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
70 const extensions::Extension* extension,
71 bool is_update,
72 bool from_ephemeral,
73 const std::string& old_name) override;
74 void OnExtensionUninstalled(content::BrowserContext* browser_context,
75 const extensions::Extension* extension,
76 extensions::UninstallReason reason) override;
78 // apps::AppLifetimeMonitor::Observer implementation.
79 void OnAppStop(Profile* profile, const std::string& app_id) override;
80 void OnChromeTerminating() override;
82 void Init();
83 void InitEphemeralAppCount();
85 void DisableEphemeralApp(const std::string& app_id);
86 void DisableEphemeralAppsOnStartup();
88 void HandleEphemeralAppPromoted(const extensions::Extension* app);
90 // Garbage collect ephemeral apps.
91 void TriggerGarbageCollect(const base::TimeDelta& delay);
92 void GarbageCollectApps();
93 static void GetAppsToRemove(int app_count,
94 const LaunchTimeAppMap& app_launch_times,
95 std::set<std::string>* remove_app_ids);
97 Profile* profile_;
99 content::NotificationRegistrar registrar_;
100 ScopedObserver<extensions::ExtensionRegistry,
101 extensions::ExtensionRegistryObserver>
102 extension_registry_observer_;
103 ScopedObserver<apps::AppLifetimeMonitor, apps::AppLifetimeMonitor::Observer>
104 app_lifetime_monitor_observer_;
106 base::OneShotTimer<EphemeralAppService> garbage_collect_apps_timer_;
108 // The count of cached ephemeral apps.
109 int ephemeral_app_count_;
111 // Number of seconds before disabling idle ephemeral apps.
112 // Overridden in tests.
113 int disable_idle_app_delay_;
115 base::WeakPtrFactory<EphemeralAppService> weak_ptr_factory_;
117 friend class EphemeralAppServiceTest;
118 friend class EphemeralAppServiceBrowserTest;
120 DISALLOW_COPY_AND_ASSIGN(EphemeralAppService);
123 #endif // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_