| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_GARBAGE_COLLECTOR_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_GARBAGE_COLLECTOR_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_GARBAGE_COLLECTOR_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_GARBAGE_COLLECTOR_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "chrome/browser/extensions/install_gate.h" |
| 14 #include "chrome/browser/extensions/install_observer.h" | 15 #include "chrome/browser/extensions/install_observer.h" |
| 15 #include "components/keyed_service/core/keyed_service.h" | 16 #include "components/keyed_service/core/keyed_service.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 class BrowserContext; | 19 class BrowserContext; |
| 19 } | 20 } |
| 20 | 21 |
| 21 namespace extensions { | 22 namespace extensions { |
| 22 | 23 |
| 23 // The class responsible for cleaning up the cruft left behind on the file | 24 // The class responsible for cleaning up the cruft left behind on the file |
| 24 // system by uninstalled (or failed install) extensions. | 25 // system by uninstalled (or failed install) extensions. |
| 25 // The class is owned by ExtensionService, but is mostly independent. Tasks to | 26 // The class is owned by ExtensionService, but is mostly independent. Tasks to |
| 26 // garbage collect extensions and isolated storage are posted once the | 27 // garbage collect extensions and isolated storage are posted once the |
| 27 // ExtensionSystem signals ready. | 28 // ExtensionSystem signals ready. |
| 28 class ExtensionGarbageCollector : public KeyedService, public InstallObserver { | 29 class ExtensionGarbageCollector : public KeyedService, |
| 30 public InstallObserver, |
| 31 public InstallGate { |
| 29 public: | 32 public: |
| 30 explicit ExtensionGarbageCollector(content::BrowserContext* context); | 33 explicit ExtensionGarbageCollector(content::BrowserContext* context); |
| 31 ~ExtensionGarbageCollector() override; | 34 ~ExtensionGarbageCollector() override; |
| 32 | 35 |
| 33 static ExtensionGarbageCollector* Get(content::BrowserContext* context); | 36 static ExtensionGarbageCollector* Get(content::BrowserContext* context); |
| 34 | 37 |
| 35 // Manually trigger GarbageCollectExtensions() for testing. | 38 // Manually trigger GarbageCollectExtensions() for testing. |
| 36 void GarbageCollectExtensionsForTest(); | 39 void GarbageCollectExtensionsForTest(); |
| 37 | 40 |
| 38 // Overriddes for KeyedService: | 41 // KeyedService: |
| 39 void Shutdown() override; | 42 void Shutdown() override; |
| 40 | 43 |
| 41 // Overriddes for InstallObserver | 44 // InstallObserver: |
| 42 void OnBeginCrxInstall(const std::string& extension_id) override; | 45 void OnBeginCrxInstall(const std::string& extension_id) override; |
| 43 void OnFinishCrxInstall(const std::string& extension_id, | 46 void OnFinishCrxInstall(const std::string& extension_id, |
| 44 bool success) override; | 47 bool success) override; |
| 45 | 48 |
| 49 // InstallGate: |
| 50 Action ShouldDelay(const Extension* extension, |
| 51 bool install_immediately) override; |
| 52 |
| 46 protected: | 53 protected: |
| 47 // Cleans up the extension install directory. It can end up with garbage in it | 54 // Cleans up the extension install directory. It can end up with garbage in it |
| 48 // if extensions can't initially be removed when they are uninstalled (eg if a | 55 // if extensions can't initially be removed when they are uninstalled (eg if a |
| 49 // file is in use). | 56 // file is in use). |
| 50 // Obsolete version directories are removed, as are directories that aren't | 57 // Obsolete version directories are removed, as are directories that aren't |
| 51 // found in the ExtensionPrefs. | 58 // found in the ExtensionPrefs. |
| 52 // The "Temp" directory that is used during extension installation will get | 59 // The "Temp" directory that is used during extension installation will get |
| 53 // removed iff there are no pending installations. | 60 // removed iff there are no pending installations. |
| 54 virtual void GarbageCollectExtensions(); | 61 virtual void GarbageCollectExtensions(); |
| 55 | 62 |
| 56 // Garbage collects apps/extensions isolated storage if it is uninstalled. | 63 // Garbage collects apps/extensions isolated storage if it is uninstalled. |
| 57 // There is an exception for ephemeral apps because they can outlive their | 64 // There is an exception for ephemeral apps because they can outlive their |
| 58 // cache lifetimes. | 65 // cache lifetimes. |
| 59 void GarbageCollectIsolatedStorageIfNeeded(); | 66 void GarbageCollectIsolatedStorageIfNeeded(); |
| 60 | 67 |
| 68 // Restart any extension installs which were delayed for isolated storage |
| 69 // garbage collection. |
| 70 void OnGarbageCollectIsolatedStorageFinished(); |
| 71 |
| 61 static void GarbageCollectExtensionsOnFileThread( | 72 static void GarbageCollectExtensionsOnFileThread( |
| 62 const base::FilePath& install_directory, | 73 const base::FilePath& install_directory, |
| 63 const std::multimap<std::string, base::FilePath>& extension_paths); | 74 const std::multimap<std::string, base::FilePath>& extension_paths); |
| 64 | 75 |
| 65 // The BrowserContext associated with the GarbageCollector. | 76 // The BrowserContext associated with the GarbageCollector. |
| 66 content::BrowserContext* context_; | 77 content::BrowserContext* context_; |
| 67 | 78 |
| 68 // The number of currently ongoing CRX installations. This is used to prevent | 79 // The number of currently ongoing CRX installations. This is used to prevent |
| 69 // garbage collection from running while a CRX is being installed. | 80 // garbage collection from running while a CRX is being installed. |
| 70 int crx_installs_in_progress_; | 81 int crx_installs_in_progress_; |
| 71 | 82 |
| 83 // Set to true to delay all new extension installations. Acts as a lock to |
| 84 // allow background processing of garbage collection of on-disk state without |
| 85 // needing to worry about race conditions caused by extension installation and |
| 86 // reinstallation. |
| 87 bool installs_delayed_for_gc_ = false; |
| 88 |
| 72 // Generate weak pointers for safely posting to the file thread for garbage | 89 // Generate weak pointers for safely posting to the file thread for garbage |
| 73 // collection. | 90 // collection. |
| 74 base::WeakPtrFactory<ExtensionGarbageCollector> weak_factory_; | 91 base::WeakPtrFactory<ExtensionGarbageCollector> weak_factory_; |
| 75 | 92 |
| 76 DISALLOW_COPY_AND_ASSIGN(ExtensionGarbageCollector); | 93 DISALLOW_COPY_AND_ASSIGN(ExtensionGarbageCollector); |
| 77 }; | 94 }; |
| 78 | 95 |
| 79 } // namespace extensions | 96 } // namespace extensions |
| 80 | 97 |
| 81 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_GARBAGE_COLLECTOR_H_ | 98 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_GARBAGE_COLLECTOR_H_ |
| OLD | NEW |