Home | History | Annotate | Download | only in apps
      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 "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"
     17 
     18 class Profile;
     19 
     20 namespace extensions {
     21 class Extension;
     22 class ExtensionRegistry;
     23 }  // namespace extensions
     24 
     25 // Performs the background garbage collection of ephemeral apps.
     26 class EphemeralAppService : public KeyedService,
     27                             public content::NotificationObserver,
     28                             public extensions::ExtensionRegistryObserver {
     29  public:
     30   // Returns the instance for the given profile. This is a convenience wrapper
     31   // around EphemeralAppServiceFactory::GetForProfile.
     32   static EphemeralAppService* Get(Profile* profile);
     33 
     34   explicit EphemeralAppService(Profile* profile);
     35   virtual ~EphemeralAppService();
     36 
     37   int ephemeral_app_count() const { return ephemeral_app_count_; }
     38 
     39   // Constants exposed for testing purposes:
     40 
     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;
     48 
     49  private:
     50   // A map used to order the ephemeral apps by their last launch time.
     51   typedef std::multimap<base::Time, std::string> LaunchTimeAppMap;
     52 
     53   // content::NotificationObserver implementation.
     54   virtual void Observe(int type,
     55                        const content::NotificationSource& source,
     56                        const content::NotificationDetails& details) OVERRIDE;
     57 
     58   // extensions::ExtensionRegistryObserver.
     59   virtual void OnExtensionWillBeInstalled(
     60       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   virtual void OnExtensionUninstalled(
     66       content::BrowserContext* browser_context,
     67       const extensions::Extension* extension) OVERRIDE;
     68 
     69   void Init();
     70   void InitEphemeralAppCount();
     71 
     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);
     78 
     79   Profile* profile_;
     80 
     81   content::NotificationRegistrar registrar_;
     82   ScopedObserver<extensions::ExtensionRegistry,
     83                  extensions::ExtensionRegistryObserver>
     84       extension_registry_observer_;
     85 
     86   base::OneShotTimer<EphemeralAppService> garbage_collect_apps_timer_;
     87 
     88   // The count of cached ephemeral apps.
     89   int ephemeral_app_count_;
     90 
     91   friend class EphemeralAppServiceTest;
     92   friend class EphemeralAppServiceBrowserTest;
     93 
     94   DISALLOW_COPY_AND_ASSIGN(EphemeralAppService);
     95 };
     96 
     97 #endif  // CHROME_BROWSER_APPS_EPHEMERAL_APP_SERVICE_H_
     98