blob: 4e5b956bfc822a966c6ca451332f817153e66d97 [file] [log] [blame]
Ben Chan6e726922011-06-28 15:54:32 -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 Chan5ccd9fe2013-11-13 18:28:27 -08005#include "cros-disks/device_event_queue.h"
Ben Chan6e726922011-06-28 15:54:32 -07006
Qijiang Fan713061e2021-03-08 15:45:12 +09007#include <base/check.h>
Ben Chan6e726922011-06-28 15:54:32 -07008#include <base/logging.h>
9
Ben Chan5ccd9fe2013-11-13 18:28:27 -080010#include "cros-disks/device_event.h"
Ben Chan6e726922011-06-28 15:54:32 -070011
12namespace cros_disks {
13
Ben Chan6e726922011-06-28 15:54:32 -070014void DeviceEventQueue::Remove() {
15 if (!events_.empty())
16 events_.pop_back();
17}
18
19void 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 Chan7599f992011-12-13 16:04:34 -080025 for (DeviceEventList::iterator last_event_iterator = events_.begin();
26 last_event_iterator != events_.end(); ++last_event_iterator) {
Ben Chan6e726922011-06-28 15:54:32 -070027 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 Chan7599f992011-12-13 16:04:34 -080042 // 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 Chan6e726922011-06-28 15:54:32 -070044 if (event.event_type == DeviceEvent::kDeviceRemoved ||
45 event.event_type == DeviceEvent::kDiskRemoved) {
46 CHECK(last_event.event_type != DeviceEvent::kDeviceRemoved &&
Ben Chan7599f992011-12-13 16:04:34 -080047 last_event.event_type != DeviceEvent::kDiskRemoved)
48 << "Last event should not be a Removed event";
Ben Chan6e726922011-06-28 15:54:32 -070049 events_.erase(last_event_iterator);
50 return;
51 }
52
Ben Chan7599f992011-12-13 16:04:34 -080053 // Discard a DiskChanged event if a related DiskAdded event is already
54 // in the queue.
Ben Chan6e726922011-06-28 15:54:32 -070055 if (event.event_type == DeviceEvent::kDiskChanged &&
Ben Chan7599f992011-12-13 16:04:34 -080056 last_event.event_type == DeviceEvent::kDiskAdded)
Ben Chan6e726922011-06-28 15:54:32 -070057 return;
58 }
59
60 events_.push_front(event);
61}
62
63const DeviceEvent* DeviceEventQueue::Head() const {
Ben Chan44e7ea62014-08-29 18:13:37 -070064 return events_.empty() ? nullptr : &events_.back();
Ben Chan6e726922011-06-28 15:54:32 -070065}
66
67} // namespace cros_disks