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 | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 5 | #ifndef CROS_DISKS_SERVER_IMPL_H_ |
| 6 | #define CROS_DISKS_SERVER_IMPL_H_ |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 11 | #include "cros-disks/cros-disks-server.h" |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 12 | #include "cros-disks/device-event-queue.h" |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 13 | #include "cros-disks/disk.h" |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 14 | #include "cros-disks/power-manager-observer.h" |
| 15 | #include "cros-disks/session-manager-observer.h" |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 16 | |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 17 | namespace cros_disks { |
| 18 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 19 | class ArchiveManager; |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 20 | class DeviceEvent; |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 21 | class DiskManager; |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 22 | class FormatManager; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 23 | class MountManager; |
| 24 | class Platform; |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 25 | |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 26 | // The d-bus server for the cros-disks daemon. |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 27 | // |
| 28 | // Example Usage: |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 29 | // |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 30 | // DBus::Connection server_conn = DBus::Connection::SystemBus(); |
| 31 | // server_conn.request_name("org.chromium.CrosDisks"); |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 32 | // ArchiveManager archive_manager(...); |
| 33 | // DiskManager disk_manager(...); |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 34 | // FormatManager format_manager; |
| 35 | // CrosDisksServer* server = new(std::nothrow) |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 36 | // CrosDisksServer(server_conn, &platform, |
| 37 | // &archive_manager, |
| 38 | // &disk_manager, &format_manager); |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 39 | // |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 40 | // At this point the server should be attached to the main loop. |
| 41 | // |
| 42 | class CrosDisksServer : public org::chromium::CrosDisks_adaptor, |
| 43 | public DBus::IntrospectableAdaptor, |
Ben Chan | b092d75 | 2011-07-13 11:44:38 -0700 | [diff] [blame] | 44 | public DBus::PropertiesAdaptor, |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 45 | public DBus::ObjectAdaptor, |
| 46 | public PowerManagerObserver, |
| 47 | public SessionManagerObserver { |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 48 | public: |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 49 | CrosDisksServer(DBus::Connection& connection, // NOLINT |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 50 | Platform* platform, |
| 51 | ArchiveManager* archive_manager, |
| 52 | DiskManager* disk_manager, |
| 53 | FormatManager* format_manager); |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 54 | virtual ~CrosDisksServer(); |
| 55 | |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 56 | // Called by FormatManager when the formatting is finished |
| 57 | virtual void SignalFormattingFinished(const std::string& device_path, |
| 58 | int status); |
| 59 | |
| 60 | // Returns filesystem of a device. In case of any error (unrecognised device, |
| 61 | // unrecognised file system returns empty string |
| 62 | virtual std::string GetDeviceFilesystem(const std::string& device_path, |
| 63 | ::DBus::Error &error); // NOLINT |
| 64 | |
| 65 | // A method for asynchronous formating device using specified file system. |
| 66 | // Assumes device path is vaild (should it be invaild singal |
| 67 | // FormattingFinished(false) will be sent) |
| 68 | // Return true if formatting is successfully INITIALIZED, rather than finished |
| 69 | virtual bool FormatDevice(const std::string& device_path, |
| 70 | const std::string& filesystem, ::DBus::Error &error); // NOLINT |
| 71 | |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 72 | // A method for checking if the daemon is running. Always returns true. |
| 73 | virtual bool IsAlive(DBus::Error& error); // NOLINT |
| 74 | |
Ben Chan | d3fdc72 | 2011-07-21 18:15:22 -0700 | [diff] [blame] | 75 | // TODO(benchan): Deprecate this method. |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 76 | // Unmounts a device when invoked. |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 77 | virtual void FilesystemUnmount(const std::string& device_path, |
| 78 | const std::vector<std::string>& mount_options, |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 79 | DBus::Error& error); // NOLINT |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 80 | |
Ben Chan | d3fdc72 | 2011-07-21 18:15:22 -0700 | [diff] [blame] | 81 | // Mounts a path when invoked. |
| 82 | virtual void Mount(const std::string& path, |
| 83 | const std::string& filesystem_type, |
| 84 | const std::vector<std::string>& options, |
| 85 | DBus::Error& error); // NOLINT |
| 86 | |
| 87 | // Unmounts a path when invoked. |
| 88 | virtual void Unmount(const std::string& path, |
| 89 | const std::vector<std::string>& options, |
| 90 | DBus::Error& error); // NOLINT |
| 91 | |
| 92 | // TODO(benchan): Deprecate this method. |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 93 | // Mounts a device when invoked. |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 94 | virtual std::string FilesystemMount(const std::string& device_path, |
| 95 | const std::string& filesystem_type, |
| 96 | const std::vector<std::string>& mount_options, |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 97 | DBus::Error& error); // NOLINT |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 98 | |
Ben Chan | 490319f | 2011-05-06 14:00:42 -0700 | [diff] [blame] | 99 | // 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] | 100 | // the system. |
Ben Chan | 490319f | 2011-05-06 14:00:42 -0700 | [diff] [blame] | 101 | virtual std::vector<std::string> EnumerateDevices( |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 102 | DBus::Error& error); // NOLINT |
Ben Chan | 490319f | 2011-05-06 14:00:42 -0700 | [diff] [blame] | 103 | |
| 104 | // Returns a list of device sysfs paths for all auto-mountable disk devices |
| 105 | // attached to the system. Currently, all external disk devices, which are |
| 106 | // neither on the boot device nor virtual, are considered auto-mountable. |
| 107 | virtual std::vector<std::string> EnumerateAutoMountableDevices( |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 108 | DBus::Error& error); // NOLINT |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 109 | |
| 110 | // Returns properties of a disk device attached to the system. |
| 111 | virtual DBusDisk GetDeviceProperties(const std::string& device_path, |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 112 | DBus::Error& error); // NOLINT |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 113 | |
| 114 | // Emits appropriate DBus signals notifying device changes. |
| 115 | void SignalDeviceChanges(); |
| 116 | |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 117 | // Implements the PowerManagerObserver interface to handle the event |
| 118 | // when the screen is locked. |
| 119 | virtual void OnScreenIsLocked(); |
| 120 | |
| 121 | // Implements the PowerManagerObserver interface to handle the event |
| 122 | // when the screen is unlocked. |
| 123 | virtual void OnScreenIsUnlocked(); |
| 124 | |
| 125 | // Implements the SessionManagerObserver interface to handle the event |
| 126 | // when the session has been started. |
| 127 | virtual void OnSessionStarted(const std::string& user); |
| 128 | |
| 129 | // Implements the SessionManagerObserver interface to handle the event |
| 130 | // when the session has been stopped. |
| 131 | virtual void OnSessionStopped(const std::string& user); |
| 132 | |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 133 | private: |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 134 | // Dispatches a device event by emitting the corresponding D-Bus signal. |
| 135 | void DispatchDeviceEvent(const DeviceEvent& event); |
| 136 | |
| 137 | // Dispatches all queued device events by emitting the corresponding |
| 138 | // D-Bus signals. |
| 139 | void DispatchQueuedDeviceEvents(); |
| 140 | |
Ben Chan | b092d75 | 2011-07-13 11:44:38 -0700 | [diff] [blame] | 141 | // Initializes DBus properties. |
| 142 | void InitializeProperties(); |
| 143 | |
| 144 | // Overrides PropertiesAdaptor::on_set_property to handle |
| 145 | // org.freedesktop.DBus.Properties.Set calls. |
| 146 | virtual void on_set_property(DBus::InterfaceAdaptor& interface, // NOLINT |
| 147 | const std::string& property, const DBus::Variant& value); |
| 148 | |
Ben Chan | 490319f | 2011-05-06 14:00:42 -0700 | [diff] [blame] | 149 | // Returns a list of device sysfs paths for all disk devices attached to |
| 150 | // the system. If auto_mountable_only is true, only auto-mountable disk |
| 151 | // devices are returned. |
| 152 | std::vector<std::string> DoEnumerateDevices(bool auto_mountable_only) const; |
| 153 | |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 154 | // A list of deferred disk events to be fired. |
| 155 | DeviceEventQueue device_event_queue_; |
| 156 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 157 | Platform* platform_; |
| 158 | |
| 159 | ArchiveManager* archive_manager_; |
| 160 | |
Ben Chan | f51ff00 | 2011-04-25 12:41:57 -0700 | [diff] [blame] | 161 | DiskManager* disk_manager_; |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 162 | |
Szymon Sidor | 2733b51 | 2011-06-30 18:00:51 -0700 | [diff] [blame] | 163 | FormatManager* format_manager_; |
| 164 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame^] | 165 | std::vector<MountManager*> mount_managers_; |
| 166 | |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 167 | // This variable is set to true if any new device event should be queued |
| 168 | // instead of being dispatched immediately. |
| 169 | bool is_device_event_queued_; |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 170 | }; |
Ryan Cairns | ea6505f | 2011-04-10 19:54:53 -0700 | [diff] [blame] | 171 | |
Ben Chan | bdc3974 | 2011-05-11 17:51:26 -0700 | [diff] [blame] | 172 | } // namespace cros_disks |
| 173 | |
| 174 | #endif // CROS_DISKS_SERVER_IMPL_H_ |