blob: 73258ca132df44719b88ca51523235cc85709c66 [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
8#include <string>
9#include <vector>
10
Ben Chan5ccd9fe2013-11-13 18:28:27 -080011#include "cros-disks/dbus_adaptors/org.chromium.CrosDisks.h"
12#include "cros-disks/device_event_dispatcher_interface.h"
13#include "cros-disks/device_event_queue.h"
Ben Chanbdc39742011-05-11 17:51:26 -070014#include "cros-disks/disk.h"
Ben Chan5ccd9fe2013-11-13 18:28:27 -080015#include "cros-disks/format_manager_observer_interface.h"
Ben Chan8fb742b2014-04-28 23:46:57 -070016#include "cros-disks/mount_entry.h"
Ben Chan5ccd9fe2013-11-13 18:28:27 -080017#include "cros-disks/session_manager_observer_interface.h"
Ben Chanbdc39742011-05-11 17:51:26 -070018
Ryan Cairnsea6505f2011-04-10 19:54:53 -070019namespace cros_disks {
20
Ben Chanf51ff002011-04-25 12:41:57 -070021class DiskManager;
Szymon Sidor2733b512011-06-30 18:00:51 -070022class FormatManager;
Ben Chan8dcede82011-07-25 20:56:13 -070023class MountManager;
24class Platform;
Ben Chanf51ff002011-04-25 12:41:57 -070025
Ben Chan2a5ea752011-12-21 09:48:19 -080026struct DeviceEvent;
27
Ben Chanbdc39742011-05-11 17:51:26 -070028// The d-bus server for the cros-disks daemon.
Ryan Cairnsea6505f2011-04-10 19:54:53 -070029//
30// Example Usage:
Ben Chanbdc39742011-05-11 17:51:26 -070031//
Ryan Cairnsea6505f2011-04-10 19:54:53 -070032// DBus::Connection server_conn = DBus::Connection::SystemBus();
Ben Chana8661cb2014-04-17 11:38:02 -070033// CHECK(server_conn.acquire_name("org.chromium.CrosDisks"));
Ben Chan8dcede82011-07-25 20:56:13 -070034// ArchiveManager archive_manager(...);
35// DiskManager disk_manager(...);
Szymon Sidor2733b512011-06-30 18:00:51 -070036// FormatManager format_manager;
37// CrosDisksServer* server = new(std::nothrow)
Ben Chan8dcede82011-07-25 20:56:13 -070038// CrosDisksServer(server_conn, &platform,
Ben Chan8dcede82011-07-25 20:56:13 -070039// &disk_manager, &format_manager);
Ben Chanf47fc352013-05-03 11:28:57 -070040// server.RegisterMountManager(&disk_manager);
41// server.RegisterMountManager(&archive_manager);
Ben Chanbdc39742011-05-11 17:51:26 -070042//
Ryan Cairnsea6505f2011-04-10 19:54:53 -070043// At this point the server should be attached to the main loop.
44//
45class CrosDisksServer : public org::chromium::CrosDisks_adaptor,
46 public DBus::IntrospectableAdaptor,
Ben Chan6e726922011-06-28 15:54:32 -070047 public DBus::ObjectAdaptor,
Ben Chan89cf29e2011-08-10 13:11:05 -070048 public DeviceEventDispatcherInterface,
Ben Chanc1e766c2011-11-21 12:56:59 -080049 public FormatManagerObserverInterface,
Ben Chan89cf29e2011-08-10 13:11:05 -070050 public SessionManagerObserverInterface {
Ryan Cairnsea6505f2011-04-10 19:54:53 -070051 public:
Ben Chan6e726922011-06-28 15:54:32 -070052 CrosDisksServer(DBus::Connection& connection, // NOLINT
Ben Chan8dcede82011-07-25 20:56:13 -070053 Platform* platform,
Ben Chan8dcede82011-07-25 20:56:13 -070054 DiskManager* disk_manager,
55 FormatManager* format_manager);
Ben Chan55123552014-08-24 16:22:16 -070056 ~CrosDisksServer() override = default;
Ryan Cairnsea6505f2011-04-10 19:54:53 -070057
Ben Chanf47fc352013-05-03 11:28:57 -070058 // Registers a mount manager.
59 void RegisterMountManager(MountManager* mount_manager);
60
Ben Chanc1e766c2011-11-21 12:56:59 -080061 // A method for formatting a device specified by |path|.
62 // On completion, a FormatCompleted signal is emitted to indicate whether
63 // the operation succeeded or failed using a FormatErrorType enum value.
Ben Chan1c6c1942014-08-12 09:48:29 -070064 void Format(const std::string& path,
65 const std::string& filesystem_type,
66 const std::vector<std::string>& options,
67 DBus::Error& error) override; // NOLINT
Szymon Sidor2733b512011-06-30 18:00:51 -070068
Ryan Cairnsea6505f2011-04-10 19:54:53 -070069 // A method for checking if the daemon is running. Always returns true.
Ben Chan1c6c1942014-08-12 09:48:29 -070070 bool IsAlive(DBus::Error& error) override; // NOLINT
Ryan Cairnsea6505f2011-04-10 19:54:53 -070071
Ben Chand3fdc722011-07-21 18:15:22 -070072 // Mounts a path when invoked.
Ben Chan1c6c1942014-08-12 09:48:29 -070073 void Mount(const std::string& path,
74 const std::string& filesystem_type,
75 const std::vector<std::string>& options,
76 DBus::Error& error) override; // NOLINT
Ben Chand3fdc722011-07-21 18:15:22 -070077
78 // Unmounts a path when invoked.
Ben Chan1c6c1942014-08-12 09:48:29 -070079 void Unmount(const std::string& path,
80 const std::vector<std::string>& options,
81 DBus::Error& error) override; // NOLINT
Ben Chand3fdc722011-07-21 18:15:22 -070082
Ben Chan8f513762011-11-14 12:44:42 -080083 // Unmounts all paths mounted by Mount() when invoked.
Ben Chan1c6c1942014-08-12 09:48:29 -070084 void UnmountAll(DBus::Error& error) override; // NOLINT
Ben Chan8f513762011-11-14 12:44:42 -080085
Ben Chan490319f2011-05-06 14:00:42 -070086 // Returns a list of device sysfs paths for all disk devices attached to
Ben Chanf51ff002011-04-25 12:41:57 -070087 // the system.
Ben Chan1c6c1942014-08-12 09:48:29 -070088 std::vector<std::string> EnumerateDevices(
89 DBus::Error& error) override; // NOLINT
Ben Chan490319f2011-05-06 14:00:42 -070090
91 // Returns a list of device sysfs paths for all auto-mountable disk devices
92 // attached to the system. Currently, all external disk devices, which are
93 // neither on the boot device nor virtual, are considered auto-mountable.
Ben Chan1c6c1942014-08-12 09:48:29 -070094 std::vector<std::string> EnumerateAutoMountableDevices(
95 DBus::Error& error) override; // NOLINT
Ben Chanf51ff002011-04-25 12:41:57 -070096
Ben Chan8fb742b2014-04-28 23:46:57 -070097 // Returns a list of mount entries (<error type, source path, source type,
98 // mount path>) that are currently managed by cros-disks.
Ben Chan1c6c1942014-08-12 09:48:29 -070099 DBusMountEntries EnumerateMountEntries(DBus::Error& error) override; // NOLINT
Ben Chan8fb742b2014-04-28 23:46:57 -0700100
Ben Chanf51ff002011-04-25 12:41:57 -0700101 // Returns properties of a disk device attached to the system.
Ben Chan1c6c1942014-08-12 09:48:29 -0700102 DBusDisk GetDeviceProperties(const std::string& device_path,
103 DBus::Error& error) override; // NOLINT
Ben Chanf51ff002011-04-25 12:41:57 -0700104
Ben Chanc1e766c2011-11-21 12:56:59 -0800105 // Implements the FormatManagerObserverInterface interface to handle
106 // the event when a formatting operation has completed.
Ben Chan1c6c1942014-08-12 09:48:29 -0700107 void OnFormatCompleted(const std::string& device_path,
108 FormatErrorType error_type) override;
Ben Chanc1e766c2011-11-21 12:56:59 -0800109
Ben Chan89cf29e2011-08-10 13:11:05 -0700110 // Implements the SessionManagerObserverInterface interface to handle
Ben Chan5988f292012-09-18 08:32:42 -0700111 // the event when the screen is locked.
Ben Chan1c6c1942014-08-12 09:48:29 -0700112 void OnScreenIsLocked() override;
Ben Chan5988f292012-09-18 08:32:42 -0700113
114 // Implements the SessionManagerObserverInterface interface to handle
115 // the event when the screen is unlocked.
Ben Chan1c6c1942014-08-12 09:48:29 -0700116 void OnScreenIsUnlocked() override;
Ben Chan5988f292012-09-18 08:32:42 -0700117
118 // Implements the SessionManagerObserverInterface interface to handle
Ben Chan89cf29e2011-08-10 13:11:05 -0700119 // the event when the session has been started.
Ben Chan1c6c1942014-08-12 09:48:29 -0700120 void OnSessionStarted() override;
Ben Chan6e726922011-06-28 15:54:32 -0700121
Ben Chan89cf29e2011-08-10 13:11:05 -0700122 // Implements the SessionManagerObserverInterface interface to handle
123 // the event when the session has been stopped.
Ben Chan1c6c1942014-08-12 09:48:29 -0700124 void OnSessionStopped() override;
Ben Chan6e726922011-06-28 15:54:32 -0700125
Ben Chanf51ff002011-04-25 12:41:57 -0700126 private:
Ben Chan89cf29e2011-08-10 13:11:05 -0700127 // Implements the DeviceEventDispatcherInterface to dispatch a device event
128 // by emitting the corresponding D-Bus signal.
Ben Chan1c6c1942014-08-12 09:48:29 -0700129 void DispatchDeviceEvent(const DeviceEvent& event) override;
Ben Chan6e726922011-06-28 15:54:32 -0700130
Ben Chan490319f2011-05-06 14:00:42 -0700131 // Returns a list of device sysfs paths for all disk devices attached to
132 // the system. If auto_mountable_only is true, only auto-mountable disk
133 // devices are returned.
134 std::vector<std::string> DoEnumerateDevices(bool auto_mountable_only) const;
135
Tatsuhisa Yamaguchib670bd12016-09-28 23:06:44 +0900136 // Finds and returns a mounter which can mount |source_path|, or nullptr if no
137 // one can.
138 MountManager* FindMounter(const std::string& source_path) const;
139
Ben Chan8f513762011-11-14 12:44:42 -0800140 // Unmounts all paths mounted by Mount().
141 void DoUnmountAll();
142
Ben Chan8dcede82011-07-25 20:56:13 -0700143 Platform* platform_;
144
Ben Chanf51ff002011-04-25 12:41:57 -0700145 DiskManager* disk_manager_;
Ben Chan6e726922011-06-28 15:54:32 -0700146
Szymon Sidor2733b512011-06-30 18:00:51 -0700147 FormatManager* format_manager_;
148
Ben Chan8dcede82011-07-25 20:56:13 -0700149 std::vector<MountManager*> mount_managers_;
Ryan Cairnsea6505f2011-04-10 19:54:53 -0700150};
Ryan Cairnsea6505f2011-04-10 19:54:53 -0700151
Ben Chanbdc39742011-05-11 17:51:26 -0700152} // namespace cros_disks
153
Ben Chand4ae1b12014-07-03 00:01:58 -0700154#endif // CROS_DISKS_CROS_DISKS_SERVER_H_