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