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 input_method_id.size() >= kExtensionIMEPrefixLength + 44 kExtensionIdLength) { 45 return input_method_id.substr(kExtensionIMEPrefixLength, 46 kExtensionIdLength); 47 } 48 if (IsComponentExtensionIME(input_method_id) && 49 input_method_id.size() >= kComponentExtensionIMEPrefixLength + 50 kExtensionIdLength) { 51 return input_method_id.substr(kComponentExtensionIMEPrefixLength, 52 kExtensionIdLength); 53 } 54 return ""; 55 } 56 57 std::string GetInputMethodIDByEngineID(const std::string& engine_id) { 58 if (StartsWithASCII(engine_id, kComponentExtensionIMEPrefix, true) || 59 StartsWithASCII(engine_id, kExtensionIMEPrefix, true)) { 60 return engine_id; 61 } 62 if (StartsWithASCII(engine_id, "xkb:", true)) 63 return GetComponentInputMethodID(kXkbExtensionId, engine_id); 64 if (StartsWithASCII(engine_id, "vkd_", true)) 65 return GetComponentInputMethodID(kM17nExtensionId, engine_id); 66 if (StartsWithASCII(engine_id, "nacl_mozc_", true)) 67 return GetComponentInputMethodID(kMozcExtensionId, engine_id); 68 if (StartsWithASCII(engine_id, "hangul_", true)) 69 return GetComponentInputMethodID(kHangulExtensionId, engine_id); 70 71 if (StartsWithASCII(engine_id, "zh-", true) && 72 engine_id.find("pinyin") != std::string::npos) { 73 return GetComponentInputMethodID(kChinesePinyinExtensionId, engine_id); 74 } 75 if (StartsWithASCII(engine_id, "zh-", true) && 76 engine_id.find("zhuyin") != std::string::npos) { 77 return GetComponentInputMethodID(kChineseZhuyinExtensionId, engine_id); 78 } 79 if (StartsWithASCII(engine_id, "zh-", true) && 80 engine_id.find("cangjie") != std::string::npos) { 81 return GetComponentInputMethodID(kChineseCangjieExtensionId, engine_id); 82 } 83 if (engine_id.find("-t-i0-") != std::string::npos) 84 return GetComponentInputMethodID(kT13nExtensionId, engine_id); 85 86 return engine_id; 87 } 88 89 bool IsExtensionIME(const std::string& input_method_id) { 90 return StartsWithASCII(input_method_id, 91 kExtensionIMEPrefix, 92 true); // Case sensitive. 93 } 94 95 bool IsComponentExtensionIME(const std::string& input_method_id) { 96 return StartsWithASCII(input_method_id, 97 kComponentExtensionIMEPrefix, 98 true); // Case sensitive. 99 } 100 101 bool IsMemberOfExtension(const std::string& input_method_id, 102 const std::string& extension_id) { 103 return StartsWithASCII(input_method_id, 104 kExtensionIMEPrefix + extension_id, 105 true); // Case sensitive. 106 } 107 108 bool IsKeyboardLayoutExtension(const std::string& input_method_id) { 109 std::string prefix = kComponentExtensionIMEPrefix; 110 return StartsWithASCII(input_method_id, prefix + kXkbExtensionId, true); 111 } 112 113 std::string MaybeGetLegacyXkbId(const std::string& input_method_id) { 114 if (IsKeyboardLayoutExtension(input_method_id)) { 115 size_t pos = input_method_id.find("xkb:"); 116 if (pos != std::string::npos) 117 return input_method_id.substr(pos); 118 } 119 return input_method_id; 120 } 121 122 } // namespace extension_ime_util 123 } // namespace chromeos 124