directory_lister.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #ifndef NET_BASE_DIRECTORY_LISTER_H_
  5. #define NET_BASE_DIRECTORY_LISTER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/atomicops.h"
  9. #include "base/files/file_enumerator.h"
  10. #include "base/files/file_path.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "net/base/net_export.h"
  14. namespace base {
  15. class SequencedTaskRunner;
  16. }
  17. namespace net {
  18. // This class provides an API for asynchronously listing the contents of a
  19. // directory on the filesystem. It runs a task on a background thread, and
  20. // enumerates all files in the specified directory on that thread. Destroying
  21. // the lister cancels the list operation. The DirectoryLister must only be
  22. // used on a thread with a MessageLoop.
  23. class NET_EXPORT DirectoryLister {
  24. public:
  25. // Represents one file found.
  26. struct DirectoryListerData {
  27. base::FileEnumerator::FileInfo info;
  28. base::FilePath path;
  29. base::FilePath absolute_path;
  30. };
  31. // Implement this class to receive directory entries.
  32. class DirectoryListerDelegate {
  33. public:
  34. // Called for each file found by the lister.
  35. virtual void OnListFile(const DirectoryListerData& data) = 0;
  36. // Called when the listing is complete.
  37. virtual void OnListDone(int error) = 0;
  38. protected:
  39. virtual ~DirectoryListerDelegate() = default;
  40. };
  41. // Listing options
  42. // ALPHA_DIRS_FIRST is the default listing type:
  43. // directories first in name order, then files by name order
  44. // Listing is recursive only if listing type is NO_SORT_RECURSIVE.
  45. // TODO(mmenke): Improve testing.
  46. enum ListingType {
  47. NO_SORT,
  48. NO_SORT_RECURSIVE,
  49. ALPHA_DIRS_FIRST,
  50. };
  51. DirectoryLister(const base::FilePath& dir,
  52. DirectoryListerDelegate* delegate);
  53. DirectoryLister(const base::FilePath& dir,
  54. ListingType type,
  55. DirectoryListerDelegate* delegate);
  56. DirectoryLister(const DirectoryLister&) = delete;
  57. DirectoryLister& operator=(const DirectoryLister&) = delete;
  58. // Will invoke Cancel().
  59. ~DirectoryLister();
  60. // Call this method to start the asynchronous directory enumeration.
  61. void Start();
  62. // Call this method to asynchronously stop directory enumeration. The
  63. // delegate will not be called back.
  64. void Cancel();
  65. private:
  66. typedef std::vector<DirectoryListerData> DirectoryList;
  67. // Class responsible for retrieving and sorting the actual directory list on
  68. // a worker pool thread. Created on the DirectoryLister's thread. As it's
  69. // refcounted, it's destroyed when the final reference is released, which may
  70. // happen on either thread.
  71. //
  72. // It's kept alive during the calls to Start() and DoneOnOriginSequence() by
  73. // the reference owned by the callback itself.
  74. class Core : public base::RefCountedThreadSafe<Core> {
  75. public:
  76. Core(const base::FilePath& dir, ListingType type, DirectoryLister* lister);
  77. Core(const Core&) = delete;
  78. Core& operator=(const Core&) = delete;
  79. // May only be called on a worker pool thread.
  80. void Start();
  81. // Must be called on the origin thread.
  82. void CancelOnOriginSequence();
  83. private:
  84. friend class base::RefCountedThreadSafe<Core>;
  85. class DataEvent;
  86. ~Core();
  87. // Called on both threads.
  88. bool IsCancelled() const;
  89. // Called on origin thread.
  90. void DoneOnOriginSequence(std::unique_ptr<DirectoryList> directory_list,
  91. int error) const;
  92. const base::FilePath dir_;
  93. const ListingType type_;
  94. const scoped_refptr<base::SequencedTaskRunner> origin_task_runner_;
  95. // Only used on the origin thread.
  96. raw_ptr<DirectoryLister> lister_;
  97. // Set to 1 on cancellation. Used both to abort listing files early on the
  98. // worker pool thread for performance reasons and to ensure |lister_| isn't
  99. // called after cancellation on the origin thread.
  100. base::subtle::Atomic32 cancelled_ = 0;
  101. };
  102. // Call into the corresponding DirectoryListerDelegate. Must not be called
  103. // after cancellation.
  104. void OnListFile(const DirectoryListerData& data);
  105. void OnListDone(int error);
  106. scoped_refptr<Core> core_;
  107. const raw_ptr<DirectoryListerDelegate> delegate_;
  108. };
  109. } // namespace net
  110. #endif // NET_BASE_DIRECTORY_LISTER_H_