OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ash/mus/accelerators/accelerator_registrar_impl.h" | 5 #include "ash/mus/accelerators/accelerator_registrar_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
| 10 #include "ash/common/accelerators/accelerator_controller.h" |
| 11 #include "ash/common/wm_shell.h" |
10 #include "ash/mus/accelerators/accelerator_ids.h" | 12 #include "ash/mus/accelerators/accelerator_ids.h" |
11 #include "ash/mus/root_window_controller.h" | 13 #include "ash/mus/root_window_controller.h" |
12 #include "ash/mus/window_manager.h" | 14 #include "ash/mus/window_manager.h" |
13 #include "base/bind.h" | 15 #include "base/bind.h" |
14 #include "services/ui/public/cpp/window_manager_delegate.h" | 16 #include "services/ui/public/cpp/window_manager_delegate.h" |
| 17 #include "ui/events/event.h" |
15 | 18 |
16 namespace ash { | 19 namespace ash { |
17 namespace mus { | 20 namespace mus { |
18 | 21 |
19 namespace { | 22 namespace { |
20 | 23 |
21 void CallAddAcceleratorCallback( | 24 void CallAddAcceleratorCallback( |
22 const ::ui::mojom::AcceleratorRegistrar::AddAcceleratorCallback& callback, | 25 const ::ui::mojom::AcceleratorRegistrar::AddAcceleratorCallback& callback, |
23 bool result) { | 26 bool result) { |
24 callback.Run(result); | 27 callback.Run(result); |
25 } | 28 } |
26 | 29 |
| 30 // Returns true if |event_matcher| corresponds to matching an Accelerator. |
| 31 bool IsMatcherForKeyAccelerator(const ui::mojom::EventMatcher& event_matcher) { |
| 32 return ( |
| 33 event_matcher.accelerator_phase == |
| 34 ui::mojom::AcceleratorPhase::PRE_TARGET && |
| 35 event_matcher.type_matcher && |
| 36 (event_matcher.type_matcher->type == ui::mojom::EventType::KEY_PRESSED || |
| 37 event_matcher.type_matcher->type == |
| 38 ui::mojom::EventType::KEY_RELEASED) && |
| 39 event_matcher.key_matcher && event_matcher.flags_matcher); |
| 40 } |
| 41 |
| 42 // Converts |event_matcher| into the ui::Accelerator it matches. Only valid if |
| 43 // IsMatcherForKeyAccelerator() returns true. |
| 44 ui::Accelerator EventMatcherToAccelerator( |
| 45 const ui::mojom::EventMatcher& event_matcher) { |
| 46 DCHECK(IsMatcherForKeyAccelerator(event_matcher)); |
| 47 ui::Accelerator accelerator( |
| 48 static_cast<ui::KeyboardCode>(event_matcher.key_matcher->keyboard_code), |
| 49 event_matcher.flags_matcher->flags); |
| 50 accelerator.set_type(event_matcher.type_matcher->type == |
| 51 ui::mojom::EventType::KEY_PRESSED |
| 52 ? ui::ET_KEY_PRESSED |
| 53 : ui::ET_KEY_RELEASED); |
| 54 return accelerator; |
| 55 } |
| 56 |
27 } // namespace | 57 } // namespace |
28 | 58 |
29 AcceleratorRegistrarImpl::AcceleratorRegistrarImpl( | 59 AcceleratorRegistrarImpl::AcceleratorRegistrarImpl( |
30 WindowManager* window_manager, | 60 WindowManager* window_manager, |
31 uint16_t accelerator_namespace, | 61 uint16_t accelerator_namespace, |
32 mojo::InterfaceRequest<AcceleratorRegistrar> request, | 62 mojo::InterfaceRequest<AcceleratorRegistrar> request, |
33 const DestroyCallback& destroy_callback) | 63 const DestroyCallback& destroy_callback) |
34 : window_manager_(window_manager), | 64 : window_manager_(window_manager), |
35 binding_(this, std::move(request)), | 65 binding_(this, std::move(request)), |
36 accelerator_namespace_(accelerator_namespace), | 66 accelerator_namespace_(accelerator_namespace), |
(...skipping 25 matching lines...) Expand all Loading... |
62 window_manager_->RemoveAcceleratorHandler(accelerator_namespace_); | 92 window_manager_->RemoveAcceleratorHandler(accelerator_namespace_); |
63 window_manager_->RemoveObserver(this); | 93 window_manager_->RemoveObserver(this); |
64 RemoveAllAccelerators(); | 94 RemoveAllAccelerators(); |
65 destroy_callback_.Run(this); | 95 destroy_callback_.Run(this); |
66 } | 96 } |
67 | 97 |
68 void AcceleratorRegistrarImpl::OnBindingGone() { | 98 void AcceleratorRegistrarImpl::OnBindingGone() { |
69 binding_.Unbind(); | 99 binding_.Unbind(); |
70 // If there's no outstanding accelerators for this connection, then destroy | 100 // If there's no outstanding accelerators for this connection, then destroy |
71 // it. | 101 // it. |
72 if (accelerators_.empty()) | 102 if (accelerators_.empty() && keyboard_accelerator_to_id_.empty()) |
73 delete this; | 103 delete this; |
74 } | 104 } |
75 | 105 |
76 void AcceleratorRegistrarImpl::OnHandlerGone() { | 106 void AcceleratorRegistrarImpl::OnHandlerGone() { |
77 // The handler is dead. If AcceleratorRegistrar connection is also closed, | 107 // The handler is dead. If AcceleratorRegistrar connection is also closed, |
78 // then destroy this. Otherwise, remove all the accelerators, but keep the | 108 // then destroy this. Otherwise, remove all the accelerators, but keep the |
79 // AcceleratorRegistrar connection alive (the client could still set another | 109 // AcceleratorRegistrar connection alive (the client could still set another |
80 // handler and install new accelerators). | 110 // handler and install new accelerators). |
81 if (!binding_.is_bound()) { | 111 if (!binding_.is_bound()) { |
82 delete this; | 112 delete this; |
83 return; | 113 return; |
84 } | 114 } |
85 accelerator_handler_.reset(); | 115 accelerator_handler_.reset(); |
86 RemoveAllAccelerators(); | 116 RemoveAllAccelerators(); |
87 } | 117 } |
88 | 118 |
89 void AcceleratorRegistrarImpl::RemoveAllAccelerators() { | 119 void AcceleratorRegistrarImpl::RemoveAllAccelerators() { |
90 for (uint32_t accelerator : accelerators_) | 120 for (uint32_t accelerator : accelerators_) |
91 window_manager_->window_manager_client()->RemoveAccelerator(accelerator); | 121 window_manager_->window_manager_client()->RemoveAccelerator(accelerator); |
92 | 122 |
| 123 WmShell::Get()->accelerator_controller()->UnregisterAll(this); |
| 124 keyboard_accelerator_to_id_.clear(); |
| 125 id_to_keyboard_accelerator_.clear(); |
| 126 |
93 accelerators_.clear(); | 127 accelerators_.clear(); |
94 } | 128 } |
95 | 129 |
| 130 bool AcceleratorRegistrarImpl::AddAcceleratorForKeyBinding( |
| 131 uint32_t accelerator_id, |
| 132 const ::ui::mojom::EventMatcher& matcher, |
| 133 const AddAcceleratorCallback& callback) { |
| 134 if (!IsMatcherForKeyAccelerator(matcher)) |
| 135 return false; |
| 136 |
| 137 const ui::Accelerator accelerator = EventMatcherToAccelerator(matcher); |
| 138 if (keyboard_accelerator_to_id_.count(accelerator)) { |
| 139 callback.Run(false); |
| 140 return true; |
| 141 } |
| 142 |
| 143 AcceleratorController* accelerator_controller = |
| 144 WmShell::Get()->accelerator_controller(); |
| 145 // TODO(sky): reenable this when we decide on the future of AppDriver. |
| 146 // http://crbug.com/631836. |
| 147 /* |
| 148 if (accelerator_controller->IsRegistered(accelerator)) { |
| 149 DVLOG(1) << "Attempt to register accelerator that is already registered"; |
| 150 callback.Run(false); |
| 151 // Even though we're not registering the accelerator it's a key that should |
| 152 // be handled as an accelerator, so return true. |
| 153 return true; |
| 154 } |
| 155 */ |
| 156 |
| 157 const uint16_t local_id = GetAcceleratorLocalId(accelerator_id); |
| 158 keyboard_accelerator_to_id_[accelerator] = local_id; |
| 159 id_to_keyboard_accelerator_[local_id] = accelerator; |
| 160 accelerator_controller->Register(accelerator, this); |
| 161 callback.Run(true); |
| 162 return true; |
| 163 } |
| 164 |
96 void AcceleratorRegistrarImpl::SetHandler( | 165 void AcceleratorRegistrarImpl::SetHandler( |
97 ::ui::mojom::AcceleratorHandlerPtr handler) { | 166 ::ui::mojom::AcceleratorHandlerPtr handler) { |
98 accelerator_handler_ = std::move(handler); | 167 accelerator_handler_ = std::move(handler); |
99 accelerator_handler_.set_connection_error_handler(base::Bind( | 168 accelerator_handler_.set_connection_error_handler(base::Bind( |
100 &AcceleratorRegistrarImpl::OnHandlerGone, base::Unretained(this))); | 169 &AcceleratorRegistrarImpl::OnHandlerGone, base::Unretained(this))); |
101 } | 170 } |
102 | 171 |
103 void AcceleratorRegistrarImpl::AddAccelerator( | 172 void AcceleratorRegistrarImpl::AddAccelerator( |
104 uint32_t accelerator_id, | 173 uint32_t accelerator_id, |
105 ::ui::mojom::EventMatcherPtr matcher, | 174 ::ui::mojom::EventMatcherPtr matcher, |
106 const AddAcceleratorCallback& callback) { | 175 const AddAcceleratorCallback& callback) { |
107 if (!accelerator_handler_ || accelerator_id > 0xFFFF) { | 176 if (!accelerator_handler_ || accelerator_id > 0xFFFF) { |
108 // The |accelerator_id| is too large, and it can't be handled correctly. | 177 // The |accelerator_id| is too large, and it can't be handled correctly. |
109 callback.Run(false); | 178 callback.Run(false); |
110 DVLOG(1) << "AddAccelerator failed because of bogus id"; | 179 DVLOG(1) << "AddAccelerator failed because of bogus id"; |
111 return; | 180 return; |
112 } | 181 } |
| 182 |
113 uint32_t namespaced_accelerator_id = ComputeAcceleratorId( | 183 uint32_t namespaced_accelerator_id = ComputeAcceleratorId( |
114 accelerator_namespace_, static_cast<uint16_t>(accelerator_id)); | 184 accelerator_namespace_, static_cast<uint16_t>(accelerator_id)); |
| 185 |
| 186 if (accelerators_.count(namespaced_accelerator_id) || |
| 187 id_to_keyboard_accelerator_.count( |
| 188 GetAcceleratorLocalId(namespaced_accelerator_id))) { |
| 189 callback.Run(false); |
| 190 DVLOG(1) << "AddAccelerator failed because id already in use " |
| 191 << accelerator_id; |
| 192 return; |
| 193 } |
| 194 |
| 195 if (AddAcceleratorForKeyBinding(accelerator_id, *matcher, callback)) |
| 196 return; |
| 197 |
115 accelerators_.insert(namespaced_accelerator_id); | 198 accelerators_.insert(namespaced_accelerator_id); |
116 window_manager_->window_manager_client()->AddAccelerator( | 199 window_manager_->window_manager_client()->AddAccelerator( |
117 namespaced_accelerator_id, std::move(matcher), | 200 namespaced_accelerator_id, std::move(matcher), |
118 base::Bind(&CallAddAcceleratorCallback, callback)); | 201 base::Bind(&CallAddAcceleratorCallback, callback)); |
119 } | 202 } |
120 | 203 |
121 void AcceleratorRegistrarImpl::RemoveAccelerator(uint32_t accelerator_id) { | 204 void AcceleratorRegistrarImpl::RemoveAccelerator(uint32_t accelerator_id) { |
122 if (accelerator_id > 0xFFFF) | 205 if (accelerator_id > 0xFFFF) |
123 return; | 206 return; |
124 | 207 |
| 208 const uint16_t local_id = GetAcceleratorLocalId(accelerator_id); |
| 209 auto iter = id_to_keyboard_accelerator_.find(local_id); |
| 210 if (iter != id_to_keyboard_accelerator_.end()) { |
| 211 WmShell::Get()->accelerator_controller()->Unregister(iter->second, this); |
| 212 keyboard_accelerator_to_id_.erase(iter->second); |
| 213 id_to_keyboard_accelerator_.erase(iter); |
| 214 return; |
| 215 } |
| 216 |
125 uint32_t namespaced_accelerator_id = ComputeAcceleratorId( | 217 uint32_t namespaced_accelerator_id = ComputeAcceleratorId( |
126 accelerator_namespace_, static_cast<uint16_t>(accelerator_id)); | 218 accelerator_namespace_, static_cast<uint16_t>(accelerator_id)); |
127 if (accelerators_.erase(namespaced_accelerator_id) == 0) | 219 if (accelerators_.erase(namespaced_accelerator_id) == 0) |
128 return; | 220 return; |
129 window_manager_->window_manager_client()->RemoveAccelerator( | 221 window_manager_->window_manager_client()->RemoveAccelerator( |
130 namespaced_accelerator_id); | 222 namespaced_accelerator_id); |
131 | 223 |
132 // If the registrar is not bound anymore (i.e. the client can no longer | 224 // If the registrar is not bound anymore (i.e. the client can no longer |
133 // install new accelerators), and the last accelerator has been removed, then | 225 // install new accelerators), and the last accelerator has been removed, then |
134 // there's no point keeping this alive anymore. | 226 // there's no point keeping this alive anymore. |
135 if (accelerators_.empty() && !binding_.is_bound()) | 227 if (accelerators_.empty() && keyboard_accelerator_to_id_.empty() && |
| 228 !binding_.is_bound()) { |
136 delete this; | 229 delete this; |
| 230 } |
137 } | 231 } |
138 | 232 |
139 ui::mojom::EventResult AcceleratorRegistrarImpl::OnAccelerator( | 233 ui::mojom::EventResult AcceleratorRegistrarImpl::OnAccelerator( |
140 uint32_t id, | 234 uint32_t id, |
141 const ui::Event& event) { | 235 const ui::Event& event) { |
142 if (OwnsAccelerator(id)) | 236 if (OwnsAccelerator(id)) |
143 ProcessAccelerator(id, event); | 237 ProcessAccelerator(id, event); |
144 return ui::mojom::EventResult::HANDLED; | 238 return ui::mojom::EventResult::HANDLED; |
145 } | 239 } |
146 | 240 |
147 void AcceleratorRegistrarImpl::OnWindowTreeClientDestroyed() { | 241 void AcceleratorRegistrarImpl::OnWindowTreeClientDestroyed() { |
148 delete this; | 242 delete this; |
149 } | 243 } |
150 | 244 |
| 245 bool AcceleratorRegistrarImpl::AcceleratorPressed( |
| 246 const ui::Accelerator& accelerator) { |
| 247 auto iter = keyboard_accelerator_to_id_.find(accelerator); |
| 248 DCHECK(iter != keyboard_accelerator_to_id_.end()); |
| 249 const ui::KeyEvent key_event(accelerator.type(), accelerator.key_code(), |
| 250 accelerator.modifiers()); |
| 251 // TODO(moshayedi): crbug.com/617167. Don't clone even once we map |
| 252 // mojom::Event directly to ui::Event. |
| 253 accelerator_handler_->OnAccelerator(iter->second, |
| 254 ui::Event::Clone(key_event)); |
| 255 return true; |
| 256 } |
| 257 |
| 258 bool AcceleratorRegistrarImpl::CanHandleAccelerators() const { |
| 259 return true; |
| 260 } |
| 261 |
151 } // namespace mus | 262 } // namespace mus |
152 } // namespace ash | 263 } // namespace ash |
OLD | NEW |