Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(172)

Side by Side Diff: ash/mus/accelerators/accelerator_controller_registrar.cc

Issue 2586333003: Make mash register initial batch of accelerators in single shot. (Closed)
Patch Set: Remove OnAcceleratorRegistered. Update comments. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/mus/accelerators/accelerator_controller_registrar.h" 5 #include "ash/mus/accelerators/accelerator_controller_registrar.h"
6 6
7 #include <limits> 7 #include <limits>
8 8
9 #include "ash/common/accelerators/accelerator_controller.h" 9 #include "ash/common/accelerators/accelerator_controller.h"
10 #include "ash/common/accelerators/accelerator_router.h" 10 #include "ash/common/accelerators/accelerator_router.h"
11 #include "ash/common/wm_shell.h" 11 #include "ash/common/wm_shell.h"
12 #include "ash/mus/accelerators/accelerator_ids.h" 12 #include "ash/mus/accelerators/accelerator_ids.h"
13 #include "ash/mus/window_manager.h" 13 #include "ash/mus/window_manager.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "services/ui/common/accelerator_util.h" 15 #include "services/ui/common/accelerator_util.h"
16 #include "ui/base/accelerators/accelerator_history.h" 16 #include "ui/base/accelerators/accelerator_history.h"
17 17
18 namespace ash { 18 namespace ash {
19 namespace mus { 19 namespace mus {
20 namespace { 20 namespace {
21 21
22 // Callback from registering the accelerator. 22 // Callback from registering the accelerators.
23 void OnAcceleratorAdded(const ui::Accelerator& accelerator, bool added) { 23 void OnAcceleratorVectorAdded(const std::vector<ui::Accelerator>& accelerators,
24 // All our accelerators should be registered, so we expect |added| to be true. 24 bool added) {
25 DCHECK(added) << "duplicate accelerator key_code=" << accelerator.key_code() 25 DCHECK(added) << "Unexpected accelerator vector registration failure.";
mfomitchev 2017/01/26 21:39:49 Please add a comment similar to the one we had in
thanhph1 2017/01/26 22:24:05 Done.
26 << " type=" << accelerator.type()
27 << " modifiers=" << accelerator.modifiers();
28 } 26 }
29 27
30 } // namespace 28 } // namespace
31 29
32 AcceleratorControllerRegistrar::AcceleratorControllerRegistrar( 30 AcceleratorControllerRegistrar::AcceleratorControllerRegistrar(
33 WindowManager* window_manager, 31 WindowManager* window_manager,
34 uint16_t id_namespace) 32 uint16_t id_namespace)
35 : window_manager_(window_manager), 33 : window_manager_(window_manager),
36 id_namespace_(id_namespace), 34 id_namespace_(id_namespace),
37 next_id_(0), 35 next_id_(0),
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 ? ui::mojom::EventResult::HANDLED 89 ? ui::mojom::EventResult::HANDLED
92 : ui::mojom::EventResult::UNHANDLED; 90 : ui::mojom::EventResult::UNHANDLED;
93 } 91 }
94 DCHECK_EQ(GetAcceleratorLocalId(id), ids.post_id); 92 DCHECK_EQ(GetAcceleratorLocalId(id), ids.post_id);
95 // NOTE: for post return value doesn't really matter. 93 // NOTE: for post return value doesn't really matter.
96 return WmShell::Get()->accelerator_controller()->Process(accelerator) 94 return WmShell::Get()->accelerator_controller()->Process(accelerator)
97 ? ui::mojom::EventResult::HANDLED 95 ? ui::mojom::EventResult::HANDLED
98 : ui::mojom::EventResult::UNHANDLED; 96 : ui::mojom::EventResult::UNHANDLED;
99 } 97 }
100 98
101 void AcceleratorControllerRegistrar::OnAcceleratorRegistered( 99 void AcceleratorControllerRegistrar::OnAcceleratorUnregistered(
102 const ui::Accelerator& accelerator) { 100 const ui::Accelerator& accelerator) {
101 auto iter = accelerator_to_ids_.find(accelerator);
102 DCHECK(iter != accelerator_to_ids_.end());
103 Ids ids = iter->second;
104 accelerator_to_ids_.erase(iter);
105 ids_.erase(ids.pre_id);
106 ids_.erase(ids.post_id);
107 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size());
108 window_manager_->window_manager_client()->RemoveAccelerator(
109 ComputeAcceleratorId(id_namespace_, ids.pre_id));
110 window_manager_->window_manager_client()->RemoveAccelerator(
111 ComputeAcceleratorId(id_namespace_, ids.post_id));
112 }
113
114 void AcceleratorControllerRegistrar::OnAcceleratorVectorRegistered(
115 const std::vector<ui::Accelerator>& accelerators) {
116 std::vector<ui::mojom::AcceleratorPtr> accelerator_vector;
117
118 for (auto iter = accelerators.begin(); iter != accelerators.end(); ++iter)
119 AddAcceleratorToVector(*iter, accelerator_vector);
120
121 window_manager_->window_manager_client()->AddAccelerators(
122 std::move(accelerator_vector),
123 base::Bind(OnAcceleratorVectorAdded, accelerators));
124 }
125
126 void AcceleratorControllerRegistrar::AddAcceleratorToVector(
127 const ui::Accelerator& accelerator,
128 std::vector<ui::mojom::AcceleratorPtr>& accelerator_vector) {
103 Ids ids; 129 Ids ids;
104 if (!GenerateIds(&ids)) { 130 if (!GenerateIds(&ids)) {
105 DVLOG(1) << "max number of accelerators registered, dropping request"; 131 DVLOG(1) << "max number of accelerators registered, dropping request";
mfomitchev 2017/01/26 21:39:49 Let's turn this into LOG(ERROR) - this seems like
thanhph1 2017/01/26 22:24:05 Done.
106 return; 132 return;
107 } 133 }
108 DCHECK_EQ(0u, accelerator_to_ids_.count(accelerator)); 134 DCHECK_EQ(0u, accelerator_to_ids_.count(accelerator));
109 accelerator_to_ids_[accelerator] = ids; 135 accelerator_to_ids_[accelerator] = ids;
110 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size()); 136 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size());
111 137
112 ui::mojom::EventMatcherPtr event_matcher = ui::CreateKeyMatcher( 138 ui::mojom::EventMatcherPtr pre_event_matcher = ui::CreateKeyMatcher(
113 static_cast<ui::mojom::KeyboardCode>(accelerator.key_code()), 139 static_cast<ui::mojom::KeyboardCode>(accelerator.key_code()),
114 accelerator.modifiers()); 140 accelerator.modifiers());
115 event_matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET; 141 pre_event_matcher->accelerator_phase =
142 ui::mojom::AcceleratorPhase::PRE_TARGET;
116 DCHECK(accelerator.type() == ui::ET_KEY_PRESSED || 143 DCHECK(accelerator.type() == ui::ET_KEY_PRESSED ||
117 accelerator.type() == ui::ET_KEY_RELEASED); 144 accelerator.type() == ui::ET_KEY_RELEASED);
118 event_matcher->type_matcher->type = accelerator.type() == ui::ET_KEY_PRESSED 145 pre_event_matcher->type_matcher->type =
119 ? ui::mojom::EventType::KEY_PRESSED 146 accelerator.type() == ui::ET_KEY_PRESSED
120 : ui::mojom::EventType::KEY_RELEASED; 147 ? ui::mojom::EventType::KEY_PRESSED
148 : ui::mojom::EventType::KEY_RELEASED;
121 149
122 ui::mojom::EventMatcherPtr post_event_matcher = event_matcher.Clone(); 150 ui::mojom::EventMatcherPtr post_event_matcher = pre_event_matcher.Clone();
123 post_event_matcher->accelerator_phase = 151 post_event_matcher->accelerator_phase =
124 ui::mojom::AcceleratorPhase::POST_TARGET; 152 ui::mojom::AcceleratorPhase::POST_TARGET;
125 153
126 window_manager_->window_manager_client()->AddAccelerators( 154 accelerator_vector.push_back(
127 ui::CreateAcceleratorVector( 155 ui::CreateAccelerator(ComputeAcceleratorId(id_namespace_, ids.pre_id),
128 ComputeAcceleratorId(id_namespace_, ids.pre_id), 156 std::move(pre_event_matcher)));
129 std::move(event_matcher)),
130 base::Bind(OnAcceleratorAdded, accelerator));
131 157
132 window_manager_->window_manager_client()->AddAccelerators( 158 accelerator_vector.push_back(
133 ui::CreateAcceleratorVector( 159 ui::CreateAccelerator(ComputeAcceleratorId(id_namespace_, ids.post_id),
134 ComputeAcceleratorId(id_namespace_, ids.post_id), 160 std::move(post_event_matcher)));
135 std::move(post_event_matcher)),
136 base::Bind(OnAcceleratorAdded, accelerator));
137 }
138
139 void AcceleratorControllerRegistrar::OnAcceleratorUnregistered(
140 const ui::Accelerator& accelerator) {
141 auto iter = accelerator_to_ids_.find(accelerator);
142 DCHECK(iter != accelerator_to_ids_.end());
143 Ids ids = iter->second;
144 accelerator_to_ids_.erase(iter);
145 ids_.erase(ids.pre_id);
146 ids_.erase(ids.post_id);
147 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size());
148 window_manager_->window_manager_client()->RemoveAccelerator(
149 ComputeAcceleratorId(id_namespace_, ids.pre_id));
150 window_manager_->window_manager_client()->RemoveAccelerator(
151 ComputeAcceleratorId(id_namespace_, ids.post_id));
152 } 161 }
153 162
154 bool AcceleratorControllerRegistrar::GenerateIds(Ids* ids) { 163 bool AcceleratorControllerRegistrar::GenerateIds(Ids* ids) {
155 if (ids_.size() + 2 >= std::numeric_limits<uint16_t>::max()) 164 if (ids_.size() + 2 >= std::numeric_limits<uint16_t>::max())
156 return false; 165 return false;
157 ids->pre_id = GetNextLocalAcceleratorId(); 166 ids->pre_id = GetNextLocalAcceleratorId();
158 ids->post_id = GetNextLocalAcceleratorId(); 167 ids->post_id = GetNextLocalAcceleratorId();
159 return true; 168 return true;
160 } 169 }
161 170
162 uint16_t AcceleratorControllerRegistrar::GetNextLocalAcceleratorId() { 171 uint16_t AcceleratorControllerRegistrar::GetNextLocalAcceleratorId() {
163 DCHECK_LT(ids_.size(), std::numeric_limits<uint16_t>::max()); 172 DCHECK_LT(ids_.size(), std::numeric_limits<uint16_t>::max());
164 // Common case is we never wrap once, so this is typically cheap. Additionally 173 // Common case is we never wrap once, so this is typically cheap. Additionally
165 // we expect there not to be too many accelerators. 174 // we expect there not to be too many accelerators.
166 while (ids_.count(next_id_) > 0) 175 while (ids_.count(next_id_) > 0)
167 ++next_id_; 176 ++next_id_;
168 ids_.insert(next_id_); 177 ids_.insert(next_id_);
169 return next_id_++; 178 return next_id_++;
170 } 179 }
171 180
172 } // namespace mus 181 } // namespace mus
173 } // namespace ash 182 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698