OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "ash/mus/accelerators/accelerator_controller_registrar.h" | |
6 | |
7 #include <limits> | |
8 | |
9 #include "ash/common/accelerators/accelerator_controller.h" | |
10 #include "ash/common/accelerators/accelerator_router.h" | |
11 #include "ash/common/wm_shell.h" | |
12 #include "ash/mus/accelerators/accelerator_ids.h" | |
13 #include "ash/mus/bridge/wm_window_mus.h" | |
14 #include "ash/mus/window_manager.h" | |
15 #include "base/logging.h" | |
16 #include "services/ui/common/event_matcher_util.h" | |
17 #include "services/ui/public/cpp/window_manager_delegate.h" | |
18 #include "services/ui/public/cpp/window_tree_client.h" | |
19 #include "ui/base/accelerators/accelerator_history.h" | |
20 | |
21 namespace ash { | |
22 namespace mus { | |
23 namespace { | |
24 | |
25 // Callback from registering the accelerator. All our accelerators should be | |
26 // registered, so we expect |added| to be true. | |
27 void OnAcceleratorAdded(bool added) { | |
28 DCHECK(added); | |
29 } | |
30 | |
31 } // namespace | |
32 | |
33 AcceleratorControllerRegistrar::AcceleratorControllerRegistrar( | |
34 WindowManager* window_manager, | |
35 uint16_t id_namespace) | |
36 : window_manager_(window_manager), | |
37 id_namespace_(id_namespace), | |
38 next_id_(0), | |
39 router_(new AcceleratorRouter) { | |
40 window_manager_->AddAcceleratorHandler(id_namespace, this); | |
41 } | |
42 | |
43 AcceleratorControllerRegistrar::~AcceleratorControllerRegistrar() { | |
44 window_manager_->RemoveAcceleratorHandler(id_namespace_); | |
45 | |
46 if (!window_manager_->window_manager_client()) | |
47 return; | |
48 | |
49 // TODO(sky): consider not doing this. If we assume the destructor is called | |
50 // during shutdown, then this is unnecessary and results in a bunch of | |
51 // messages that are dropped. | |
52 for (uint16_t local_id : ids_) { | |
53 window_manager_->window_manager_client()->RemoveAccelerator( | |
54 ComputeAcceleratorId(id_namespace_, local_id)); | |
55 } | |
56 } | |
57 | |
58 ui::mojom::EventResult AcceleratorControllerRegistrar::OnAccelerator( | |
59 uint32_t id, | |
60 const ui::Event& event) { | |
61 const ui::Accelerator accelerator(*event.AsKeyEvent()); | |
62 auto iter = accelerator_to_ids_.find(accelerator); | |
63 if (iter == accelerator_to_ids_.end()) { | |
64 // Because of timing we may have unregistered the accelerator already, | |
65 // ignore in that case. | |
66 return ui::mojom::EventResult::UNHANDLED; | |
67 } | |
68 | |
69 const Ids& ids = iter->second; | |
70 AcceleratorController* accelerator_controller = | |
71 WmShell::Get()->accelerator_controller(); | |
72 const bool is_pre = GetAcceleratorLocalId(id) == ids.pre_id; | |
73 if (is_pre) { | |
74 // TODO(sky): this does not exactly match ash code. In particular ash code | |
75 // is called for *all* key events, where as this is only called for | |
76 // registered accelerators. This means the previous accelerator isn't the | |
77 // same as it was in ash. We need to figure out exactly what is needed of | |
78 // previous accelerator so that we can either register for the right set of | |
79 // accelerators, or make WS send the previous accelerator. | |
80 // http://crbug.com/630683. | |
81 accelerator_controller->accelerator_history()->StoreCurrentAccelerator( | |
82 accelerator); | |
83 WmWindow* target_window = WmShell::Get()->GetFocusedWindow(); | |
84 if (!target_window) | |
85 target_window = WmShell::Get()->GetRootWindowForNewWindows(); | |
86 DCHECK(target_window); | |
87 return router_->ProcessAccelerator(target_window, *(event.AsKeyEvent()), | |
88 accelerator) | |
89 ? ui::mojom::EventResult::HANDLED | |
90 : ui::mojom::EventResult::UNHANDLED; | |
91 } | |
92 DCHECK_EQ(GetAcceleratorLocalId(id), ids.post_id); | |
93 // NOTE: for post return value doesn't really matter. | |
94 return WmShell::Get()->accelerator_controller()->Process(accelerator) | |
95 ? ui::mojom::EventResult::HANDLED | |
96 : ui::mojom::EventResult::UNHANDLED; | |
97 } | |
98 | |
99 void AcceleratorControllerRegistrar::OnAcceleratorRegistered( | |
100 const ui::Accelerator& accelerator) { | |
101 Ids ids; | |
102 if (!GenerateIds(&ids)) { | |
103 DVLOG(1) << "max number of accelerators registered, dropping request"; | |
104 return; | |
105 } | |
106 DCHECK_EQ(0u, accelerator_to_ids_.count(accelerator)); | |
107 accelerator_to_ids_[accelerator] = ids; | |
108 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size()); | |
109 | |
110 ui::mojom::EventMatcherPtr event_matcher = ui::CreateKeyMatcher( | |
111 static_cast<ui::mojom::KeyboardCode>(accelerator.key_code()), | |
112 accelerator.modifiers()); | |
113 event_matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET; | |
114 DCHECK(accelerator.type() == ui::ET_KEY_PRESSED || | |
115 accelerator.type() == ui::ET_KEY_RELEASED); | |
116 event_matcher->type_matcher->type = accelerator.type() == ui::ET_KEY_PRESSED | |
117 ? ui::mojom::EventType::KEY_PRESSED | |
118 : ui::mojom::EventType::KEY_RELEASED; | |
119 | |
120 ui::mojom::EventMatcherPtr post_event_matcher = event_matcher.Clone(); | |
121 post_event_matcher->accelerator_phase = | |
122 ui::mojom::AcceleratorPhase::POST_TARGET; | |
123 | |
124 window_manager_->window_manager_client()->AddAccelerator( | |
125 ComputeAcceleratorId(id_namespace_, ids.pre_id), std::move(event_matcher), | |
126 base::Bind(OnAcceleratorAdded)); | |
127 window_manager_->window_manager_client()->AddAccelerator( | |
128 ComputeAcceleratorId(id_namespace_, ids.post_id), | |
129 std::move(post_event_matcher), base::Bind(OnAcceleratorAdded)); | |
130 } | |
sadrul
2016/07/27 17:20:00
We install a fair number of accelerators during st
sky
2016/07/27 17:39:03
Definitely. I filed 632050 and 632049 for this.
| |
131 | |
132 void AcceleratorControllerRegistrar::OnAcceleratorUnregistered( | |
133 const ui::Accelerator& accelerator) { | |
134 auto iter = accelerator_to_ids_.find(accelerator); | |
135 DCHECK(iter != accelerator_to_ids_.end()); | |
136 Ids ids = iter->second; | |
137 accelerator_to_ids_.erase(iter); | |
138 ids_.erase(ids.pre_id); | |
139 ids_.erase(ids.post_id); | |
140 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size()); | |
141 window_manager_->window_manager_client()->RemoveAccelerator( | |
142 ComputeAcceleratorId(id_namespace_, ids.pre_id)); | |
143 window_manager_->window_manager_client()->RemoveAccelerator( | |
144 ComputeAcceleratorId(id_namespace_, ids.post_id)); | |
145 } | |
146 | |
147 bool AcceleratorControllerRegistrar::GenerateIds(Ids* ids) { | |
148 if (ids_.size() + 2 >= std::numeric_limits<uint16_t>::max()) | |
149 return false; | |
150 ids->pre_id = GetNextLocalAcceleratorId(); | |
151 ids->post_id = GetNextLocalAcceleratorId(); | |
152 return true; | |
153 } | |
154 | |
155 uint16_t AcceleratorControllerRegistrar::GetNextLocalAcceleratorId() { | |
156 DCHECK(ids_.size() < std::numeric_limits<uint16_t>::max()); | |
sadrul
2016/07/27 17:20:00
DCHECK_LT?
sky
2016/07/27 17:39:03
Done.
| |
157 // Common case is we never wrap once, so this is typically cheap. Additionally | |
158 // we expect there not to be too many accelerators. | |
159 while (ids_.count(next_id_) > 0) | |
160 ++next_id_; | |
161 ids_.insert(next_id_); | |
162 return next_id_++; | |
163 } | |
164 | |
165 } // namespace mus | |
166 } // namespace ash | |
OLD | NEW |