Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 | #ifndef CROS_DISKS_DEVICE_EJECTOR_H_ |
| 6 | #define CROS_DISKS_DEVICE_EJECTOR_H_ |
| 7 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 8 | #include <map> |
Ben Chan | bba00ef | 2017-01-05 13:26:04 -0800 | [diff] [blame] | 9 | #include <memory> |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 10 | #include <string> |
| 11 | |
Simon Glass | 2b1da09 | 2020-05-21 12:24:16 -0600 | [diff] [blame] | 12 | #include <brillo/process/process_reaper.h> |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 13 | |
| 14 | #include "cros-disks/sandboxed_process.h" |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 15 | |
| 16 | namespace cros_disks { |
| 17 | |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 18 | // A class for ejecting any removable media on a device. |
| 19 | class DeviceEjector { |
| 20 | public: |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 21 | explicit DeviceEjector(brillo::ProcessReaper* process_reaper); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 22 | DeviceEjector(const DeviceEjector&) = delete; |
| 23 | DeviceEjector& operator=(const DeviceEjector&) = delete; |
| 24 | |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 25 | virtual ~DeviceEjector(); |
| 26 | |
| 27 | // Ejects any removable media on a device at |device_path| using the |
| 28 | // 'eject' program. Returns true if the eject process has launched |
| 29 | // successfully (but may not complete until OnEjectProcessTerminated |
| 30 | // is called). |
| 31 | virtual bool Eject(const std::string& device_path); |
| 32 | |
| 33 | private: |
| 34 | // Invoked when an eject process has terminated. |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 35 | void OnEjectProcessTerminated(const std::string& device_path, |
| 36 | const siginfo_t& info); |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 37 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 38 | brillo::ProcessReaper* process_reaper_; |
| 39 | |
| 40 | // A list of outstanding eject processes indexed by device path. |
| 41 | std::map<std::string, SandboxedProcess> eject_process_; |
| 42 | |
| 43 | base::WeakPtrFactory<DeviceEjector> weak_ptr_factory_; |
Ben Chan | 1e5a0cb | 2012-03-22 00:41:52 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | } // namespace cros_disks |
| 47 | |
| 48 | #endif // CROS_DISKS_DEVICE_EJECTOR_H_ |