OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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_commands_global_registry_ views.h" | |
6 | |
7 #include "base/lazy_instance.h" | |
8 #include "chrome/browser/chrome_notification_types.h" | |
9 #include "chrome/browser/extensions/api/commands/command_service.h" | |
10 #include "chrome/browser/extensions/extension_keybinding_registry.h" | |
11 #include "chrome/browser/extensions/extension_service.h" | |
12 #include "chrome/browser/extensions/global_shortcut_listener.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
14 #include "chrome/common/extensions/extension.h" | |
15 | |
16 namespace extensions { | |
17 | |
18 ExtensionCommandsGlobalRegistryViews::ExtensionCommandsGlobalRegistryViews( | |
19 Profile* profile) | |
20 : ExtensionKeybindingRegistry( | |
21 profile, ExtensionKeybindingRegistry::ALL_EXTENSIONS, NULL), | |
22 profile_(profile) { | |
23 Init(); | |
24 } | |
25 | |
26 ExtensionCommandsGlobalRegistryViews::~ExtensionCommandsGlobalRegistryViews() { | |
27 EventTargets::const_iterator iter; | |
28 for (iter = event_targets_.begin(); iter != event_targets_.end(); ++iter) { | |
29 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( | |
30 iter->first, this); | |
31 } | |
32 } | |
33 | |
34 static base::LazyInstance< | |
35 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistryViews> > | |
36 g_factory = LAZY_INSTANCE_INITIALIZER; | |
37 | |
38 // static | |
39 ProfileKeyedAPIFactory<ExtensionCommandsGlobalRegistryViews>* | |
40 ExtensionCommandsGlobalRegistryViews::GetFactoryInstance() { | |
41 return &g_factory.Get(); | |
42 } | |
43 | |
44 template<> | |
45 void ProfileKeyedAPIFactory< | |
46 ExtensionCommandsGlobalRegistryViews>::DeclareFactoryDependencies() { | |
47 DependsOn(ExtensionSystemFactory::GetInstance()); | |
48 } | |
49 | |
50 // static | |
51 ExtensionCommandsGlobalRegistryViews* | |
52 ExtensionCommandsGlobalRegistryViews::Get(Profile* profile) { | |
53 return ProfileKeyedAPIFactory< | |
54 ExtensionCommandsGlobalRegistryViews>::GetForProfile(profile); | |
55 } | |
56 | |
57 | |
58 void ExtensionCommandsGlobalRegistryViews::AddExtensionKeybinding( | |
59 const extensions::Extension* extension, | |
60 const std::string& command_name) { | |
61 // This object only handles named commands, not browser/page actions. | |
62 if (ShouldIgnoreCommand(command_name)) | |
63 return; | |
64 | |
65 extensions::CommandService* command_service = | |
66 extensions::CommandService::Get(profile_); | |
67 // Add all the active keybindings (except page actions and browser actions, | |
68 // which are handled elsewhere). | |
69 extensions::CommandMap commands; | |
70 if (!command_service->GetNamedCommands( | |
71 extension->id(), | |
72 extensions::CommandService::ACTIVE_ONLY, | |
73 extensions::CommandService::GLOBAL, | |
74 &commands)) | |
75 return; | |
76 | |
77 extensions::CommandMap::const_iterator iter = commands.begin(); | |
78 for (; iter != commands.end(); ++iter) { | |
79 if (!command_name.empty() && (iter->second.command_name() != command_name)) | |
80 continue; | |
81 | |
82 LOG(ERROR) << "Adding global keybinding for " << extension->name().c_str() | |
83 << " " << command_name.c_str() | |
84 << " key: " << iter->second.accelerator().GetShortcutText(); | |
85 | |
86 event_targets_[iter->second.accelerator()] = | |
87 std::make_pair(extension->id(), iter->second.command_name()); | |
88 | |
89 GlobalShortcutListener::GetInstance()->RegisterAccelerator( | |
90 iter->second.accelerator(), this); | |
91 } | |
92 } | |
93 | |
94 void ExtensionCommandsGlobalRegistryViews::RemoveExtensionKeybinding( | |
95 const extensions::Extension* extension, | |
96 const std::string& command_name) { | |
97 LOG(ERROR) << "Removing keybinding for " << extension->name().c_str() | |
98 << " " << command_name.c_str(); | |
99 | |
100 // This object only handles named commands, not browser/page actions. | |
zhchbin
2013/09/24 02:41:22
We should handle *global* named commands only. Cur
Finnur
2013/09/24 10:53:23
I don't see the code here as being incorrect. The
zhchbin
2013/09/24 11:25:27
ExtensionKeybindingRegistry is observing chrome::N
Finnur
2013/09/25 13:11:40
I see. I am currently investigating a crash (not r
zhchbin
2013/09/25 13:27:13
I would begin as soon as I can! So many homework f
Finnur
2013/09/26 13:53:08
OK, found some time to think about this some more.
zhchbin
2013/09/26 14:44:59
Hm... I mean to said that we can check whether the
Finnur
2013/09/26 15:00:59
No, that's what I'm saying: The Command in questio
zhchbin
2013/09/26 15:13:05
:) OK let's stop talking about this and go on.
(
| |
101 if (ShouldIgnoreCommand(command_name)) | |
102 return; | |
103 | |
104 EventTargets::iterator iter = event_targets_.begin(); | |
105 while (iter != event_targets_.end()) { | |
106 if (iter->second.first != extension->id() || | |
107 (!command_name.empty() && (iter->second.second != command_name))) { | |
108 ++iter; | |
109 continue; // Not the extension or command we asked for. | |
110 } | |
111 | |
112 GlobalShortcutListener::GetInstance()->UnregisterAccelerator( | |
113 iter->first, this); | |
114 | |
115 EventTargets::iterator old = iter++; | |
116 event_targets_.erase(old); | |
117 } | |
118 } | |
119 | |
120 void ExtensionCommandsGlobalRegistryViews::OnKeyPressed( | |
121 const ui::Accelerator& accelerator) { | |
122 EventTargets::iterator it = event_targets_.find(accelerator); | |
123 if (it == event_targets_.end()) { | |
124 NOTREACHED(); // Shouldn't get this event for something not registered. | |
125 return; | |
126 } | |
127 | |
128 CommandExecuted(it->second.first, it->second.second); | |
129 } | |
130 | |
131 } // namespace extensions | |
OLD | NEW |