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

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

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

Powered by Google App Engine
This is Rietveld 408576698