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