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
|
// 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 "base/bind.h"
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.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/extension_util.h"
#include "chrome/browser/extensions/install_tracker.h"
#include "chrome/browser/extensions/pending_extension_manager.h"
#include "chrome/common/extensions/manifest_handlers/app_isolation_info.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_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "extensions/common/file_util.h"
namespace extensions {
namespace {
// Wait this many seconds before trying to garbage collect extensions again.
const int kGarbageCollectRetryDelayInSeconds = 30;
// Wait this many seconds after startup to see if there are any extensions
// which can be garbage collected.
const int kGarbageCollectStartupDelay = 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 (!Extension::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 (Iter 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.
}
}
void 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);
}
}
} // namespace
ExtensionGarbageCollector::ExtensionGarbageCollector(
content::BrowserContext* context)
: context_(context), crx_installs_in_progress_(0), weak_factory_(this) {
#if defined(OS_CHROMEOS)
disable_garbage_collection_ = false;
#endif
ExtensionSystem* extension_system = ExtensionSystem::Get(context_);
DCHECK(extension_system);
extension_system->ready().PostDelayed(
FROM_HERE,
base::Bind(&ExtensionGarbageCollector::GarbageCollectExtensions,
weak_factory_.GetWeakPtr()),
base::TimeDelta::FromSeconds(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();
}
void ExtensionGarbageCollector::GarbageCollectExtensions() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
#if defined(OS_CHROMEOS)
if (disable_garbage_collection_)
return;
#endif
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::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&ExtensionGarbageCollector::GarbageCollectExtensions,
weak_factory_.GetWeakPtr()),
base::TimeDelta::FromSeconds(kGarbageCollectRetryDelayInSeconds));
return;
}
scoped_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 (!service->GetFileTaskRunner()->PostTask(
FROM_HERE,
base::Bind(&GarbageCollectExtensionsOnFileThread,
service->install_directory(),
extension_paths))) {
NOTREACHED();
}
}
void ExtensionGarbageCollector::GarbageCollectIsolatedStorageIfNeeded() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
DCHECK(extension_prefs);
if (!extension_prefs->NeedsStorageGarbageCollection())
return;
extension_prefs->SetNeedsStorageGarbageCollection(false);
scoped_ptr<base::hash_set<base::FilePath> > active_paths(
new base::hash_set<base::FilePath>());
scoped_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());
}
}
// The data of ephemeral apps can outlive their cache lifetime. Ensure
// they are not garbage collected.
scoped_ptr<ExtensionPrefs::ExtensionsInfo> evicted_apps_info(
extension_prefs->GetEvictedEphemeralAppsInfo());
for (size_t i = 0; i < evicted_apps_info->size(); ++i) {
ExtensionInfo* info = evicted_apps_info->at(i).get();
if (util::HasIsolatedStorage(*info)) {
active_paths->insert(content::BrowserContext::GetStoragePartitionForSite(
context_,
util::GetSiteForExtensionId(
info->extension_id, context_))->GetPath());
}
}
ExtensionService* service =
ExtensionSystem::Get(context_)->extension_service();
service->OnGarbageCollectIsolatedStorageStart();
content::BrowserContext::GarbageCollectStoragePartitions(
context_,
active_paths.Pass(),
base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
service->AsWeakPtr()));
}
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;
}
}
} // namespace extensions
|