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

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: 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 ExtensionSystem* extension_system = ExtensionSystem::Get(context_);
107 DCHECK(extension_system);
108
109 extension_system->ready().PostDelayed(
110 FROM_HERE,
111 base::Bind(&ExtensionGarbageCollector::GarbageCollectExtensions,
112 weak_factory_.GetWeakPtr()),
113 base::TimeDelta::FromSeconds(kGarbageCollectStartupDelay));
114
115 extension_system->ready().Post(
116 FROM_HERE,
117 base::Bind(
118 &ExtensionGarbageCollector::GarbageCollectIsolatedStorageIfNeeded,
119 weak_factory_.GetWeakPtr()));
120 }
121
122 ExtensionGarbageCollector::~ExtensionGarbageCollector() {}
123
124 void ExtensionGarbageCollector::GarbageCollectExtensionsForTest() {
125 GarbageCollectExtensions();
126 }
127
128 void ExtensionGarbageCollector::GarbageCollectExtensions() {
129 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
130
131 #if defined(OS_CHROMEOS)
132 if (disable_garbage_collection_)
Yoyo Zhou 2014/03/26 01:54:12 This should be initialized to false.
Devlin 2014/03/26 23:56:32 Done.
133 return;
134 #endif
135
136 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
137 DCHECK(extension_prefs);
138
139 if (extension_prefs->pref_service()->ReadOnly())
140 return;
141
142 bool clean_temp_dir = true;
143
144 if (extension_service_->pending_extension_manager()->HasPendingExtensions()) {
145 // Don't garbage collect temp dir while there are pending installations,
146 // which may be using the temporary installation directory. Try to garbage
147 // collect again later.
148 clean_temp_dir = false;
149 base::MessageLoop::current()->PostDelayedTask(
150 FROM_HERE,
151 base::Bind(&ExtensionGarbageCollector::GarbageCollectExtensions,
152 weak_factory_.GetWeakPtr()),
153 base::TimeDelta::FromSeconds(kGarbageCollectRetryDelayInSeconds));
154 }
155
156 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
157 extension_prefs->GetInstalledExtensionsInfo());
158 std::multimap<std::string, base::FilePath> extension_paths;
159 for (size_t i = 0; i < info->size(); ++i) {
160 extension_paths.insert(
161 std::make_pair(info->at(i)->extension_id, info->at(i)->extension_path));
162 }
163
164 info = extension_prefs->GetAllDelayedInstallInfo();
165 for (size_t i = 0; i < info->size(); ++i) {
166 extension_paths.insert(
167 std::make_pair(info->at(i)->extension_id, info->at(i)->extension_path));
168 }
169
170 if (!extension_service_->GetFileTaskRunner()->PostTask(
171 FROM_HERE,
172 base::Bind(
173 &ExtensionGarbageCollector::GarbageCollectExtensionsOnFileThread,
174 weak_factory_.GetWeakPtr(),
175 extension_paths,
176 clean_temp_dir))) {
177 NOTREACHED();
178 }
179 }
180
181 void ExtensionGarbageCollector::GarbageCollectIsolatedStorageIfNeeded() {
182 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
183 DCHECK(extension_prefs);
184 if (!extension_prefs->NeedsStorageGarbageCollection())
185 return;
186 extension_prefs->SetNeedsStorageGarbageCollection(false);
187
188 scoped_ptr<base::hash_set<base::FilePath> > active_paths(
189 new base::hash_set<base::FilePath>());
190 const ExtensionSet& extensions =
191 ExtensionRegistry::Get(context_)->enabled_extensions();
192 for (ExtensionSet::const_iterator iter = extensions.begin();
193 iter != extensions.end();
194 ++iter) {
195 if (AppIsolationInfo::HasIsolatedStorage(iter->get())) {
196 active_paths->insert(
197 content::BrowserContext::GetStoragePartitionForSite(
198 context_, util::GetSiteForExtensionId((*iter)->id(), context_))
199 ->GetPath());
200 }
201 }
202
203 // The data of ephemeral apps can outlive their cache lifetime. Ensure
204 // they are not garbage collected.
205 scoped_ptr<ExtensionPrefs::ExtensionsInfo> evicted_apps_info(
206 extension_prefs->GetEvictedEphemeralAppsInfo());
207 for (size_t i = 0; i < evicted_apps_info->size(); ++i) {
208 ExtensionInfo* info = evicted_apps_info->at(i).get();
209 if (util::HasIsolatedStorage(*info)) {
210 active_paths->insert(content::BrowserContext::GetStoragePartitionForSite(
211 context_,
212 util::GetSiteForExtensionId(
213 info->extension_id, context_))->GetPath());
214 }
215 }
216
217 extension_service_->OnGarbageCollectIsolatedStorageStart();
218 content::BrowserContext::GarbageCollectStoragePartitions(
219 context_,
220 active_paths.Pass(),
221 base::Bind(&ExtensionService::OnGarbageCollectIsolatedStorageFinished,
222 extension_service_->AsWeakPtr()));
223 }
224
225 void ExtensionGarbageCollector::GarbageCollectExtensionsOnFileThread(
226 const ExtensionPathsMultimap& extension_paths,
227 bool clean_temp_dir) {
228 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE));
229
230 // Nothing to clean up if it doesn't exist.
231 if (!base::DirectoryExists(install_directory_))
232 return;
233
234 base::FileEnumerator enumerator(install_directory_,
235 false, // Not recursive.
236 base::FileEnumerator::DIRECTORIES);
237
238 for (base::FilePath extension_path = enumerator.Next();
239 !extension_path.empty();
240 extension_path = enumerator.Next()) {
241 CheckExtensionDirectory(extension_path, extension_paths, clean_temp_dir);
242 }
243 }
244
245 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698