This source file includes following definitions.
- GetInputMethodID
- GetComponentInputMethodID
- GetExtensionIDFromInputMethodID
- GetInputMethodIDByKeyboardLayout
- IsExtensionIME
- IsComponentExtensionIME
- IsMemberOfExtension
- IsKeyboardLayoutExtension
- UseWrappedExtensionKeyboardLayouts
- MaybeGetLegacyXkbId
- ScopedUseExtensionKeyboardFlagForTesting
- ScopedUseExtensionKeyboardFlagForTesting
#include "chromeos/ime/extension_ime_util.h"
#include "base/strings/string_util.h"
namespace chromeos {
namespace {
const char kExtensionIMEPrefix[] = "_ext_ime_";
const int kExtensionIMEPrefixLength =
sizeof(kExtensionIMEPrefix) / sizeof(kExtensionIMEPrefix[0]) - 1;
const char kComponentExtensionIMEPrefix[] = "_comp_ime_";
const char kPublicExtensionXkbIdPrefix[] =
"_comp_ime_fgoepimhcoialccpbmpnnblemnepkkao";
const char kInternalExtensionXkbIdPrefix[] =
"_comp_ime_jkghodnilhceideoidjikpgommlajknk";
const int kComponentExtensionIMEPrefixLength =
sizeof(kComponentExtensionIMEPrefix) /
sizeof(kComponentExtensionIMEPrefix[0]) - 1;
const int kExtensionIdLength = 32;
bool g_use_wrapped_extension_keyboard_layouts = true;
}
namespace extension_ime_util {
std::string GetInputMethodID(const std::string& extension_id,
const std::string& engine_id) {
DCHECK(!extension_id.empty());
DCHECK(!engine_id.empty());
return kExtensionIMEPrefix + extension_id + engine_id;
}
std::string GetComponentInputMethodID(const std::string& extension_id,
const std::string& engine_id) {
DCHECK(!extension_id.empty());
DCHECK(!engine_id.empty());
return kComponentExtensionIMEPrefix + extension_id + engine_id;
}
std::string GetExtensionIDFromInputMethodID(
const std::string& input_method_id) {
if (IsExtensionIME(input_method_id) &&
input_method_id.size() >= kExtensionIMEPrefixLength +
kExtensionIdLength) {
return input_method_id.substr(kExtensionIMEPrefixLength,
kExtensionIdLength);
}
if (IsComponentExtensionIME(input_method_id) &&
input_method_id.size() >= kComponentExtensionIMEPrefixLength +
kExtensionIdLength) {
return input_method_id.substr(kComponentExtensionIMEPrefixLength,
kExtensionIdLength);
}
return "";
}
std::string GetInputMethodIDByKeyboardLayout(
const std::string& keyboard_layout_id) {
const char* kExtensionXkbIdPrefix =
#if defined(OFFICIAL_BUILD)
kInternalExtensionXkbIdPrefix;
#else
kPublicExtensionXkbIdPrefix;
#endif
bool migrate = UseWrappedExtensionKeyboardLayouts();
if (IsKeyboardLayoutExtension(keyboard_layout_id)) {
std::string id = keyboard_layout_id.substr(
arraysize(kPublicExtensionXkbIdPrefix) - 1);
if (migrate)
return kExtensionXkbIdPrefix + id;
return id;
}
if (migrate && StartsWithASCII(keyboard_layout_id, "xkb:", true))
return kExtensionXkbIdPrefix + keyboard_layout_id;
return keyboard_layout_id;
}
bool IsExtensionIME(const std::string& input_method_id) {
return StartsWithASCII(input_method_id,
kExtensionIMEPrefix,
true);
}
bool IsComponentExtensionIME(const std::string& input_method_id) {
return StartsWithASCII(input_method_id,
kComponentExtensionIMEPrefix,
true);
}
bool IsMemberOfExtension(const std::string& input_method_id,
const std::string& extension_id) {
return StartsWithASCII(input_method_id,
kExtensionIMEPrefix + extension_id,
true);
}
bool IsKeyboardLayoutExtension(const std::string& input_method_id) {
return StartsWithASCII(input_method_id, kPublicExtensionXkbIdPrefix, true) ||
StartsWithASCII(input_method_id, kInternalExtensionXkbIdPrefix, true);
}
bool UseWrappedExtensionKeyboardLayouts() {
return g_use_wrapped_extension_keyboard_layouts;
}
std::string MaybeGetLegacyXkbId(const std::string& input_method_id) {
if (IsKeyboardLayoutExtension(input_method_id)) {
size_t pos = input_method_id.find("xkb:");
if (pos != std::string::npos)
return input_method_id.substr(pos);
}
return input_method_id;
}
ScopedUseExtensionKeyboardFlagForTesting::
ScopedUseExtensionKeyboardFlagForTesting(bool new_flag)
: auto_reset_(&g_use_wrapped_extension_keyboard_layouts, new_flag) {
}
ScopedUseExtensionKeyboardFlagForTesting::
~ScopedUseExtensionKeyboardFlagForTesting() {
}
}
}