1 // Copyright (c) 2012 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 "chrome/browser/ui/views/extensions/extension_keybinding_registry_views.h"
7 #include "chrome/browser/extensions/api/commands/command_service.h"
8 #include "chrome/browser/extensions/extension_keybinding_registry.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "extensions/common/extension.h"
12 #include "ui/views/focus/focus_manager.h"
15 void extensions::ExtensionKeybindingRegistry::SetShortcutHandlingSuspended(
17 views::FocusManager::set_shortcut_handling_suspended(suspended);
20 ExtensionKeybindingRegistryViews::ExtensionKeybindingRegistryViews(
22 views::FocusManager* focus_manager,
23 ExtensionFilter extension_filter,
25 : ExtensionKeybindingRegistry(profile, extension_filter, delegate),
27 focus_manager_(focus_manager) {
31 ExtensionKeybindingRegistryViews::~ExtensionKeybindingRegistryViews() {
32 for (EventTargets::const_iterator iter = event_targets_.begin();
33 iter != event_targets_.end(); ++iter)
34 focus_manager_->UnregisterAccelerator(iter->first, this);
37 void ExtensionKeybindingRegistryViews::AddExtensionKeybinding(
38 const extensions::Extension* extension,
39 const std::string& command_name) {
40 // This object only handles named commands, not browser/page actions.
41 if (ShouldIgnoreCommand(command_name))
44 extensions::CommandService* command_service =
45 extensions::CommandService::Get(profile_);
46 // Add all the active keybindings (except page actions and browser actions,
47 // which are handled elsewhere).
48 extensions::CommandMap commands;
49 if (!command_service->GetNamedCommands(
51 extensions::CommandService::ACTIVE_ONLY,
52 extensions::CommandService::REGULAR,
55 extensions::CommandMap::const_iterator iter = commands.begin();
56 for (; iter != commands.end(); ++iter) {
57 if (!command_name.empty() && (iter->second.command_name() != command_name))
60 event_targets_[iter->second.accelerator()].push_back(
61 std::make_pair(extension->id(), iter->second.command_name()));
62 // Shortcuts except media keys have only one target in the list. See comment
63 // about |event_targets_|.
64 if (!extensions::CommandService::IsMediaKey(iter->second.accelerator()))
65 DCHECK_EQ(1u, event_targets_[iter->second.accelerator()].size());
66 if (event_targets_[iter->second.accelerator()].size() > 1)
67 continue; // It is already registered with the focus manager.
69 focus_manager_->RegisterAccelerator(
70 iter->second.accelerator(),
71 ui::AcceleratorManager::kHighPriority, this);
75 void ExtensionKeybindingRegistryViews::RemoveExtensionKeybindingImpl(
76 const ui::Accelerator& accelerator,
77 const std::string& command_name) {
78 focus_manager_->UnregisterAccelerator(accelerator, this);
81 bool ExtensionKeybindingRegistryViews::AcceleratorPressed(
82 const ui::Accelerator& accelerator) {
83 return ExtensionKeybindingRegistry::NotifyEventTargets(accelerator);
86 bool ExtensionKeybindingRegistryViews::CanHandleAccelerators() const {