1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/extension_garbage_collector.h"
#include <stddef.h>
#include <memory>
#include <unordered_set>
#include <utility>
#include "base/bind.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "chrome/browser/extensions/extension_garbage_collector_factory.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/install_tracker.h"
#include "chrome/browser/extensions/pending_extension_manager.h"
#include "components/crx_file/id_util.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/browser/extension_file_task_runner.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extension_util.h"
#include "extensions/common/extension.h"
#include "extensions/common/file_util.h"
#include "extensions/common/manifest_handlers/app_isolation_info.h"
#include "extensions/common/one_shot_event.h"
namespace extensions {
namespace {
// Wait this long before trying to garbage collect extensions again.
constexpr base::TimeDelta kGarbageCollectRetryDelay =
base::TimeDelta::FromSeconds(30);
// Wait this long after startup to see if there are any extensions which can be
// garbage collected.
constexpr base::TimeDelta kGarbageCollectStartupDelay =
base::TimeDelta::FromSeconds(30);
typedef std::multimap<std::string, base::FilePath> ExtensionPathsMultimap;
void CheckExtensionDirectory(const base::FilePath& path,
const ExtensionPathsMultimap& extension_paths) {
base::FilePath basename = path.BaseName();
// Clean up temporary files left if Chrome crashed or quit in the middle
// of an extension install.
if (basename.value() == file_util::kTempDirectoryName) {
base::DeleteFile(path, true); // Recursive.
return;
}
// Parse directory name as a potential extension ID.
std::string extension_id;
if (base::IsStringASCII(basename.value())) {
extension_id = base::UTF16ToASCII(basename.LossyDisplayName());
if (!crx_file::id_util::IdIsValid(extension_id))
extension_id.clear();
}
// Delete directories that aren't valid IDs.
if (extension_id.empty()) {
base::DeleteFile(path, true); // Recursive.
return;
}
typedef ExtensionPathsMultimap::const_iterator Iter;
std::pair<Iter, Iter> iter_pair = extension_paths.equal_range(extension_id);
// If there is no entry in the prefs file, just delete the directory and
// move on. This can legitimately happen when an uninstall does not
// complete, for example, when a plugin is in use at uninstall time.
if (iter_pair.first == iter_pair.second) {
base::DeleteFile(path, true); // Recursive.
return;
}
// Clean up old version directories.
base::FileEnumerator versions_enumerator(
path, false /* Not recursive */, base::FileEnumerator::DIRECTORIES);
for (base::FilePath version_dir = versions_enumerator.Next();
!version_dir.empty();
version_dir = versions_enumerator.Next()) {
bool known_version = false;
for (auto iter = iter_pair.first; iter != iter_pair.second; ++iter) {
if (version_dir.BaseName() == iter->second.BaseName()) {
known_version = true;
break;
}
}
if (!known_version)
base::DeleteFile(version_dir, true); // Recursive.
}
}
} // namespace
ExtensionGarbageCollector::ExtensionGarbageCollector(
content::BrowserContext* context)
: context_(context), crx_installs_in_progress_(0), weak_factory_(this) {
ExtensionSystem* extension_system = ExtensionSystem::Get(context_);
DCHECK(extension_system);
extension_system->ready().PostDelayed(
FROM_HERE,
base::Bind(&ExtensionGarbageCollector::GarbageCollectExtensions,
weak_factory_.GetWeakPtr()),
kGarbageCollectStartupDelay);
extension_system->ready().Post(
FROM_HERE,
base::Bind(
&ExtensionGarbageCollector::GarbageCollectIsolatedStorageIfNeeded,
weak_factory_.GetWeakPtr()));
InstallTracker::Get(context_)->AddObserver(this);
}
ExtensionGarbageCollector::~ExtensionGarbageCollector() {}
// static
ExtensionGarbageCollector* ExtensionGarbageCollector::Get(
content::BrowserContext* context) {
return ExtensionGarbageCollectorFactory::GetForBrowserContext(context);
}
void ExtensionGarbageCollector::Shutdown() {
InstallTracker::Get(context_)->RemoveObserver(this);
}
void ExtensionGarbageCollector::GarbageCollectExtensionsForTest() {
GarbageCollectExtensions();
}
// static
void ExtensionGarbageCollector::GarbageCollectExtensionsOnFileThread(
const base::FilePath& install_directory,
const ExtensionPathsMultimap& extension_paths) {
DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
// Nothing to clean up if it doesn't exist.
if (!base::DirectoryExists(install_directory))
return;
base::FileEnumerator enumerator(install_directory,
false, // Not recursive.
base::FileEnumerator::DIRECTORIES);
for (base::FilePath extension_path = enumerator.Next();
!extension_path.empty();
extension_path = enumerator.Next()) {
CheckExtensionDirectory(extension_path, extension_paths);
}
}
void ExtensionGarbageCollector::GarbageCollectExtensions() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
DCHECK(extension_prefs);
if (extension_prefs->pref_service()->ReadOnly())
return;
if (crx_installs_in_progress_ > 0) {
// Don't garbage collect while there are installations in progress,
// which may be using the temporary installation directory. Try to garbage
// collect again later.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::BindOnce(&ExtensionGarbageCollector::GarbageCollectExtensions,
weak_factory_.GetWeakPtr()),
kGarbageCollectRetryDelay);
return;
}
std::unique_ptr<ExtensionPrefs::ExtensionsInfo> info(
extension_prefs->GetInstalledExtensionsInfo());
std::multimap<std::string, base::FilePath> extension_paths;
for (size_t i = 0; i < info->size(); ++i) {
extension_paths.insert(
std::make_pair(info->at(i)->extension_id, info->at(i)->extension_path));
}
info = extension_prefs->GetAllDelayedInstallInfo();
for (size_t i = 0; i < info->size(); ++i) {
extension_paths.insert(
std::make_pair(info->at(i)->extension_id, info->at(i)->extension_path));
}
ExtensionService* service =
ExtensionSystem::Get(context_)->extension_service();
if (!GetExtensionFileTaskRunner()->PostTask(
FROM_HERE,
base::BindOnce(&GarbageCollectExtensionsOnFileThread,
service->install_directory(), extension_paths))) {
NOTREACHED();
}
}
void ExtensionGarbageCollector::GarbageCollectIsolatedStorageIfNeeded() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
DCHECK(extension_prefs);
if (!extension_prefs->NeedsStorageGarbageCollection())
return;
extension_prefs->SetNeedsStorageGarbageCollection(false);
std::unique_ptr<std::unordered_set<base::FilePath>> active_paths(
new std::unordered_set<base::FilePath>());
std::unique_ptr<ExtensionSet> extensions =
ExtensionRegistry::Get(context_)->GenerateInstalledExtensionsSet();
for (ExtensionSet::const_iterator iter = extensions->begin();
iter != extensions->end();
++iter) {
if (AppIsolationInfo::HasIsolatedStorage(iter->get())) {
active_paths->insert(
content::BrowserContext::GetStoragePartitionForSite(
context_, util::GetSiteForExtensionId((*iter)->id(), context_))
->GetPath());
}
}
DCHECK(!installs_delayed_for_gc_);
installs_delayed_for_gc_ = true;
content::BrowserContext::GarbageCollectStoragePartitions(
context_, std::move(active_paths),
base::Bind(
&ExtensionGarbageCollector::OnGarbageCollectIsolatedStorageFinished,
weak_factory_.GetWeakPtr()));
}
void ExtensionGarbageCollector::OnGarbageCollectIsolatedStorageFinished() {
DCHECK(installs_delayed_for_gc_);
installs_delayed_for_gc_ = false;
ExtensionSystem::Get(context_)
->extension_service()
->MaybeFinishDelayedInstallations();
}
void ExtensionGarbageCollector::OnBeginCrxInstall(
const std::string& extension_id) {
crx_installs_in_progress_++;
}
void ExtensionGarbageCollector::OnFinishCrxInstall(
const std::string& extension_id,
bool success) {
crx_installs_in_progress_--;
if (crx_installs_in_progress_ < 0) {
// This can only happen if there is a mismatch in our begin/finish
// accounting.
NOTREACHED();
// Don't let the count go negative to avoid garbage collecting when
// an install is actually in progress.
crx_installs_in_progress_ = 0;
}
}
InstallGate::Action ExtensionGarbageCollector::ShouldDelay(
const Extension* extension,
bool install_immediately) {
return installs_delayed_for_gc_ ? DELAY : INSTALL;
}
} // namespace extensions
|