Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / extensions / extension_keybinding_registry_views.cc
blob9e8dec3f38f3d3a62cf3db71f2cfab8a54df4042
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/profiles/profile.h"
10 #include "chrome/browser/ui/extensions/accelerator_priority.h"
11 #include "extensions/common/extension.h"
12 #include "ui/views/focus/focus_manager.h"
14 ExtensionKeybindingRegistryViews::ExtensionKeybindingRegistryViews(
15 Profile* profile,
16 views::FocusManager* focus_manager,
17 ExtensionFilter extension_filter,
18 Delegate* delegate)
19 : ExtensionKeybindingRegistry(profile, extension_filter, delegate),
20 profile_(profile),
21 focus_manager_(focus_manager) {
22 Init();
25 ExtensionKeybindingRegistryViews::~ExtensionKeybindingRegistryViews() {
26 focus_manager_->UnregisterAccelerators(this);
29 void ExtensionKeybindingRegistryViews::AddExtensionKeybindings(
30 const extensions::Extension* extension,
31 const std::string& command_name) {
32 // This object only handles named commands, not browser/page actions.
33 if (ShouldIgnoreCommand(command_name))
34 return;
36 extensions::CommandService* command_service =
37 extensions::CommandService::Get(profile_);
38 // Add all the active keybindings (except page actions and browser actions,
39 // which are handled elsewhere).
40 extensions::CommandMap commands;
41 if (!command_service->GetNamedCommands(
42 extension->id(),
43 extensions::CommandService::ACTIVE,
44 extensions::CommandService::REGULAR,
45 &commands))
46 return;
47 extensions::CommandMap::const_iterator iter = commands.begin();
48 for (; iter != commands.end(); ++iter) {
49 if (!command_name.empty() && (iter->second.command_name() != command_name))
50 continue;
51 const ui::Accelerator &accelerator = iter->second.accelerator();
52 if (!IsAcceleratorRegistered(accelerator)) {
53 focus_manager_->RegisterAccelerator(
54 accelerator, GetAcceleratorPriority(accelerator, extension), this);
57 AddEventTarget(accelerator, extension->id(), iter->second.command_name());
61 void ExtensionKeybindingRegistryViews::RemoveExtensionKeybindingImpl(
62 const ui::Accelerator& accelerator,
63 const std::string& command_name) {
64 focus_manager_->UnregisterAccelerator(accelerator, this);
67 void ExtensionKeybindingRegistryViews::OnShortcutHandlingSuspended(
68 bool suspended) {
69 focus_manager_->set_shortcut_handling_suspended(suspended);
72 bool ExtensionKeybindingRegistryViews::AcceleratorPressed(
73 const ui::Accelerator& accelerator) {
74 std::string extension_id, command_name;
75 GetFirstTarget(accelerator, &extension_id, &command_name);
76 const ui::AcceleratorManager::HandlerPriority priority =
77 GetAcceleratorPriorityById(accelerator, extension_id, browser_context());
78 // Normal priority shortcuts must be handled via standard browser commands to
79 // be processed at the proper time.
80 return (priority == ui::AcceleratorManager::kHighPriority) &&
81 ExtensionKeybindingRegistry::NotifyEventTargets(accelerator);
84 bool ExtensionKeybindingRegistryViews::CanHandleAccelerators() const {
85 return true;