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 | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/string_util.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 if (!content::BrowserThread::PostTask( | |
|
Aaron Boodman
2012/04/05 21:53:14
I think the current guidance to fire-and-forget Po
Devlin
2012/04/07 17:38:21
There are a few instances where checking the value
| |
| 31 content::BrowserThread::FILE, FROM_HERE, | |
|
Aaron Boodman
2012/04/05 21:53:14
Add a comment saying something like:
// NOTE: It'
Devlin
2012/04/07 17:38:21
Done.
| |
| 32 base::Bind( | |
| 33 &ExtensionGarbageCollector::CheckExtensionDirectories, | |
| 34 this, | |
| 35 install_directory))) | |
| 36 NOTREACHED(); | |
| 37 } | |
| 38 | |
| 39 void ExtensionGarbageCollector::CheckExtensionDirectories( | |
| 40 const FilePath& install_directory) { | |
| 41 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 42 | |
| 43 DVLOG(1) << "Garbage collecting extensions..."; | |
| 44 | |
| 45 if (!file_util::DirectoryExists(install_directory)) | |
| 46 return; | |
| 47 | |
| 48 std::set<FilePath> extension_paths; | |
| 49 | |
| 50 file_util::FileEnumerator enumerator(install_directory, | |
| 51 false, // Not recursive. | |
| 52 file_util::FileEnumerator::DIRECTORIES); | |
| 53 | |
| 54 for (FilePath extension_path = enumerator.Next(); | |
| 55 !extension_path.value().empty(); | |
|
Aaron Boodman
2012/04/05 21:53:14
Can this part fit on the previous line?
Devlin
2012/04/07 17:38:21
Done.
| |
| 56 extension_path = enumerator.Next()) { | |
| 57 std::string extension_id; | |
| 58 FilePath basename = extension_path.BaseName(); | |
| 59 if (IsStringASCII(basename.value())) { | |
| 60 extension_id = UTF16ToASCII(basename.LossyDisplayName()); | |
| 61 if (!Extension::IdIsValid(extension_id)) | |
| 62 extension_id.clear(); | |
| 63 } | |
| 64 | |
| 65 // Delete directories that aren't valid IDs. | |
| 66 if (extension_id.empty()) { | |
| 67 DLOG(WARNING) << "Invalid extension ID encountered in extensions " | |
| 68 "directory: " << basename.value(); | |
| 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 if (!content::BrowserThread::PostTask( | |
| 79 content::BrowserThread::UI, FROM_HERE, | |
| 80 base::Bind( | |
| 81 &ExtensionGarbageCollector::CheckExtensionPreferences, | |
| 82 this, | |
| 83 extension_paths))) | |
| 84 NOTREACHED(); | |
| 85 } | |
| 86 | |
| 87 void ExtensionGarbageCollector::CheckExtensionPreferences( | |
| 88 const std::set<FilePath>& extension_paths) { | |
| 89 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 90 | |
| 91 ExtensionPrefs* extension_prefs = extension_service_->extension_prefs(); | |
| 92 | |
| 93 if (extension_prefs->pref_service()->ReadOnly()) | |
| 94 return; | |
| 95 | |
| 96 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( | |
| 97 extension_prefs->GetInstalledExtensionsInfo()); | |
| 98 | |
| 99 std::map<std::string, FilePath> extension_pref_paths; | |
| 100 for (size_t i = 0; i < info->size(); ++i) { | |
| 101 extension_pref_paths[info->at(i)->extension_id] = | |
| 102 info->at(i)->extension_path; | |
| 103 } | |
| 104 | |
| 105 std::set<std::string> extension_ids; | |
| 106 | |
| 107 // Check each directory for a reference in the preferences. | |
| 108 for (std::set<FilePath>::const_iterator path = extension_paths.begin(); | |
| 109 path != extension_paths.end(); | |
| 110 ++path) { | |
| 111 std::string extension_id; | |
| 112 FilePath basename = path->BaseName(); | |
| 113 if (IsStringASCII(basename.value())) | |
| 114 extension_id = UTF16ToASCII(basename.LossyDisplayName()); | |
|
Aaron Boodman
2012/04/05 21:53:14
basename.MaybeAsASCII would simplify this a bit.
Devlin
2012/04/07 17:38:21
Done.
| |
| 115 if (extension_id.empty()) | |
| 116 continue; | |
| 117 | |
| 118 extension_ids.insert(extension_id); | |
| 119 | |
| 120 std::map<std::string, FilePath>::const_iterator iter = | |
| 121 extension_pref_paths.find(extension_id); | |
| 122 | |
| 123 if (iter == extension_pref_paths.end()) { | |
| 124 DVLOG(1) << "Deleting unreferenced install for directory " | |
| 125 << path->LossyDisplayName() << "."; | |
| 126 if (!content::BrowserThread::PostTask( | |
| 127 content::BrowserThread::FILE, FROM_HERE, | |
| 128 base::Bind( | |
| 129 &extension_file_util::DeleteFile, | |
| 130 *path, | |
| 131 true))) // recursive | |
| 132 NOTREACHED(); | |
| 133 continue; | |
| 134 } | |
| 135 | |
| 136 // Clean up old version directories. | |
| 137 file_util::FileEnumerator versions_enumerator( | |
|
Aaron Boodman
2012/04/05 21:53:14
You are on the UI thread here. No filesystem acces
Devlin
2012/04/07 17:38:21
Done.
| |
| 138 *path, | |
| 139 false, // Not recursive. | |
| 140 file_util::FileEnumerator::DIRECTORIES); | |
| 141 for (FilePath version_dir = versions_enumerator.Next(); | |
| 142 !version_dir.value().empty(); | |
| 143 version_dir = versions_enumerator.Next()) { | |
| 144 if (version_dir.BaseName() != iter->second.BaseName()) { | |
| 145 DVLOG(1) << "Deleting old version for directory " | |
| 146 << version_dir.LossyDisplayName() << "."; | |
| 147 if (!content::BrowserThread::PostTask( | |
| 148 content::BrowserThread::FILE, FROM_HERE, | |
| 149 base::Bind( | |
| 150 &extension_file_util::DeleteFile, | |
| 151 version_dir, | |
| 152 true))) // Recursive. | |
| 153 NOTREACHED(); | |
| 154 } | |
| 155 } | |
| 156 } | |
| 157 | |
| 158 // Check each entry in the preferences for an existing path. | |
| 159 for (std::map<std::string, FilePath>::const_iterator extension = | |
| 160 extension_pref_paths.begin(); | |
| 161 extension != extension_pref_paths.end(); | |
| 162 ++extension) { | |
| 163 std::set<std::string>::const_iterator iter = extension_ids.find( | |
| 164 extension->first); | |
| 165 | |
| 166 if (iter != extension_ids.end()) | |
| 167 continue; | |
| 168 | |
| 169 std::string extension_id = extension->first; | |
| 170 | |
| 171 DVLOG(1) << "Could not access local content for extension with id " | |
| 172 << extension_id << "; uninstalling extension."; | |
| 173 | |
| 174 // If the extension failed to load fully (e.g. the user deleted an unpacked | |
| 175 // extension's manifest or the manifest points to the wrong path), we cannot | |
| 176 // use UninstallExtension, which relies on a valid Extension object. | |
| 177 if (!extension_service_->GetInstalledExtension(extension_id)) { | |
| 178 scoped_ptr<ExtensionInfo> info(extension_service_->extension_prefs() | |
| 179 ->GetInstalledExtensionInfo(extension_id)); | |
| 180 extension_service_->extension_prefs()->OnExtensionUninstalled( | |
| 181 extension_id, info->extension_location, false); | |
| 182 } else { | |
| 183 extension_service_->UninstallExtension(extension_id, false, NULL); | |
| 184 } | |
| 185 } | |
| 186 } | |
| OLD | NEW |