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 5 // MtabWatcherLinux listens for mount point changes from a mtab file and 6 // notifies a StorageMonitorLinux about them. 7 // MtabWatcherLinux lives on the FILE thread. 8 9 #ifndef COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_ 10 #define COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_ 11 12 #if defined(OS_CHROMEOS) 13 #error "ChromeOS does not use MtabWatcherLinux." 14 #endif 15 16 #include <map> 17 18 #include "base/basictypes.h" 19 #include "base/files/file_path.h" 20 #include "base/files/file_path_watcher.h" 21 #include "base/memory/weak_ptr.h" 22 23 namespace storage_monitor { 24 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. 30 typedef std::map<base::FilePath, base::FilePath> MountPointDeviceMap; 31 32 class Delegate { 33 public: 34 virtual ~Delegate() {} 35 36 // Parses |new_mtab| and find all changes. Called on the UI thread. 37 virtual void UpdateMtab(const MountPointDeviceMap& new_mtab) = 0; 38 }; 39 40 MtabWatcherLinux(const base::FilePath& mtab_path, 41 base::WeakPtr<Delegate> delegate); 42 ~MtabWatcherLinux(); 43 44 private: 45 // Reads mtab file entries into |mtab|. 46 void ReadMtab() const; 47 48 // Called when |mtab_path_| changes. 49 void OnFilePathChanged(const base::FilePath& path, bool error); 50 51 // Mtab file that lists the mount points. 52 const base::FilePath mtab_path_; 53 54 // Watcher for |mtab_path_|. 55 base::FilePathWatcher file_watcher_; 56 57 base::WeakPtr<Delegate> delegate_; 58 59 base::WeakPtrFactory<MtabWatcherLinux> weak_ptr_factory_; 60 61 DISALLOW_COPY_AND_ASSIGN(MtabWatcherLinux); 62 }; 63 64 } // namespace storage_monitor 65 66 #endif // COMPONENTS_STORAGE_MONITOR_MTAB_WATCHER_LINUX_H_ 67