Ben Chan | 6e72692 | 2011-06-28 15:54:32 -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 | |
Ben Chan | 5ccd9fe | 2013-11-13 18:28:27 -0800 | [diff] [blame] | 5 | #include "cros-disks/device_event_queue.h" |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 6 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 7 | #include <base/check.h> |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 8 | #include <base/logging.h> |
| 9 | |
Ben Chan | 5ccd9fe | 2013-11-13 18:28:27 -0800 | [diff] [blame] | 10 | #include "cros-disks/device_event.h" |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 11 | |
| 12 | namespace cros_disks { |
| 13 | |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 14 | void DeviceEventQueue::Remove() { |
| 15 | if (!events_.empty()) |
| 16 | events_.pop_back(); |
| 17 | } |
| 18 | |
| 19 | void DeviceEventQueue::Add(const DeviceEvent& event) { |
| 20 | // Discard an Ignored or DeviceScanned event. |
| 21 | if (event.event_type == DeviceEvent::kIgnored || |
| 22 | event.event_type == DeviceEvent::kDeviceScanned) |
| 23 | return; |
| 24 | |
Ben Chan | 7599f99 | 2011-12-13 16:04:34 -0800 | [diff] [blame] | 25 | for (DeviceEventList::iterator last_event_iterator = events_.begin(); |
| 26 | last_event_iterator != events_.end(); ++last_event_iterator) { |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 27 | const DeviceEvent& last_event = *last_event_iterator; |
| 28 | |
| 29 | // Skip an unrelated event. |
| 30 | if (event.device_path != last_event.device_path || |
| 31 | event.IsDiskEvent() != last_event.IsDiskEvent()) |
| 32 | continue; |
| 33 | |
| 34 | // Combine events of the same type and device path and keep the latest one. |
| 35 | if (event.event_type == last_event.event_type) { |
| 36 | events_.erase(last_event_iterator); |
| 37 | events_.push_front(event); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | // Discard a Removed event and its last related event, which is an |
Ben Chan | 7599f99 | 2011-12-13 16:04:34 -0800 | [diff] [blame] | 42 | // Added/Changed event. Note that the last related event cannot be |
| 43 | // a Removed event as that is already handled by the code above. |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 44 | if (event.event_type == DeviceEvent::kDeviceRemoved || |
| 45 | event.event_type == DeviceEvent::kDiskRemoved) { |
| 46 | CHECK(last_event.event_type != DeviceEvent::kDeviceRemoved && |
Ben Chan | 7599f99 | 2011-12-13 16:04:34 -0800 | [diff] [blame] | 47 | last_event.event_type != DeviceEvent::kDiskRemoved) |
| 48 | << "Last event should not be a Removed event"; |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 49 | events_.erase(last_event_iterator); |
| 50 | return; |
| 51 | } |
| 52 | |
Ben Chan | 7599f99 | 2011-12-13 16:04:34 -0800 | [diff] [blame] | 53 | // Discard a DiskChanged event if a related DiskAdded event is already |
| 54 | // in the queue. |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 55 | if (event.event_type == DeviceEvent::kDiskChanged && |
Ben Chan | 7599f99 | 2011-12-13 16:04:34 -0800 | [diff] [blame] | 56 | last_event.event_type == DeviceEvent::kDiskAdded) |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 57 | return; |
| 58 | } |
| 59 | |
| 60 | events_.push_front(event); |
| 61 | } |
| 62 | |
| 63 | const DeviceEvent* DeviceEventQueue::Head() const { |
Ben Chan | 44e7ea6 | 2014-08-29 18:13:37 -0700 | [diff] [blame] | 64 | return events_.empty() ? nullptr : &events_.back(); |
Ben Chan | 6e72692 | 2011-06-28 15:54:32 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | } // namespace cros_disks |