Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/browser/extensions/extension_garbage_collector.cc

Issue 204983020: Remove ExtensionService Garbage-Collecting methods. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove lazy global empty delta Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 #include "chrome/browser/extensions/extension_garbage_collector.h"
6
7 #include "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/files/file_enumerator.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/extensions/extension_service.h"
18 #include "chrome/browser/extensions/extension_util.h"
19 #include "chrome/browser/extensions/pending_extension_manager.h"
20 #include "chrome/common/extensions/extension_file_util.h"
21 #include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
22 #include "content/public/browser/browser_context.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/storage_partition.h"
25 #include "extensions/browser/extension_prefs.h"
26 #include "extensions/browser/extension_registry.h"
27 #include "extensions/browser/extension_system.h"
28 #include "extensions/common/extension.h"
29
30 namespace extensions {
31
32 namespace {
33
34 // Wait this many seconds before trying to garbage collect extensions again.
35 const int kGarbageCollectRetryDelayInSeconds = 30;
36
37 // Wait this many seconds after startup to see if there are any extensions
38 // which can be garbage collected.
39 const int kGarbageCollectStartupDelay = 30;
40
41 typedef std::multimap<std::string, base::FilePath> ExtensionPathsMultimap;
42
43 void CheckExtensionDirectory(const base::FilePath& path,
44 const ExtensionPathsMultimap& extension_paths,
45 bool clean_temp_dir) {
46 base::FilePath basename = path.BaseName();
47 // Clean up temporary files left if Chrome crashed or quit in the middle
48 // of an extension install.
49 if (basename.value() == extension_file_util::kTempDirectoryName) {
50 if (clean_temp_dir)
51 base::DeleteFile(path, true); // Recursive.
52 return;
53 }
54
55 // Parse directory name as a potential extension ID.
56 std::string extension_id;
57 if (IsStringASCII(basename.value())) {
58 extension_id = base::UTF16ToASCII(basename.LossyDisplayName());
59 if (!Extension::IdIsValid(extension_id))
60 extension_id.clear();
61 }
62
63 // Delete directories that aren't valid IDs.
64 if (extension_id.empty()) {
65 base::DeleteFile(path, true); // Recursive.
66 return;
67 }
68
69 typedef ExtensionPathsMultimap::const_iterator Iter;
70 std::pair<Iter, Iter> iter_pair = extension_paths.equal_range(extension_id);
71
72 // If there is no entry in the prefs file, just delete the directory and
73 // move on. This can legitimately happen when an uninstall does not
74 // complete, for example, when a plugin is in use at uninstall time.
75 if (iter_pair.first == iter_pair.second) {
76 base::DeleteFile(path, true); // Recursive.
77 return;
78 }
79
80 // Clean up old version directories.
81 base::FileEnumerator versions_enumerator(
82 path, false /* Not recursive */, base::FileEnumerator::DIRECTORIES);
83 for (base::FilePath version_dir = versions_enumerator.Next();
84 !version_dir.empty();
85 version_dir = versions_enumerator.Next()) {
86 bool known_version = false;
87 for (Iter iter = iter_pair.first; iter != iter_pair.second; ++iter) {
88 if (version_dir.BaseName() == iter->second.BaseName()) {
89 known_version = true;
90 break;
91 }
92 }
93 if (!known_version)
94 base::DeleteFile(version_dir, true); // Recursive.
95 }
96 }
97
98 } // namespace
99
100 ExtensionGarbageCollector::ExtensionGarbageCollector(
101 ExtensionService* extension_service)
102 : extension_service_(extension_service),
103 context_(extension_service->GetBrowserContext()),
104 install_directory_(extension_service->install_directory()),
105 weak_factory_(this) {
106 #if defined(OS_CHROMEOS)
107 disable_garbage_collection_ = false;
108 #endif
109
110 ExtensionSystem* extension_system = ExtensionSystem::Get(context_);
111 DCHECK(extension_system);
112
113 extension_system->ready().PostDelayed(
114 FROM_HERE,
115 base::Bind(&ExtensionGarbageCollector::GarbageCollectExtensions,
116 weak_factory_.GetWeakPtr()),
117 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
118
119 extension_system->ready().Post(
120 FROM_HERE,
121 base::Bind(
122 &ExtensionGarbageCollector::GarbageCollectIsolatedStorageIfNeeded,
123 weak_factory_.GetWeakPtr()));
124 }
125
126 ExtensionGarbageCollector::~ExtensionGarbageCollector() {}
127
128 void ExtensionGarbageCollector::GarbageCollectExtensionsForTest() {
129 GarbageCollectExtensions();
130 }
131
132 void ExtensionGarbageCollector::GarbageCollectExtensions() {
133 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
134
135 #if defined(OS_CHROMEOS)
136 if (disable_garbage_collection_)
137 return;
138 #endif
139
140 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
141 DCHECK(extension_prefs);
142
143 if (extension_prefs->pref_service()->ReadOnly())
144 return;
145
146 bool clean_temp_dir = true;
147
148 if (extension_service_->pending_extension_manager()->HasPendingExtensions()) {
149 // Don't garbage collect temp dir while there are pending installations,
150 // which may be using the temporary installation directory. Try to garbage
151 // collect again later.
152 clean_temp_dir = false;
153 base::MessageLoop::current()->PostDelayedTask(
154 FROM_HERE,
155 base::Bind(&ExtensionGarbageCollector::GarbageCollectExtensions,
156 weak_factory_.GetWeakPtr()),
157 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelayInSeconds));
158 }
159
160 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
161 extension_prefs->GetInstalledExtensionsInfo());
162 std::multimap<std::string, base::FilePath> extension_paths;
163 for (size_t i = 0; i < info->size(); ++i) {
164 extension_paths.insert(
165 std::make_pair(info->at(i)->extension_id, info->at(i)->extension_path));
166 }
167
168 info = extension_prefs->GetAllDelayedInstallInfo();
169 for (size_t i = 0; i < info->size(); ++i) {
170 extension_paths.insert(
171 std::make_pair(info->at(i)->extension_id, info->at(i)->extension_path));
172 }
173
174 if (!extension_service_->GetFileTaskRunner()->PostTask(
175 FROM_HERE,
176 base::Bind(
177 &ExtensionGarbageCollector::GarbageCollectExtensionsOnFileThread,
178 weak_factory_.GetWeakPtr(),
179 extension_paths,
180 clean_temp_dir))) {
181 NOTREACHED();
182 }
183 }
184
185 void ExtensionGarbageCollector::GarbageCollectIsolatedStorageIfNeeded() {
186 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
187 DCHECK(extension_prefs);
188 if (!extension_prefs->NeedsStorageGarbageCollection())
189 return;
190 extension_prefs->SetNeedsStorageGarbageCollection(false);
191
192 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
193 new base::hash_set<base::FilePath>());
194 const ExtensionSet& extensions =
195 ExtensionRegistry::Get(context_)->enabled_extensions();
196 for (ExtensionSet::const_iterator iter = extensions.begin();
197 iter != extensions.end();
198 ++iter) {
199 if (AppIsolationInfo::HasIsolatedStorage(iter->get())) {
200 active_paths->insert(
201 content::BrowserContext::GetStoragePartitionForSite(
202 context_, util::GetSiteForExtensionId((*iter)->id(), context_))
203 ->GetPath());
204 }
205 }
206
207 // The data of ephemeral apps can outlive their cache lifetime. Ensure
208 // they are not garbage collected.
209 scoped_ptr<ExtensionPrefs::ExtensionsInfo> evicted_apps_info(
210 extension_prefs->GetEvictedEphemeralAppsInfo());
211 for (size_t i = 0; i < evicted_apps_info->size(); ++i) {
212 ExtensionInfo* info = evicted_apps_info->at(i).get();
213 if (util::HasIsolatedStorage(*info)) {
214 active_paths->insert(content::BrowserContext::GetStoragePartitionForSite(
215 context_,
216 util::GetSiteForExtensionId(
217 info->extension_id, context_))->GetPath());
218 }
219 }
220
221 extension_service_->OnGarbageCollectIsolatedStorageStart();
222 content::BrowserContext::GarbageCollectStoragePartitions(
223 context_,
224 active_paths.Pass(),
225 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
226 extension_service_->AsWeakPtr()));
227 }
228
229 void ExtensionGarbageCollector::GarbageCollectExtensionsOnFileThread(
230 const ExtensionPathsMultimap& extension_paths,
231 bool clean_temp_dir) {
232 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
233
234 // Nothing to clean up if it doesn't exist.
235 if (!base::DirectoryExists(install_directory_))
236 return;
237
238 base::FileEnumerator enumerator(install_directory_,
239 false, // Not recursive.
240 base::FileEnumerator::DIRECTORIES);
241
242 for (base::FilePath extension_path = enumerator.Next();
243 !extension_path.empty();
244 extension_path = enumerator.Next()) {
245 CheckExtensionDirectory(extension_path, extension_paths, clean_temp_dir);
246 }
247 }
248
249 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698