1 // Copyright 2013 The Chromium 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.
5 #include "media/base/keyboard_event_counter.h"
7 #include "base/atomicops.h"
8 #include "base/logging.h"
12 KeyboardEventCounter::KeyboardEventCounter() : total_key_presses_(0) {}
14 KeyboardEventCounter::~KeyboardEventCounter() {}
16 void KeyboardEventCounter::Reset() {
17 pressed_keys_
.clear();
18 base::subtle::NoBarrier_Store(
19 reinterpret_cast<base::subtle::AtomicWord
*>(&total_key_presses_
), 0);
22 void KeyboardEventCounter::OnKeyboardEvent(ui::EventType event
,
23 ui::KeyboardCode key_code
) {
24 // Updates the pressed keys and the total count of key presses.
25 if (event
== ui::ET_KEY_PRESSED
) {
26 if (pressed_keys_
.find(key_code
) != pressed_keys_
.end())
28 pressed_keys_
.insert(key_code
);
29 base::subtle::NoBarrier_AtomicIncrement(
30 reinterpret_cast<base::subtle::AtomicWord
*>(&total_key_presses_
), 1);
32 DCHECK_EQ(ui::ET_KEY_RELEASED
, event
);
33 pressed_keys_
.erase(key_code
);
37 size_t KeyboardEventCounter::GetKeyPressCount() const {
38 return base::subtle::NoBarrier_Load(
39 reinterpret_cast<const base::subtle::AtomicWord
*>(&total_key_presses_
));