Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 1 | // 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 Chan | 5ccd9fe | 2013-11-13 18:28:27 -0800 | [diff] [blame] | 8 | #include "cros-disks/device_event_queue.h" |
| 9 | #include "cros-disks/session_manager_observer_interface.h" |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 10 | |
| 11 | namespace cros_disks { |
| 12 | |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 13 | class DeviceEventDispatcherInterface; |
| 14 | class 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 Perreault | 8104e6b | 2016-04-18 14:54:47 -0400 | [diff] [blame] | 19 | // 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 Chan | 5988f29 | 2012-09-18 08:32:42 -0700 | [diff] [blame] | 29 | class DeviceEventModerator : public SessionManagerObserverInterface { |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 30 | public: |
| 31 | DeviceEventModerator(DeviceEventDispatcherInterface* event_dispatcher, |
Mathieu Perreault | 8104e6b | 2016-04-18 14:54:47 -0400 | [diff] [blame] | 32 | DeviceEventSourceInterface* event_source, |
| 33 | bool dispatch_initially); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 34 | DeviceEventModerator(const DeviceEventModerator&) = delete; |
| 35 | DeviceEventModerator& operator=(const DeviceEventModerator&) = delete; |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 36 | |
Ben Chan | 5512355 | 2014-08-24 16:22:16 -0700 | [diff] [blame] | 37 | virtual ~DeviceEventModerator() = default; |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 38 | |
| 39 | // Dispatches all queued device events through the event dispatcher. |
| 40 | void DispatchQueuedDeviceEvents(); |
| 41 | |
Ben Chan | 5988f29 | 2012-09-18 08:32:42 -0700 | [diff] [blame] | 42 | // Implements the SessionManagerObserverInterface interface to handle |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 43 | // the event when the screen is locked. |
| 44 | virtual void OnScreenIsLocked(); |
| 45 | |
Ben Chan | 5988f29 | 2012-09-18 08:32:42 -0700 | [diff] [blame] | 46 | // Implements the SessionManagerObserverInterface interface to handle |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 47 | // 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 Chan | b3bf8d1 | 2013-04-23 13:57:55 -0700 | [diff] [blame] | 52 | virtual void OnSessionStarted(); |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 53 | |
| 54 | // Implements the SessionManagerObserverInterface interface to handle |
| 55 | // the event when the session has been stopped. |
Ben Chan | b3bf8d1 | 2013-04-23 13:57:55 -0700 | [diff] [blame] | 56 | virtual void OnSessionStopped(); |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 57 | |
Ben Chan | 7599f99 | 2011-12-13 16:04:34 -0800 | [diff] [blame] | 58 | // Process the available device events from the event source. |
| 59 | void ProcessDeviceEvents(); |
Ben Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 60 | |
| 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 Chan | 89cf29e | 2011-08-10 13:11:05 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // namespace cros_disks |
| 79 | |
| 80 | #endif // CROS_DISKS_DEVICE_EVENT_MODERATOR_H_ |