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..87074c8166c9318945898296aea1b760783efae3 |
--- /dev/null |
+++ b/mash/wm/accelerator_registrar_impl.cc |
@@ -0,0 +1,74 @@ |
+// 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 "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) {} |
+ |
+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); |
+} |
+ |
+// mus::mojom::AcceleratorRegistrar: |
+void AcceleratorRegistrarImpl::AddAccelerator( |
+ uint32_t accelerator_id, |
+ mus::mojom::EventMatcherPtr matcher, |
+ mus::mojom::AcceleratorHandlerPtr handler, |
+ 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); |
+ 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); |
+} |
+ |
+} // namespace wm |
+} // namespace mash |