blob: 75fe496dfffdd4e9cbce5e59132173afce51ec70 [file] [log] [blame]
Ben Chan89cf29e2011-08-10 13:11:05 -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
5#ifndef CROS_DISKS_DEVICE_EVENT_MODERATOR_H_
6#define CROS_DISKS_DEVICE_EVENT_MODERATOR_H_
7
Ben Chan5ccd9fe2013-11-13 18:28:27 -08008#include "cros-disks/device_event_queue.h"
9#include "cros-disks/session_manager_observer_interface.h"
Ben Chan89cf29e2011-08-10 13:11:05 -070010
11namespace cros_disks {
12
Ben Chan89cf29e2011-08-10 13:11:05 -070013class DeviceEventDispatcherInterface;
14class DeviceEventSourceInterface;
15
16// A class for moderating device events by retrieving events from an event
17// source and dispatching them through a dispatcher at appropriate moments.
18//
Mathieu Perreault8104e6b2016-04-18 14:54:47 -040019// If |dispatch_initially| is true, device events are dispatched immediately
20// only during an active user session. Then, after a user session ends or the
21// screen is locked, any received device event is temporarily queued and only
22// dispatched after a new user session starts or the screen is unlocked. This
23// is to minimize the chance of device insertion attacks when the system is not
24// actively used.
25//
26// If |dispatch_initially| is false, device events are not queued and
27// dispatched immediately regardless of a session status. This is for use in
28// environments where the concept of sessions is not relevant.
Ben Chan5988f292012-09-18 08:32:42 -070029class DeviceEventModerator : public SessionManagerObserverInterface {
Ben Chan89cf29e2011-08-10 13:11:05 -070030 public:
31 DeviceEventModerator(DeviceEventDispatcherInterface* event_dispatcher,
Mathieu Perreault8104e6b2016-04-18 14:54:47 -040032 DeviceEventSourceInterface* event_source,
33 bool dispatch_initially);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090034 DeviceEventModerator(const DeviceEventModerator&) = delete;
35 DeviceEventModerator& operator=(const DeviceEventModerator&) = delete;
Ben Chan89cf29e2011-08-10 13:11:05 -070036
Ben Chan55123552014-08-24 16:22:16 -070037 virtual ~DeviceEventModerator() = default;
Ben Chan89cf29e2011-08-10 13:11:05 -070038
39 // Dispatches all queued device events through the event dispatcher.
40 void DispatchQueuedDeviceEvents();
41
Ben Chan5988f292012-09-18 08:32:42 -070042 // Implements the SessionManagerObserverInterface interface to handle
Ben Chan89cf29e2011-08-10 13:11:05 -070043 // the event when the screen is locked.
44 virtual void OnScreenIsLocked();
45
Ben Chan5988f292012-09-18 08:32:42 -070046 // Implements the SessionManagerObserverInterface interface to handle
Ben Chan89cf29e2011-08-10 13:11:05 -070047 // the event when the screen is unlocked.
48 virtual void OnScreenIsUnlocked();
49
50 // Implements the SessionManagerObserverInterface interface to handle
51 // the event when the session has been started.
Ben Chanb3bf8d12013-04-23 13:57:55 -070052 virtual void OnSessionStarted();
Ben Chan89cf29e2011-08-10 13:11:05 -070053
54 // Implements the SessionManagerObserverInterface interface to handle
55 // the event when the session has been stopped.
Ben Chanb3bf8d12013-04-23 13:57:55 -070056 virtual void OnSessionStopped();
Ben Chan89cf29e2011-08-10 13:11:05 -070057
Ben Chan7599f992011-12-13 16:04:34 -080058 // Process the available device events from the event source.
59 void ProcessDeviceEvents();
Ben Chan89cf29e2011-08-10 13:11:05 -070060
61 bool is_event_queued() const { return is_event_queued_; }
62
63 private:
64 // An object that dispatches device events.
65 DeviceEventDispatcherInterface* event_dispatcher_;
66
67 // An object that queues up device events when the system is not active.
68 DeviceEventQueue event_queue_;
69
70 // An object from which device events are retrieved.
71 DeviceEventSourceInterface* event_source_;
72
73 // This variable is set to true if any new device event should be queued
74 // instead of being dispatched immediately.
75 bool is_event_queued_;
Ben Chan89cf29e2011-08-10 13:11:05 -070076};
77
78} // namespace cros_disks
79
80#endif // CROS_DISKS_DEVICE_EVENT_MODERATOR_H_