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