blob: b5ecd65cda3b9c4392c032341fe82c486b9f4e32 [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;
21
Ben Chanbdc39742011-05-11 17:51:26 -070022// The d-bus server for the cros-disks daemon.
Ryan Cairnsea6505f2011-04-10 19:54:53 -070023//
24// Example Usage:
Ben Chanbdc39742011-05-11 17:51:26 -070025//
Ryan Cairnsea6505f2011-04-10 19:54:53 -070026// DBus::Connection server_conn = DBus::Connection::SystemBus();
27// server_conn.request_name("org.chromium.CrosDisks");
Ben Chanbdc39742011-05-11 17:51:26 -070028// DiskManager manager;
29// CrosDisksServer* server = new(std::nothrow) CrosDisksServer(server_conn,
30// &manager);
31//
Ryan Cairnsea6505f2011-04-10 19:54:53 -070032// At this point the server should be attached to the main loop.
33//
34class CrosDisksServer : public org::chromium::CrosDisks_adaptor,
35 public DBus::IntrospectableAdaptor,
Ben Chan6e726922011-06-28 15:54:32 -070036 public DBus::ObjectAdaptor,
37 public PowerManagerObserver,
38 public SessionManagerObserver {
Ryan Cairnsea6505f2011-04-10 19:54:53 -070039 public:
Ben Chan6e726922011-06-28 15:54:32 -070040 CrosDisksServer(DBus::Connection& connection, // NOLINT
41 DiskManager* disk_manager);
Ryan Cairnsea6505f2011-04-10 19:54:53 -070042 virtual ~CrosDisksServer();
43
44 // A method for checking if the daemon is running. Always returns true.
45 virtual bool IsAlive(DBus::Error& error); // NOLINT
46
47 // Unmounts a device when invoked.
Ben Chanf51ff002011-04-25 12:41:57 -070048 virtual void FilesystemUnmount(const std::string& device_path,
49 const std::vector<std::string>& mount_options,
Ben Chanbdc39742011-05-11 17:51:26 -070050 DBus::Error& error); // NOLINT
Ryan Cairnsea6505f2011-04-10 19:54:53 -070051
52 // Mounts a device when invoked.
Ben Chanf51ff002011-04-25 12:41:57 -070053 virtual std::string FilesystemMount(const std::string& device_path,
54 const std::string& filesystem_type,
55 const std::vector<std::string>& mount_options,
Ben Chanbdc39742011-05-11 17:51:26 -070056 DBus::Error& error); // NOLINT
Ryan Cairnsea6505f2011-04-10 19:54:53 -070057
Ben Chan490319f2011-05-06 14:00:42 -070058 // Returns a list of device sysfs paths for all disk devices attached to
Ben Chanf51ff002011-04-25 12:41:57 -070059 // the system.
Ben Chan490319f2011-05-06 14:00:42 -070060 virtual std::vector<std::string> EnumerateDevices(
Ben Chanbdc39742011-05-11 17:51:26 -070061 DBus::Error& error); // NOLINT
Ben Chan490319f2011-05-06 14:00:42 -070062
63 // Returns a list of device sysfs paths for all auto-mountable disk devices
64 // attached to the system. Currently, all external disk devices, which are
65 // neither on the boot device nor virtual, are considered auto-mountable.
66 virtual std::vector<std::string> EnumerateAutoMountableDevices(
Ben Chanbdc39742011-05-11 17:51:26 -070067 DBus::Error& error); // NOLINT
Ben Chanf51ff002011-04-25 12:41:57 -070068
69 // Returns properties of a disk device attached to the system.
70 virtual DBusDisk GetDeviceProperties(const std::string& device_path,
Ben Chanbdc39742011-05-11 17:51:26 -070071 DBus::Error& error); // NOLINT
Ben Chanf51ff002011-04-25 12:41:57 -070072
73 // Emits appropriate DBus signals notifying device changes.
74 void SignalDeviceChanges();
75
Ben Chan6e726922011-06-28 15:54:32 -070076 // Implements the PowerManagerObserver interface to handle the event
77 // when the screen is locked.
78 virtual void OnScreenIsLocked();
79
80 // Implements the PowerManagerObserver interface to handle the event
81 // when the screen is unlocked.
82 virtual void OnScreenIsUnlocked();
83
84 // Implements the SessionManagerObserver interface to handle the event
85 // when the session has been started.
86 virtual void OnSessionStarted(const std::string& user);
87
88 // Implements the SessionManagerObserver interface to handle the event
89 // when the session has been stopped.
90 virtual void OnSessionStopped(const std::string& user);
91
Ben Chanf51ff002011-04-25 12:41:57 -070092 private:
Ben Chan6e726922011-06-28 15:54:32 -070093 // Dispatches a device event by emitting the corresponding D-Bus signal.
94 void DispatchDeviceEvent(const DeviceEvent& event);
95
96 // Dispatches all queued device events by emitting the corresponding
97 // D-Bus signals.
98 void DispatchQueuedDeviceEvents();
99
Ben Chan490319f2011-05-06 14:00:42 -0700100 // Returns a list of device sysfs paths for all disk devices attached to
101 // the system. If auto_mountable_only is true, only auto-mountable disk
102 // devices are returned.
103 std::vector<std::string> DoEnumerateDevices(bool auto_mountable_only) const;
104
Ben Chan6e726922011-06-28 15:54:32 -0700105 // A list of deferred disk events to be fired.
106 DeviceEventQueue device_event_queue_;
107
Ben Chanf51ff002011-04-25 12:41:57 -0700108 DiskManager* disk_manager_;
Ben Chan6e726922011-06-28 15:54:32 -0700109
110 // This variable is set to true if any new device event should be queued
111 // instead of being dispatched immediately.
112 bool is_device_event_queued_;
Ryan Cairnsea6505f2011-04-10 19:54:53 -0700113};
Ryan Cairnsea6505f2011-04-10 19:54:53 -0700114
Ben Chanbdc39742011-05-11 17:51:26 -0700115} // namespace cros_disks
116
117#endif // CROS_DISKS_SERVER_IMPL_H_