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 "base/bind.h" | |
8 #include "components/mus/public/interfaces/window_tree_host.mojom.h" | |
9 | |
10 namespace mash { | |
11 namespace wm { | |
12 | |
13 namespace { | |
14 const int kAcceleratorIdMask = 0xffff; | |
15 } | |
16 | |
17 AcceleratorRegistrarImpl::AcceleratorRegistrarImpl( | |
18 mus::mojom::WindowTreeHost* host, | |
19 uint32_t accelerator_namespace, | |
20 mojo::InterfaceRequest<AcceleratorRegistrar> request, | |
21 const DestroyCallback& destroy_callback) | |
22 : host_(host), | |
23 accelerator_namespace_(accelerator_namespace & 0xffff), | |
24 binding_(this, request.Pass()), | |
25 destroy_callback_(destroy_callback) { | |
26 binding_.set_connection_error_handler(base::Bind( | |
27 &AcceleratorRegistrarImpl::OnBindingGone, base::Unretained(this))); | |
28 } | |
29 | |
30 AcceleratorRegistrarImpl::~AcceleratorRegistrarImpl() { | |
31 for (const auto& pair : accelerator_handlers_) | |
32 host_->RemoveAccelerator(pair.first); | |
33 destroy_callback_.Run(this); | |
34 } | |
35 | |
36 bool AcceleratorRegistrarImpl::OwnsAccelerator(uint32_t accelerator_id) const { | |
37 return !!accelerator_handlers_.count(accelerator_id); | |
38 } | |
39 | |
40 void AcceleratorRegistrarImpl::ProcessAccelerator(uint32_t accelerator_id, | |
41 mus::mojom::EventPtr event) { | |
42 DCHECK(OwnsAccelerator(accelerator_id)); | |
43 accelerator_handlers_[accelerator_id]->OnAccelerator( | |
44 accelerator_id & kAcceleratorIdMask, event.Pass()); | |
45 } | |
46 | |
47 uint32_t AcceleratorRegistrarImpl::ComputeAcceleratorId( | |
48 uint32_t accelerator_id) const { | |
49 return (accelerator_namespace_ << 16) | (accelerator_id & kAcceleratorIdMask); | |
50 } | |
51 | |
52 void AcceleratorRegistrarImpl::OnBindingGone() { | |
53 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.
| |
54 // If there's no outstanding accelerator handler for this namespace, then | |
55 // destroy it. | |
56 if (accelerator_handlers_.empty()) | |
57 delete this; | |
58 } | |
59 | |
60 void AcceleratorRegistrarImpl::OnHandlerGone(uint32_t accelerator_id) { | |
61 RemoveAccelerator(accelerator_id); | |
62 } | |
63 | |
64 void AcceleratorRegistrarImpl::AddAccelerator( | |
65 uint32_t accelerator_id, | |
66 mus::mojom::EventMatcherPtr matcher, | |
67 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.
| |
68 const AddAcceleratorCallback& callback) { | |
69 if ((accelerator_id & kAcceleratorIdMask) != accelerator_id) { | |
70 // The |accelerator_id| is too large, and it can't be handled correctly. | |
71 callback.Run(false); | |
72 return; | |
73 } | |
74 uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); | |
75 handler.set_connection_error_handler( | |
76 base::Bind(&AcceleratorRegistrarImpl::OnHandlerGone, | |
77 base::Unretained(this), accelerator_id)); | |
78 accelerator_handlers_.insert( | |
79 std::make_pair(namespaced_accelerator_id, handler.Pass())); | |
80 host_->AddAccelerator(namespaced_accelerator_id, matcher.Pass()); | |
81 callback.Run(true); | |
82 } | |
83 | |
84 void AcceleratorRegistrarImpl::RemoveAccelerator(uint32_t accelerator_id) { | |
85 uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); | |
86 if (!accelerator_handlers_.count(namespaced_accelerator_id)) | |
87 return; | |
88 host_->RemoveAccelerator(namespaced_accelerator_id); | |
89 accelerator_handlers_.erase(namespaced_accelerator_id); | |
90 // If the registrar is not bound anymore (i.e. the client can no longer | |
91 // install new accelerators), and the last accelerator has been removed, then | |
92 // there's no point keeping this alive anymore. | |
93 if (accelerator_handlers_.empty() && !binding_.is_bound()) | |
94 delete this; | |
95 } | |
96 | |
97 } // namespace wm | |
98 } // namespace mash | |
OLD | NEW |