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