Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / base / ime / chromeos / extension_ime_util.cc
blob84a111c86def7cd99e0872b5265f8a9e132173b0
1 // Copyright 2013 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.
5 #include "ui/base/ime/chromeos/extension_ime_util.h"
7 #include "base/strings/string_util.h"
9 namespace chromeos {
11 namespace {
13 const char kExtensionIMEPrefix[] = "_ext_ime_";
14 const int kExtensionIMEPrefixLength =
15 sizeof(kExtensionIMEPrefix) / sizeof(kExtensionIMEPrefix[0]) - 1;
16 const char kComponentExtensionIMEPrefix[] = "_comp_ime_";
17 const int kComponentExtensionIMEPrefixLength =
18 sizeof(kComponentExtensionIMEPrefix) /
19 sizeof(kComponentExtensionIMEPrefix[0]) - 1;
20 const int kExtensionIdLength = 32;
22 } // namespace
24 namespace extension_ime_util {
26 std::string GetInputMethodID(const std::string& extension_id,
27 const std::string& engine_id) {
28 DCHECK(!extension_id.empty());
29 DCHECK(!engine_id.empty());
30 return kExtensionIMEPrefix + extension_id + engine_id;
33 std::string GetComponentInputMethodID(const std::string& extension_id,
34 const std::string& engine_id) {
35 DCHECK(!extension_id.empty());
36 DCHECK(!engine_id.empty());
37 return kComponentExtensionIMEPrefix + extension_id + engine_id;
40 std::string GetExtensionIDFromInputMethodID(
41 const std::string& input_method_id) {
42 if (IsExtensionIME(input_method_id)) {
43 return input_method_id.substr(kExtensionIMEPrefixLength,
44 kExtensionIdLength);
46 if (IsComponentExtensionIME(input_method_id)) {
47 return input_method_id.substr(kComponentExtensionIMEPrefixLength,
48 kExtensionIdLength);
50 return "";
53 std::string GetComponentIDByInputMethodID(const std::string& input_method_id) {
54 if (IsComponentExtensionIME(input_method_id))
55 return input_method_id.substr(kComponentExtensionIMEPrefixLength +
56 kExtensionIdLength);
57 if (IsExtensionIME(input_method_id))
58 return input_method_id.substr(kExtensionIMEPrefixLength +
59 kExtensionIdLength);
60 return input_method_id;
63 std::string GetInputMethodIDByEngineID(const std::string& engine_id) {
64 if (StartsWithASCII(engine_id, kComponentExtensionIMEPrefix, true) ||
65 StartsWithASCII(engine_id, kExtensionIMEPrefix, true)) {
66 return engine_id;
68 if (StartsWithASCII(engine_id, "xkb:", true))
69 return GetComponentInputMethodID(kXkbExtensionId, engine_id);
70 if (StartsWithASCII(engine_id, "vkd_", true))
71 return GetComponentInputMethodID(kM17nExtensionId, engine_id);
72 if (StartsWithASCII(engine_id, "nacl_mozc_", true))
73 return GetComponentInputMethodID(kMozcExtensionId, engine_id);
74 if (StartsWithASCII(engine_id, "hangul_", true))
75 return GetComponentInputMethodID(kHangulExtensionId, engine_id);
77 if (StartsWithASCII(engine_id, "zh-", true) &&
78 engine_id.find("pinyin") != std::string::npos) {
79 return GetComponentInputMethodID(kChinesePinyinExtensionId, engine_id);
81 if (StartsWithASCII(engine_id, "zh-", true) &&
82 engine_id.find("zhuyin") != std::string::npos) {
83 return GetComponentInputMethodID(kChineseZhuyinExtensionId, engine_id);
85 if (StartsWithASCII(engine_id, "zh-", true) &&
86 engine_id.find("cangjie") != std::string::npos) {
87 return GetComponentInputMethodID(kChineseCangjieExtensionId, engine_id);
89 if (engine_id.find("-t-i0-") != std::string::npos)
90 return GetComponentInputMethodID(kT13nExtensionId, engine_id);
92 return engine_id;
95 bool IsExtensionIME(const std::string& input_method_id) {
96 return StartsWithASCII(input_method_id,
97 kExtensionIMEPrefix,
98 true /* Case sensitive */) &&
99 input_method_id.size() > kExtensionIMEPrefixLength +
100 kExtensionIdLength;
103 bool IsComponentExtensionIME(const std::string& input_method_id) {
104 return StartsWithASCII(input_method_id,
105 kComponentExtensionIMEPrefix,
106 true /* Case sensitive */) &&
107 input_method_id.size() > kComponentExtensionIMEPrefixLength +
108 kExtensionIdLength;
111 bool IsMemberOfExtension(const std::string& input_method_id,
112 const std::string& extension_id) {
113 return StartsWithASCII(input_method_id,
114 kExtensionIMEPrefix + extension_id,
115 true /* Case sensitive */);
118 bool IsKeyboardLayoutExtension(const std::string& input_method_id) {
119 if (IsComponentExtensionIME(input_method_id))
120 return StartsWithASCII(GetComponentIDByInputMethodID(input_method_id),
121 "xkb:", true);
122 return false;
125 std::string MaybeGetLegacyXkbId(const std::string& input_method_id) {
126 if (IsKeyboardLayoutExtension(input_method_id))
127 return GetComponentIDByInputMethodID(input_method_id);
128 return input_method_id;
131 } // namespace extension_ime_util
132 } // namespace chromeos