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

Unified Diff: chrome/browser/extensions/extension_garbage_collector.cc

Issue 9817018: Cleaning Up Extensions When Local Content Removed (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Pulled garbage collection into its own class. Created 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/extension_garbage_collector.cc
diff --git a/chrome/browser/extensions/extension_garbage_collector.cc b/chrome/browser/extensions/extension_garbage_collector.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ed767d32d3fa4ab049e801e3934ffbb31672414d
--- /dev/null
+++ b/chrome/browser/extensions/extension_garbage_collector.cc
@@ -0,0 +1,153 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/extension_garbage_collector.h"
+
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/string_util.h"
+#include "chrome/browser/extensions/extension_prefs.h"
+#include "chrome/browser/extensions/extension_service.h"
+#include "content/public/browser/browser_thread.h"
+
+ExtensionGarbageCollector::ExtensionGarbageCollector(
+ ExtensionService* extension_service)
+ : extension_service_(extension_service) {
+}
+
+ExtensionGarbageCollector::~ExtensionGarbageCollector() {
+}
+
+void ExtensionGarbageCollector::GarbageCollectExtensions() {
+ CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ ExtensionPrefs* extension_prefs = extension_service_->extension_prefs();
+
+ if (extension_prefs->pref_service()->ReadOnly())
+ return;
+
+ scoped_ptr<ExtensionPrefs::ExtensionsInfo> info(
+ extension_prefs->GetInstalledExtensionsInfo());
+
+ std::map<std::string, FilePath> extension_paths;
+ for (size_t i = 0; i < info->size(); ++i)
+ extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path;
+
+ if (!content::BrowserThread::PostTask(
+ content::BrowserThread::FILE, FROM_HERE,
+ base::Bind(
+ &ExtensionGarbageCollector::CheckExtensionDirectories,
+ this,
+ extension_paths)))
+ NOTREACHED();
+}
+
+void ExtensionGarbageCollector::CheckExtensionDirectories(
+ const std::map<std::string, FilePath>& extension_paths) {
+ DVLOG(1) << "Garbage collecting extensions...";
Aaron Boodman 2012/03/31 23:33:33 Add a CHECK that this is on the file thread.
Devlin 2012/04/05 01:58:01 Done.
+
+ CheckInstalledDirectories(extension_paths);
+
+ // Find any extensions for which the directory is nonexistent; add these to a
+ // list of extensions to be uninstalled.
+ std::vector<std::string> bad_extensions;
+ for (std::map<std::string, FilePath>::const_iterator iter =
+ extension_paths.begin(); iter != extension_paths.end(); ++iter) {
+ if (!file_util::PathExists(iter->second)) {
+ bad_extensions.push_back(iter->first);
+ }
+ }
+
+ if (bad_extensions.size() && !content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(
+ &ExtensionGarbageCollector::RemoveExtensionsMissingLocalContent,
+ this,
+ bad_extensions)))
+ NOTREACHED();
+}
+
+void ExtensionGarbageCollector::CheckInstalledDirectories(
+ const std::map<std::string, FilePath>& extension_paths) {
+ FilePath install_directory = extension_service_->install_directory();
Aaron Boodman 2012/03/31 23:33:33 Add a CHECK that on the file thread.
Aaron Boodman 2012/03/31 23:33:33 Not safe to access extension_service on a backgrou
Devlin 2012/04/05 01:58:01 Done.
Devlin 2012/04/05 01:58:01 Done.
+
+ if (!file_util::DirectoryExists(install_directory))
+ return;
+
+ file_util::FileEnumerator enumerator(install_directory,
+ false, // Not recursive.
+ file_util::FileEnumerator::DIRECTORIES);
+
+ FilePath extension_path;
+ for (extension_path = enumerator.Next(); !extension_path.value().empty();
+ extension_path = enumerator.Next()) {
+ std::string extension_id;
+ FilePath basename = extension_path.BaseName();
+ if (IsStringASCII(basename.value())) {
+ extension_id = UTF16ToASCII(basename.LossyDisplayName());
+ if (!Extension::IdIsValid(extension_id))
+ extension_id.clear();
+ }
+
+ // Delete directories that aren't valid IDs.
+ if (extension_id.empty()) {
+ DLOG(WARNING) << "Invalid extension ID enecountered in extensions "
+ "directory: " << basename.value();
+ DVLOG(1) << "Deleting invalid extension directory "
+ << extension_path.value() << ".";
+ file_util::Delete(extension_path, true); // Recursive.
+ continue;
+ }
+
+ std::map<std::string, FilePath>::const_iterator iter =
+ extension_paths.find(extension_id);
+
+ // If there is no entry in the prefs file, just delete the directory and
+ // move on. This can legitimately happen when an uninstall does not
+ // complete, for example, when a plugin is in use at uninstall time.
+ if (iter == extension_paths.end()) {
+ DVLOG(1) << "Deleting unreferenced install for directory "
+ << extension_path.LossyDisplayName() << ".";
+ file_util::Delete(extension_path, true); // Recursive.
+ continue;
+ }
+
+ // Clean up old version directories.
+ file_util::FileEnumerator versions_enumerator(
+ extension_path,
+ false, // Not recursive.
+ file_util::FileEnumerator::DIRECTORIES);
+ for (FilePath version_dir = versions_enumerator.Next();
+ !version_dir.value().empty();
+ version_dir = versions_enumerator.Next()) {
+ if (version_dir.BaseName() != iter->second.BaseName()) {
+ DVLOG(1) << "Deleting old version for directory "
+ << version_dir.LossyDisplayName() << ".";
+ file_util::Delete(version_dir, true); // Recursive.
+ }
+ }
+ }
+}
+
+void ExtensionGarbageCollector::RemoveExtensionsMissingLocalContent(
+ const std::vector<std::string>& bad_extensions) {
+ for (std::vector<std::string>::const_iterator iter = bad_extensions.begin();
Aaron Boodman 2012/03/31 23:33:33 Add a CHECK for UI thread?
Devlin 2012/04/05 01:58:01 Done.
+ iter != bad_extensions.end(); ++iter) {
+ DVLOG(1) << "Could not access local content for extension with id " <<
+ *iter << "; uninstalling extension.";
+
+ // If the extension failed to load fully (e.g. the user deleted an unpacked
+ // extension's manifest or the manifest points to the wrong path), we cannot
+ // use UninstallExtension, which relies on a valid Extension object.
+ if (!extension_service_->GetInstalledExtension(*iter)) {
+ scoped_ptr<ExtensionInfo> info(extension_service_->extension_prefs()
+ ->GetInstalledExtensionInfo(*iter));
+ extension_service_->UnloadExtension(
Aaron Boodman 2012/03/31 23:39:50 In that case, it seems like we wouldn't need to se
Devlin 2012/04/05 01:58:01 Done.
+ *iter, extension_misc::UNLOAD_REASON_UNINSTALL);
+ extension_service_->extension_prefs()->OnExtensionUninstalled(
+ *iter, info->extension_location, false);
+ } else {
+ extension_service_->UninstallExtension(*iter, false, NULL);
+ }
+ }
+}
« 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