Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame^] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | e4c5f97b | 2014-02-17 18:57:17 | [diff] [blame] | 5 | #ifndef COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_ |
6 | #define COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_ | ||||
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 7 | |
Yuta Hijikata | ba10346 | 2020-11-30 08:33:35 | [diff] [blame] | 8 | #include "build/chromeos_buildflags.h" |
9 | |||||
10 | #if BUILDFLAG(IS_CHROMEOS_ASH) | ||||
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 11 | #error "ChromeOS does not use MtabWatcherLinux." |
12 | #endif | ||||
13 | |||||
14 | #include <map> | ||||
15 | |||||
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 16 | #include "base/files/file_path.h" |
17 | #include "base/files/file_path_watcher.h" | ||||
18 | #include "base/memory/weak_ptr.h" | ||||
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 19 | #include "base/sequence_checker.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 20 | #include "build/build_config.h" |
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 21 | |
[email protected] | 7bfe9460 | 2014-02-25 10:22:39 | [diff] [blame] | 22 | namespace storage_monitor { |
23 | |||||
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 24 | // 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] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 27 | class 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. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 32 | using MountPointDeviceMap = std::map<base::FilePath, base::FilePath>; |
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 33 | |
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 34 | using UpdateMtabCallback = |
Ken Rockot | 51240d7 | 2019-12-18 03:16:13 | [diff] [blame] | 35 | base::RepeatingCallback<void(const MountPointDeviceMap& new_mtab)>; |
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 36 | |
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 37 | // |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] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 39 | MtabWatcherLinux(const base::FilePath& mtab_path, |
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 40 | const UpdateMtabCallback& callback); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 41 | |
42 | MtabWatcherLinux(const MtabWatcherLinux&) = delete; | ||||
43 | MtabWatcherLinux& operator=(const MtabWatcherLinux&) = delete; | ||||
44 | |||||
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 45 | ~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. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 60 | UpdateMtabCallback callback_; |
61 | |||||
62 | SEQUENCE_CHECKER(sequence_checker_); | ||||
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 63 | |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 64 | base::WeakPtrFactory<MtabWatcherLinux> weak_ptr_factory_{this}; |
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 65 | }; |
66 | |||||
[email protected] | 7bfe9460 | 2014-02-25 10:22:39 | [diff] [blame] | 67 | } // namespace storage_monitor |
68 | |||||
[email protected] | e4c5f97b | 2014-02-17 18:57:17 | [diff] [blame] | 69 | #endif // COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_ |