| 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 "components/mus/public/interfaces/window_tree_host.mojom.h" |
| 8 |
| 9 namespace mash { |
| 10 namespace wm { |
| 11 |
| 12 namespace { |
| 13 const int kAcceleratorIdMask = 0xffff; |
| 14 } |
| 15 |
| 16 AcceleratorRegistrarImpl::AcceleratorRegistrarImpl( |
| 17 mus::mojom::WindowTreeHost* host, |
| 18 uint32_t accelerator_namespace, |
| 19 mojo::InterfaceRequest<AcceleratorRegistrar> request, |
| 20 const DestroyCallback& destroy_callback) |
| 21 : host_(host), |
| 22 accelerator_namespace_(accelerator_namespace & 0xffff), |
| 23 binding_(this, request.Pass()), |
| 24 destroy_callback_(destroy_callback) {} |
| 25 |
| 26 AcceleratorRegistrarImpl::~AcceleratorRegistrarImpl() { |
| 27 for (const auto& pair : accelerator_handlers_) |
| 28 host_->RemoveAccelerator(pair.first); |
| 29 destroy_callback_.Run(this); |
| 30 } |
| 31 |
| 32 bool AcceleratorRegistrarImpl::OwnsAccelerator(uint32_t accelerator_id) const { |
| 33 return !!accelerator_handlers_.count(accelerator_id); |
| 34 } |
| 35 |
| 36 void AcceleratorRegistrarImpl::ProcessAccelerator(uint32_t accelerator_id, |
| 37 mus::mojom::EventPtr event) { |
| 38 DCHECK(OwnsAccelerator(accelerator_id)); |
| 39 accelerator_handlers_[accelerator_id]->OnAccelerator( |
| 40 accelerator_id & kAcceleratorIdMask, event.Pass()); |
| 41 } |
| 42 |
| 43 uint32_t AcceleratorRegistrarImpl::ComputeAcceleratorId( |
| 44 uint32_t accelerator_id) const { |
| 45 return (accelerator_namespace_ << 16) | (accelerator_id & kAcceleratorIdMask); |
| 46 } |
| 47 |
| 48 // mus::mojom::AcceleratorRegistrar: |
| 49 void AcceleratorRegistrarImpl::AddAccelerator( |
| 50 uint32_t accelerator_id, |
| 51 mus::mojom::EventMatcherPtr matcher, |
| 52 mus::mojom::AcceleratorHandlerPtr handler, |
| 53 const AddAcceleratorCallback& callback) { |
| 54 if ((accelerator_id & kAcceleratorIdMask) != accelerator_id) { |
| 55 // The |accelerator_id| is too large, and it can't be handled correctly. |
| 56 callback.Run(false); |
| 57 return; |
| 58 } |
| 59 uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); |
| 60 accelerator_handlers_.insert( |
| 61 std::make_pair(namespaced_accelerator_id, handler.Pass())); |
| 62 host_->AddAccelerator(namespaced_accelerator_id, matcher.Pass()); |
| 63 callback.Run(true); |
| 64 } |
| 65 |
| 66 void AcceleratorRegistrarImpl::RemoveAccelerator(uint32_t accelerator_id) { |
| 67 uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); |
| 68 if (!accelerator_handlers_.count(namespaced_accelerator_id)) |
| 69 return; |
| 70 host_->RemoveAccelerator(namespaced_accelerator_id); |
| 71 } |
| 72 |
| 73 } // namespace wm |
| 74 } // namespace mash |
| OLD | NEW |