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 binding_(this, request.Pass()), | |
24 accelerator_namespace_(accelerator_namespace & 0xffff), | |
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 (uint32_t accelerator_id : accelerator_ids_) | |
32 host_->RemoveAccelerator(accelerator_id); | |
33 destroy_callback_.Run(this); | |
34 } | |
35 | |
36 bool AcceleratorRegistrarImpl::OwnsAccelerator(uint32_t accelerator_id) const { | |
37 return !!accelerator_ids_.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_handler_->OnAccelerator(accelerator_id & kAcceleratorIdMask, | |
44 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(); | |
54 // If there's no outstanding accelerators for this connection, then destroy | |
55 // it. | |
56 if (accelerator_ids_.empty()) | |
57 delete this; | |
58 } | |
59 | |
60 void AcceleratorRegistrarImpl::OnHandlerGone() { | |
61 // The handler is dead. If AcceleratorRegistrar connection is also closed, | |
62 // then destroy this. Otherwise, remove all the accelerators, but keep the | |
63 // AcceleratorRegistrar connection alive (the client could still set another | |
64 // handler and install new accelerators). | |
sadrul
2015/12/01 19:18:05
Not entirely sure about this. It would be unusual
Ben Goodger (Google)
2015/12/01 19:25:04
SGTM
| |
65 if (!binding_.is_bound()) { | |
66 delete this; | |
67 return; | |
68 } | |
69 accelerator_handler_.reset(); | |
70 for (uint32_t accelerator_id : accelerator_ids_) | |
71 host_->RemoveAccelerator(accelerator_id); | |
72 } | |
73 | |
74 void AcceleratorRegistrarImpl::SetHandler( | |
75 mus::mojom::AcceleratorHandlerPtr handler) { | |
76 accelerator_handler_ = handler.Pass(); | |
77 accelerator_handler_.set_connection_error_handler(base::Bind( | |
78 &AcceleratorRegistrarImpl::OnHandlerGone, base::Unretained(this))); | |
79 } | |
80 | |
81 void AcceleratorRegistrarImpl::AddAccelerator( | |
82 uint32_t accelerator_id, | |
83 mus::mojom::EventMatcherPtr matcher, | |
84 const AddAcceleratorCallback& callback) { | |
85 if (!accelerator_handler_ || | |
86 (accelerator_id & kAcceleratorIdMask) != accelerator_id) { | |
87 // The |accelerator_id| is too large, and it can't be handled correctly. | |
88 callback.Run(false); | |
89 return; | |
90 } | |
91 uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); | |
92 accelerator_ids_.insert(namespaced_accelerator_id); | |
93 host_->AddAccelerator(namespaced_accelerator_id, matcher.Pass()); | |
94 callback.Run(true); | |
95 } | |
96 | |
97 void AcceleratorRegistrarImpl::RemoveAccelerator(uint32_t accelerator_id) { | |
98 uint32_t namespaced_accelerator_id = ComputeAcceleratorId(accelerator_id); | |
99 if (!accelerator_ids_.count(namespaced_accelerator_id)) | |
100 return; | |
101 host_->RemoveAccelerator(namespaced_accelerator_id); | |
102 accelerator_ids_.erase(namespaced_accelerator_id); | |
103 // If the registrar is not bound anymore (i.e. the client can no longer | |
104 // install new accelerators), and the last accelerator has been removed, then | |
105 // there's no point keeping this alive anymore. | |
106 if (accelerator_ids_.empty() && !binding_.is_bound()) | |
107 delete this; | |
108 } | |
109 | |
110 } // namespace wm | |
111 } // namespace mash | |
OLD | NEW |