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 | |
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 8 | #include <map> |
9 | |||||
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
11 | #include "base/files/file_path_watcher.h" | ||||
12 | #include "base/memory/weak_ptr.h" | ||||
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 13 | #include "base/sequence_checker.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 15 | |
Maksim Sisov | 764f01d | 2024-12-18 08:34:42 | [diff] [blame] | 16 | #if BUILDFLAG(IS_CHROMEOS) |
17 | #error "ChromeOS does not use MtabWatcherLinux." | ||||
18 | #endif | ||||
19 | |||||
[email protected] | 7bfe9460 | 2014-02-25 10:22:39 | [diff] [blame] | 20 | namespace storage_monitor { |
21 | |||||
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 22 | // MtabWatcherLinux listens for mount point changes from a mtab file and |
23 | // notifies a StorageMonitorLinux about them. This class should be created and | ||||
24 | // destroyed on a single sequence suitable for file IO. | ||||
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 25 | class MtabWatcherLinux { |
26 | public: | ||||
27 | // (mount point, mount device) | ||||
28 | // A mapping from mount point to mount device, as extracted from the mtab | ||||
29 | // file. | ||||
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 30 | using MountPointDeviceMap = std::map<base::FilePath, base::FilePath>; |
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 31 | |
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 32 | using UpdateMtabCallback = |
Ken Rockot | 51240d7 | 2019-12-18 03:16:13 | [diff] [blame] | 33 | base::RepeatingCallback<void(const MountPointDeviceMap& new_mtab)>; |
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 34 | |
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 35 | // |callback| is called on the same sequence as the rest of the class. |
36 | // Caller is responsible for bouncing to the correct sequence. | ||||
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 37 | MtabWatcherLinux(const base::FilePath& mtab_path, |
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 38 | const UpdateMtabCallback& callback); |
Peter Boström | 09c0182 | 2021-09-20 22:43:27 | [diff] [blame] | 39 | |
40 | MtabWatcherLinux(const MtabWatcherLinux&) = delete; | ||||
41 | MtabWatcherLinux& operator=(const MtabWatcherLinux&) = delete; | ||||
42 | |||||
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 43 | ~MtabWatcherLinux(); |
44 | |||||
45 | private: | ||||
46 | // Reads mtab file entries into |mtab|. | ||||
47 | void ReadMtab() const; | ||||
48 | |||||
49 | // Called when |mtab_path_| changes. | ||||
50 | void OnFilePathChanged(const base::FilePath& path, bool error); | ||||
51 | |||||
52 | // Mtab file that lists the mount points. | ||||
53 | const base::FilePath mtab_path_; | ||||
54 | |||||
55 | // Watcher for |mtab_path_|. | ||||
56 | base::FilePathWatcher file_watcher_; | ||||
57 | |||||
Tommy C. Li | 5731fee | 2017-07-17 17:37:49 | [diff] [blame] | 58 | UpdateMtabCallback callback_; |
59 | |||||
60 | SEQUENCE_CHECKER(sequence_checker_); | ||||
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 61 | |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 62 | base::WeakPtrFactory<MtabWatcherLinux> weak_ptr_factory_{this}; |
[email protected] | a4fec1a | 2013-04-03 04:43:38 | [diff] [blame] | 63 | }; |
64 | |||||
[email protected] | 7bfe9460 | 2014-02-25 10:22:39 | [diff] [blame] | 65 | } // namespace storage_monitor |
66 | |||||
[email protected] | e4c5f97b | 2014-02-17 18:57:17 | [diff] [blame] |