Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |
Ben Chan | d4ae1b1 | 2014-07-03 00:01:58 -0700 | [diff] [blame] | 5 | #ifndef CROS_DISKS_CROS_DISKS_SERVER_H_ |
| 6 | #define CROS_DISKS_CROS_DISKS_SERVER_H_ |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 7 | |
Ben Chan | ff92fa3 | 2017-10-17 16:17:15 -0700 | [diff] [blame] | 8 | #include <map> |
Omid Tourzan | 7943a9d | 2020-08-31 13:54:36 +1000 | [diff] [blame] | 9 | #include <memory> |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 10 | #include <string> |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 11 | #include <tuple> |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Omid Tourzan | 7943a9d | 2020-08-31 13:54:36 +1000 | [diff] [blame] | 14 | #include <base/files/file_path.h> |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 15 | #include <base/memory/ref_counted.h> |
| 16 | #include <brillo/dbus/dbus_object.h> |
| 17 | #include <dbus/bus.h> |
| 18 | |
Ben Chan | 5ccd9fe | 2013-11-13 18:28:27 -0800 | [diff] [blame] | 19 | #include "cros-disks/dbus_adaptors/org.chromium.CrosDisks.h" |
| 20 | #include "cros-disks/device_event_dispatcher_interface.h" |
| 21 | #include "cros-disks/device_event_queue.h" |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 22 | #include "cros-disks/disk.h" |
Ben Chan | 5ccd9fe | 2013-11-13 18:28:27 -0800 | [diff] [blame] | 23 | #include "cros-disks/format_manager_observer_interface.h" |
Ben Chan | 8fb742b | 2014-04-28 23:46:57 -0700 | [diff] [blame] | 24 | #include "cros-disks/mount_entry.h" |
Klemen Kozjek | b065885 | 2017-08-15 13:03:48 +0900 | [diff] [blame] | 25 | #include "cros-disks/rename_manager_observer_interface.h" |
Ben Chan | 5ccd9fe | 2013-11-13 18:28:27 -0800 | [diff] [blame] | 26 | #include "cros-disks/session_manager_observer_interface.h" |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 27 | |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 28 | namespace cros_disks { |
| 29 | |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 30 | class DiskMonitor; |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 31 | class FormatManager; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 32 | class MountManager; |
Omid Tourzan | 7943a9d | 2020-08-31 13:54:36 +1000 | [diff] [blame] | 33 | class PartitionManager; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 34 | class Platform; |
Klemen Kozjek | b065885 | 2017-08-15 13:03:48 +0900 | [diff] [blame] | 35 | class RenameManager; |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 36 | |
Ben Chan | 2a5ea75 | 2011-12-21 09:48:19 -0800 | [diff] [blame] | 37 | struct DeviceEvent; |
| 38 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 39 | class CrosDisksServer : public org::chromium::CrosDisksAdaptor, |
| 40 | public org::chromium::CrosDisksInterface, |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 41 | public DeviceEventDispatcherInterface, |
Ben Chan | c1e766c | 2011-11-21 12:56:59 -0800 | [diff] [blame] | 42 | public FormatManagerObserverInterface, |
Klemen Kozjek | b065885 | 2017-08-15 13:03:48 +0900 | [diff] [blame] | 43 | public SessionManagerObserverInterface, |
| 44 | public RenameManagerObserverInterface { |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 45 | public: |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 46 | CrosDisksServer(scoped_refptr<dbus::Bus> bus, |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 47 | Platform* platform, |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 48 | DiskMonitor* disk_monitor, |
Klemen Kozjek | b065885 | 2017-08-15 13:03:48 +0900 | [diff] [blame] | 49 | FormatManager* format_manager, |
Omid Tourzan | 7943a9d | 2020-08-31 13:54:36 +1000 | [diff] [blame] | 50 | PartitionManager* partition_manager, |
Klemen Kozjek | b065885 | 2017-08-15 13:03:48 +0900 | [diff] [blame] | 51 | RenameManager* rename_manager); |
Ben Chan | 5512355 | 2014-08-24 16:22:16 -0700 | [diff] [blame] | 52 | ~CrosDisksServer() override = default; |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 53 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 54 | // Registers the D-Bus object and interfaces. |
| 55 | void RegisterAsync( |
| 56 | const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb); |
| 57 | |
Ben Chan | f47fc35 | 2013-05-03 11:28:57 -0700 | [diff] [blame] | 58 | // Registers a mount manager. |
| 59 | void RegisterMountManager(MountManager* mount_manager); |
| 60 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 61 | // Implementation of org::chromium::CrosDisks: |
| 62 | |
Ben Chan | c1e766c | 2011-11-21 12:56:59 -0800 | [diff] [blame] | 63 | // A method for formatting a device specified by |path|. |
| 64 | // On completion, a FormatCompleted signal is emitted to indicate whether |
| 65 | // the operation succeeded or failed using a FormatErrorType enum value. |
Ben Chan | 1c6c194 | 2014-08-12 09:48:29 -0700 | [diff] [blame] | 66 | void Format(const std::string& path, |
| 67 | const std::string& filesystem_type, |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 68 | const std::vector<std::string>& options) override; |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 69 | |
Omid Tourzan | 7943a9d | 2020-08-31 13:54:36 +1000 | [diff] [blame] | 70 | // Partitions a device into a single partition taking up the whole drive. |
| 71 | // Returns error code to indicate whether operation succeeded or failed using |
| 72 | // a PartitionErrorType enum value. |
| 73 | void SinglePartitionFormat( |
| 74 | std::unique_ptr<brillo::dbus_utils::DBusMethodResponse<uint32_t>> |
| 75 | response, |
| 76 | const std::string& path) override; |
| 77 | |
Klemen Kozjek | b065885 | 2017-08-15 13:03:48 +0900 | [diff] [blame] | 78 | // A method for renaming a device specified by |path|. |
| 79 | // On completion, a RenameCompleted signal is emitted to indicate whether |
| 80 | // the operation succeeded or failed using a RenameErrorType enum value. |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 81 | void Rename(const std::string& path, const std::string& volume_name) override; |
Klemen Kozjek | b065885 | 2017-08-15 13:03:48 +0900 | [diff] [blame] | 82 | |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 83 | // Mounts a source when invoked. |
| 84 | void Mount(const std::string& source, |
Ben Chan | 1c6c194 | 2014-08-12 09:48:29 -0700 | [diff] [blame] | 85 | const std::string& filesystem_type, |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 86 | const std::vector<std::string>& options) override; |
Ben Chan | d3fdc72 | 2011-07-21 18:15:22 -0700 | [diff] [blame] | 87 | |
| 88 | // Unmounts a path when invoked. |
Anand K Mistry | 36d2b30 | 2018-08-15 14:57:05 +1000 | [diff] [blame] | 89 | uint32_t Unmount(const std::string& path, |
| 90 | const std::vector<std::string>& options) override; |
Ben Chan | d3fdc72 | 2011-07-21 18:15:22 -0700 | [diff] [blame] | 91 | |
Ben Chan | 8f51376 | 2011-11-14 12:44:42 -0800 | [diff] [blame] | 92 | // Unmounts all paths mounted by Mount() when invoked. |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 93 | void UnmountAll() override; |
Ben Chan | 8f51376 | 2011-11-14 12:44:42 -0800 | [diff] [blame] | 94 | |
Ben Chan | 490319f | 2011-05-06 14:00:42 -0700 | [diff] [blame] | 95 | // Returns a list of device sysfs paths for all disk devices attached to |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 96 | // the system. |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 97 | std::vector<std::string> EnumerateDevices() override; |
Ben Chan | 490319f | 2011-05-06 14:00:42 -0700 | [diff] [blame] | 98 | |
Ben Chan | 8fb742b | 2014-04-28 23:46:57 -0700 | [diff] [blame] | 99 | // Returns a list of mount entries (<error type, source path, source type, |
| 100 | // mount path>) that are currently managed by cros-disks. |
Ben Chan | 0214e30 | 2017-10-17 15:39:16 -0700 | [diff] [blame] | 101 | using DBusMountEntry = |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 102 | std::tuple<uint32_t, std::string, uint32_t, std::string>; |
| 103 | std::vector<DBusMountEntry> EnumerateMountEntries() override; |
Ben Chan | 8fb742b | 2014-04-28 23:46:57 -0700 | [diff] [blame] | 104 | |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 105 | // Returns properties of a disk device attached to the system. |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 106 | bool GetDeviceProperties(brillo::ErrorPtr* error, |
| 107 | const std::string& device_path, |
| 108 | brillo::VariantDictionary* properties) override; |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 109 | |
Sergei Datsenko | ad8b504 | 2020-09-21 22:39:04 +1000 | [diff] [blame] | 110 | // Used in tests to allow loopback devices to be used for operations. |
| 111 | // |device_path| specifies the syspath of the device (e.g. /sys/devices/...). |
| 112 | void AddDeviceToAllowlist(const std::string& device_path) override; |
| 113 | void RemoveDeviceFromAllowlist(const std::string& device_path) override; |
| 114 | |
Ben Chan | c1e766c | 2011-11-21 12:56:59 -0800 | [diff] [blame] | 115 | // Implements the FormatManagerObserverInterface interface to handle |
| 116 | // the event when a formatting operation has completed. |
Ben Chan | 1c6c194 | 2014-08-12 09:48:29 -0700 | [diff] [blame] | 117 | void OnFormatCompleted(const std::string& device_path, |
| 118 | FormatErrorType error_type) override; |
Ben Chan | c1e766c | 2011-11-21 12:56:59 -0800 | [diff] [blame] | 119 | |
Omid Tourzan | 7943a9d | 2020-08-31 13:54:36 +1000 | [diff] [blame] | 120 | // The callback called when a partitioning operation has completed. |
| 121 | void OnPartitionCompleted( |
| 122 | std::unique_ptr<brillo::dbus_utils::DBusMethodResponse<uint32_t>> |
| 123 | response, |
| 124 | const base::FilePath& device_path, |
| 125 | PartitionErrorType error_type); |
| 126 | |
Klemen Kozjek | b065885 | 2017-08-15 13:03:48 +0900 | [diff] [blame] | 127 | // Implements the RenameManagerObserverInterface interface to handle |
| 128 | // the event when a renaming operation has completed. |
| 129 | void OnRenameCompleted(const std::string& device_path, |
| 130 | RenameErrorType error_type) override; |
| 131 | |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 132 | // Implements the SessionManagerObserverInterface interface to handle |
Ben Chan | 5988f29 | 2012-09-18 08:32:42 -0700 | [diff] [blame] | 133 | // the event when the screen is locked. |
Ben Chan | 1c6c194 | 2014-08-12 09:48:29 -0700 | [diff] [blame] | 134 | void OnScreenIsLocked() override; |
Ben Chan | 5988f29 | 2012-09-18 08:32:42 -0700 | [diff] [blame] | 135 | |
| 136 | // Implements the SessionManagerObserverInterface interface to handle |
| 137 | // the event when the screen is unlocked. |
Ben Chan | 1c6c194 | 2014-08-12 09:48:29 -0700 | [diff] [blame] | 138 | void OnScreenIsUnlocked() override; |
Ben Chan | 5988f29 | 2012-09-18 08:32:42 -0700 | [diff] [blame] | 139 | |
| 140 | // Implements the SessionManagerObserverInterface interface to handle |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 141 | // the event when the session has been started. |
Ben Chan | 1c6c194 | 2014-08-12 09:48:29 -0700 | [diff] [blame] | 142 | void OnSessionStarted() override; |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 143 | |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 144 | // Implements the SessionManagerObserverInterface interface to handle |
| 145 | // the event when the session has been stopped. |
Ben Chan | 1c6c194 | 2014-08-12 09:48:29 -0700 | [diff] [blame] | 146 | void OnSessionStopped() override; |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 147 | |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 148 | private: |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 149 | // Implements the DeviceEventDispatcherInterface to dispatch a device event |
| 150 | // by emitting the corresponding D-Bus signal. |
Ben Chan | 1c6c194 | 2014-08-12 09:48:29 -0700 | [diff] [blame] | 151 | void DispatchDeviceEvent(const DeviceEvent& event) override; |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 152 | |
Tatsuhisa Yamaguchi | b670bd1 | 2016-09-28 23:06:44 +0900 | [diff] [blame] | 153 | // Finds and returns a mounter which can mount |source_path|, or nullptr if no |
| 154 | // one can. |
| 155 | MountManager* FindMounter(const std::string& source_path) const; |
| 156 | |
Ben Chan | 445852f | 2017-10-02 23:00:16 -0700 | [diff] [blame] | 157 | brillo::dbus_utils::DBusObject dbus_object_; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 158 | Platform* platform_; |
Sergei Datsenko | 1682189 | 2019-04-05 11:26:38 +1100 | [diff] [blame] | 159 | DiskMonitor* disk_monitor_; |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 160 | FormatManager* format_manager_; |
Omid Tourzan | 7943a9d | 2020-08-31 13:54:36 +1000 | [diff] [blame] | 161 | PartitionManager* partition_manager_; |
Klemen Kozjek | b065885 | 2017-08-15 13:03:48 +0900 | [diff] [blame] | 162 | RenameManager* rename_manager_; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 163 | std::vector<MountManager*> mount_managers_; |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 164 | }; |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 165 | |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 166 | } // namespace cros_disks |
| 167 | |
Ben Chan | d4ae1b1 | 2014-07-03 00:01:58 -0700 | [diff] [blame] | 168 | #endif // CROS_DISKS_CROS_DISKS_SERVER_H_ |