blob: 6bf217523f75d51ead9f4e5def55970c0455ba99 [file] [log] [blame]
Ryan Cairnsea6505f2011-04-10 19:54:53 -07001// 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 Chand4ae1b12014-07-03 00:01:58 -07005#ifndef CROS_DISKS_CROS_DISKS_SERVER_H_
6#define CROS_DISKS_CROS_DISKS_SERVER_H_
Ryan Cairnsea6505f2011-04-10 19:54:53 -07007
Ben Chanff92fa32017-10-17 16:17:15 -07008#include <map>
Omid Tourzan7943a9d2020-08-31 13:54:36 +10009#include <memory>
Ryan Cairnsea6505f2011-04-10 19:54:53 -070010#include <string>
Ben Chan445852f2017-10-02 23:00:16 -070011#include <tuple>
Ryan Cairnsea6505f2011-04-10 19:54:53 -070012#include <vector>
13
Omid Tourzan7943a9d2020-08-31 13:54:36 +100014#include <base/files/file_path.h>
Ben Chan445852f2017-10-02 23:00:16 -070015#include <base/memory/ref_counted.h>
16#include <brillo/dbus/dbus_object.h>
17#include <dbus/bus.h>
18
Ben Chan5ccd9fe2013-11-13 18:28:27 -080019#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 Chanbdc39742011-05-11 17:51:26 -070022#include "cros-disks/disk.h"
Ben Chan5ccd9fe2013-11-13 18:28:27 -080023#include "cros-disks/format_manager_observer_interface.h"
Ben Chan8fb742b2014-04-28 23:46:57 -070024#include "cros-disks/mount_entry.h"
Klemen Kozjekb0658852017-08-15 13:03:48 +090025#include "cros-disks/rename_manager_observer_interface.h"
Ben Chan5ccd9fe2013-11-13 18:28:27 -080026#include "cros-disks/session_manager_observer_interface.h"
Ben Chanbdc39742011-05-11 17:51:26 -070027
Ryan Cairnsea6505f2011-04-10 19:54:53 -070028namespace cros_disks {
29
Sergei Datsenko16821892019-04-05 11:26:38 +110030class DiskMonitor;
Szymon Sidor2733b512011-06-30 18:00:51 -070031class FormatManager;
Ben Chan8dcede82011-07-25 20:56:13 -070032class MountManager;
Omid Tourzan7943a9d2020-08-31 13:54:36 +100033class PartitionManager;
Ben Chan8dcede82011-07-25 20:56:13 -070034class Platform;
Klemen Kozjekb0658852017-08-15 13:03:48 +090035class RenameManager;
Ben Chanf51ff002011-04-25 12:41:57 -070036
Ben Chan2a5ea752011-12-21 09:48:19 -080037struct DeviceEvent;
38
Ben Chan445852f2017-10-02 23:00:16 -070039class CrosDisksServer : public org::chromium::CrosDisksAdaptor,
40 public org::chromium::CrosDisksInterface,
Ben Chan89cf29e2011-08-10 13:11:05 -070041 public DeviceEventDispatcherInterface,
Ben Chanc1e766c2011-11-21 12:56:59 -080042 public FormatManagerObserverInterface,
Klemen Kozjekb0658852017-08-15 13:03:48 +090043 public SessionManagerObserverInterface,
44 public RenameManagerObserverInterface {
Ryan Cairnsea6505f2011-04-10 19:54:53 -070045 public:
Ben Chan445852f2017-10-02 23:00:16 -070046 CrosDisksServer(scoped_refptr<dbus::Bus> bus,
Ben Chan8dcede82011-07-25 20:56:13 -070047 Platform* platform,
Sergei Datsenko16821892019-04-05 11:26:38 +110048 DiskMonitor* disk_monitor,
Klemen Kozjekb0658852017-08-15 13:03:48 +090049 FormatManager* format_manager,
Omid Tourzan7943a9d2020-08-31 13:54:36 +100050 PartitionManager* partition_manager,
Klemen Kozjekb0658852017-08-15 13:03:48 +090051 RenameManager* rename_manager);
Ben Chan55123552014-08-24 16:22:16 -070052 ~CrosDisksServer() override = default;
Ryan Cairnsea6505f2011-04-10 19:54:53 -070053
Ben Chan445852f2017-10-02 23:00:16 -070054 // Registers the D-Bus object and interfaces.
55 void RegisterAsync(
56 const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& cb);
57
Ben Chanf47fc352013-05-03 11:28:57 -070058 // Registers a mount manager.
59 void RegisterMountManager(MountManager* mount_manager);
60
Ben Chan445852f2017-10-02 23:00:16 -070061 // Implementation of org::chromium::CrosDisks:
62
Ben Chanc1e766c2011-11-21 12:56:59 -080063 // 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 Chan1c6c1942014-08-12 09:48:29 -070066 void Format(const std::string& path,
67 const std::string& filesystem_type,
Ben Chan445852f2017-10-02 23:00:16 -070068 const std::vector<std::string>& options) override;
Szymon Sidor2733b512011-06-30 18:00:51 -070069
Omid Tourzan7943a9d2020-08-31 13:54:36 +100070 // 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 Kozjekb0658852017-08-15 13:03:48 +090078 // 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 Chan445852f2017-10-02 23:00:16 -070081 void Rename(const std::string& path, const std::string& volume_name) override;
Klemen Kozjekb0658852017-08-15 13:03:48 +090082
Sergei Datsenko0f014d22018-04-04 16:37:22 +100083 // Mounts a source when invoked.
84 void Mount(const std::string& source,
Ben Chan1c6c1942014-08-12 09:48:29 -070085 const std::string& filesystem_type,
Ben Chan445852f2017-10-02 23:00:16 -070086 const std::vector<std::string>& options) override;
Ben Chand3fdc722011-07-21 18:15:22 -070087
88 // Unmounts a path when invoked.
Anand K Mistry36d2b302018-08-15 14:57:05 +100089 uint32_t Unmount(const std::string& path,
90 const std::vector<std::string>& options) override;
Ben Chand3fdc722011-07-21 18:15:22 -070091
Ben Chan8f513762011-11-14 12:44:42 -080092 // Unmounts all paths mounted by Mount() when invoked.
Ben Chan445852f2017-10-02 23:00:16 -070093 void UnmountAll() override;
Ben Chan8f513762011-11-14 12:44:42 -080094
Ben Chan490319f2011-05-06 14:00:42 -070095 // Returns a list of device sysfs paths for all disk devices attached to
Ben Chanf51ff002011-04-25 12:41:57 -070096 // the system.
Ben Chan445852f2017-10-02 23:00:16 -070097 std::vector<std::string> EnumerateDevices() override;
Ben Chan490319f2011-05-06 14:00:42 -070098
Ben Chan8fb742b2014-04-28 23:46:57 -070099 // Returns a list of mount entries (<error type, source path, source type,
100 // mount path>) that are currently managed by cros-disks.
Ben Chan0214e302017-10-17 15:39:16 -0700101 using DBusMountEntry =
Ben Chan445852f2017-10-02 23:00:16 -0700102 std::tuple<uint32_t, std::string, uint32_t, std::string>;
103 std::vector<DBusMountEntry> EnumerateMountEntries() override;
Ben Chan8fb742b2014-04-28 23:46:57 -0700104
Ben Chanf51ff002011-04-25 12:41:57 -0700105 // Returns properties of a disk device attached to the system.
Ben Chan445852f2017-10-02 23:00:16 -0700106 bool GetDeviceProperties(brillo::ErrorPtr* error,
107 const std::string& device_path,
108 brillo::VariantDictionary* properties) override;
Ben Chanf51ff002011-04-25 12:41:57 -0700109
Sergei Datsenkoad8b5042020-09-21 22:39:04 +1000110 // 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 Chanc1e766c2011-11-21 12:56:59 -0800115 // Implements the FormatManagerObserverInterface interface to handle
116 // the event when a formatting operation has completed.
Ben Chan1c6c1942014-08-12 09:48:29 -0700117 void OnFormatCompleted(const std::string& device_path,
118 FormatErrorType error_type) override;
Ben Chanc1e766c2011-11-21 12:56:59 -0800119
Omid Tourzan7943a9d2020-08-31 13:54:36 +1000120 // 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 Kozjekb0658852017-08-15 13:03:48 +0900127 // 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 Chan89cf29e2011-08-10 13:11:05 -0700132 // Implements the SessionManagerObserverInterface interface to handle
Ben Chan5988f292012-09-18 08:32:42 -0700133 // the event when the screen is locked.
Ben Chan1c6c1942014-08-12 09:48:29 -0700134 void OnScreenIsLocked() override;
Ben Chan5988f292012-09-18 08:32:42 -0700135
136 // Implements the SessionManagerObserverInterface interface to handle
137 // the event when the screen is unlocked.
Ben Chan1c6c1942014-08-12 09:48:29 -0700138 void OnScreenIsUnlocked() override;
Ben Chan5988f292012-09-18 08:32:42 -0700139
140 // Implements the SessionManagerObserverInterface interface to handle
Ben Chan89cf29e2011-08-10 13:11:05 -0700141 // the event when the session has been started.
Ben Chan1c6c1942014-08-12 09:48:29 -0700142 void OnSessionStarted() override;
Ben Chan6e726922011-06-28 15:54:32 -0700143
Ben Chan89cf29e2011-08-10 13:11:05 -0700144 // Implements the SessionManagerObserverInterface interface to handle
145 // the event when the session has been stopped.
Ben Chan1c6c1942014-08-12 09:48:29 -0700146 void OnSessionStopped() override;
Ben Chan6e726922011-06-28 15:54:32 -0700147
Ben Chanf51ff002011-04-25 12:41:57 -0700148 private:
Ben Chan89cf29e2011-08-10 13:11:05 -0700149 // Implements the DeviceEventDispatcherInterface to dispatch a device event
150 // by emitting the corresponding D-Bus signal.
Ben Chan1c6c1942014-08-12 09:48:29 -0700151 void DispatchDeviceEvent(const DeviceEvent& event) override;
Ben Chan6e726922011-06-28 15:54:32 -0700152
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +0900153 // 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 Chan445852f2017-10-02 23:00:16 -0700157 brillo::dbus_utils::DBusObject dbus_object_;
Ben Chan8dcede82011-07-25 20:56:13 -0700158 Platform* platform_;
Sergei Datsenko16821892019-04-05 11:26:38 +1100159 DiskMonitor* disk_monitor_;
Szymon Sidor2733b512011-06-30 18:00:51 -0700160 FormatManager* format_manager_;
Omid Tourzan7943a9d2020-08-31 13:54:36 +1000161 PartitionManager* partition_manager_;
Klemen Kozjekb0658852017-08-15 13:03:48 +0900162 RenameManager* rename_manager_;
Ben Chan8dcede82011-07-25 20:56:13 -0700163 std::vector<MountManager*> mount_managers_;
Ryan Cairnsea6505f2011-04-10 19:54:53 -0700164};
Ryan Cairnsea6505f2011-04-10 19:54:53 -0700165
Ben Chanbdc39742011-05-11 17:51:26 -0700166} // namespace cros_disks
167
Ben Chand4ae1b12014-07-03 00:01:58 -0700168#endif // CROS_DISKS_CROS_DISKS_SERVER_H_