| 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 "mash/wm/accelerator_registrar_impl.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "components/mus/public/cpp/window_manager_delegate.h" | |
| 12 #include "mash/wm/root_window_controller.h" | |
| 13 #include "mash/wm/window_manager.h" | |
| 14 #include "mash/wm/window_manager_application.h" | |
| 15 | |
| 16 namespace mash { | |
| 17 namespace wm { | |
| 18 | |
| 19 namespace { | |
| 20 const int kAcceleratorIdMask = 0xffff; | |
| 21 | |
| 22 void OnAcceleratorAdded(bool result) {} | |
| 23 void CallAddAcceleratorCallback( | |
| 24 const mus::mojom::AcceleratorRegistrar::AddAcceleratorCallback& callback, | |
| 25 bool result) { | |
| 26 callback.Run(result); | |
| 27 } | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 struct AcceleratorRegistrarImpl::Accelerator { | |
| 32 mus::mojom::EventMatcherPtr event_matcher; | |
| 33 AddAcceleratorCallback callback; | |
| 34 bool callback_used = false; | |
| 35 }; | |
| 36 | |
| 37 AcceleratorRegistrarImpl::AcceleratorRegistrarImpl( | |
| 38 WindowManagerApplication* wm_app, | |
| 39 uint32_t accelerator_namespace, | |
| 40 mojo::InterfaceRequest<AcceleratorRegistrar> request, | |
| 41 const DestroyCallback& destroy_callback) | |
| 42 : wm_app_(wm_app), | |
| 43 binding_(this, std::move(request)), | |
| 44 accelerator_namespace_(accelerator_namespace & 0xffff), | |
| 45 destroy_callback_(destroy_callback) { | |
| 46 wm_app_->AddRootWindowsObserver(this); | |
| 47 binding_.set_connection_error_handler(base::Bind( | |
| 48 &AcceleratorRegistrarImpl::OnBindingGone, base::Unretained(this))); | |
| 49 } | |
| 50 | |
| 51 void AcceleratorRegistrarImpl::Destroy() { | |
| 52 delete this; | |
| 53 } | |
| 54 | |
| 55 bool AcceleratorRegistrarImpl::OwnsAccelerator(uint32_t accelerator_id) const { | |
| 56 return !!accelerators_.count(accelerator_id); | |
| 57 } | |
| 58 | |
| 59 void AcceleratorRegistrarImpl::ProcessAccelerator(uint32_t accelerator_id, | |
| 60 mus::mojom::EventPtr event) { | |
| 61 DCHECK(OwnsAccelerator(accelerator_id)); | |
| 62 accelerator_handler_->OnAccelerator(accelerator_id & kAcceleratorIdMask, | |
| 63 std::move(event)); | |
| 64 } | |
| 65 | |
| 66 AcceleratorRegistrarImpl::~AcceleratorRegistrarImpl() { | |
| 67 wm_app_->RemoveRootWindowsObserver(this); | |
| 68 RemoveAllAccelerators(); | |
| 69 destroy_callback_.Run(this); | |
| 70 } | |
| 71 | |
| 72 uint32_t AcceleratorRegistrarImpl::ComputeAcceleratorId( | |
| 73 uint32_t accelerator_id) const { | |
| 74 return (accelerator_namespace_ << 16) | (accelerator_id & kAcceleratorIdMask); | |
| 75 } | |
| 76 | |
| 77 void AcceleratorRegistrarImpl::OnBindingGone() { | |
| 78 binding_.Unbind(); | |
| 79 // If there's no outstanding accelerators for this connection, then destroy | |
| 80 // it. | |
| 81 if (accelerators_.empty()) | |
| 82 delete this; | |
| 83 } | |
| 84 | |
| 85 void AcceleratorRegistrarImpl::OnHandlerGone() { | |
| 86 // The handler is dead. If AcceleratorRegistrar connection is also closed, | |
| 87 // then destroy this. Otherwise, remove all the accelerators, but keep the | |
| 88 // AcceleratorRegistrar connection alive (the client could still set another | |
| 89 // handler and install new accelerators). | |
| 90 if (!binding_.is_bound()) { | |
| 91 delete this; | |
| 92 return; | |
| 93 } | |
| 94 accelerator_handler_.reset(); | |
| 95 RemoveAllAccelerators(); | |
| 96 } | |
| 97 | |
| 98 void AcceleratorRegistrarImpl::AddAcceleratorToRoot( | |
| 99 RootWindowController* root, | |
| 100 uint32_t namespaced_accelerator_id) { | |
| 101 Accelerator& accelerator = accelerators_[namespaced_accelerator_id]; | |
| 102 AddAcceleratorCallback callback = accelerator.callback_used | |
| 103 ? base::Bind(&OnAcceleratorAdded) | |
| 104 : accelerator.callback; | |
| 105 // Ensure we only notify the callback once (as happens with mojoms). | |
| 106 accelerator.callback_used = true; | |
| 107 root->window_manager()->window_manager_client()->AddAccelerator( | |
| 108 namespaced_accelerator_id, accelerator.event_matcher.Clone(), | |
| 109 base::Bind(&CallAddAcceleratorCallback, callback)); | |
| 110 } | |
| 111 | |
| 112 void AcceleratorRegistrarImpl::RemoveAllAccelerators() { | |
| 113 for (const auto& pair : accelerators_) { | |
| 114 for (RootWindowController* root : wm_app_->GetRootControllers()) { | |
| 115 root->window_manager()->window_manager_client()->RemoveAccelerator( | |
| 116 pair.first); | |
| 117 } | |
| 118 } | |
| 119 accelerators_.clear(); | |
| 120 } | |
| 121 | |
| 122 void AcceleratorRegistrarImpl::SetHandler( | |
| 123 mus::mojom::AcceleratorHandlerPtr handler) { | |
| 124 accelerator_handler_ = std::move(handler); | |
| 125 accelerator_handler_.set_connection_error_handler(base::Bind( | |
| 126 &AcceleratorRegistrarImpl::OnHandlerGone, base::Unretained(this))); | |
| 127 } | |
| 128 | |
| 129 void AcceleratorRegistrarImpl::AddAccelerator( | |
| 130 uint32_t accelerator_id, | |
| 131 mus::mojom::EventMatcherPtr matcher, | |
| 132 const AddAcceleratorCallback& callback) { | |
| 133 if (!accelerator_handler_ || | |
| 134 (accelerator_id & kAcceleratorIdMask) != accelerator_id) { | |
| 135 // The |accelerator_id| is too large, and it can't be handled correctly. | |
| 136 callback.Run(false); | |
| 137 return; | |
| 138 } | |
| 139 uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); | |
| 140 accelerators_[namespaced_accelerator_id].event_matcher = matcher->Clone(); | |
| 141 accelerators_[namespaced_accelerator_id].callback = callback; | |
| 142 accelerators_[namespaced_accelerator_id].callback_used = false; | |
| 143 for (RootWindowController* root : wm_app_->GetRootControllers()) | |
| 144 AddAcceleratorToRoot(root, namespaced_accelerator_id); | |
| 145 } | |
| 146 | |
| 147 void AcceleratorRegistrarImpl::RemoveAccelerator(uint32_t accelerator_id) { | |
| 148 uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); | |
| 149 auto iter = accelerators_.find(namespaced_accelerator_id); | |
| 150 if (iter == accelerators_.end()) | |
| 151 return; | |
| 152 for (RootWindowController* root : wm_app_->GetRootControllers()) { | |
| 153 root->window_manager()->window_manager_client()->RemoveAccelerator( | |
| 154 namespaced_accelerator_id); | |
| 155 } | |
| 156 accelerators_.erase(iter); | |
| 157 // If the registrar is not bound anymore (i.e. the client can no longer | |
| 158 // install new accelerators), and the last accelerator has been removed, then | |
| 159 // there's no point keeping this alive anymore. | |
| 160 if (accelerators_.empty() && !binding_.is_bound()) | |
| 161 delete this; | |
| 162 } | |
| 163 | |
| 164 void AcceleratorRegistrarImpl::OnRootWindowControllerAdded( | |
| 165 RootWindowController* controller) { | |
| 166 for (const auto& pair : accelerators_) | |
| 167 AddAcceleratorToRoot(controller, pair.first); | |
| 168 } | |
| 169 | |
| 170 } // namespace wm | |
| 171 } // namespace mash | |
| OLD | NEW |