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