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

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 unnecessary std::vector. 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 accelerator.
23 void OnAcceleratorAdded(const ui::Accelerator& accelerator, bool added) { 23 void OnAcceleratorAdded(const ui::Accelerator& accelerator, bool added) {
24 // All our accelerators should be registered, so we expect |added| to be true. 24 // All our accelerators should be registered, so we expect |added| to be true.
25 DCHECK(added) << "duplicate accelerator key_code=" << accelerator.key_code() 25 DCHECK(added) << "duplicate accelerator key_code=" << accelerator.key_code()
26 << " type=" << accelerator.type() 26 << " type=" << accelerator.type()
27 << " modifiers=" << accelerator.modifiers(); 27 << " modifiers=" << accelerator.modifiers();
28 } 28 }
29 29
30 // Callback from registering the accelerators.
31 void OnAcceleratorVectorAdded(
32 const std::vector<ui::Accelerator>& ui_accelerators,
mfomitchev 2017/01/24 22:56:02 For consistency: ui_accelerators -> accelerators
thanhph1 2017/01/25 20:12:37 Done.
33 bool added) {
34 for (auto iter = ui_accelerators.begin(); iter != ui_accelerators.end();
35 ++iter)
36 OnAcceleratorAdded(*iter, added);
mfomitchev 2017/01/24 22:56:02 This will result in misleading DCHECK errors: if t
thanhph1 2017/01/25 20:12:37 Acknowledged. Since |added| is a variable and not
mfomitchev 2017/01/26 16:03:36 This was the reason I asked you to add logging in
30 } // namespace 37 } // namespace
38 }
31 39
32 AcceleratorControllerRegistrar::AcceleratorControllerRegistrar( 40 AcceleratorControllerRegistrar::AcceleratorControllerRegistrar(
33 WindowManager* window_manager, 41 WindowManager* window_manager,
34 uint16_t id_namespace) 42 uint16_t id_namespace)
35 : window_manager_(window_manager), 43 : window_manager_(window_manager),
36 id_namespace_(id_namespace), 44 id_namespace_(id_namespace),
37 next_id_(0), 45 next_id_(0),
38 router_(new AcceleratorRouter) { 46 router_(new AcceleratorRouter) {
39 window_manager_->AddAcceleratorHandler(id_namespace, this); 47 window_manager_->AddAcceleratorHandler(id_namespace, this);
40 } 48 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 ? ui::mojom::EventResult::HANDLED 99 ? ui::mojom::EventResult::HANDLED
92 : ui::mojom::EventResult::UNHANDLED; 100 : ui::mojom::EventResult::UNHANDLED;
93 } 101 }
94 DCHECK_EQ(GetAcceleratorLocalId(id), ids.post_id); 102 DCHECK_EQ(GetAcceleratorLocalId(id), ids.post_id);
95 // NOTE: for post return value doesn't really matter. 103 // NOTE: for post return value doesn't really matter.
96 return WmShell::Get()->accelerator_controller()->Process(accelerator) 104 return WmShell::Get()->accelerator_controller()->Process(accelerator)
97 ? ui::mojom::EventResult::HANDLED 105 ? ui::mojom::EventResult::HANDLED
98 : ui::mojom::EventResult::UNHANDLED; 106 : ui::mojom::EventResult::UNHANDLED;
99 } 107 }
100 108
109 // add_accelerator_immediately default set to true.
mfomitchev 2017/01/24 22:56:02 Is this leftover from previous revision?
thanhph1 2017/01/25 20:12:37 Oops. I removed it. Thanks!
101 void AcceleratorControllerRegistrar::OnAcceleratorRegistered( 110 void AcceleratorControllerRegistrar::OnAcceleratorRegistered(
102 const ui::Accelerator& accelerator) { 111 const ui::Accelerator& accelerator) {
103 Ids ids; 112 std::vector<ui::mojom::AcceleratorPtr> accelerator_vector;
104 if (!GenerateIds(&ids)) {
105 DVLOG(1) << "max number of accelerators registered, dropping request";
106 return;
107 }
108 DCHECK_EQ(0u, accelerator_to_ids_.count(accelerator));
109 accelerator_to_ids_[accelerator] = ids;
110 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size());
111 113
112 ui::mojom::EventMatcherPtr event_matcher = ui::CreateKeyMatcher( 114 AddAcceleratorToVector(accelerator, accelerator_vector);
113 static_cast<ui::mojom::KeyboardCode>(accelerator.key_code()),
114 accelerator.modifiers());
115 event_matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET;
116 DCHECK(accelerator.type() == ui::ET_KEY_PRESSED ||
117 accelerator.type() == ui::ET_KEY_RELEASED);
118 event_matcher->type_matcher->type = accelerator.type() == ui::ET_KEY_PRESSED
119 ? ui::mojom::EventType::KEY_PRESSED
120 : ui::mojom::EventType::KEY_RELEASED;
121
122 ui::mojom::EventMatcherPtr post_event_matcher = event_matcher.Clone();
123 post_event_matcher->accelerator_phase =
124 ui::mojom::AcceleratorPhase::POST_TARGET;
125 115
126 window_manager_->window_manager_client()->AddAccelerators( 116 window_manager_->window_manager_client()->AddAccelerators(
127 ui::CreateAcceleratorVector( 117 std::move(accelerator_vector),
128 ComputeAcceleratorId(id_namespace_, ids.pre_id),
129 std::move(event_matcher)),
130 base::Bind(OnAcceleratorAdded, accelerator));
131
132 window_manager_->window_manager_client()->AddAccelerators(
133 ui::CreateAcceleratorVector(
134 ComputeAcceleratorId(id_namespace_, ids.post_id),
135 std::move(post_event_matcher)),
136 base::Bind(OnAcceleratorAdded, accelerator)); 118 base::Bind(OnAcceleratorAdded, accelerator));
137 } 119 }
138 120
139 void AcceleratorControllerRegistrar::OnAcceleratorUnregistered( 121 void AcceleratorControllerRegistrar::OnAcceleratorUnregistered(
140 const ui::Accelerator& accelerator) { 122 const ui::Accelerator& accelerator) {
141 auto iter = accelerator_to_ids_.find(accelerator); 123 auto iter = accelerator_to_ids_.find(accelerator);
142 DCHECK(iter != accelerator_to_ids_.end()); 124 DCHECK(iter != accelerator_to_ids_.end());
143 Ids ids = iter->second; 125 Ids ids = iter->second;
144 accelerator_to_ids_.erase(iter); 126 accelerator_to_ids_.erase(iter);
145 ids_.erase(ids.pre_id); 127 ids_.erase(ids.pre_id);
146 ids_.erase(ids.post_id); 128 ids_.erase(ids.post_id);
147 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size()); 129 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size());
148 window_manager_->window_manager_client()->RemoveAccelerator( 130 window_manager_->window_manager_client()->RemoveAccelerator(
149 ComputeAcceleratorId(id_namespace_, ids.pre_id)); 131 ComputeAcceleratorId(id_namespace_, ids.pre_id));
150 window_manager_->window_manager_client()->RemoveAccelerator( 132 window_manager_->window_manager_client()->RemoveAccelerator(
151 ComputeAcceleratorId(id_namespace_, ids.post_id)); 133 ComputeAcceleratorId(id_namespace_, ids.post_id));
152 } 134 }
153 135
136 void AcceleratorControllerRegistrar::OnAcceleratorVectorRegistered(
137 const std::vector<ui::Accelerator>& accelerators) {
138 std::vector<ui::mojom::AcceleratorPtr> accelerator_vector;
139
140 for (auto iter = accelerators.begin(); iter != accelerators.end(); ++iter)
141 AddAcceleratorToVector(*iter, accelerator_vector);
142
143 window_manager_->window_manager_client()->AddAccelerators(
144 std::move(accelerator_vector),
145 base::Bind(OnAcceleratorVectorAdded, accelerators));
mfomitchev 2017/01/24 22:56:02 Because AddAcceleratorToVector may abort when Gene
thanhph1 2017/01/25 20:12:37 I think accelerator_vector is already used in std:
mfomitchev 2017/01/26 21:39:48 Ok. Then lets not pass accelerators vector to OnAc
146 }
147
148 void AcceleratorControllerRegistrar::AddAcceleratorToVector(
149 const ui::Accelerator& accelerator,
150 std::vector<ui::mojom::AcceleratorPtr>& accelerator_vector) {
151 Ids ids;
152 if (!GenerateIds(&ids)) {
153 DVLOG(1) << "max number of accelerators registered, dropping request";
154 return;
155 }
156 DCHECK_EQ(0u, accelerator_to_ids_.count(accelerator));
157 accelerator_to_ids_[accelerator] = ids;
158 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size());
159
160 ui::mojom::EventMatcherPtr pre_event_matcher = ui::CreateKeyMatcher(
161 static_cast<ui::mojom::KeyboardCode>(accelerator.key_code()),
162 accelerator.modifiers());
163 pre_event_matcher->accelerator_phase =
164 ui::mojom::AcceleratorPhase::PRE_TARGET;
165 DCHECK(accelerator.type() == ui::ET_KEY_PRESSED ||
166 accelerator.type() == ui::ET_KEY_RELEASED);
167 pre_event_matcher->type_matcher->type =
168 accelerator.type() == ui::ET_KEY_PRESSED
169 ? ui::mojom::EventType::KEY_PRESSED
170 : ui::mojom::EventType::KEY_RELEASED;
171
172 ui::mojom::EventMatcherPtr post_event_matcher = pre_event_matcher.Clone();
173 post_event_matcher->accelerator_phase =
174 ui::mojom::AcceleratorPhase::POST_TARGET;
175
176 accelerator_vector.push_back(
177 ui::CreateAccelerator(ComputeAcceleratorId(id_namespace_, ids.pre_id),
178 std::move(pre_event_matcher)));
179
180 accelerator_vector.push_back(
181 ui::CreateAccelerator(ComputeAcceleratorId(id_namespace_, ids.post_id),
182 std::move(post_event_matcher)));
183 }
184
154 bool AcceleratorControllerRegistrar::GenerateIds(Ids* ids) { 185 bool AcceleratorControllerRegistrar::GenerateIds(Ids* ids) {
155 if (ids_.size() + 2 >= std::numeric_limits<uint16_t>::max()) 186 if (ids_.size() + 2 >= std::numeric_limits<uint16_t>::max())
156 return false; 187 return false;
157 ids->pre_id = GetNextLocalAcceleratorId(); 188 ids->pre_id = GetNextLocalAcceleratorId();
158 ids->post_id = GetNextLocalAcceleratorId(); 189 ids->post_id = GetNextLocalAcceleratorId();
159 return true; 190 return true;
160 } 191 }
161 192
162 uint16_t AcceleratorControllerRegistrar::GetNextLocalAcceleratorId() { 193 uint16_t AcceleratorControllerRegistrar::GetNextLocalAcceleratorId() {
163 DCHECK_LT(ids_.size(), std::numeric_limits<uint16_t>::max()); 194 DCHECK_LT(ids_.size(), std::numeric_limits<uint16_t>::max());
164 // Common case is we never wrap once, so this is typically cheap. Additionally 195 // Common case is we never wrap once, so this is typically cheap. Additionally
165 // we expect there not to be too many accelerators. 196 // we expect there not to be too many accelerators.
166 while (ids_.count(next_id_) > 0) 197 while (ids_.count(next_id_) > 0)
167 ++next_id_; 198 ++next_id_;
168 ids_.insert(next_id_); 199 ids_.insert(next_id_);
169 return next_id_++; 200 return next_id_++;
170 } 201 }
171 202
172 } // namespace mus 203 } // namespace mus
173 } // namespace ash 204 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698