blob: 951a08b5d384f2bdd276efbd61eb72f98ccd3fb6 [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 Chan6e726922011-06-28 15:54:32 -070019class DeviceEvent;
Ben Chanf51ff002011-04-25 12:41:57 -070020class DiskManager;
Szymon Sidor2733b512011-06-30 18:00:51 -070021class FormatManager;
Ben Chanf51ff002011-04-25 12:41:57 -070022
Ben Chanbdc39742011-05-11 17:51:26 -070023// The d-bus server for the cros-disks daemon.
Ryan Cairnsea6505f2011-04-10 19:54:53 -070024//
25// Example Usage:
Ben Chanbdc39742011-05-11 17:51:26 -070026//
Ryan Cairnsea6505f2011-04-10 19:54:53 -070027// DBus::Connection server_conn = DBus::Connection::SystemBus();
28// server_conn.request_name("org.chromium.CrosDisks");
Szymon Sidor2733b512011-06-30 18:00:51 -070029// DiskManager disk_manager;
30// FormatManager format_manager;
31// CrosDisksServer* server = new(std::nothrow)
32// CrosDisksServer(server_conn, &disk_manager, &format_manager);
Ben Chanbdc39742011-05-11 17:51:26 -070033//
Ryan Cairnsea6505f2011-04-10 19:54:53 -070034// At this point the server should be attached to the main loop.
35//
36class CrosDisksServer : public org::chromium::CrosDisks_adaptor,
37 public DBus::IntrospectableAdaptor,
Ben Chanb092d752011-07-13 11:44:38 -070038 public DBus::PropertiesAdaptor,
Ben Chan6e726922011-06-28 15:54:32 -070039 public DBus::ObjectAdaptor,
40 public PowerManagerObserver,
41 public SessionManagerObserver {
Ryan Cairnsea6505f2011-04-10 19:54:53 -070042 public:
Ben Chan6e726922011-06-28 15:54:32 -070043 CrosDisksServer(DBus::Connection& connection, // NOLINT
Szymon Sidor2733b512011-06-30 18:00:51 -070044 DiskManager* disk_manager,
45 FormatManager* format_manager);
Ryan Cairnsea6505f2011-04-10 19:54:53 -070046 virtual ~CrosDisksServer();
47
Szymon Sidor2733b512011-06-30 18:00:51 -070048 // 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 Cairnsea6505f2011-04-10 19:54:53 -070064 // A method for checking if the daemon is running. Always returns true.
65 virtual bool IsAlive(DBus::Error& error); // NOLINT
66
Ben Chand3fdc722011-07-21 18:15:22 -070067 // TODO(benchan): Deprecate this method.
Ryan Cairnsea6505f2011-04-10 19:54:53 -070068 // Unmounts a device when invoked.
Ben Chanf51ff002011-04-25 12:41:57 -070069 virtual void FilesystemUnmount(const std::string& device_path,
70 const std::vector<std::string>& mount_options,
Ben Chanbdc39742011-05-11 17:51:26 -070071 DBus::Error& error); // NOLINT
Ryan Cairnsea6505f2011-04-10 19:54:53 -070072
Ben Chand3fdc722011-07-21 18:15:22 -070073 // 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 Cairnsea6505f2011-04-10 19:54:53 -070085 // Mounts a device when invoked.
Ben Chanf51ff002011-04-25 12:41:57 -070086 virtual std::string FilesystemMount(const std::string& device_path,
87 const std::string& filesystem_type,
88 const std::vector<std::string>& mount_options,
Ben Chanbdc39742011-05-11 17:51:26 -070089 DBus::Error& error); // NOLINT
Ryan Cairnsea6505f2011-04-10 19:54:53 -070090
Ben Chan490319f2011-05-06 14:00:42 -070091 // Returns a list of device sysfs paths for all disk devices attached to
Ben Chanf51ff002011-04-25 12:41:57 -070092 // the system.
Ben Chan490319f2011-05-06 14:00:42 -070093 virtual std::vector<std::string> EnumerateDevices(
Ben Chanbdc39742011-05-11 17:51:26 -070094 DBus::Error& error); // NOLINT
Ben Chan490319f2011-05-06 14:00:42 -070095
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 Chanbdc39742011-05-11 17:51:26 -0700100 DBus::Error& error); // NOLINT
Ben Chanf51ff002011-04-25 12:41:57 -0700101
102 // Returns properties of a disk device attached to the system.
103 virtual DBusDisk GetDeviceProperties(const std::string& device_path,
Ben Chanbdc39742011-05-11 17:51:26 -0700104 DBus::Error& error); // NOLINT
Ben Chanf51ff002011-04-25 12:41:57 -0700105
106 // Emits appropriate DBus signals notifying device changes.
107 void SignalDeviceChanges();
108
Ben Chan6e726922011-06-28 15:54:32 -0700109 // 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 Chanf51ff002011-04-25 12:41:57 -0700125 private:
Ben Chan6e726922011-06-28 15:54:32 -0700126 // 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 Chanb092d752011-07-13 11:44:38 -0700133 // 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 Chan490319f2011-05-06 14:00:42 -0700141 // 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 Chan6e726922011-06-28 15:54:32 -0700146 // A list of deferred disk events to be fired.
147 DeviceEventQueue device_event_queue_;
148
Ben Chanf51ff002011-04-25 12:41:57 -0700149 DiskManager* disk_manager_;
Ben Chan6e726922011-06-28 15:54:32 -0700150
Szymon Sidor2733b512011-06-30 18:00:51 -0700151 FormatManager* format_manager_;
152
Ben Chan6e726922011-06-28 15:54:32 -0700153 // 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 Cairnsea6505f2011-04-10 19:54:53 -0700156};
Ryan Cairnsea6505f2011-04-10 19:54:53 -0700157
Ben Chanbdc39742011-05-11 17:51:26 -0700158} // namespace cros_disks
159
160#endif // CROS_DISKS_SERVER_IMPL_H_