Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Unified Diff: chrome/browser/extensions/global_shortcut_listener.cc

Issue 23812010: Implement first part of supporting global extension commands. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: gclient sync Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..94547adfffb16a3fe25fe481d71065458271539c
--- /dev/null
+++ b/chrome/browser/extensions/global_shortcut_listener.cc
@@ -0,0 +1,62 @@
+// 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() {
+ DCHECK(accelerator_map_.empty()); // Make sure we've cleaned up.
+}
+
+void GlobalShortcutListener::RegisterAccelerator(
+ const ui::Accelerator& accelerator, Observer* observer) {
+ AcceleratorMap::const_iterator it = accelerator_map_.find(accelerator);
+ if (it == accelerator_map_.end()) {
+ if (accelerator_map_.empty())
+ GlobalShortcutListener::GetInstance()->StartListening();
+ 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);
+ }
+}
+
+void GlobalShortcutListener::UnregisterAccelerator(
+ const ui::Accelerator& accelerator, Observer* observer) {
+ AcceleratorMap::iterator it = accelerator_map_.find(accelerator);
+ DCHECK(it != accelerator_map_.end());
+ DCHECK(it->second->HasObserver(observer));
+ it->second->RemoveObserver(observer);
+ if (!it->second->might_have_observers()) {
+ accelerator_map_.erase(it);
+ if (accelerator_map_.empty())
+ GlobalShortcutListener::GetInstance()->StopListening();
+ }
+}
+
+void GlobalShortcutListener::NotifyKeyPressed(
+ const ui::Accelerator& accelerator) {
+ AcceleratorMap::iterator iter = accelerator_map_.find(accelerator);
+ if (iter == accelerator_map_.end()) {
+ // This should never occur, because if it does, we have failed to unregister
+ // or failed to clean up the map after unregistering the shortcut.
+ NOTREACHED();
+ return; // No-one is listening to this key.
+ }
+ // The observer list should not be empty.
+ DCHECK(iter->second->might_have_observers());
+
+ FOR_EACH_OBSERVER(Observer, *(iter->second), OnKeyPressed(accelerator));
+}
+
+} // namespace extensions
« no previous file with comments | « chrome/browser/extensions/global_shortcut_listener.h ('k') | chrome/browser/extensions/global_shortcut_listener_aurax11.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698