Chromium Code Reviews| Index: mash/wm/accelerator_registrar_impl.cc |
| diff --git a/mash/wm/accelerator_registrar_impl.cc b/mash/wm/accelerator_registrar_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6662250444ee87420361e36cfe3beff1bdee45dc |
| --- /dev/null |
| +++ b/mash/wm/accelerator_registrar_impl.cc |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "mash/wm/accelerator_registrar_impl.h" |
| + |
| +#include "base/bind.h" |
| +#include "components/mus/public/interfaces/window_tree_host.mojom.h" |
| + |
| +namespace mash { |
| +namespace wm { |
| + |
| +namespace { |
| +const int kAcceleratorIdMask = 0xffff; |
| +} |
| + |
| +AcceleratorRegistrarImpl::AcceleratorRegistrarImpl( |
| + mus::mojom::WindowTreeHost* host, |
| + uint32_t accelerator_namespace, |
| + mojo::InterfaceRequest<AcceleratorRegistrar> request, |
| + const DestroyCallback& destroy_callback) |
| + : host_(host), |
| + accelerator_namespace_(accelerator_namespace & 0xffff), |
| + binding_(this, request.Pass()), |
| + destroy_callback_(destroy_callback) { |
| + binding_.set_connection_error_handler(base::Bind( |
| + &AcceleratorRegistrarImpl::OnBindingGone, base::Unretained(this))); |
| +} |
| + |
| +AcceleratorRegistrarImpl::~AcceleratorRegistrarImpl() { |
| + for (const auto& pair : accelerator_handlers_) |
| + host_->RemoveAccelerator(pair.first); |
| + destroy_callback_.Run(this); |
| +} |
| + |
| +bool AcceleratorRegistrarImpl::OwnsAccelerator(uint32_t accelerator_id) const { |
| + return !!accelerator_handlers_.count(accelerator_id); |
| +} |
| + |
| +void AcceleratorRegistrarImpl::ProcessAccelerator(uint32_t accelerator_id, |
| + mus::mojom::EventPtr event) { |
| + DCHECK(OwnsAccelerator(accelerator_id)); |
| + accelerator_handlers_[accelerator_id]->OnAccelerator( |
| + accelerator_id & kAcceleratorIdMask, event.Pass()); |
| +} |
| + |
| +uint32_t AcceleratorRegistrarImpl::ComputeAcceleratorId( |
| + uint32_t accelerator_id) const { |
| + return (accelerator_namespace_ << 16) | (accelerator_id & kAcceleratorIdMask); |
| +} |
| + |
| +void AcceleratorRegistrarImpl::OnBindingGone() { |
| + binding_.Unbind(); |
|
Ben Goodger (Google)
2015/12/01 16:01:45
is this necessary?
sadrul
2015/12/01 19:18:05
My initial thought was this wouldn't be necessary.
|
| + // If there's no outstanding accelerator handler for this namespace, then |
| + // destroy it. |
| + if (accelerator_handlers_.empty()) |
| + delete this; |
| +} |
| + |
| +void AcceleratorRegistrarImpl::OnHandlerGone(uint32_t accelerator_id) { |
| + RemoveAccelerator(accelerator_id); |
| +} |
| + |
| +void AcceleratorRegistrarImpl::AddAccelerator( |
| + uint32_t accelerator_id, |
| + mus::mojom::EventMatcherPtr matcher, |
| + mus::mojom::AcceleratorHandlerPtr handler, |
|
Ben Goodger (Google)
2015/12/01 16:01:45
just thinking about this... passing a new handler
sadrul
2015/12/01 19:18:05
Nice. That makes sense, yeah. Done.
|
| + const AddAcceleratorCallback& callback) { |
| + if ((accelerator_id & kAcceleratorIdMask) != accelerator_id) { |
| + // The |accelerator_id| is too large, and it can't be handled correctly. |
| + callback.Run(false); |
| + return; |
| + } |
| + uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); |
| + handler.set_connection_error_handler( |
| + base::Bind(&AcceleratorRegistrarImpl::OnHandlerGone, |
| + base::Unretained(this), accelerator_id)); |
| + accelerator_handlers_.insert( |
| + std::make_pair(namespaced_accelerator_id, handler.Pass())); |
| + host_->AddAccelerator(namespaced_accelerator_id, matcher.Pass()); |
| + callback.Run(true); |
| +} |
| + |
| +void AcceleratorRegistrarImpl::RemoveAccelerator(uint32_t accelerator_id) { |
| + uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); |
| + if (!accelerator_handlers_.count(namespaced_accelerator_id)) |
| + return; |
| + host_->RemoveAccelerator(namespaced_accelerator_id); |
| + accelerator_handlers_.erase(namespaced_accelerator_id); |
| + // If the registrar is not bound anymore (i.e. the client can no longer |
| + // install new accelerators), and the last accelerator has been removed, then |
| + // there's no point keeping this alive anymore. |
| + if (accelerator_handlers_.empty() && !binding_.is_bound()) |
| + delete this; |
| +} |
| + |
| +} // namespace wm |
| +} // namespace mash |