Chromium Code Reviews| Index: chrome/browser/extensions/global_shortcut_listener.cc |
| diff --git a/chrome/browser/extensions/global_shortcut_listener.cc b/chrome/browser/extensions/global_shortcut_listener.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4ace06bce5a56b27dd7e4c760c9520496d432d21 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/global_shortcut_listener.cc |
| @@ -0,0 +1,56 @@ |
| +// 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 "chrome/browser/extensions/global_shortcut_listener.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "ui/base/accelerators/accelerator.h" |
| + |
| +namespace extensions { |
| + |
| +GlobalShortcutListener::GlobalShortcutListener() { |
| +} |
| + |
| +GlobalShortcutListener::~GlobalShortcutListener() { |
| + STLDeleteContainerPairSecondPointers(accelerator_map_.begin(), |
| + accelerator_map_.end()); |
| +} |
| + |
| +void GlobalShortcutListener::RegisterAccelerator( |
| + const ui::Accelerator& accelerator, Observer* observer) { |
| + AcceleratorMap::const_iterator it = accelerator_map_.find(accelerator); |
| + if (it == accelerator_map_.end()) { |
| + Observers* observers = new Observers; |
| + observers->AddObserver(observer); |
| + accelerator_map_[accelerator] = observers; |
| + } else { |
| + // Make sure we don't register the same accelerator twice. |
| + DCHECK(!accelerator_map_[accelerator]->HasObserver(observer)); |
| + accelerator_map_[accelerator]->AddObserver(observer); |
| + } |
|
zhchbin
2013/09/20 07:49:23
On linux I need to know which accelerator is regis
Finnur
2013/09/20 13:01:12
I'm not sure I understand this comment. You have b
zhchbin
2013/09/21 02:07:50
I mean to said that this function should add some
Finnur
2013/09/21 12:23:50
I see. I'll take a look on Monday.
On 2013/09/21
|
| +} |
| + |
| +void GlobalShortcutListener::UnregisterAccelerator( |
| + const ui::Accelerator& accelerator, Observer* observer) { |
| + AcceleratorMap::const_iterator it = accelerator_map_.find(accelerator); |
| + // Make sure we're unregistering something that has been registered. |
| + DCHECK(it != accelerator_map_.end()); |
| + DCHECK(it->second->HasObserver(observer)); |
| + it->second->RemoveObserver(observer); |
| + if (!it->second->might_have_observers()) |
| + accelerator_map_.erase(it); |
| +} |
| + |
| +bool GlobalShortcutListener::NotifyKeyPressed( |
| + const ui::Accelerator& accelerator) { |
| + AcceleratorMap::const_iterator it = accelerator_map_.find(accelerator); |
| + if (it == accelerator_map_.end()) |
| + return false; // No-one is listening to this key. |
| + // The observer list should not be empty. |
| + DCHECK(it->second->might_have_observers()); |
| + |
| + FOR_EACH_OBSERVER(Observer, *(it->second), OnKeyPressed(accelerator)); |
| + return true; |
| +} |
| + |
| +} // namespace extensions |