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