Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / apps / ephemeral_app_service.h
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.
4
5 #ifndef CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_
6 #define CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_
7
8 #include <map>
9 #include <set>
10
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"
19
20 class Profile;
21
22 namespace extensions {
23 class Extension;
24 class ExtensionRegistry;
25 }  // namespace extensions
26
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);
36
37   explicit EphemeralAppService(Profile* profile);
38   virtual ~EphemeralAppService();
39
40   // Clears the ephemeral app cache. Removes all idle ephemeral apps.
41   void ClearCachedApps();
42
43   int ephemeral_app_count() const { return ephemeral_app_count_; }
44
45   void set_disable_delay_for_test(int delay) {
46     disable_idle_app_delay_ = delay;
47   }
48
49   // Constants exposed for testing purposes:
50
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;
58
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;
62
63   // content::NotificationObserver implementation.
64   virtual void Observe(int type,
65                        const content::NotificationSource& source,
66                        const content::NotificationDetails& details) OVERRIDE;
67
68   // extensions::ExtensionRegistryObserver.
69   virtual void OnExtensionWillBeInstalled(
70       content::BrowserContext* browser_context,
71       const extensions::Extension* extension,
72       bool is_update,
73       bool from_ephemeral,
74       const std::string& old_name) OVERRIDE;
75   virtual void OnExtensionUninstalled(
76       content::BrowserContext* browser_context,
77       const extensions::Extension* extension,
78       extensions::UninstallReason reason) OVERRIDE;
79
80   // apps::AppLifetimeMonitor::Observer implementation.
81   virtual void OnAppStop(Profile* profile, const std::string& app_id) OVERRIDE;
82   virtual void OnChromeTerminating() OVERRIDE;
83
84   void Init();
85   void InitEphemeralAppCount();
86
87   void DisableEphemeralApp(const std::string& app_id);
88   void DisableEphemeralAppsOnStartup();
89
90   void HandleEphemeralAppPromoted(const extensions::Extension* app);
91
92   // Garbage collect ephemeral apps.
93   void TriggerGarbageCollect(const base::TimeDelta& delay);
94   void GarbageCollectApps();
95   static void GetAppsToRemove(int app_count,
96                               const LaunchTimeAppMap& app_launch_times,
97                               std::set<std::string>* remove_app_ids);
98
99   Profile* profile_;
100
101   content::NotificationRegistrar registrar_;
102   ScopedObserver<extensions::ExtensionRegistry,
103                  extensions::ExtensionRegistryObserver>
104       extension_registry_observer_;
105   ScopedObserver<apps::AppLifetimeMonitor, apps::AppLifetimeMonitor::Observer>
106       app_lifetime_monitor_observer_;
107
108   base::OneShotTimer<EphemeralAppService> garbage_collect_apps_timer_;
109
110   // The count of cached ephemeral apps.
111   int ephemeral_app_count_;
112
113   // Number of seconds before disabling idle ephemeral apps.
114   // Overridden in tests.
115   int disable_idle_app_delay_;
116
117   base::WeakPtrFactory<EphemeralAppService> weak_ptr_factory_;
118
119   friend class EphemeralAppServiceTest;
120   friend class EphemeralAppServiceBrowserTest;
121
122   DISALLOW_COPY_AND_ASSIGN(EphemeralAppService);
123 };
124
125 #endif  // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_