Chromium Code Reviews| Index: media/audio/key_press_monitor.cc |
| diff --git a/media/audio/key_press_monitor.cc b/media/audio/key_press_monitor.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..079fd03fa7fad256a66859152eecfa8d2efc0ce2 |
| --- /dev/null |
| +++ b/media/audio/key_press_monitor.cc |
| @@ -0,0 +1,65 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/audio/key_press_monitor.h" |
| + |
| +#include "base/message_loop/message_loop_proxy.h" |
| + |
| +namespace media { |
| + |
| +KeyPressMonitor::KeyPressMonitor( |
| + const scoped_refptr<base::MessageLoopProxy>& message_loop, |
| + const scoped_refptr<base::MessageLoopProxy>& io_message_loop, |
|
DaleCurtis
2013/08/07 17:50:39
Not really necessary to pass all this in, you coul
DaleCurtis
2013/08/07 18:58:02
I forgot this is in media/ so you can't do this af
|
| + const scoped_refptr<base::MessageLoopProxy>& ui_message_loop) |
| + : message_loop_(message_loop), |
| + io_message_loop_(io_message_loop), |
| + ui_message_loop_(ui_message_loop), |
| + weak_ptr_factory_(this) {} |
| + |
| +KeyPressMonitor::~KeyPressMonitor() {} |
| + |
| +void KeyPressMonitor::OnMouseMoved(const SkIPoint& position) { |
| + NOTREACHED(); |
| +} |
| + |
| +void KeyPressMonitor::OnKeyboardEvent(ui::EventType event, |
| + ui::KeyboardCode key_code) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + if (event == ui::ET_KEY_PRESSED) { |
| + if (pressed_keys_.find(key_code) != pressed_keys_.end()) |
| + return; |
| + pressed_keys_.insert(key_code); |
| + FOR_EACH_OBSERVER(KeyPressListener, listeners_, OnKeyPressed()); |
|
tommi (sloooow) - chröme
2013/08/06 10:57:47
Would it make sense to have two events, OnKeyPress
jiayl
2013/08/06 17:24:11
It doesn't seem necessary if my understanding of t
|
| + } else { |
| + DCHECK(event == ui::ET_KEY_RELEASED); |
| + DCHECK(pressed_keys_.find(key_code) != pressed_keys_.end()); |
| + pressed_keys_.erase(key_code); |
| + } |
| +} |
| + |
| +void KeyPressMonitor::AddKeyPressListener(KeyPressListener* listener) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + listeners_.AddObserver(listener); |
| + |
| + if (listeners_.size() == 1 && !monitor_.get()) { |
| + monitor_.reset(UserInputMonitor::Create(io_message_loop_, |
| + ui_message_loop_, |
| + message_loop_, |
| + UserInputMonitor::KEYBOARD_EVENT, |
| + weak_ptr_factory_.GetWeakPtr()) |
| + .release()); |
| + } |
| +} |
| + |
| +void KeyPressMonitor::RemoveKeyPressListener(KeyPressListener* listener) { |
| + DCHECK(message_loop_->BelongsToCurrentThread()); |
| + listeners_.RemoveObserver(listener); |
| + |
| + if (listeners_.size() == 0) { |
| + monitor_.reset(NULL); |
| + weak_ptr_factory_.InvalidateWeakPtrs(); |
| + } |
| +} |
| + |
| +} // namespace media |