| 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 #include "chrome/browser/extensions/extension_garbage_collector.h" | 5 #include "chrome/browser/extensions/extension_garbage_collector.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 if (version_dir.BaseName() == iter->second.BaseName()) { | 89 if (version_dir.BaseName() == iter->second.BaseName()) { |
| 90 known_version = true; | 90 known_version = true; |
| 91 break; | 91 break; |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 if (!known_version) | 94 if (!known_version) |
| 95 base::DeleteFile(version_dir, true); // Recursive. | 95 base::DeleteFile(version_dir, true); // Recursive. |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 void GarbageCollectExtensionsOnFileThread( | |
| 100 const base::FilePath& install_directory, | |
| 101 const ExtensionPathsMultimap& extension_paths) { | |
| 102 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 103 | |
| 104 // Nothing to clean up if it doesn't exist. | |
| 105 if (!base::DirectoryExists(install_directory)) | |
| 106 return; | |
| 107 | |
| 108 base::FileEnumerator enumerator(install_directory, | |
| 109 false, // Not recursive. | |
| 110 base::FileEnumerator::DIRECTORIES); | |
| 111 | |
| 112 for (base::FilePath extension_path = enumerator.Next(); | |
| 113 !extension_path.empty(); | |
| 114 extension_path = enumerator.Next()) { | |
| 115 CheckExtensionDirectory(extension_path, extension_paths); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 } // namespace | 99 } // namespace |
| 120 | 100 |
| 121 ExtensionGarbageCollector::ExtensionGarbageCollector( | 101 ExtensionGarbageCollector::ExtensionGarbageCollector( |
| 122 content::BrowserContext* context) | 102 content::BrowserContext* context) |
| 123 : context_(context), crx_installs_in_progress_(0), weak_factory_(this) { | 103 : context_(context), crx_installs_in_progress_(0), weak_factory_(this) { |
| 124 #if defined(OS_CHROMEOS) | |
| 125 disable_garbage_collection_ = false; | |
| 126 #endif | |
| 127 | 104 |
| 128 ExtensionSystem* extension_system = ExtensionSystem::Get(context_); | 105 ExtensionSystem* extension_system = ExtensionSystem::Get(context_); |
| 129 DCHECK(extension_system); | 106 DCHECK(extension_system); |
| 130 | 107 |
| 131 extension_system->ready().PostDelayed( | 108 extension_system->ready().PostDelayed( |
| 132 FROM_HERE, | 109 FROM_HERE, |
| 133 base::Bind(&ExtensionGarbageCollector::GarbageCollectExtensions, | 110 base::Bind(&ExtensionGarbageCollector::GarbageCollectExtensions, |
| 134 weak_factory_.GetWeakPtr()), | 111 weak_factory_.GetWeakPtr()), |
| 135 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay)); | 112 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay)); |
| 136 | 113 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 152 } | 129 } |
| 153 | 130 |
| 154 void ExtensionGarbageCollector::Shutdown() { | 131 void ExtensionGarbageCollector::Shutdown() { |
| 155 InstallTracker::Get(context_)->RemoveObserver(this); | 132 InstallTracker::Get(context_)->RemoveObserver(this); |
| 156 } | 133 } |
| 157 | 134 |
| 158 void ExtensionGarbageCollector::GarbageCollectExtensionsForTest() { | 135 void ExtensionGarbageCollector::GarbageCollectExtensionsForTest() { |
| 159 GarbageCollectExtensions(); | 136 GarbageCollectExtensions(); |
| 160 } | 137 } |
| 161 | 138 |
| 139 // static |
| 140 void ExtensionGarbageCollector::GarbageCollectExtensionsOnFileThread( |
| 141 const base::FilePath& install_directory, |
| 142 const ExtensionPathsMultimap& extension_paths) { |
| 143 DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 144 |
| 145 // Nothing to clean up if it doesn't exist. |
| 146 if (!base::DirectoryExists(install_directory)) |
| 147 return; |
| 148 |
| 149 base::FileEnumerator enumerator(install_directory, |
| 150 false, // Not recursive. |
| 151 base::FileEnumerator::DIRECTORIES); |
| 152 |
| 153 for (base::FilePath extension_path = enumerator.Next(); |
| 154 !extension_path.empty(); |
| 155 extension_path = enumerator.Next()) { |
| 156 CheckExtensionDirectory(extension_path, extension_paths); |
| 157 } |
| 158 } |
| 159 |
| 162 void ExtensionGarbageCollector::GarbageCollectExtensions() { | 160 void ExtensionGarbageCollector::GarbageCollectExtensions() { |
| 163 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 161 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 164 | 162 |
| 165 #if defined(OS_CHROMEOS) | |
| 166 if (disable_garbage_collection_) | |
| 167 return; | |
| 168 #endif | |
| 169 | |
| 170 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_); | 163 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_); |
| 171 DCHECK(extension_prefs); | 164 DCHECK(extension_prefs); |
| 172 | 165 |
| 173 if (extension_prefs->pref_service()->ReadOnly()) | 166 if (extension_prefs->pref_service()->ReadOnly()) |
| 174 return; | 167 return; |
| 175 | 168 |
| 176 if (crx_installs_in_progress_ > 0) { | 169 if (crx_installs_in_progress_ > 0) { |
| 177 // Don't garbage collect while there are installations in progress, | 170 // Don't garbage collect while there are installations in progress, |
| 178 // which may be using the temporary installation directory. Try to garbage | 171 // which may be using the temporary installation directory. Try to garbage |
| 179 // collect again later. | 172 // collect again later. |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 // accounting. | 265 // accounting. |
| 273 NOTREACHED(); | 266 NOTREACHED(); |
| 274 | 267 |
| 275 // Don't let the count go negative to avoid garbage collecting when | 268 // Don't let the count go negative to avoid garbage collecting when |
| 276 // an install is actually in progress. | 269 // an install is actually in progress. |
| 277 crx_installs_in_progress_ = 0; | 270 crx_installs_in_progress_ = 0; |
| 278 } | 271 } |
| 279 } | 272 } |
| 280 | 273 |
| 281 } // namespace extensions | 274 } // namespace extensions |
| OLD | NEW |