| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/extensions/extension_file_util.h" | 5 #include "chrome/common/extensions/extension_file_util.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "net/base/file_stream.h" | 23 #include "net/base/file_stream.h" |
| 24 | 24 |
| 25 namespace errors = extension_manifest_errors; | 25 namespace errors = extension_manifest_errors; |
| 26 | 26 |
| 27 namespace extension_file_util { | 27 namespace extension_file_util { |
| 28 | 28 |
| 29 // Validates locale info. Doesn't check if messages.json files are valid. | 29 // Validates locale info. Doesn't check if messages.json files are valid. |
| 30 static bool ValidateLocaleInfo(const Extension& extension, std::string* error); | 30 static bool ValidateLocaleInfo(const Extension& extension, std::string* error); |
| 31 | 31 |
| 32 const char kInstallDirectoryName[] = "Extensions"; | 32 const char kInstallDirectoryName[] = "Extensions"; |
| 33 // TODO(mpcomplete): obsolete. remove after migration period. | |
| 34 // http://code.google.com/p/chromium/issues/detail?id=19733 | |
| 35 const char kCurrentVersionFileName[] = "Current Version"; | |
| 36 | 33 |
| 37 bool MoveDirSafely(const FilePath& source_dir, const FilePath& dest_dir) { | 34 FilePath InstallExtension(const FilePath& unpacked_source_dir, |
| 38 if (file_util::PathExists(dest_dir)) { | 35 const std::string& id, |
| 39 if (!file_util::Delete(dest_dir, true)) | 36 const std::string& version, |
| 40 return false; | 37 const FilePath& all_extensions_dir) { |
| 41 } else { | 38 FilePath extension_dir = all_extensions_dir.AppendASCII(id); |
| 42 FilePath parent = dest_dir.DirName(); | 39 FilePath version_dir; |
| 43 if (!file_util::DirectoryExists(parent)) { | 40 |
| 44 if (!file_util::CreateDirectory(parent)) | 41 // Create the extension directory if it doesn't exist already. |
| 45 return false; | 42 if (!file_util::PathExists(extension_dir)) { |
| 43 if (!file_util::CreateDirectory(extension_dir)) |
| 44 return FilePath(); |
| 45 } |
| 46 |
| 47 // Try to find a free directory. There can be legitimate conflicts in the case |
| 48 // of overinstallation of the same version. |
| 49 const int kMaxAttempts = 100; |
| 50 for (int i = 0; i < kMaxAttempts; ++i) { |
| 51 FilePath candidate = extension_dir.AppendASCII( |
| 52 StringPrintf("%s_%u", version.c_str(), i)); |
| 53 if (!file_util::PathExists(candidate)) { |
| 54 version_dir = candidate; |
| 55 break; |
| 46 } | 56 } |
| 47 } | 57 } |
| 48 | 58 |
| 49 if (!file_util::Move(source_dir, dest_dir)) | 59 if (version_dir.empty()) { |
| 50 return false; | 60 LOG(ERROR) << "Could not find a home for extension " << id << " with " |
| 61 << "version " << version << "."; |
| 62 return FilePath(); |
| 63 } |
| 51 | 64 |
| 52 return true; | 65 if (!file_util::Move(unpacked_source_dir, version_dir)) |
| 66 return FilePath(); |
| 67 |
| 68 return version_dir; |
| 53 } | 69 } |
| 54 | 70 |
| 55 Extension::InstallType CompareToInstalledVersion( | 71 void UninstallExtension(const FilePath& extensions_dir, |
| 56 const FilePath& extensions_dir, | 72 const std::string& id) { |
| 57 const std::string& extension_id, | 73 // We don't care about the return value. If this fails (and it can, due to |
| 58 const std::string& current_version_str, | 74 // plugins that aren't unloaded yet, it will get cleaned up by |
| 59 const std::string& new_version_str, | 75 // ExtensionsService::GarbageCollectExtensions). |
| 60 FilePath* version_dir) { | 76 file_util::Delete(extensions_dir.AppendASCII(id), true); // recursive. |
| 61 FilePath dest_dir = extensions_dir.AppendASCII(extension_id); | |
| 62 FilePath current_version_dir = dest_dir.AppendASCII(current_version_str); | |
| 63 *version_dir = dest_dir.AppendASCII(new_version_str); | |
| 64 | |
| 65 if (current_version_str.empty()) | |
| 66 return Extension::NEW_INSTALL; | |
| 67 | |
| 68 scoped_ptr<Version> current_version( | |
| 69 Version::GetVersionFromString(current_version_str)); | |
| 70 scoped_ptr<Version> new_version( | |
| 71 Version::GetVersionFromString(new_version_str)); | |
| 72 int comp = new_version->CompareTo(*current_version); | |
| 73 if (comp > 0) | |
| 74 return Extension::UPGRADE; | |
| 75 if (comp < 0) | |
| 76 return Extension::DOWNGRADE; | |
| 77 | |
| 78 // Same version. Treat corrupted existing installation as new install case. | |
| 79 if (!SanityCheckExtension(current_version_dir)) | |
| 80 return Extension::NEW_INSTALL; | |
| 81 | |
| 82 return Extension::REINSTALL; | |
| 83 } | |
| 84 | |
| 85 bool SanityCheckExtension(const FilePath& dir) { | |
| 86 // Verify that the directory actually exists. | |
| 87 // TODO(erikkay): A further step would be to verify that the extension | |
| 88 // has actually loaded successfully. | |
| 89 FilePath manifest_file(dir.Append(Extension::kManifestFilename)); | |
| 90 return file_util::PathExists(dir) && file_util::PathExists(manifest_file); | |
| 91 } | |
| 92 | |
| 93 bool InstallExtension(const FilePath& src_dir, | |
| 94 const FilePath& version_dir, | |
| 95 std::string* error) { | |
| 96 // If anything fails after this, we want to delete the extension dir. | |
| 97 ScopedTempDir scoped_version_dir; | |
| 98 scoped_version_dir.Set(version_dir); | |
| 99 | |
| 100 if (!MoveDirSafely(src_dir, version_dir)) { | |
| 101 *error = l10n_util::GetStringUTF8( | |
| 102 IDS_EXTENSION_MOVE_DIRECTORY_TO_PROFILE_FAILED); | |
| 103 return false; | |
| 104 } | |
| 105 | |
| 106 scoped_version_dir.Take(); | |
| 107 return true; | |
| 108 } | 77 } |
| 109 | 78 |
| 110 Extension* LoadExtension(const FilePath& extension_path, | 79 Extension* LoadExtension(const FilePath& extension_path, |
| 111 bool require_key, | 80 bool require_key, |
| 112 std::string* error) { | 81 std::string* error) { |
| 113 FilePath manifest_path = | 82 FilePath manifest_path = |
| 114 extension_path.Append(Extension::kManifestFilename); | 83 extension_path.Append(Extension::kManifestFilename); |
| 115 if (!file_util::PathExists(manifest_path)) { | 84 if (!file_util::PathExists(manifest_path)) { |
| 116 *error = | 85 *error = |
| 117 l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); | 86 l10n_util::GetStringUTF8(IDS_EXTENSION_MANIFEST_UNREADABLE); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 | 257 |
| 289 // Check children of extension root to see if any of them start with _ and is | 258 // Check children of extension root to see if any of them start with _ and is |
| 290 // not on the reserved list. | 259 // not on the reserved list. |
| 291 if (!CheckForIllegalFilenames(extension->path(), error)) { | 260 if (!CheckForIllegalFilenames(extension->path(), error)) { |
| 292 return false; | 261 return false; |
| 293 } | 262 } |
| 294 | 263 |
| 295 return true; | 264 return true; |
| 296 } | 265 } |
| 297 | 266 |
| 298 void UninstallExtension(const std::string& id, const FilePath& extensions_dir) { | |
| 299 // First, delete the Current Version file. If the directory delete fails, then | |
| 300 // at least the extension won't be loaded again. | |
| 301 FilePath extension_root = extensions_dir.AppendASCII(id); | |
| 302 | |
| 303 if (!file_util::PathExists(extension_root)) { | |
| 304 LOG(WARNING) << "Asked to remove a non-existent extension " << id; | |
| 305 return; | |
| 306 } | |
| 307 | |
| 308 FilePath current_version_file = extension_root.AppendASCII( | |
| 309 kCurrentVersionFileName); | |
| 310 if (!file_util::PathExists(current_version_file)) { | |
| 311 // This is OK, since we're phasing out the current version file. | |
| 312 } else { | |
| 313 if (!file_util::Delete(current_version_file, false)) { | |
| 314 LOG(WARNING) << "Could not delete Current Version file for extension " | |
| 315 << id; | |
| 316 return; | |
| 317 } | |
| 318 } | |
| 319 | |
| 320 // OK, now try and delete the entire rest of the directory. One major place | |
| 321 // this can fail is if the extension contains a plugin (stupid plugins). It's | |
| 322 // not a big deal though, because we'll notice next time we startup that the | |
| 323 // Current Version file is gone and finish the delete then. | |
| 324 if (!file_util::Delete(extension_root, true)) | |
| 325 LOG(WARNING) << "Could not delete directory for extension " << id; | |
| 326 } | |
| 327 | |
| 328 void GarbageCollectExtensions( | 267 void GarbageCollectExtensions( |
| 329 const FilePath& install_directory, | 268 const FilePath& install_directory, |
| 330 const std::set<std::string>& installed_ids, | 269 const std::map<std::string, FilePath>& extension_paths) { |
| 331 const std::map<std::string, std::string>& installed_versions) { | |
| 332 // Nothing to clean up if it doesn't exist. | 270 // Nothing to clean up if it doesn't exist. |
| 333 if (!file_util::DirectoryExists(install_directory)) | 271 if (!file_util::DirectoryExists(install_directory)) |
| 334 return; | 272 return; |
| 335 | 273 |
| 336 LOG(INFO) << "Loading installed extensions..."; | 274 LOG(INFO) << "Garbage collecting extensions..."; |
| 337 file_util::FileEnumerator enumerator(install_directory, | 275 file_util::FileEnumerator enumerator(install_directory, |
| 338 false, // Not recursive. | 276 false, // Not recursive. |
| 339 file_util::FileEnumerator::DIRECTORIES); | 277 file_util::FileEnumerator::DIRECTORIES); |
| 340 FilePath extension_path; | 278 FilePath extension_path; |
| 341 for (extension_path = enumerator.Next(); !extension_path.value().empty(); | 279 for (extension_path = enumerator.Next(); !extension_path.value().empty(); |
| 342 extension_path = enumerator.Next()) { | 280 extension_path = enumerator.Next()) { |
| 343 std::string extension_id = WideToASCII( | 281 std::string extension_id = WideToASCII( |
| 344 extension_path.BaseName().ToWStringHack()); | 282 extension_path.BaseName().ToWStringHack()); |
| 345 | 283 |
| 346 // If there is no entry in the prefs file, just delete the directory and | |
| 347 // move on. This can legitimately happen when an uninstall does not | |
| 348 // complete, for example, when a plugin is in use at uninstall time. | |
| 349 if (installed_ids.count(extension_id) == 0) { | |
| 350 LOG(INFO) << "Deleting unreferenced install for directory " | |
| 351 << WideToASCII(extension_path.ToWStringHack()) << "."; | |
| 352 file_util::Delete(extension_path, true); // Recursive. | |
| 353 continue; | |
| 354 } | |
| 355 | |
| 356 // Delete directories that aren't valid IDs. | 284 // Delete directories that aren't valid IDs. |
| 357 if (!Extension::IdIsValid(extension_id)) { | 285 if (!Extension::IdIsValid(extension_id)) { |
| 358 LOG(WARNING) << "Invalid extension ID encountered in extensions " | 286 LOG(WARNING) << "Invalid extension ID encountered in extensions " |
| 359 "directory: " << extension_id; | 287 "directory: " << extension_id; |
| 360 LOG(INFO) << "Deleting invalid extension directory " | 288 LOG(INFO) << "Deleting invalid extension directory " |
| 361 << WideToASCII(extension_path.ToWStringHack()) << "."; | 289 << WideToASCII(extension_path.ToWStringHack()) << "."; |
| 362 file_util::Delete(extension_path, true); // Recursive. | 290 file_util::Delete(extension_path, true); // Recursive. |
| 363 continue; | 291 continue; |
| 364 } | 292 } |
| 365 | 293 |
| 294 std::map<std::string, FilePath>::const_iterator iter = |
| 295 extension_paths.find(extension_id); |
| 296 |
| 297 // If there is no entry in the prefs file, just delete the directory and |
| 298 // move on. This can legitimately happen when an uninstall does not |
| 299 // complete, for example, when a plugin is in use at uninstall time. |
| 300 if (iter == extension_paths.end()) { |
| 301 LOG(INFO) << "Deleting unreferenced install for directory " |
| 302 << WideToASCII(extension_path.ToWStringHack()) << "."; |
| 303 file_util::Delete(extension_path, true); // Recursive. |
| 304 continue; |
| 305 } |
| 306 |
| 366 // Clean up old version directories. | 307 // Clean up old version directories. |
| 367 file_util::FileEnumerator versions_enumerator( | 308 file_util::FileEnumerator versions_enumerator( |
| 368 extension_path, | 309 extension_path, |
| 369 false, // Not recursive. | 310 false, // Not recursive. |
| 370 file_util::FileEnumerator::DIRECTORIES); | 311 file_util::FileEnumerator::DIRECTORIES); |
| 371 for (FilePath version_dir = versions_enumerator.Next(); | 312 for (FilePath version_dir = versions_enumerator.Next(); |
| 372 !version_dir.value().empty(); | 313 !version_dir.value().empty(); |
| 373 version_dir = versions_enumerator.Next()) { | 314 version_dir = versions_enumerator.Next()) { |
| 374 std::map<std::string, std::string>::const_iterator installed_version = | 315 if (version_dir.BaseName() != iter->second.BaseName()) { |
| 375 installed_versions.find(extension_id); | |
| 376 if (installed_version == installed_versions.end()) { | |
| 377 NOTREACHED() << "No installed version found for " << extension_id; | |
| 378 continue; | |
| 379 } | |
| 380 | |
| 381 std::string version = WideToASCII(version_dir.BaseName().ToWStringHack()); | |
| 382 if (version != installed_version->second) { | |
| 383 LOG(INFO) << "Deleting old version for directory " | 316 LOG(INFO) << "Deleting old version for directory " |
| 384 << WideToASCII(version_dir.ToWStringHack()) << "."; | 317 << WideToASCII(version_dir.ToWStringHack()) << "."; |
| 385 file_util::Delete(version_dir, true); // Recursive. | 318 file_util::Delete(version_dir, true); // Recursive. |
| 386 } | 319 } |
| 387 } | 320 } |
| 388 } | 321 } |
| 389 } | 322 } |
| 390 | 323 |
| 391 ExtensionMessageBundle* LoadExtensionMessageBundle( | 324 ExtensionMessageBundle* LoadExtensionMessageBundle( |
| 392 const FilePath& extension_path, | 325 const FilePath& extension_path, |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); | 459 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS); |
| 527 | 460 |
| 528 #if defined(OS_POSIX) | 461 #if defined(OS_POSIX) |
| 529 return FilePath(file_path); | 462 return FilePath(file_path); |
| 530 #elif defined(OS_WIN) | 463 #elif defined(OS_WIN) |
| 531 return FilePath(UTF8ToWide(file_path)); | 464 return FilePath(UTF8ToWide(file_path)); |
| 532 #endif | 465 #endif |
| 533 } | 466 } |
| 534 | 467 |
| 535 } // namespace extension_file_util | 468 } // namespace extension_file_util |
| OLD | NEW |