OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/cocoa/extensions/extension_keybinding_registry_cocoa .h" | 5 #include "chrome/browser/ui/cocoa/extensions/extension_keybinding_registry_cocoa .h" |
6 | 6 |
7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/extensions/api/commands/command_service.h" | 8 #include "chrome/browser/extensions/api/commands/command_service.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
(...skipping 27 matching lines...) Expand all Loading... | |
38 } | 38 } |
39 | 39 |
40 bool ExtensionKeybindingRegistryCocoa::ProcessKeyEvent( | 40 bool ExtensionKeybindingRegistryCocoa::ProcessKeyEvent( |
41 const content::NativeWebKeyboardEvent& event) { | 41 const content::NativeWebKeyboardEvent& event) { |
42 if (shortcut_handling_suspended_) | 42 if (shortcut_handling_suspended_) |
43 return false; | 43 return false; |
44 | 44 |
45 ui::Accelerator accelerator( | 45 ui::Accelerator accelerator( |
46 static_cast<ui::KeyboardCode>(event.windowsKeyCode), | 46 static_cast<ui::KeyboardCode>(event.windowsKeyCode), |
47 content::GetModifiersFromNativeWebKeyboardEvent(event)); | 47 content::GetModifiersFromNativeWebKeyboardEvent(event)); |
48 EventTargets::iterator it = event_targets_.find(accelerator); | 48 EventTargets::iterator targets = event_targets_.find(accelerator); |
49 if (it == event_targets_.end()) | 49 if (targets == event_targets_.end()) |
50 return false; | 50 return false; |
51 | 51 |
52 std::string extension_id = it->second.first; | 52 TargetList::const_iterator first_target = targets->second.begin(); |
53 std::string command_name = it->second.second; | 53 if (first_target == targets->second.end()) |
sky
2013/11/19 16:54:47
Is there a reason to allow an empty TargetList?
zhchbin
2013/11/20 02:05:40
Done.
Ping Finnur here, I have change it to DCHEC
Finnur
2013/11/20 13:26:38
No, there is no reason to allow it.
On 2013/11/2
| |
54 return false; | |
55 | |
56 std::string extension_id = first_target->first; | |
57 std::string command_name = first_target->second; | |
54 int type = 0; | 58 int type = 0; |
55 if (command_name == values::kPageActionCommandEvent) { | 59 if (command_name == values::kPageActionCommandEvent) { |
56 type = chrome::NOTIFICATION_EXTENSION_COMMAND_PAGE_ACTION_MAC; | 60 type = chrome::NOTIFICATION_EXTENSION_COMMAND_PAGE_ACTION_MAC; |
57 } else if (command_name == values::kBrowserActionCommandEvent) { | 61 } else if (command_name == values::kBrowserActionCommandEvent) { |
58 type = chrome::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC; | 62 type = chrome::NOTIFICATION_EXTENSION_COMMAND_BROWSER_ACTION_MAC; |
59 } else if (command_name == values::kScriptBadgeCommandEvent) { | 63 } else if (command_name == values::kScriptBadgeCommandEvent) { |
60 type = chrome::NOTIFICATION_EXTENSION_COMMAND_SCRIPT_BADGE_MAC; | 64 type = chrome::NOTIFICATION_EXTENSION_COMMAND_SCRIPT_BADGE_MAC; |
61 } else { | 65 } else { |
62 // Not handled by using notifications. Route it through the Browser Event | 66 // Not handled by using notifications. Route it through the Browser Event |
63 // Router. | 67 // Router using the base class (it will iterate through all targets). |
64 CommandExecuted(extension_id, command_name); | 68 return ExtensionKeybindingRegistry::NotifyEventTargets(accelerator); |
65 return true; | |
66 } | 69 } |
67 | 70 |
71 // Type != named command, so we need to dispatch this event directly. | |
68 std::pair<const std::string, gfx::NativeWindow> details = | 72 std::pair<const std::string, gfx::NativeWindow> details = |
69 std::make_pair(extension_id, window_); | 73 std::make_pair(extension_id, window_); |
70 content::NotificationService::current()->Notify( | 74 content::NotificationService::current()->Notify( |
71 type, | 75 type, |
72 content::Source<Profile>(profile_), | 76 content::Source<Profile>(profile_), |
73 content::Details< | 77 content::Details< |
74 std::pair<const std::string, gfx::NativeWindow> >(&details)); | 78 std::pair<const std::string, gfx::NativeWindow> >(&details)); |
79 // We expect only one target for these types of events. | |
80 DCHECK(++first_target == targets->second.end()); | |
sky
2013/11/19 16:54:47
Having side effects like this in a DCHECK is error
zhchbin
2013/11/20 02:05:40
Done.
| |
75 return true; | 81 return true; |
76 } | 82 } |
77 | 83 |
78 void ExtensionKeybindingRegistryCocoa::AddExtensionKeybinding( | 84 void ExtensionKeybindingRegistryCocoa::AddExtensionKeybinding( |
79 const extensions::Extension* extension, | 85 const extensions::Extension* extension, |
80 const std::string& command_name) { | 86 const std::string& command_name) { |
81 extensions::CommandService* command_service = | 87 extensions::CommandService* command_service = |
82 extensions::CommandService::Get(profile_); | 88 extensions::CommandService::Get(profile_); |
83 extensions::CommandMap commands; | 89 extensions::CommandMap commands; |
84 command_service->GetNamedCommands( | 90 command_service->GetNamedCommands( |
85 extension->id(), | 91 extension->id(), |
86 extensions::CommandService::ACTIVE_ONLY, | 92 extensions::CommandService::ACTIVE_ONLY, |
87 extensions::CommandService::REGULAR, | 93 extensions::CommandService::REGULAR, |
88 &commands); | 94 &commands); |
89 | 95 |
90 for (extensions::CommandMap::const_iterator iter = commands.begin(); | 96 for (extensions::CommandMap::const_iterator iter = commands.begin(); |
91 iter != commands.end(); ++iter) { | 97 iter != commands.end(); ++iter) { |
92 if (!command_name.empty() && (iter->second.command_name() != command_name)) | 98 if (!command_name.empty() && (iter->second.command_name() != command_name)) |
93 continue; | 99 continue; |
94 | 100 |
95 ui::Accelerator accelerator(iter->second.accelerator()); | 101 ui::Accelerator accelerator(iter->second.accelerator()); |
96 event_targets_[accelerator] = | 102 event_targets_[accelerator].push_back( |
97 std::make_pair(extension->id(), iter->second.command_name()); | 103 std::make_pair(extension->id(), iter->second.command_name())); |
104 // Shortcuts except media keys have only one target in the list. See | |
105 // comment about |event_targets_|. | |
106 if (!extensions::CommandService::IsMediaKey(iter->second.accelerator())) | |
107 DCHECK(event_targets_[iter->second.accelerator()].size() == 1); | |
sky
2013/11/19 16:54:47
DCHECK_EQ where appropriate.
zhchbin
2013/11/20 02:05:40
Done.
| |
98 } | 108 } |
99 | 109 |
100 // Mac implemenetation behaves like GTK with regards to what is kept in the | 110 // Mac implemenetation behaves like GTK with regards to what is kept in the |
101 // event_targets_ map, because both GTK and Mac need to keep track of Browser | 111 // event_targets_ map, because both GTK and Mac need to keep track of Browser |
102 // and Page actions, as well as Script Badges. | 112 // and Page actions, as well as Script Badges. |
103 extensions::Command browser_action; | 113 extensions::Command browser_action; |
104 if (command_service->GetBrowserActionCommand( | 114 if (command_service->GetBrowserActionCommand( |
105 extension->id(), | 115 extension->id(), |
106 extensions::CommandService::ACTIVE_ONLY, | 116 extensions::CommandService::ACTIVE_ONLY, |
107 &browser_action, | 117 &browser_action, |
108 NULL)) { | 118 NULL)) { |
109 ui::Accelerator accelerator(browser_action.accelerator()); | 119 ui::Accelerator accelerator(browser_action.accelerator()); |
110 event_targets_[accelerator] = | 120 event_targets_[accelerator].push_back( |
111 std::make_pair(extension->id(), browser_action.command_name()); | 121 std::make_pair(extension->id(), browser_action.command_name())); |
122 // We should have only one target. See comment about |event_targets_|. | |
123 DCHECK(event_targets_[accelerator].size() == 1); | |
112 } | 124 } |
113 | 125 |
114 // Add the Page Action (if any). | 126 // Add the Page Action (if any). |
115 extensions::Command page_action; | 127 extensions::Command page_action; |
116 if (command_service->GetPageActionCommand( | 128 if (command_service->GetPageActionCommand( |
117 extension->id(), | 129 extension->id(), |
118 extensions::CommandService::ACTIVE_ONLY, | 130 extensions::CommandService::ACTIVE_ONLY, |
119 &page_action, | 131 &page_action, |
120 NULL)) { | 132 NULL)) { |
121 ui::Accelerator accelerator(page_action.accelerator()); | 133 ui::Accelerator accelerator(page_action.accelerator()); |
122 event_targets_[accelerator] = | 134 event_targets_[accelerator].push_back( |
123 std::make_pair(extension->id(), page_action.command_name()); | 135 std::make_pair(extension->id(), page_action.command_name())); |
136 DCHECK(event_targets_[accelerator].size() == 1); // Ditto. | |
sky
2013/11/19 16:54:47
Ditto ~15 lines away from a comment is not clear.
zhchbin
2013/11/20 02:05:40
Done.
| |
124 } | 137 } |
125 | 138 |
126 // Add the Script Badge (if any). | 139 // Add the Script Badge (if any). |
127 extensions::Command script_badge; | 140 extensions::Command script_badge; |
128 if (command_service->GetScriptBadgeCommand( | 141 if (command_service->GetScriptBadgeCommand( |
129 extension->id(), | 142 extension->id(), |
130 extensions::CommandService::ACTIVE_ONLY, | 143 extensions::CommandService::ACTIVE_ONLY, |
131 &script_badge, | 144 &script_badge, |
132 NULL)) { | 145 NULL)) { |
133 ui::Accelerator accelerator(script_badge.accelerator()); | 146 ui::Accelerator accelerator(script_badge.accelerator()); |
134 event_targets_[accelerator] = | 147 event_targets_[accelerator].push_back( |
135 std::make_pair(extension->id(), script_badge.command_name()); | 148 std::make_pair(extension->id(), script_badge.command_name())); |
149 DCHECK(event_targets_[accelerator].size() == 1); // Ditto. | |
sky
2013/11/19 16:54:47
Even more so here.
zhchbin
2013/11/20 02:05:40
Done.
| |
136 } | 150 } |
137 } | 151 } |
138 | 152 |
139 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybindingImpl( | 153 void ExtensionKeybindingRegistryCocoa::RemoveExtensionKeybindingImpl( |
140 const ui::Accelerator& accelerator, | 154 const ui::Accelerator& accelerator, |
141 const std::string& command_name) { | 155 const std::string& command_name) { |
142 } | 156 } |
OLD | NEW |