blob: e92d86a6871515054c2c308be226c137819c45a4 [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 Chanbdc39742011-05-11 17:51:26 -07005#ifndef CROS_DISKS_SERVER_IMPL_H_
6#define CROS_DISKS_SERVER_IMPL_H_
Ryan Cairnsea6505f2011-04-10 19:54:53 -07007
8#include <string>
9#include <vector>
10
Ben Chanbdc39742011-05-11 17:51:26 -070011#include "cros-disks/cros-disks-server.h"
Ben Chan6e726922011-06-28 15:54:32 -070012#include "cros-disks/device-event-queue.h"
Ben Chanbdc39742011-05-11 17:51:26 -070013#include "cros-disks/disk.h"
Ben Chan6e726922011-06-28 15:54:32 -070014#include "cros-disks/power-manager-observer.h"
15#include "cros-disks/session-manager-observer.h"
Ben Chanbdc39742011-05-11 17:51:26 -070016
Ryan Cairnsea6505f2011-04-10 19:54:53 -070017namespace cros_disks {
18
Ben Chan8dcede82011-07-25 20:56:13 -070019class ArchiveManager;
Ben Chan6e726922011-06-28 15:54:32 -070020class DeviceEvent;
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 Chanbdc39742011-05-11 17:51:26 -070026// The d-bus server for the cros-disks daemon.
Ryan Cairnsea6505f2011-04-10 19:54:53 -070027//
28// Example Usage:
Ben Chanbdc39742011-05-11 17:51:26 -070029//
Ryan Cairnsea6505f2011-04-10 19:54:53 -070030// DBus::Connection server_conn = DBus::Connection::SystemBus();
31// server_conn.request_name("org.chromium.CrosDisks");
Ben Chan8dcede82011-07-25 20:56:13 -070032// ArchiveManager archive_manager(...);
33// DiskManager disk_manager(...);
Szymon Sidor2733b512011-06-30 18:00:51 -070034// FormatManager format_manager;
35// CrosDisksServer* server = new(std::nothrow)
Ben Chan8dcede82011-07-25 20:56:13 -070036// CrosDisksServer(server_conn, &platform,
37// &archive_manager,
38// &disk_manager, &format_manager);
Ben Chanbdc39742011-05-11 17:51:26 -070039//
Ryan Cairnsea6505f2011-04-10 19:54:53 -070040// At this point the server should be attached to the main loop.
41//
42class CrosDisksServer : public org::chromium::CrosDisks_adaptor,
43 public DBus::IntrospectableAdaptor,
Ben Chanb092d752011-07-13 11:44:38 -070044 public DBus::PropertiesAdaptor,
Ben Chan6e726922011-06-28 15:54:32 -070045 public DBus::ObjectAdaptor,
46 public PowerManagerObserver,
47 public SessionManagerObserver {
Ryan Cairnsea6505f2011-04-10 19:54:53 -070048 public:
Ben Chan6e726922011-06-28 15:54:32 -070049 CrosDisksServer(DBus::Connection& connection, // NOLINT
Ben Chan8dcede82011-07-25 20:56:13 -070050 Platform* platform,
51 ArchiveManager* archive_manager,
52 DiskManager* disk_manager,
53 FormatManager* format_manager);
Ryan Cairnsea6505f2011-04-10 19:54:53 -070054 virtual ~CrosDisksServer();
55
Szymon Sidor2733b512011-06-30 18:00:51 -070056 // 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 Cairnsea6505f2011-04-10 19:54:53 -070072 // A method for checking if the daemon is running. Always returns true.
73 virtual bool IsAlive(DBus::Error& error); // NOLINT
74
Ben Chand3fdc722011-07-21 18:15:22 -070075 // TODO(benchan): Deprecate this method.
Ryan Cairnsea6505f2011-04-10 19:54:53 -070076 // Unmounts a device when invoked.
Ben Chanf51ff002011-04-25 12:41:57 -070077 virtual void FilesystemUnmount(const std::string& device_path,
78 const std::vector<std::string>& mount_options,
Ben Chanbdc39742011-05-11 17:51:26 -070079 DBus::Error& error); // NOLINT
Ryan Cairnsea6505f2011-04-10 19:54:53 -070080
Ben Chand3fdc722011-07-21 18:15:22 -070081 // 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 Cairnsea6505f2011-04-10 19:54:53 -070093 // Mounts a device when invoked.
Ben Chanf51ff002011-04-25 12:41:57 -070094 virtual std::string FilesystemMount(const std::string& device_path,
95 const std::string& filesystem_type,
96 const std::vector<std::string>& mount_options,
Ben Chanbdc39742011-05-11 17:51:26 -070097 DBus::Error& error); // NOLINT
Ryan Cairnsea6505f2011-04-10 19:54:53 -070098
Ben Chan490319f2011-05-06 14:00:42 -070099 // Returns a list of device sysfs paths for all disk devices attached to
Ben Chanf51ff002011-04-25 12:41:57 -0700100 // the system.
Ben Chan490319f2011-05-06 14:00:42 -0700101 virtual std::vector<std::string> EnumerateDevices(
Ben Chanbdc39742011-05-11 17:51:26 -0700102 DBus::Error& error); // NOLINT
Ben Chan490319f2011-05-06 14:00:42 -0700103
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 Chanbdc39742011-05-11 17:51:26 -0700108 DBus::Error& error); // NOLINT
Ben Chanf51ff002011-04-25 12:41:57 -0700109
110 // Returns properties of a disk device attached to the system.
111 virtual DBusDisk GetDeviceProperties(const std::string& device_path,
Ben Chanbdc39742011-05-11 17:51:26 -0700112 DBus::Error& error); // NOLINT
Ben Chanf51ff002011-04-25 12:41:57 -0700113
114 // Emits appropriate DBus signals notifying device changes.
115 void SignalDeviceChanges();
116
Ben Chan6e726922011-06-28 15:54:32 -0700117 // 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 Chanf51ff002011-04-25 12:41:57 -0700133 private:
Ben Chan6e726922011-06-28 15:54:32 -0700134 // 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 Chanb092d752011-07-13 11:44:38 -0700141 // 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 Chan490319f2011-05-06 14:00:42 -0700149 // 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 Chan6e726922011-06-28 15:54:32 -0700154 // A list of deferred disk events to be fired.
155 DeviceEventQueue device_event_queue_;
156
Ben Chan8dcede82011-07-25 20:56:13 -0700157 Platform* platform_;
158
159 ArchiveManager* archive_manager_;
160
Ben Chanf51ff002011-04-25 12:41:57 -0700161 DiskManager* disk_manager_;
Ben Chan6e726922011-06-28 15:54:32 -0700162
Szymon Sidor2733b512011-06-30 18:00:51 -0700163 FormatManager* format_manager_;
164
Ben Chan8dcede82011-07-25 20:56:13 -0700165 std::vector<MountManager*> mount_managers_;
166
Ben Chan6e726922011-06-28 15:54:32 -0700167 // 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 Cairnsea6505f2011-04-10 19:54:53 -0700170};
Ryan Cairnsea6505f2011-04-10 19:54:53 -0700171
Ben Chanbdc39742011-05-11 17:51:26 -0700172} // namespace cros_disks
173
174#endif // CROS_DISKS_SERVER_IMPL_H_