blob: 69687f61816bf47c99e392fe809e3534ec35f38f [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2014 The Chromium Authors
[email protected]a4fec1a2013-04-03 04:43:382// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e4c5f97b2014-02-17 18:57:175#ifndef COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_
6#define COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_
[email protected]a4fec1a2013-04-03 04:43:387
Yuta Hijikataba103462020-11-30 08:33:358#include "build/chromeos_buildflags.h"
9
10#if BUILDFLAG(IS_CHROMEOS_ASH)
[email protected]a4fec1a2013-04-03 04:43:3811#error "ChromeOS does not use MtabWatcherLinux."
12#endif
13
14#include <map>
15
[email protected]a4fec1a2013-04-03 04:43:3816#include "base/files/file_path.h"
17#include "base/files/file_path_watcher.h"
18#include "base/memory/weak_ptr.h"
Tommy C. Li5731fee2017-07-17 17:37:4919#include "base/sequence_checker.h"
avi5dd91f82015-12-25 22:30:4620#include "build/build_config.h"
[email protected]a4fec1a2013-04-03 04:43:3821
[email protected]7bfe94602014-02-25 10:22:3922namespace storage_monitor {
23
Tommy C. Li5731fee2017-07-17 17:37:4924// MtabWatcherLinux listens for mount point changes from a mtab file and
25// notifies a StorageMonitorLinux about them. This class should be created and
26// destroyed on a single sequence suitable for file IO.
[email protected]a4fec1a2013-04-03 04:43:3827class MtabWatcherLinux {
28 public:
29 // (mount point, mount device)
30 // A mapping from mount point to mount device, as extracted from the mtab
31 // file.
Tommy C. Li5731fee2017-07-17 17:37:4932 using MountPointDeviceMap = std::map<base::FilePath, base::FilePath>;
[email protected]a4fec1a2013-04-03 04:43:3833
Tommy C. Li5731fee2017-07-17 17:37:4934 using UpdateMtabCallback =
Ken Rockot51240d72019-12-18 03:16:1335 base::RepeatingCallback<void(const MountPointDeviceMap& new_mtab)>;
[email protected]a4fec1a2013-04-03 04:43:3836
Tommy C. Li5731fee2017-07-17 17:37:4937 // |callback| is called on the same sequence as the rest of the class.
38 // Caller is responsible for bouncing to the correct sequence.
[email protected]a4fec1a2013-04-03 04:43:3839 MtabWatcherLinux(const base::FilePath& mtab_path,
Tommy C. Li5731fee2017-07-17 17:37:4940 const UpdateMtabCallback& callback);
Peter Boström09c01822021-09-20 22:43:2741
42 MtabWatcherLinux(const MtabWatcherLinux&) = delete;
43 MtabWatcherLinux& operator=(const MtabWatcherLinux&) = delete;
44
[email protected]a4fec1a2013-04-03 04:43:3845 ~MtabWatcherLinux();
46
47 private:
48 // Reads mtab file entries into |mtab|.
49 void ReadMtab() const;
50
51 // Called when |mtab_path_| changes.
52 void OnFilePathChanged(const base::FilePath& path, bool error);
53
54 // Mtab file that lists the mount points.
55 const base::FilePath mtab_path_;
56
57 // Watcher for |mtab_path_|.
58 base::FilePathWatcher file_watcher_;
59
Tommy C. Li5731fee2017-07-17 17:37:4960 UpdateMtabCallback callback_;
61
62 SEQUENCE_CHECKER(sequence_checker_);
[email protected]a4fec1a2013-04-03 04:43:3863
Jeremy Roman5c341f6d2019-07-15 15:56:1064 base::WeakPtrFactory<MtabWatcherLinux> weak_ptr_factory_{this};
[email protected]a4fec1a2013-04-03 04:43:3865};
66
[email protected]7bfe94602014-02-25 10:22:3967} // namespace storage_monitor
68
[email protected]e4c5f97b2014-02-17 18:57:1769#endif // COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_