vk: Use switch instead of look up table for state transitions.
We currently use a set of pairs to validate state transitions. A much
more lightweight solution is to use the hashes of the transitions.
Since our hash can be computed at compile time, we mark it as constexpr
and use it in a switch statement to verify state transitions.
Bug: 845780
Change-Id: I96d2b0cf59a9ca54ea03767fa6b84c408ebb1de9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1647902
Commit-Queue: Darren Shen <shend@chromium.org>
Reviewed-by: James Cook <jamescook@chromium.org>
Cr-Commit-Position: refs/heads/master@{#668651}
diff --git a/ash/keyboard/ui/keyboard_ui_model.cc b/ash/keyboard/ui/keyboard_ui_model.cc
index 78b3264..b660514 100644
--- a/ash/keyboard/ui/keyboard_ui_model.cc
+++ b/ash/keyboard/ui/keyboard_ui_model.cc
@@ -4,11 +4,7 @@
#include "ash/keyboard/ui/keyboard_ui_model.h"
-#include <set>
-#include <utility>
-
#include "base/metrics/histogram_functions.h"
-#include "base/no_destructor.h"
namespace keyboard {
@@ -17,32 +13,33 @@
// Returns whether a given state transition is valid.
// See the design document linked in https://crbug.com/71990.
bool IsAllowedStateTransition(KeyboardUIState from, KeyboardUIState to) {
- static const base::NoDestructor<
- std::set<std::pair<KeyboardUIState, KeyboardUIState>>>
- kAllowedStateTransition({
- // The initial ShowKeyboard scenario
- // INITIAL -> LOADING_EXTENSION -> HIDDEN -> SHOWN.
- {KeyboardUIState::kInitial, KeyboardUIState::kLoading},
- {KeyboardUIState::kLoading, KeyboardUIState::kHidden},
- {KeyboardUIState::kHidden, KeyboardUIState::kShown},
+ using State = KeyboardUIState;
+ switch (GetStateTransitionHash(from, to)) {
+ // The initial ShowKeyboard scenario
+ // INITIAL -> LOADING -> HIDDEN -> SHOWN.
+ case GetStateTransitionHash(State::kInitial, State::kLoading):
+ case GetStateTransitionHash(State::kLoading, State::kHidden):
+ case GetStateTransitionHash(State::kHidden, State::kShown):
- // Hide scenario
- // SHOWN -> WILL_HIDE -> HIDDEN.
- {KeyboardUIState::kShown, KeyboardUIState::kWillHide},
- {KeyboardUIState::kWillHide, KeyboardUIState::kHidden},
+ // Hide scenario
+ // SHOWN -> WILL_HIDE -> HIDDEN.
+ case GetStateTransitionHash(State::kShown, State::kWillHide):
+ case GetStateTransitionHash(State::kWillHide, State::kHidden):
- // Focus transition scenario
- // SHOWN -> WILL_HIDE -> SHOWN.
- {KeyboardUIState::kWillHide, KeyboardUIState::kShown},
+ // Focus transition scenario
+ // SHOWN -> WILL_HIDE -> SHOWN.
+ case GetStateTransitionHash(State::kWillHide, State::kShown):
- // HideKeyboard can be called at anytime for example on shutdown.
- {KeyboardUIState::kShown, KeyboardUIState::kHidden},
+ // HideKeyboard can be called at anytime (for example on shutdown).
+ case GetStateTransitionHash(State::kShown, State::kHidden):
- // Return to INITIAL when keyboard is disabled.
- {KeyboardUIState::kLoading, KeyboardUIState::kInitial},
- {KeyboardUIState::kHidden, KeyboardUIState::kInitial},
- });
- return kAllowedStateTransition->count(std::make_pair(from, to)) == 1;
+ // Return to INITIAL when keyboard is disabled.
+ case GetStateTransitionHash(State::kLoading, State::kInitial):
+ case GetStateTransitionHash(State::kHidden, State::kInitial):
+ return true;
+ default:
+ return false;
+ }
}
// Records a state transition for metrics.
@@ -77,12 +74,6 @@
}
}
-int GetStateTransitionHash(KeyboardUIState prev, KeyboardUIState next) {
- // The hashes correspond to the KeyboardControllerStateTransition entry in
- // tools/metrics/enums.xml.
- return static_cast<int>(prev) * 1000 + static_cast<int>(next);
-}
-
KeyboardUIModel::KeyboardUIModel() = default;
void KeyboardUIModel::ChangeState(KeyboardUIState new_state) {
diff --git a/ash/keyboard/ui/keyboard_ui_model.h b/ash/keyboard/ui/keyboard_ui_model.h
index c3a3a97..e9ce0f9 100644
--- a/ash/keyboard/ui/keyboard_ui_model.h
+++ b/ash/keyboard/ui/keyboard_ui_model.h
@@ -39,8 +39,12 @@
std::string StateToStr(KeyboardUIState state);
// Returns a unique hash of a state transition, used for histograms.
-KEYBOARD_EXPORT int GetStateTransitionHash(KeyboardUIState prev,
- KeyboardUIState next);
+// The hashes correspond to the KeyboardControllerStateTransition entry in
+// tools/metrics/enums.xml.
+constexpr int GetStateTransitionHash(KeyboardUIState prev,
+ KeyboardUIState next) {
+ return static_cast<int>(prev) * 1000 + static_cast<int>(next);
+}
// Model for the virtual keyboard UI.
class KEYBOARD_EXPORT KeyboardUIModel {