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