| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/files/file_enumerator.h" | 11 #include "base/files/file_enumerator.h" |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/json/json_file_value_serializer.h" | 13 #include "base/json/json_file_value_serializer.h" |
| 15 #include "base/logging.h" | 14 #include "base/logging.h" |
| 16 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
| 17 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 18 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
| 21 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
| 22 #include "chrome/common/extensions/api/extension_action/action_info.h" | 21 #include "chrome/common/extensions/api/extension_action/action_info.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 38 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
| 39 | 38 |
| 40 using extensions::Extension; | 39 using extensions::Extension; |
| 41 using extensions::ExtensionResource; | 40 using extensions::ExtensionResource; |
| 42 using extensions::Manifest; | 41 using extensions::Manifest; |
| 43 | 42 |
| 44 namespace errors = extensions::manifest_errors; | 43 namespace errors = extensions::manifest_errors; |
| 45 | 44 |
| 46 namespace { | 45 namespace { |
| 47 | 46 |
| 48 const base::FilePath::CharType kTempDirectoryName[] = FILE_PATH_LITERAL("Temp"); | |
| 49 | |
| 50 // Add the image paths contained in the |icon_set| to |image_paths|. | 47 // Add the image paths contained in the |icon_set| to |image_paths|. |
| 51 void AddPathsFromIconSet(const ExtensionIconSet& icon_set, | 48 void AddPathsFromIconSet(const ExtensionIconSet& icon_set, |
| 52 std::set<base::FilePath>* image_paths) { | 49 std::set<base::FilePath>* image_paths) { |
| 53 // TODO(viettrungluu): These |FilePath::FromUTF8Unsafe()| indicate that we're | 50 // TODO(viettrungluu): These |FilePath::FromUTF8Unsafe()| indicate that we're |
| 54 // doing something wrong. | 51 // doing something wrong. |
| 55 for (ExtensionIconSet::IconMap::const_iterator iter = icon_set.map().begin(); | 52 for (ExtensionIconSet::IconMap::const_iterator iter = icon_set.map().begin(); |
| 56 iter != icon_set.map().end(); ++iter) { | 53 iter != icon_set.map().end(); ++iter) { |
| 57 image_paths->insert(base::FilePath::FromUTF8Unsafe(iter->second)); | 54 image_paths->insert(base::FilePath::FromUTF8Unsafe(iter->second)); |
| 58 } | 55 } |
| 59 } | 56 } |
| 60 | 57 |
| 61 } // namespace | 58 } // namespace |
| 62 | 59 |
| 63 namespace extension_file_util { | 60 namespace extension_file_util { |
| 64 | 61 |
| 62 const base::FilePath::CharType kTempDirectoryName[] = FILE_PATH_LITERAL("Temp"); |
| 63 |
| 65 base::FilePath InstallExtension(const base::FilePath& unpacked_source_dir, | 64 base::FilePath InstallExtension(const base::FilePath& unpacked_source_dir, |
| 66 const std::string& id, | 65 const std::string& id, |
| 67 const std::string& version, | 66 const std::string& version, |
| 68 const base::FilePath& extensions_dir) { | 67 const base::FilePath& extensions_dir) { |
| 69 base::FilePath extension_dir = extensions_dir.AppendASCII(id); | 68 base::FilePath extension_dir = extensions_dir.AppendASCII(id); |
| 70 base::FilePath version_dir; | 69 base::FilePath version_dir; |
| 71 | 70 |
| 72 // Create the extension directory if it doesn't exist already. | 71 // Create the extension directory if it doesn't exist already. |
| 73 if (!base::PathExists(extension_dir)) { | 72 if (!base::PathExists(extension_dir)) { |
| 74 if (!base::CreateDirectory(extension_dir)) | 73 if (!base::CreateDirectory(extension_dir)) |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 return base::FilePath(); | 114 return base::FilePath(); |
| 116 } | 115 } |
| 117 | 116 |
| 118 return version_dir; | 117 return version_dir; |
| 119 } | 118 } |
| 120 | 119 |
| 121 void UninstallExtension(const base::FilePath& extensions_dir, | 120 void UninstallExtension(const base::FilePath& extensions_dir, |
| 122 const std::string& id) { | 121 const std::string& id) { |
| 123 // We don't care about the return value. If this fails (and it can, due to | 122 // We don't care about the return value. If this fails (and it can, due to |
| 124 // plugins that aren't unloaded yet), it will get cleaned up by | 123 // plugins that aren't unloaded yet), it will get cleaned up by |
| 125 // ExtensionService::GarbageCollectExtensions. | 124 // ExtensionGarbageCollector::GarbageCollectExtensions. |
| 126 base::DeleteFile(extensions_dir.AppendASCII(id), true); // recursive. | 125 base::DeleteFile(extensions_dir.AppendASCII(id), true); // recursive. |
| 127 } | 126 } |
| 128 | 127 |
| 129 scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_path, | 128 scoped_refptr<Extension> LoadExtension(const base::FilePath& extension_path, |
| 130 Manifest::Location location, | 129 Manifest::Location location, |
| 131 int flags, | 130 int flags, |
| 132 std::string* error) { | 131 std::string* error) { |
| 133 return LoadExtension(extension_path, std::string(), location, flags, error); | 132 return LoadExtension(extension_path, std::string(), location, flags, error); |
| 134 } | 133 } |
| 135 | 134 |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 AddPathsFromIconSet(page_action->default_icon, &image_paths); | 315 AddPathsFromIconSet(page_action->default_icon, &image_paths); |
| 317 | 316 |
| 318 const extensions::ActionInfo* browser_action = | 317 const extensions::ActionInfo* browser_action = |
| 319 extensions::ActionInfo::GetBrowserActionInfo(extension); | 318 extensions::ActionInfo::GetBrowserActionInfo(extension); |
| 320 if (browser_action && !browser_action->default_icon.empty()) | 319 if (browser_action && !browser_action->default_icon.empty()) |
| 321 AddPathsFromIconSet(browser_action->default_icon, &image_paths); | 320 AddPathsFromIconSet(browser_action->default_icon, &image_paths); |
| 322 | 321 |
| 323 return image_paths; | 322 return image_paths; |
| 324 } | 323 } |
| 325 | 324 |
| 326 void GarbageCollectExtensions( | |
| 327 const base::FilePath& install_directory, | |
| 328 const std::multimap<std::string, base::FilePath>& extension_paths, | |
| 329 bool clean_temp_dir) { | |
| 330 // Nothing to clean up if it doesn't exist. | |
| 331 if (!base::DirectoryExists(install_directory)) | |
| 332 return; | |
| 333 | |
| 334 DVLOG(1) << "Garbage collecting extensions..."; | |
| 335 base::FileEnumerator enumerator(install_directory, | |
| 336 false, // Not recursive. | |
| 337 base::FileEnumerator::DIRECTORIES); | |
| 338 base::FilePath extension_path; | |
| 339 for (extension_path = enumerator.Next(); !extension_path.value().empty(); | |
| 340 extension_path = enumerator.Next()) { | |
| 341 std::string extension_id; | |
| 342 | |
| 343 base::FilePath basename = extension_path.BaseName(); | |
| 344 // Clean up temporary files left if Chrome crashed or quit in the middle | |
| 345 // of an extension install. | |
| 346 if (basename.value() == kTempDirectoryName) { | |
| 347 if (clean_temp_dir) | |
| 348 base::DeleteFile(extension_path, true); // Recursive | |
| 349 continue; | |
| 350 } | |
| 351 | |
| 352 // Parse directory name as a potential extension ID. | |
| 353 if (IsStringASCII(basename.value())) { | |
| 354 extension_id = base::UTF16ToASCII(basename.LossyDisplayName()); | |
| 355 if (!Extension::IdIsValid(extension_id)) | |
| 356 extension_id.clear(); | |
| 357 } | |
| 358 | |
| 359 // Delete directories that aren't valid IDs. | |
| 360 if (extension_id.empty()) { | |
| 361 DLOG(WARNING) << "Invalid extension ID encountered in extensions " | |
| 362 "directory: " << basename.value(); | |
| 363 DVLOG(1) << "Deleting invalid extension directory " | |
| 364 << extension_path.value() << "."; | |
| 365 base::DeleteFile(extension_path, true); // Recursive. | |
| 366 continue; | |
| 367 } | |
| 368 | |
| 369 typedef std::multimap<std::string, base::FilePath>::const_iterator Iter; | |
| 370 std::pair<Iter, Iter> iter_pair = extension_paths.equal_range(extension_id); | |
| 371 | |
| 372 // If there is no entry in the prefs file, just delete the directory and | |
| 373 // move on. This can legitimately happen when an uninstall does not | |
| 374 // complete, for example, when a plugin is in use at uninstall time. | |
| 375 if (iter_pair.first == iter_pair.second) { | |
| 376 DVLOG(1) << "Deleting unreferenced install for directory " | |
| 377 << extension_path.LossyDisplayName() << "."; | |
| 378 base::DeleteFile(extension_path, true); // Recursive. | |
| 379 continue; | |
| 380 } | |
| 381 | |
| 382 // Clean up old version directories. | |
| 383 base::FileEnumerator versions_enumerator( | |
| 384 extension_path, | |
| 385 false, // Not recursive. | |
| 386 base::FileEnumerator::DIRECTORIES); | |
| 387 for (base::FilePath version_dir = versions_enumerator.Next(); | |
| 388 !version_dir.value().empty(); | |
| 389 version_dir = versions_enumerator.Next()) { | |
| 390 bool knownVersion = false; | |
| 391 for (Iter it = iter_pair.first; it != iter_pair.second; ++it) | |
| 392 if (version_dir.BaseName() == it->second.BaseName()) { | |
| 393 knownVersion = true; | |
| 394 break; | |
| 395 } | |
| 396 if (!knownVersion) { | |
| 397 DVLOG(1) << "Deleting old version for directory " | |
| 398 << version_dir.LossyDisplayName() << "."; | |
| 399 base::DeleteFile(version_dir, true); // Recursive. | |
| 400 } | |
| 401 } | |
| 402 } | |
| 403 } | |
| 404 | |
| 405 extensions::MessageBundle* LoadMessageBundle( | 325 extensions::MessageBundle* LoadMessageBundle( |
| 406 const base::FilePath& extension_path, | 326 const base::FilePath& extension_path, |
| 407 const std::string& default_locale, | 327 const std::string& default_locale, |
| 408 std::string* error) { | 328 std::string* error) { |
| 409 error->clear(); | 329 error->clear(); |
| 410 // Load locale information if available. | 330 // Load locale information if available. |
| 411 base::FilePath locale_path = extension_path.Append(extensions::kLocaleFolder); | 331 base::FilePath locale_path = extension_path.Append(extensions::kLocaleFolder); |
| 412 if (!base::PathExists(locale_path)) | 332 if (!base::PathExists(locale_path)) |
| 413 return NULL; | 333 return NULL; |
| 414 | 334 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 return base::FilePath(); | 444 return base::FilePath(); |
| 525 } | 445 } |
| 526 return temp_path; | 446 return temp_path; |
| 527 } | 447 } |
| 528 | 448 |
| 529 void DeleteFile(const base::FilePath& path, bool recursive) { | 449 void DeleteFile(const base::FilePath& path, bool recursive) { |
| 530 base::DeleteFile(path, recursive); | 450 base::DeleteFile(path, recursive); |
| 531 } | 451 } |
| 532 | 452 |
| 533 } // namespace extension_file_util | 453 } // namespace extension_file_util |
| OLD | NEW |