| 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" | |
| 12 #include "base/files/file_path.h" | 11 #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/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 19 #include "base/threading/thread_restrictions.h" | 18 #include "base/threading/thread_restrictions.h" |
| 20 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 21 #include "chrome/common/chrome_constants.h" | 20 #include "chrome/common/chrome_constants.h" |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 return NULL; | 195 return NULL; |
| 197 } | 196 } |
| 198 | 197 |
| 199 return static_cast<DictionaryValue*>(root.release()); | 198 return static_cast<DictionaryValue*>(root.release()); |
| 200 } | 199 } |
| 201 | 200 |
| 202 std::vector<base::FilePath> FindPrivateKeyFiles( | 201 std::vector<base::FilePath> FindPrivateKeyFiles( |
| 203 const base::FilePath& extension_dir) { | 202 const base::FilePath& extension_dir) { |
| 204 std::vector<base::FilePath> result; | 203 std::vector<base::FilePath> result; |
| 205 // Pattern matching only works at the root level, so filter manually. | 204 // Pattern matching only works at the root level, so filter manually. |
| 206 base::FileEnumerator traversal(extension_dir, /*recursive=*/true, | 205 file_util::FileEnumerator traversal(extension_dir, /*recursive=*/true, |
| 207 base::FileEnumerator::FILES); | 206 file_util::FileEnumerator::FILES); |
| 208 for (base::FilePath current = traversal.Next(); !current.empty(); | 207 for (base::FilePath current = traversal.Next(); !current.empty(); |
| 209 current = traversal.Next()) { | 208 current = traversal.Next()) { |
| 210 if (!current.MatchesExtension(chrome::kExtensionKeyFileExtension)) | 209 if (!current.MatchesExtension(chrome::kExtensionKeyFileExtension)) |
| 211 continue; | 210 continue; |
| 212 | 211 |
| 213 std::string key_contents; | 212 std::string key_contents; |
| 214 if (!file_util::ReadFileToString(current, &key_contents)) { | 213 if (!file_util::ReadFileToString(current, &key_contents)) { |
| 215 // If we can't read the file, assume it's not a private key. | 214 // If we can't read the file, assume it's not a private key. |
| 216 continue; | 215 continue; |
| 217 } | 216 } |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 } | 324 } |
| 326 | 325 |
| 327 void GarbageCollectExtensions( | 326 void GarbageCollectExtensions( |
| 328 const base::FilePath& install_directory, | 327 const base::FilePath& install_directory, |
| 329 const std::multimap<std::string, base::FilePath>& extension_paths) { | 328 const std::multimap<std::string, base::FilePath>& extension_paths) { |
| 330 // Nothing to clean up if it doesn't exist. | 329 // Nothing to clean up if it doesn't exist. |
| 331 if (!file_util::DirectoryExists(install_directory)) | 330 if (!file_util::DirectoryExists(install_directory)) |
| 332 return; | 331 return; |
| 333 | 332 |
| 334 DVLOG(1) << "Garbage collecting extensions..."; | 333 DVLOG(1) << "Garbage collecting extensions..."; |
| 335 base::FileEnumerator enumerator(install_directory, | 334 file_util::FileEnumerator enumerator(install_directory, |
| 336 false, // Not recursive. | 335 false, // Not recursive. |
| 337 base::FileEnumerator::DIRECTORIES); | 336 file_util::FileEnumerator::DIRECTORIES); |
| 338 base::FilePath extension_path; | 337 base::FilePath extension_path; |
| 339 for (extension_path = enumerator.Next(); !extension_path.value().empty(); | 338 for (extension_path = enumerator.Next(); !extension_path.value().empty(); |
| 340 extension_path = enumerator.Next()) { | 339 extension_path = enumerator.Next()) { |
| 341 std::string extension_id; | 340 std::string extension_id; |
| 342 | 341 |
| 343 base::FilePath basename = extension_path.BaseName(); | 342 base::FilePath basename = extension_path.BaseName(); |
| 344 // Clean up temporary files left if Chrome crashed or quit in the middle | 343 // Clean up temporary files left if Chrome crashed or quit in the middle |
| 345 // of an extension install. | 344 // of an extension install. |
| 346 if (basename.value() == kTempDirectoryName) { | 345 if (basename.value() == kTempDirectoryName) { |
| 347 file_util::Delete(extension_path, true); // Recursive | 346 file_util::Delete(extension_path, true); // Recursive |
| (...skipping 24 matching lines...) Expand all Loading... |
| 372 // move on. This can legitimately happen when an uninstall does not | 371 // move on. This can legitimately happen when an uninstall does not |
| 373 // complete, for example, when a plugin is in use at uninstall time. | 372 // complete, for example, when a plugin is in use at uninstall time. |
| 374 if (iter_pair.first == iter_pair.second) { | 373 if (iter_pair.first == iter_pair.second) { |
| 375 DVLOG(1) << "Deleting unreferenced install for directory " | 374 DVLOG(1) << "Deleting unreferenced install for directory " |
| 376 << extension_path.LossyDisplayName() << "."; | 375 << extension_path.LossyDisplayName() << "."; |
| 377 file_util::Delete(extension_path, true); // Recursive. | 376 file_util::Delete(extension_path, true); // Recursive. |
| 378 continue; | 377 continue; |
| 379 } | 378 } |
| 380 | 379 |
| 381 // Clean up old version directories. | 380 // Clean up old version directories. |
| 382 base::FileEnumerator versions_enumerator( | 381 file_util::FileEnumerator versions_enumerator( |
| 383 extension_path, | 382 extension_path, |
| 384 false, // Not recursive. | 383 false, // Not recursive. |
| 385 base::FileEnumerator::DIRECTORIES); | 384 file_util::FileEnumerator::DIRECTORIES); |
| 386 for (base::FilePath version_dir = versions_enumerator.Next(); | 385 for (base::FilePath version_dir = versions_enumerator.Next(); |
| 387 !version_dir.value().empty(); | 386 !version_dir.value().empty(); |
| 388 version_dir = versions_enumerator.Next()) { | 387 version_dir = versions_enumerator.Next()) { |
| 389 bool knownVersion = false; | 388 bool knownVersion = false; |
| 390 for (Iter it = iter_pair.first; it != iter_pair.second; ++it) | 389 for (Iter it = iter_pair.first; it != iter_pair.second; ++it) |
| 391 if (version_dir.BaseName() == it->second.BaseName()) { | 390 if (version_dir.BaseName() == it->second.BaseName()) { |
| 392 knownVersion = true; | 391 knownVersion = true; |
| 393 break; | 392 break; |
| 394 } | 393 } |
| 395 if (!knownVersion) { | 394 if (!knownVersion) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 FILE_PATH_LITERAL("__MACOSX"), | 464 FILE_PATH_LITERAL("__MACOSX"), |
| 466 }; | 465 }; |
| 467 CR_DEFINE_STATIC_LOCAL( | 466 CR_DEFINE_STATIC_LOCAL( |
| 468 std::set<base::FilePath::StringType>, reserved_underscore_names, | 467 std::set<base::FilePath::StringType>, reserved_underscore_names, |
| 469 (reserved_names, reserved_names + arraysize(reserved_names))); | 468 (reserved_names, reserved_names + arraysize(reserved_names))); |
| 470 | 469 |
| 471 // Enumerate all files and directories in the extension root. | 470 // Enumerate all files and directories in the extension root. |
| 472 // There is a problem when using pattern "_*" with FileEnumerator, so we have | 471 // There is a problem when using pattern "_*" with FileEnumerator, so we have |
| 473 // to cheat with find_first_of and match all. | 472 // to cheat with find_first_of and match all. |
| 474 const int kFilesAndDirectories = | 473 const int kFilesAndDirectories = |
| 475 base::FileEnumerator::DIRECTORIES | base::FileEnumerator::FILES; | 474 file_util::FileEnumerator::DIRECTORIES | file_util::FileEnumerator::FILES; |
| 476 base::FileEnumerator all_files(extension_path, false, kFilesAndDirectories); | 475 file_util::FileEnumerator all_files( |
| 476 extension_path, false, kFilesAndDirectories); |
| 477 | 477 |
| 478 base::FilePath file; | 478 base::FilePath file; |
| 479 while (!(file = all_files.Next()).empty()) { | 479 while (!(file = all_files.Next()).empty()) { |
| 480 base::FilePath::StringType filename = file.BaseName().value(); | 480 base::FilePath::StringType filename = file.BaseName().value(); |
| 481 // Skip all that don't start with "_". | 481 // Skip all that don't start with "_". |
| 482 if (filename.find_first_of(FILE_PATH_LITERAL("_")) != 0) continue; | 482 if (filename.find_first_of(FILE_PATH_LITERAL("_")) != 0) continue; |
| 483 if (reserved_underscore_names.find(filename) == | 483 if (reserved_underscore_names.find(filename) == |
| 484 reserved_underscore_names.end()) { | 484 reserved_underscore_names.end()) { |
| 485 *error = base::StringPrintf( | 485 *error = base::StringPrintf( |
| 486 "Cannot load extension with file or directory name %s. " | 486 "Cannot load extension with file or directory name %s. " |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 return base::FilePath(); | 574 return base::FilePath(); |
| 575 } | 575 } |
| 576 return temp_path; | 576 return temp_path; |
| 577 } | 577 } |
| 578 | 578 |
| 579 void DeleteFile(const base::FilePath& path, bool recursive) { | 579 void DeleteFile(const base::FilePath& path, bool recursive) { |
| 580 file_util::Delete(path, recursive); | 580 file_util::Delete(path, recursive); |
| 581 } | 581 } |
| 582 | 582 |
| 583 } // namespace extension_file_util | 583 } // namespace extension_file_util |
| OLD | NEW |