extension_keybinding_registry_views.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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. 4 5#include "chrome/browser/ui/views/extensions/extension_keybinding_registry_views.h" 6 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 "chrome/common/extensions/extension.h" 12#include "ui/views/focus/focus_manager.h" 13 14// static 15void extensions::ExtensionKeybindingRegistry::SetShortcutHandlingSuspended( 16 bool suspended) { 17 views::FocusManager::set_shortcut_handling_suspended(suspended); 18} 19 20ExtensionKeybindingRegistryViews::ExtensionKeybindingRegistryViews( 21 Profile* profile, 22 views::FocusManager* focus_manager, 23 ExtensionFilter extension_filter, 24 Delegate* delegate) 25 : ExtensionKeybindingRegistry(profile, extension_filter, delegate), 26 profile_(profile), 27 focus_manager_(focus_manager) { 28 Init(); 29} 30 31ExtensionKeybindingRegistryViews::~ExtensionKeybindingRegistryViews() { 32 EventTargets::const_iterator iter; 33 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) 34 focus_manager_->UnregisterAccelerator(iter->first, this); 35} 36 37void 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)) 42 return; 43 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( 50 extension->id(), 51 extensions::CommandService::ACTIVE_ONLY, 52 extensions::CommandService::REGULAR, 53 &commands)) 54 return; 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)) 58 continue; 59 60 event_targets_[iter->second.accelerator()] = 61 std::make_pair(extension->id(), iter->second.command_name()); 62 focus_manager_->RegisterAccelerator( 63 iter->second.accelerator(), 64 ui::AcceleratorManager::kHighPriority, this); 65 } 66} 67 68void ExtensionKeybindingRegistryViews::RemoveExtensionKeybindingImpl( 69 const ui::Accelerator& accelerator, 70 const std::string& command_name) { 71 focus_manager_->UnregisterAccelerator(accelerator, this); 72} 73 74bool ExtensionKeybindingRegistryViews::AcceleratorPressed( 75 const ui::Accelerator& accelerator) { 76 EventTargets::iterator it = event_targets_.find(accelerator); 77 if (it == event_targets_.end()) { 78 NOTREACHED(); // Shouldn't get this event for something not registered. 79 return false; 80 } 81 82 CommandExecuted(it->second.first, it->second.second); 83 84 return true; 85} 86 87bool ExtensionKeybindingRegistryViews::CanHandleAccelerators() const { 88 return true; 89} 90