Home | History | Annotate | Download | only in extensions
      1 // Copyright 2014 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_EXTENSIONS_EXTENSION_GARBAGE_COLLECTOR_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_GARBAGE_COLLECTOR_H_
      7 
      8 #include <map>
      9 #include <string>
     10 
     11 #include "base/files/file_path.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "chrome/browser/extensions/install_observer.h"
     14 #include "components/keyed_service/core/keyed_service.h"
     15 
     16 namespace content {
     17 class BrowserContext;
     18 }
     19 
     20 namespace extensions {
     21 
     22 // The class responsible for cleaning up the cruft left behind on the file
     23 // system by uninstalled (or failed install) extensions.
     24 // The class is owned by ExtensionService, but is mostly independent. Tasks to
     25 // garbage collect extensions and isolated storage are posted once the
     26 // ExtensionSystem signals ready.
     27 class ExtensionGarbageCollector : public KeyedService, public InstallObserver {
     28  public:
     29   explicit ExtensionGarbageCollector(content::BrowserContext* context);
     30   virtual ~ExtensionGarbageCollector();
     31 
     32   static ExtensionGarbageCollector* Get(content::BrowserContext* context);
     33 
     34   // Manually trigger GarbageCollectExtensions() for testing.
     35   void GarbageCollectExtensionsForTest();
     36 
     37   // Overriddes for KeyedService:
     38   virtual void Shutdown() OVERRIDE;
     39 
     40   // Overriddes for InstallObserver
     41   virtual void OnBeginCrxInstall(const std::string& extension_id) OVERRIDE;
     42   virtual void OnFinishCrxInstall(const std::string& extension_id,
     43                                   bool success) OVERRIDE;
     44 
     45  protected:
     46   // Cleans up the extension install directory. It can end up with garbage in it
     47   // if extensions can't initially be removed when they are uninstalled (eg if a
     48   // file is in use).
     49   // Obsolete version directories are removed, as are directories that aren't
     50   // found in the ExtensionPrefs.
     51   // The "Temp" directory that is used during extension installation will get
     52   // removed iff there are no pending installations.
     53   virtual void GarbageCollectExtensions();
     54 
     55   // Garbage collects apps/extensions isolated storage if it is uninstalled.
     56   // There is an exception for ephemeral apps because they can outlive their
     57   // cache lifetimes.
     58   void GarbageCollectIsolatedStorageIfNeeded();
     59 
     60   static void GarbageCollectExtensionsOnFileThread(
     61       const base::FilePath& install_directory,
     62       const std::multimap<std::string, base::FilePath>& extension_paths);
     63 
     64   // The BrowserContext associated with the GarbageCollector.
     65   content::BrowserContext* context_;
     66 
     67   // The number of currently ongoing CRX installations. This is used to prevent
     68   // garbage collection from running while a CRX is being installed.
     69   int crx_installs_in_progress_;
     70 
     71   // Generate weak pointers for safely posting to the file thread for garbage
     72   // collection.
     73   base::WeakPtrFactory<ExtensionGarbageCollector> weak_factory_;
     74 
     75   DISALLOW_COPY_AND_ASSIGN(ExtensionGarbageCollector);
     76 };
     77 
     78 }  // namespace extensions
     79 
     80 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_GARBAGE_COLLECTOR_H_
     81