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