OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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 <map> | |
8 #include <string> | |
9 | |
10 #include "base/file_util.h" | |
11 #include "base/logging.h" | |
12 #include "chrome/browser/extensions/extension_prefs.h" | |
13 #include "chrome/browser/extensions/extension_service.h" | |
14 #include "chrome/common/extensions/extension_file_util.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 | |
17 ExtensionGarbageCollector::ExtensionGarbageCollector( | |
18 ExtensionService* extension_service) | |
19 : extension_service_(extension_service) { | |
20 } | |
21 | |
22 ExtensionGarbageCollector::~ExtensionGarbageCollector() { | |
23 } | |
24 | |
25 void ExtensionGarbageCollector::GarbageCollectExtensions() { | |
26 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
27 | |
28 FilePath install_directory = extension_service_->install_directory(); | |
29 | |
30 // NOTE: It's important to use the file thread here because that is the thread | |
31 // ExtensionService uses, and there are ordering dependencies between all of | |
32 // the extension-file-management tasks. | |
33 content::BrowserThread::PostTask( | |
34 content::BrowserThread::FILE, FROM_HERE, | |
35 base::Bind( | |
36 &ExtensionGarbageCollector::CheckExtensionDirectories, | |
37 this, | |
38 install_directory)); | |
39 } | |
40 | |
41 void ExtensionGarbageCollector::CheckExtensionDirectories( | |
42 const FilePath& install_directory) { | |
43 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
44 | |
45 DVLOG(1) << "Garbage collecting extensions..."; | |
46 | |
47 if (!file_util::DirectoryExists(install_directory)) | |
48 return; | |
49 | |
50 std::set<FilePath> extension_paths; | |
51 | |
52 file_util::FileEnumerator enumerator(install_directory, | |
53 false, // Not recursive. | |
54 file_util::FileEnumerator::DIRECTORIES); | |
55 | |
56 for (FilePath extension_path = enumerator.Next(); !extension_path.empty(); | |
57 extension_path = enumerator.Next()) { | |
58 std::string extension_id = extension_path.BaseName().MaybeAsASCII(); | |
59 if (!Extension::IdIsValid(extension_id)) | |
60 extension_id.clear(); | |
61 | |
62 // Delete directories that aren't valid IDs. | |
63 if (extension_id.empty()) { | |
64 DLOG(WARNING) << "Invalid extension ID encountered in extensions " | |
65 "directory: " << extension_path.BaseName().value(); | |
66 DVLOG(1) << "Deleting invalid extension directory " | |
67 << extension_path.value() << "."; | |
68 file_util::Delete(extension_path, true); // Recursive. | |
69 continue; | |
70 } | |
71 | |
72 extension_paths.insert(extension_path); | |
73 } | |
74 | |
75 content::BrowserThread::PostTask( | |
76 content::BrowserThread::UI, FROM_HERE, | |
77 base::Bind( | |
78 &ExtensionGarbageCollector::CheckExtensionPreferences, | |
79 this, | |
80 extension_paths)); | |
81 } | |
82 | |
83 void ExtensionGarbageCollector::CleanupOldVersions( | |
Aaron Boodman
2012/04/07 18:01:03
Nit: can you move the definition of this method do
Devlin
2012/04/14 18:13:21
Done.
| |
84 const FilePath& path, | |
85 const FilePath& current_version) { | |
86 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
87 file_util::FileEnumerator versions_enumerator( | |
88 path, | |
89 false, // Not recursive. | |
90 file_util::FileEnumerator::DIRECTORIES); | |
91 for (FilePath version_dir = versions_enumerator.Next(); | |
92 !version_dir.value().empty(); | |
93 version_dir = versions_enumerator.Next()) { | |
94 if (version_dir.BaseName() != current_version) { | |
95 DVLOG(1) << "Deleting old version for directory " | |
96 << version_dir.LossyDisplayName() << "."; | |
97 if (!file_util::Delete(version_dir, true)) // Recursive. | |
98 NOTREACHED(); | |
99 } | |
100 } | |
101 } | |
102 | |
103 void ExtensionGarbageCollector::CheckExtensionPreferences( | |
104 const std::set<FilePath>& extension_paths) { | |
105 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
106 | |
107 ExtensionPrefs* extension_prefs = extension_service_->extension_prefs(); | |
108 | |
109 if (extension_prefs->pref_service()->ReadOnly()) | |
110 return; | |
111 | |
112 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( | |
113 extension_prefs->GetInstalledExtensionsInfo()); | |
114 | |
115 std::map<std::string, FilePath> extension_pref_paths; | |
116 for (size_t i = 0; i < info->size(); ++i) { | |
117 extension_pref_paths[info->at(i)->extension_id] = | |
118 info->at(i)->extension_path; | |
119 } | |
120 | |
121 std::set<std::string> extension_ids; | |
122 | |
123 // Check each directory for a reference in the preferences. | |
124 for (std::set<FilePath>::const_iterator path = extension_paths.begin(); | |
125 path != extension_paths.end(); | |
126 ++path) { | |
127 std::string extension_id; | |
128 extension_id = path->BaseName().MaybeAsASCII(); | |
129 if (extension_id.empty()) | |
130 continue; | |
131 | |
132 extension_ids.insert(extension_id); | |
133 | |
134 std::map<std::string, FilePath>::const_iterator iter = | |
135 extension_pref_paths.find(extension_id); | |
136 | |
137 if (iter == extension_pref_paths.end()) { | |
138 DVLOG(1) << "Deleting unreferenced install for directory " | |
139 << path->LossyDisplayName() << "."; | |
140 content::BrowserThread::PostTask( | |
141 content::BrowserThread::FILE, FROM_HERE, | |
142 base::Bind( | |
143 &extension_file_util::DeleteFile, | |
144 *path, | |
145 true)); // recursive. | |
146 continue; | |
147 } | |
148 | |
149 // Clean up old version directories. | |
150 content::BrowserThread::PostTask( | |
151 content::BrowserThread::FILE, FROM_HERE, | |
152 base::Bind( | |
153 &ExtensionGarbageCollector::CleanupOldVersions, | |
154 this, *path, iter->second.BaseName())); | |
155 } | |
156 | |
157 // Check each entry in the preferences for an existing path. | |
158 for (std::map<std::string, FilePath>::const_iterator extension = | |
159 extension_pref_paths.begin(); | |
160 extension != extension_pref_paths.end(); | |
161 ++extension) { | |
162 std::set<std::string>::const_iterator iter = extension_ids.find( | |
163 extension->first); | |
164 | |
165 if (iter != extension_ids.end()) | |
166 continue; | |
167 | |
168 std::string extension_id = extension->first; | |
169 | |
170 DVLOG(1) << "Could not access local content for extension with id " | |
171 << extension_id << "; uninstalling extension."; | |
172 | |
173 // If the extension failed to load fully (e.g. the user deleted an unpacked | |
174 // extension's manifest or the manifest points to the wrong path), we cannot | |
175 // use UninstallExtension, which relies on a valid Extension object. | |
176 if (!extension_service_->GetInstalledExtension(extension_id)) { | |
177 scoped_ptr<ExtensionInfo> info(extension_service_->extension_prefs() | |
178 ->GetInstalledExtensionInfo(extension_id)); | |
179 extension_service_->extension_prefs()->OnExtensionUninstalled( | |
180 extension_id, info->extension_location, false); | |
181 } else { | |
182 extension_service_->UninstallExtension(extension_id, false, NULL); | |
183 } | |
184 } | |
185 } | |
OLD | NEW |