Home | History | Annotate | Download | only in base
      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 
      5 #ifndef NET_BASE_DIRECTORY_LISTER_H_
      6 #define NET_BASE_DIRECTORY_LISTER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/files/file_enumerator.h"
     11 #include "base/files/file_path.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/message_loop/message_loop_proxy.h"
     14 #include "net/base/net_export.h"
     15 
     16 namespace net {
     17 
     18 //
     19 // This class provides an API for listing the contents of a directory on the
     20 // filesystem asynchronously.  It spawns a background thread, and enumerates
     21 // the specified directory on that thread.  It marshalls WIN32_FIND_DATA
     22 // structs over to the main application thread.  The consumer of this class
     23 // is insulated from any of the multi-threading details.
     24 //
     25 class NET_EXPORT DirectoryLister  {
     26  public:
     27   // Represents one file found.
     28   struct DirectoryListerData {
     29     base::FileEnumerator::FileInfo info;
     30     base::FilePath path;
     31   };
     32 
     33   // Implement this class to receive directory entries.
     34   class DirectoryListerDelegate {
     35    public:
     36     // Called for each file found by the lister.
     37     virtual void OnListFile(const DirectoryListerData& data) = 0;
     38 
     39     // Called when the listing is complete.
     40     virtual void OnListDone(int error) = 0;
     41 
     42    protected:
     43     virtual ~DirectoryListerDelegate() {}
     44   };
     45 
     46   // Sort options
     47   // ALPHA_DIRS_FIRST is the default sort :
     48   //   directories first in name order, then files by name order
     49   // FULL_PATH sorts by paths as strings, ignoring files v. directories
     50   // DATE sorts by last modified date
     51   enum SortType {
     52     NO_SORT,
     53     DATE,
     54     ALPHA_DIRS_FIRST,
     55     FULL_PATH
     56   };
     57 
     58   DirectoryLister(const base::FilePath& dir,
     59                   DirectoryListerDelegate* delegate);
     60 
     61   DirectoryLister(const base::FilePath& dir,
     62                   bool recursive,
     63                   SortType sort,
     64                   DirectoryListerDelegate* delegate);
     65 
     66   // Will invoke Cancel().
     67   ~DirectoryLister();
     68 
     69   // Call this method to start the directory enumeration thread.
     70   bool Start();
     71 
     72   // Call this method to asynchronously stop directory enumeration.  The
     73   // delegate will not be called back.
     74   void Cancel();
     75 
     76  private:
     77   class Core : public base::RefCountedThreadSafe<Core> {
     78    public:
     79     Core(const base::FilePath& dir,
     80          bool recursive,
     81          SortType sort,
     82          DirectoryLister* lister);
     83 
     84     bool Start();
     85 
     86     void Cancel();
     87 
     88    private:
     89     friend class base::RefCountedThreadSafe<Core>;
     90     class DataEvent;
     91 
     92     ~Core();
     93 
     94     // This method runs on a WorkerPool thread.
     95     void StartInternal();
     96 
     97     void SendData(const std::vector<DirectoryListerData>& data);
     98 
     99     void OnDone(int error);
    100 
    101     base::FilePath dir_;
    102     bool recursive_;
    103     SortType sort_;
    104     scoped_refptr<base::MessageLoopProxy> origin_loop_;
    105 
    106     // |lister_| gets set to NULL when canceled.
    107     DirectoryLister* lister_;
    108 
    109     DISALLOW_COPY_AND_ASSIGN(Core);
    110   };
    111 
    112   void OnReceivedData(const DirectoryListerData& data);
    113   void OnDone(int error);
    114 
    115   const scoped_refptr<Core> core_;
    116   DirectoryListerDelegate* const delegate_;
    117 
    118   DISALLOW_COPY_AND_ASSIGN(DirectoryLister);
    119 };
    120 
    121 }  // namespace net
    122 
    123 #endif  // NET_BASE_DIRECTORY_LISTER_H_
    124