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#ifndef CHROME_BROWSER_UI_COCOA_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_COCOA_H_
6#define CHROME_BROWSER_UI_COCOA_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_COCOA_H_
7
8#include <string>
9#include <utility>
10
11#include "chrome/browser/extensions/extension_keybinding_registry.h"
12#include "ui/base/accelerators/accelerator.h"
13#include "ui/gfx/native_widget_types.h"
14
15class Profile;
16
17namespace content {
18  struct NativeWebKeyboardEvent;
19}
20namespace extensions {
21  class Extension;
22}
23
24// The ExtensionKeybindingRegistryCocoa is the Cocoa specialization of the
25// ExtensionKeybindingRegistry class that handles turning keyboard shortcuts
26// into events that get sent to the extension.
27
28// ExtensionKeybindingRegistryCocoa is a class that handles Cocoa-specific
29// implemenation of the Extension Commands shortcuts (keyboard accelerators).
30// It also routes the events to the intended recipient (ie. to the browser
31// action button in case of browser action commands).
32class ExtensionKeybindingRegistryCocoa
33    : public extensions::ExtensionKeybindingRegistry {
34 public:
35  ExtensionKeybindingRegistryCocoa(Profile* profile,
36                                   gfx::NativeWindow window,
37                                   ExtensionFilter extension_filter,
38                                   Delegate* delegate);
39  virtual ~ExtensionKeybindingRegistryCocoa();
40
41  static void set_shortcut_handling_suspended(bool suspended) {
42    shortcut_handling_suspended_ = suspended;
43  }
44  static bool shortcut_handling_suspended() {
45    return shortcut_handling_suspended_;
46  }
47
48  // For a given keyboard |event|, see if a known Extension Command registration
49  // exists and route the event to it. Returns true if the event was handled,
50  // false otherwise.
51  bool ProcessKeyEvent(const content::NativeWebKeyboardEvent& event);
52
53 protected:
54  // Overridden from ExtensionKeybindingRegistry:
55  virtual void AddExtensionKeybinding(
56      const extensions::Extension* extension,
57      const std::string& command_name) OVERRIDE;
58  virtual void RemoveExtensionKeybindingImpl(
59      const ui::Accelerator& accelerator,
60      const std::string& command_name) OVERRIDE;
61
62 private:
63  // Keeps track of whether shortcut handling is currently suspended. Shortcuts
64  // are suspended briefly while capturing which shortcut to assign to an
65  // extension command in the Config UI. If handling isn't suspended while
66  // capturing then trying to assign Ctrl+F to a command would instead result
67  // in the Find box opening.
68  static bool shortcut_handling_suspended_;
69
70  // Weak pointer to the our profile. Not owned by us.
71  Profile* profile_;
72
73  // The window we are associated with.
74  gfx::NativeWindow window_;
75
76  // The content notification registrar for listening to extension events.
77  content::NotificationRegistrar registrar_;
78
79  DISALLOW_COPY_AND_ASSIGN(ExtensionKeybindingRegistryCocoa);
80};
81
82#endif  // CHROME_BROWSER_UI_COCOA_EXTENSIONS_EXTENSION_KEYBINDING_REGISTRY_COCOA_H_
83