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"
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;
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
,
46 if (IsComponentExtensionIME(input_method_id
)) {
47 return input_method_id
.substr(kComponentExtensionIMEPrefixLength
,
53 std::string
GetComponentIDByInputMethodID(const std::string
& input_method_id
) {
54 if (IsComponentExtensionIME(input_method_id
))
55 return input_method_id
.substr(kComponentExtensionIMEPrefixLength
+
57 if (IsExtensionIME(input_method_id
))
58 return input_method_id
.substr(kExtensionIMEPrefixLength
+
60 return input_method_id
;
63 std::string
GetInputMethodIDByEngineID(const std::string
& engine_id
) {
64 if (base::StartsWith(engine_id
, kComponentExtensionIMEPrefix
,
65 base::CompareCase::SENSITIVE
) ||
66 base::StartsWith(engine_id
, kExtensionIMEPrefix
,
67 base::CompareCase::SENSITIVE
)) {
70 if (base::StartsWith(engine_id
, "xkb:", base::CompareCase::SENSITIVE
))
71 return GetComponentInputMethodID(kXkbExtensionId
, engine_id
);
72 if (base::StartsWith(engine_id
, "vkd_", base::CompareCase::SENSITIVE
))
73 return GetComponentInputMethodID(kM17nExtensionId
, engine_id
);
74 if (base::StartsWith(engine_id
, "nacl_mozc_", base::CompareCase::SENSITIVE
))
75 return GetComponentInputMethodID(kMozcExtensionId
, engine_id
);
76 if (base::StartsWith(engine_id
, "hangul_",
77 base::CompareCase::SENSITIVE
))
78 return GetComponentInputMethodID(kHangulExtensionId
, engine_id
);
80 if (base::StartsWith(engine_id
, "zh-", base::CompareCase::SENSITIVE
) &&
81 engine_id
.find("pinyin") != std::string::npos
) {
82 return GetComponentInputMethodID(kChinesePinyinExtensionId
, engine_id
);
84 if (base::StartsWith(engine_id
, "zh-", base::CompareCase::SENSITIVE
) &&
85 engine_id
.find("zhuyin") != std::string::npos
) {
86 return GetComponentInputMethodID(kChineseZhuyinExtensionId
, engine_id
);
88 if (base::StartsWith(engine_id
, "zh-", base::CompareCase::SENSITIVE
) &&
89 engine_id
.find("cangjie") != std::string::npos
) {
90 return GetComponentInputMethodID(kChineseCangjieExtensionId
, engine_id
);
92 if (engine_id
.find("-t-i0-") != std::string::npos
)
93 return GetComponentInputMethodID(kT13nExtensionId
, engine_id
);
98 bool IsExtensionIME(const std::string
& input_method_id
) {
99 return base::StartsWith(input_method_id
, kExtensionIMEPrefix
,
100 base::CompareCase::SENSITIVE
) &&
101 input_method_id
.size() >
102 kExtensionIMEPrefixLength
+ kExtensionIdLength
;
105 bool IsComponentExtensionIME(const std::string
& input_method_id
) {
106 return base::StartsWith(input_method_id
, kComponentExtensionIMEPrefix
,
107 base::CompareCase::SENSITIVE
) &&
108 input_method_id
.size() >
109 kComponentExtensionIMEPrefixLength
+ kExtensionIdLength
;
112 bool IsMemberOfExtension(const std::string
& input_method_id
,
113 const std::string
& extension_id
) {
114 return base::StartsWith(input_method_id
,
115 kExtensionIMEPrefix
+ extension_id
,
116 base::CompareCase::SENSITIVE
);
119 bool IsKeyboardLayoutExtension(const std::string
& input_method_id
) {
120 if (IsComponentExtensionIME(input_method_id
))
121 return base::StartsWith(GetComponentIDByInputMethodID(input_method_id
),
122 "xkb:", base::CompareCase::SENSITIVE
);
126 std::string
MaybeGetLegacyXkbId(const std::string
& input_method_id
) {
127 if (IsKeyboardLayoutExtension(input_method_id
))
128 return GetComponentIDByInputMethodID(input_method_id
);
129 return input_method_id
;
132 } // namespace extension_ime_util
133 } // namespace chromeos