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 "base/file_util.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/string_util.h" | |
| 10 #include "chrome/browser/extensions/extension_prefs.h" | |
| 11 #include "chrome/browser/extensions/extension_service.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 | |
| 14 ExtensionGarbageCollector::ExtensionGarbageCollector( | |
| 15 ExtensionService* extension_service) | |
| 16 : extension_service_(extension_service) { | |
| 17 } | |
| 18 | |
| 19 ExtensionGarbageCollector::~ExtensionGarbageCollector() { | |
| 20 } | |
| 21 | |
| 22 void ExtensionGarbageCollector::GarbageCollectExtensions() { | |
| 23 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 24 ExtensionPrefs* extension_prefs = extension_service_->extension_prefs(); | |
| 25 | |
| 26 if (extension_prefs->pref_service()->ReadOnly()) | |
| 27 return; | |
| 28 | |
| 29 scoped_ptr<ExtensionPrefs::ExtensionsInfo> info( | |
| 30 extension_prefs->GetInstalledExtensionsInfo()); | |
| 31 | |
| 32 std::map<std::string, FilePath> extension_paths; | |
| 33 for (size_t i = 0; i < info->size(); ++i) | |
| 34 extension_paths[info->at(i)->extension_id] = info->at(i)->extension_path; | |
| 35 | |
| 36 if (!content::BrowserThread::PostTask( | |
| 37 content::BrowserThread::FILE, FROM_HERE, | |
| 38 base::Bind( | |
| 39 &ExtensionGarbageCollector::CheckExtensionDirectories, | |
| 40 this, | |
| 41 extension_paths))) | |
| 42 NOTREACHED(); | |
| 43 } | |
| 44 | |
| 45 void ExtensionGarbageCollector::CheckExtensionDirectories( | |
| 46 const std::map<std::string, FilePath>& extension_paths) { | |
| 47 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.
| |
| 48 | |
| 49 CheckInstalledDirectories(extension_paths); | |
| 50 | |
| 51 // Find any extensions for which the directory is nonexistent; add these to a | |
| 52 // list of extensions to be uninstalled. | |
| 53 std::vector<std::string> bad_extensions; | |
| 54 for (std::map<std::string, FilePath>::const_iterator iter = | |
| 55 extension_paths.begin(); iter != extension_paths.end(); ++iter) { | |
| 56 if (!file_util::PathExists(iter->second)) { | |
| 57 bad_extensions.push_back(iter->first); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 if (bad_extensions.size() && !content::BrowserThread::PostTask( | |
| 62 content::BrowserThread::UI, FROM_HERE, | |
| 63 base::Bind( | |
| 64 &ExtensionGarbageCollector::RemoveExtensionsMissingLocalContent, | |
| 65 this, | |
| 66 bad_extensions))) | |
| 67 NOTREACHED(); | |
| 68 } | |
| 69 | |
| 70 void ExtensionGarbageCollector::CheckInstalledDirectories( | |
| 71 const std::map<std::string, FilePath>& extension_paths) { | |
| 72 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.
| |
| 73 | |
| 74 if (!file_util::DirectoryExists(install_directory)) | |
| 75 return; | |
| 76 | |
| 77 file_util::FileEnumerator enumerator(install_directory, | |
| 78 false, // Not recursive. | |
| 79 file_util::FileEnumerator::DIRECTORIES); | |
| 80 | |
| 81 FilePath extension_path; | |
| 82 for (extension_path = enumerator.Next(); !extension_path.value().empty(); | |
| 83 extension_path = enumerator.Next()) { | |
| 84 std::string extension_id; | |
| 85 FilePath basename = extension_path.BaseName(); | |
| 86 if (IsStringASCII(basename.value())) { | |
| 87 extension_id = UTF16ToASCII(basename.LossyDisplayName()); | |
| 88 if (!Extension::IdIsValid(extension_id)) | |
| 89 extension_id.clear(); | |
| 90 } | |
| 91 | |
| 92 // Delete directories that aren't valid IDs. | |
| 93 if (extension_id.empty()) { | |
| 94 DLOG(WARNING) << "Invalid extension ID enecountered in extensions " | |
| 95 "directory: " << basename.value(); | |
| 96 DVLOG(1) << "Deleting invalid extension directory " | |
| 97 << extension_path.value() << "."; | |
| 98 file_util::Delete(extension_path, true); // Recursive. | |
| 99 continue; | |
| 100 } | |
| 101 | |
| 102 std::map<std::string, FilePath>::const_iterator iter = | |
| 103 extension_paths.find(extension_id); | |
| 104 | |
| 105 // If there is no entry in the prefs file, just delete the directory and | |
| 106 // move on. This can legitimately happen when an uninstall does not | |
| 107 // complete, for example, when a plugin is in use at uninstall time. | |
| 108 if (iter == extension_paths.end()) { | |
| 109 DVLOG(1) << "Deleting unreferenced install for directory " | |
| 110 << extension_path.LossyDisplayName() << "."; | |
| 111 file_util::Delete(extension_path, true); // Recursive. | |
| 112 continue; | |
| 113 } | |
| 114 | |
| 115 // Clean up old version directories. | |
| 116 file_util::FileEnumerator versions_enumerator( | |
| 117 extension_path, | |
| 118 false, // Not recursive. | |
| 119 file_util::FileEnumerator::DIRECTORIES); | |
| 120 for (FilePath version_dir = versions_enumerator.Next(); | |
| 121 !version_dir.value().empty(); | |
| 122 version_dir = versions_enumerator.Next()) { | |
| 123 if (version_dir.BaseName() != iter->second.BaseName()) { | |
| 124 DVLOG(1) << "Deleting old version for directory " | |
| 125 << version_dir.LossyDisplayName() << "."; | |
| 126 file_util::Delete(version_dir, true); // Recursive. | |
| 127 } | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 void ExtensionGarbageCollector::RemoveExtensionsMissingLocalContent( | |
| 133 const std::vector<std::string>& bad_extensions) { | |
| 134 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.
| |
| 135 iter != bad_extensions.end(); ++iter) { | |
| 136 DVLOG(1) << "Could not access local content for extension with id " << | |
| 137 *iter << "; uninstalling extension."; | |
| 138 | |
| 139 // If the extension failed to load fully (e.g. the user deleted an unpacked | |
| 140 // extension's manifest or the manifest points to the wrong path), we cannot | |
| 141 // use UninstallExtension, which relies on a valid Extension object. | |
| 142 if (!extension_service_->GetInstalledExtension(*iter)) { | |
| 143 scoped_ptr<ExtensionInfo> info(extension_service_->extension_prefs() | |
| 144 ->GetInstalledExtensionInfo(*iter)); | |
| 145 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.
| |
| 146 *iter, extension_misc::UNLOAD_REASON_UNINSTALL); | |
| 147 extension_service_->extension_prefs()->OnExtensionUninstalled( | |
| 148 *iter, info->extension_location, false); | |
| 149 } else { | |
| 150 extension_service_->UninstallExtension(*iter, false, NULL); | |
| 151 } | |
| 152 } | |
| 153 } | |
| OLD | NEW |