Home | History | Annotate | Download | only in extensions
      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/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"
     13 
     14 // static
     15 void extensions::ExtensionKeybindingRegistry::SetShortcutHandlingSuspended(
     16     bool suspended) {
     17   views::FocusManager::set_shortcut_handling_suspended(suspended);
     18 }
     19 
     20 ExtensionKeybindingRegistryViews::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 
     31 ExtensionKeybindingRegistryViews::~ExtensionKeybindingRegistryViews() {
     32   focus_manager_->UnregisterAccelerators(this);
     33 }
     34 
     35 void ExtensionKeybindingRegistryViews::AddExtensionKeybinding(
     36     const extensions::Extension* extension,
     37     const std::string& command_name) {
     38   // This object only handles named commands, not browser/page actions.
     39   if (ShouldIgnoreCommand(command_name))
     40     return;
     41 
     42   extensions::CommandService* command_service =
     43       extensions::CommandService::Get(profile_);
     44   // Add all the active keybindings (except page actions and browser actions,
     45   // which are handled elsewhere).
     46   extensions::CommandMap commands;
     47   if (!command_service->GetNamedCommands(
     48           extension->id(),
     49           extensions::CommandService::ACTIVE_ONLY,
     50           extensions::CommandService::REGULAR,
     51           &commands))
     52     return;
     53   extensions::CommandMap::const_iterator iter = commands.begin();
     54   for (; iter != commands.end(); ++iter) {
     55     if (!command_name.empty() && (iter->second.command_name() != command_name))
     56       continue;
     57     const ui::Accelerator &accelerator = iter->second.accelerator();
     58     if (!IsAcceleratorRegistered(accelerator)) {
     59       focus_manager_->RegisterAccelerator(
     60           accelerator, GetAcceleratorPriority(accelerator, extension), this);
     61     }
     62 
     63     AddEventTarget(accelerator, extension->id(), iter->second.command_name());
     64   }
     65 }
     66 
     67 void ExtensionKeybindingRegistryViews::RemoveExtensionKeybindingImpl(
     68     const ui::Accelerator& accelerator,
     69     const std::string& command_name) {
     70   focus_manager_->UnregisterAccelerator(accelerator, this);
     71 }
     72 
     73 bool ExtensionKeybindingRegistryViews::AcceleratorPressed(
     74     const ui::Accelerator& accelerator) {
     75   std::string extension_id, command_name;
     76   GetFirstTarget(accelerator, &extension_id, &command_name);
     77   const ui::AcceleratorManager::HandlerPriority priority =
     78       GetAcceleratorPriorityById(accelerator, extension_id, browser_context());
     79   // Normal priority shortcuts must be handled via standard browser commands to
     80   // be processed at the proper time.
     81   return (priority == ui::AcceleratorManager::kHighPriority) &&
     82       ExtensionKeybindingRegistry::NotifyEventTargets(accelerator);
     83 }
     84 
     85 bool ExtensionKeybindingRegistryViews::CanHandleAccelerators() const {
     86   return true;
     87 }
     88