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. 4 5 #include "chromeos/ime/extension_ime_util.h" 6 7 #include "base/strings/string_util.h" 8 9 namespace chromeos { 10 11 namespace { 12 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; 21 22 } // namespace 23 24 namespace extension_ime_util { 25 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; 31 } 32 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; 38 } 39 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); 45 } 46 if (IsComponentExtensionIME(input_method_id)) { 47 return input_method_id.substr(kComponentExtensionIMEPrefixLength, 48 kExtensionIdLength); 49 } 50 return ""; 51 } 52 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; 61 } 62 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; 67 } 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); 76 77 if (StartsWithASCII(engine_id, "zh-", true) && 78 engine_id.find("pinyin") != std::string::npos) { 79 return GetComponentInputMethodID(kChinesePinyinExtensionId, engine_id); 80 } 81 if (StartsWithASCII(engine_id, "zh-", true) && 82 engine_id.find("zhuyin") != std::string::npos) { 83 return GetComponentInputMethodID(kChineseZhuyinExtensionId, engine_id); 84 } 85 if (StartsWithASCII(engine_id, "zh-", true) && 86 engine_id.find("cangjie") != std::string::npos) { 87 return GetComponentInputMethodID(kChineseCangjieExtensionId, engine_id); 88 } 89 if (engine_id.find("-t-i0-") != std::string::npos) 90 return GetComponentInputMethodID(kT13nExtensionId, engine_id); 91 92 return engine_id; 93 } 94 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; 101 } 102 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; 109 } 110 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 */); 116 } 117 118 bool IsKeyboardLayoutExtension(const std::string& input_method_id) { 119 std::string prefix = kComponentExtensionIMEPrefix; 120 return StartsWithASCII(input_method_id, prefix + kXkbExtensionId, true); 121 } 122 123 std::string MaybeGetLegacyXkbId(const std::string& input_method_id) { 124 if (IsKeyboardLayoutExtension(input_method_id)) 125 return GetComponentIDByInputMethodID(input_method_id); 126 return input_method_id; 127 } 128 129 } // namespace extension_ime_util 130 } // namespace chromeos 131