| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/mus/accelerators/accelerator_registrar_impl.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "ash/common/accelerators/accelerator_controller.h" | |
| 11 #include "ash/common/wm_shell.h" | |
| 12 #include "ash/mus/accelerators/accelerator_ids.h" | |
| 13 #include "ash/mus/root_window_controller.h" | |
| 14 #include "ash/mus/window_manager.h" | |
| 15 #include "base/bind.h" | |
| 16 #include "services/ui/public/cpp/window_manager_delegate.h" | |
| 17 #include "ui/events/event.h" | |
| 18 | |
| 19 namespace ash { | |
| 20 namespace mus { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 void CallAddAcceleratorCallback( | |
| 25 const ui::mojom::AcceleratorRegistrar::AddAcceleratorCallback& callback, | |
| 26 bool result) { | |
| 27 callback.Run(result); | |
| 28 } | |
| 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 | |
| 57 } // namespace | |
| 58 | |
| 59 AcceleratorRegistrarImpl::AcceleratorRegistrarImpl( | |
| 60 WindowManager* window_manager, | |
| 61 uint16_t accelerator_namespace, | |
| 62 mojo::InterfaceRequest<AcceleratorRegistrar> request, | |
| 63 const DestroyCallback& destroy_callback) | |
| 64 : window_manager_(window_manager), | |
| 65 binding_(this, std::move(request)), | |
| 66 accelerator_namespace_(accelerator_namespace), | |
| 67 destroy_callback_(destroy_callback) { | |
| 68 window_manager_->AddObserver(this); | |
| 69 window_manager_->AddAcceleratorHandler(accelerator_namespace_, this); | |
| 70 binding_.set_connection_error_handler(base::Bind( | |
| 71 &AcceleratorRegistrarImpl::OnBindingGone, base::Unretained(this))); | |
| 72 } | |
| 73 | |
| 74 void AcceleratorRegistrarImpl::Destroy() { | |
| 75 delete this; | |
| 76 } | |
| 77 | |
| 78 bool AcceleratorRegistrarImpl::OwnsAccelerator(uint32_t accelerator_id) const { | |
| 79 return !!accelerators_.count(accelerator_id); | |
| 80 } | |
| 81 | |
| 82 void AcceleratorRegistrarImpl::ProcessAccelerator(uint32_t accelerator_id, | |
| 83 const ui::Event& event) { | |
| 84 DCHECK(OwnsAccelerator(accelerator_id)); | |
| 85 // TODO(moshayedi): crbug.com/617167. Don't clone even once we map | |
| 86 // mojom::Event directly to ui::Event. | |
| 87 accelerator_handler_->OnAccelerator(accelerator_id & kLocalIdMask, | |
| 88 ui::Event::Clone(event)); | |
| 89 } | |
| 90 | |
| 91 AcceleratorRegistrarImpl::~AcceleratorRegistrarImpl() { | |
| 92 window_manager_->RemoveAcceleratorHandler(accelerator_namespace_); | |
| 93 window_manager_->RemoveObserver(this); | |
| 94 RemoveAllAccelerators(); | |
| 95 destroy_callback_.Run(this); | |
| 96 } | |
| 97 | |
| 98 void AcceleratorRegistrarImpl::OnBindingGone() { | |
| 99 binding_.Unbind(); | |
| 100 // If there's no outstanding accelerators for this connection, then destroy | |
| 101 // it. | |
| 102 if (accelerators_.empty() && keyboard_accelerator_to_id_.empty()) | |
| 103 delete this; | |
| 104 } | |
| 105 | |
| 106 void AcceleratorRegistrarImpl::OnHandlerGone() { | |
| 107 // The handler is dead. If AcceleratorRegistrar connection is also closed, | |
| 108 // then destroy this. Otherwise, remove all the accelerators, but keep the | |
| 109 // AcceleratorRegistrar connection alive (the client could still set another | |
| 110 // handler and install new accelerators). | |
| 111 if (!binding_.is_bound()) { | |
| 112 delete this; | |
| 113 return; | |
| 114 } | |
| 115 accelerator_handler_.reset(); | |
| 116 RemoveAllAccelerators(); | |
| 117 } | |
| 118 | |
| 119 void AcceleratorRegistrarImpl::RemoveAllAccelerators() { | |
| 120 for (uint32_t accelerator : accelerators_) | |
| 121 window_manager_->window_manager_client()->RemoveAccelerator(accelerator); | |
| 122 | |
| 123 WmShell::Get()->accelerator_controller()->UnregisterAll(this); | |
| 124 keyboard_accelerator_to_id_.clear(); | |
| 125 id_to_keyboard_accelerator_.clear(); | |
| 126 | |
| 127 accelerators_.clear(); | |
| 128 } | |
| 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 | |
| 165 void AcceleratorRegistrarImpl::SetHandler( | |
| 166 ui::mojom::AcceleratorHandlerPtr handler) { | |
| 167 accelerator_handler_ = std::move(handler); | |
| 168 accelerator_handler_.set_connection_error_handler(base::Bind( | |
| 169 &AcceleratorRegistrarImpl::OnHandlerGone, base::Unretained(this))); | |
| 170 } | |
| 171 | |
| 172 void AcceleratorRegistrarImpl::AddAccelerator( | |
| 173 uint32_t accelerator_id, | |
| 174 ui::mojom::EventMatcherPtr matcher, | |
| 175 const AddAcceleratorCallback& callback) { | |
| 176 if (!accelerator_handler_ || accelerator_id > 0xFFFF) { | |
| 177 // The |accelerator_id| is too large, and it can't be handled correctly. | |
| 178 callback.Run(false); | |
| 179 DVLOG(1) << "AddAccelerator failed because of bogus id"; | |
| 180 return; | |
| 181 } | |
| 182 | |
| 183 uint32_t namespaced_accelerator_id = ComputeAcceleratorId( | |
| 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 | |
| 198 accelerators_.insert(namespaced_accelerator_id); | |
| 199 window_manager_->window_manager_client()->AddAccelerator( | |
| 200 namespaced_accelerator_id, std::move(matcher), | |
| 201 base::Bind(&CallAddAcceleratorCallback, callback)); | |
| 202 } | |
| 203 | |
| 204 void AcceleratorRegistrarImpl::RemoveAccelerator(uint32_t accelerator_id) { | |
| 205 if (accelerator_id > 0xFFFF) | |
| 206 return; | |
| 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 | |
| 217 uint32_t namespaced_accelerator_id = ComputeAcceleratorId( | |
| 218 accelerator_namespace_, static_cast<uint16_t>(accelerator_id)); | |
| 219 if (accelerators_.erase(namespaced_accelerator_id) == 0) | |
| 220 return; | |
| 221 window_manager_->window_manager_client()->RemoveAccelerator( | |
| 222 namespaced_accelerator_id); | |
| 223 | |
| 224 // If the registrar is not bound anymore (i.e. the client can no longer | |
| 225 // install new accelerators), and the last accelerator has been removed, then | |
| 226 // there's no point keeping this alive anymore. | |
| 227 if (accelerators_.empty() && keyboard_accelerator_to_id_.empty() && | |
| 228 !binding_.is_bound()) { | |
| 229 delete this; | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 ui::mojom::EventResult AcceleratorRegistrarImpl::OnAccelerator( | |
| 234 uint32_t id, | |
| 235 const ui::Event& event) { | |
| 236 if (OwnsAccelerator(id)) | |
| 237 ProcessAccelerator(id, event); | |
| 238 return ui::mojom::EventResult::HANDLED; | |
| 239 } | |
| 240 | |
| 241 void AcceleratorRegistrarImpl::OnWindowTreeClientDestroyed() { | |
| 242 delete this; | |
| 243 } | |
| 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 | |
| 262 } // namespace mus | |
| 263 } // namespace ash | |
| OLD | NEW |