| 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/accelerator_registrar_impl.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "ash/mus/root_window_controller.h" | |
| 11 #include "ash/mus/window_manager.h" | |
| 12 #include "base/bind.h" | |
| 13 #include "services/ui/public/cpp/window_manager_delegate.h" | |
| 14 | |
| 15 namespace ash { | |
| 16 namespace mus { | |
| 17 | |
| 18 namespace { | |
| 19 const int kAcceleratorIdMask = 0xffff; | |
| 20 | |
| 21 void CallAddAcceleratorCallback( | |
| 22 const ::ui::mojom::AcceleratorRegistrar::AddAcceleratorCallback& callback, | |
| 23 bool result) { | |
| 24 callback.Run(result); | |
| 25 } | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 AcceleratorRegistrarImpl::AcceleratorRegistrarImpl( | |
| 30 WindowManager* window_manager, | |
| 31 uint32_t accelerator_namespace, | |
| 32 mojo::InterfaceRequest<AcceleratorRegistrar> request, | |
| 33 const DestroyCallback& destroy_callback) | |
| 34 : window_manager_(window_manager), | |
| 35 binding_(this, std::move(request)), | |
| 36 accelerator_namespace_(accelerator_namespace & 0xffff), | |
| 37 destroy_callback_(destroy_callback) { | |
| 38 window_manager_->AddObserver(this); | |
| 39 binding_.set_connection_error_handler(base::Bind( | |
| 40 &AcceleratorRegistrarImpl::OnBindingGone, base::Unretained(this))); | |
| 41 } | |
| 42 | |
| 43 void AcceleratorRegistrarImpl::Destroy() { | |
| 44 delete this; | |
| 45 } | |
| 46 | |
| 47 bool AcceleratorRegistrarImpl::OwnsAccelerator(uint32_t accelerator_id) const { | |
| 48 return !!accelerators_.count(accelerator_id); | |
| 49 } | |
| 50 | |
| 51 void AcceleratorRegistrarImpl::ProcessAccelerator(uint32_t accelerator_id, | |
| 52 const ui::Event& event) { | |
| 53 DCHECK(OwnsAccelerator(accelerator_id)); | |
| 54 // TODO(moshayedi): crbug.com/617167. Don't clone even once we map | |
| 55 // mojom::Event directly to ui::Event. | |
| 56 accelerator_handler_->OnAccelerator(accelerator_id & kAcceleratorIdMask, | |
| 57 ui::Event::Clone(event)); | |
| 58 } | |
| 59 | |
| 60 AcceleratorRegistrarImpl::~AcceleratorRegistrarImpl() { | |
| 61 window_manager_->RemoveObserver(this); | |
| 62 RemoveAllAccelerators(); | |
| 63 destroy_callback_.Run(this); | |
| 64 } | |
| 65 | |
| 66 uint32_t AcceleratorRegistrarImpl::ComputeAcceleratorId( | |
| 67 uint32_t accelerator_id) const { | |
| 68 return (accelerator_namespace_ << 16) | (accelerator_id & kAcceleratorIdMask); | |
| 69 } | |
| 70 | |
| 71 void AcceleratorRegistrarImpl::OnBindingGone() { | |
| 72 binding_.Unbind(); | |
| 73 // If there's no outstanding accelerators for this connection, then destroy | |
| 74 // it. | |
| 75 if (accelerators_.empty()) | |
| 76 delete this; | |
| 77 } | |
| 78 | |
| 79 void AcceleratorRegistrarImpl::OnHandlerGone() { | |
| 80 // The handler is dead. If AcceleratorRegistrar connection is also closed, | |
| 81 // then destroy this. Otherwise, remove all the accelerators, but keep the | |
| 82 // AcceleratorRegistrar connection alive (the client could still set another | |
| 83 // handler and install new accelerators). | |
| 84 if (!binding_.is_bound()) { | |
| 85 delete this; | |
| 86 return; | |
| 87 } | |
| 88 accelerator_handler_.reset(); | |
| 89 RemoveAllAccelerators(); | |
| 90 } | |
| 91 | |
| 92 void AcceleratorRegistrarImpl::RemoveAllAccelerators() { | |
| 93 for (uint32_t accelerator : accelerators_) | |
| 94 window_manager_->window_manager_client()->RemoveAccelerator(accelerator); | |
| 95 | |
| 96 accelerators_.clear(); | |
| 97 } | |
| 98 | |
| 99 void AcceleratorRegistrarImpl::SetHandler( | |
| 100 ::ui::mojom::AcceleratorHandlerPtr handler) { | |
| 101 accelerator_handler_ = std::move(handler); | |
| 102 accelerator_handler_.set_connection_error_handler(base::Bind( | |
| 103 &AcceleratorRegistrarImpl::OnHandlerGone, base::Unretained(this))); | |
| 104 } | |
| 105 | |
| 106 void AcceleratorRegistrarImpl::AddAccelerator( | |
| 107 uint32_t accelerator_id, | |
| 108 ::ui::mojom::EventMatcherPtr matcher, | |
| 109 const AddAcceleratorCallback& callback) { | |
| 110 if (!accelerator_handler_ || | |
| 111 (accelerator_id & kAcceleratorIdMask) != accelerator_id) { | |
| 112 // The |accelerator_id| is too large, and it can't be handled correctly. | |
| 113 callback.Run(false); | |
| 114 return; | |
| 115 } | |
| 116 uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); | |
| 117 accelerators_.insert(namespaced_accelerator_id); | |
| 118 window_manager_->window_manager_client()->AddAccelerator( | |
| 119 namespaced_accelerator_id, std::move(matcher), | |
| 120 base::Bind(&CallAddAcceleratorCallback, callback)); | |
| 121 } | |
| 122 | |
| 123 void AcceleratorRegistrarImpl::RemoveAccelerator(uint32_t accelerator_id) { | |
| 124 uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); | |
| 125 if (accelerators_.erase(namespaced_accelerator_id) == 0) | |
| 126 return; | |
| 127 window_manager_->window_manager_client()->RemoveAccelerator( | |
| 128 namespaced_accelerator_id); | |
| 129 | |
| 130 // If the registrar is not bound anymore (i.e. the client can no longer | |
| 131 // install new accelerators), and the last accelerator has been removed, then | |
| 132 // there's no point keeping this alive anymore. | |
| 133 if (accelerators_.empty() && !binding_.is_bound()) | |
| 134 delete this; | |
| 135 } | |
| 136 | |
| 137 void AcceleratorRegistrarImpl::OnAccelerator(uint32_t id, | |
| 138 const ui::Event& event) { | |
| 139 if (OwnsAccelerator(id)) | |
| 140 ProcessAccelerator(id, event); | |
| 141 } | |
| 142 | |
| 143 void AcceleratorRegistrarImpl::OnWindowTreeClientDestroyed() { | |
| 144 delete this; | |
| 145 } | |
| 146 | |
| 147 } // namespace mus | |
| 148 } // namespace ash | |
| OLD | NEW |