mtab_watcher_linux.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Copyright 2014 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 COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_
  5. #define COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_
  6. #include "build/chromeos_buildflags.h"
  7. #if BUILDFLAG(IS_CHROMEOS_ASH)
  8. #error "ChromeOS does not use MtabWatcherLinux."
  9. #endif
  10. #include <map>
  11. #include "base/files/file_path.h"
  12. #include "base/files/file_path_watcher.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/sequence_checker.h"
  15. #include "build/build_config.h"
  16. #include "build/chromeos_buildflags.h"
  17. namespace storage_monitor {
  18. // MtabWatcherLinux listens for mount point changes from a mtab file and
  19. // notifies a StorageMonitorLinux about them. This class should be created and
  20. // destroyed on a single sequence suitable for file IO.
  21. class MtabWatcherLinux {
  22. public:
  23. // (mount point, mount device)
  24. // A mapping from mount point to mount device, as extracted from the mtab
  25. // file.
  26. using MountPointDeviceMap = std::map<base::FilePath, base::FilePath>;
  27. using UpdateMtabCallback =
  28. base::RepeatingCallback<void(const MountPointDeviceMap& new_mtab)>;
  29. // |callback| is called on the same sequence as the rest of the class.
  30. // Caller is responsible for bouncing to the correct sequence.
  31. MtabWatcherLinux(const base::FilePath& mtab_path,
  32. const UpdateMtabCallback& callback);
  33. MtabWatcherLinux(const MtabWatcherLinux&) = delete;
  34. MtabWatcherLinux& operator=(const MtabWatcherLinux&) = delete;
  35. ~MtabWatcherLinux();
  36. private:
  37. // Reads mtab file entries into |mtab|.
  38. void ReadMtab() const;
  39. // Called when |mtab_path_| changes.
  40. void OnFilePathChanged(const base::FilePath& path, bool error);
  41. // Mtab file that lists the mount points.
  42. const base::FilePath mtab_path_;
  43. // Watcher for |mtab_path_|.
  44. base::FilePathWatcher file_watcher_;
  45. UpdateMtabCallback callback_;
  46. SEQUENCE_CHECKER(sequence_checker_);
  47. base::WeakPtrFactory<MtabWatcherLinux> weak_ptr_factory_{this};
  48. };
  49. } // namespace storage_monitor
  50. #endif // COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_