| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chromeos/ime/extension_ime_util.h" | 5 #include "chromeos/ime/extension_ime_util.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 | 8 |
| 9 namespace chromeos { | 9 namespace chromeos { |
| 10 |
| 10 namespace { | 11 namespace { |
| 12 |
| 11 const char kExtensionIMEPrefix[] = "_ext_ime_"; | 13 const char kExtensionIMEPrefix[] = "_ext_ime_"; |
| 12 const int kExtensionIMEPrefixLength = | 14 const int kExtensionIMEPrefixLength = |
| 13 sizeof(kExtensionIMEPrefix) / sizeof(kExtensionIMEPrefix[0]) - 1; | 15 sizeof(kExtensionIMEPrefix) / sizeof(kExtensionIMEPrefix[0]) - 1; |
| 14 const char kComponentExtensionIMEPrefix[] = "_comp_ime_"; | 16 const char kComponentExtensionIMEPrefix[] = "_comp_ime_"; |
| 15 const char kPublicExtensionXkbIdPrefix[] = | |
| 16 "_comp_ime_fgoepimhcoialccpbmpnnblemnepkkao"; | |
| 17 const char kInternalExtensionXkbIdPrefix[] = | |
| 18 "_comp_ime_jkghodnilhceideoidjikpgommlajknk"; | |
| 19 | |
| 20 const int kComponentExtensionIMEPrefixLength = | 17 const int kComponentExtensionIMEPrefixLength = |
| 21 sizeof(kComponentExtensionIMEPrefix) / | 18 sizeof(kComponentExtensionIMEPrefix) / |
| 22 sizeof(kComponentExtensionIMEPrefix[0]) - 1; | 19 sizeof(kComponentExtensionIMEPrefix[0]) - 1; |
| 23 const int kExtensionIdLength = 32; | 20 const int kExtensionIdLength = 32; |
| 24 // Hard coded to true. If the wrapped extension keyboards misbehaves, | 21 |
| 25 // we can easily change this to false to switch back to legacy xkb keyboards. | |
| 26 bool g_use_wrapped_extension_keyboard_layouts = true; | |
| 27 } // namespace | 22 } // namespace |
| 28 | 23 |
| 29 namespace extension_ime_util { | 24 namespace extension_ime_util { |
| 25 |
| 30 std::string GetInputMethodID(const std::string& extension_id, | 26 std::string GetInputMethodID(const std::string& extension_id, |
| 31 const std::string& engine_id) { | 27 const std::string& engine_id) { |
| 32 DCHECK(!extension_id.empty()); | 28 DCHECK(!extension_id.empty()); |
| 33 DCHECK(!engine_id.empty()); | 29 DCHECK(!engine_id.empty()); |
| 34 return kExtensionIMEPrefix + extension_id + engine_id; | 30 return kExtensionIMEPrefix + extension_id + engine_id; |
| 35 } | 31 } |
| 36 | 32 |
| 37 std::string GetComponentInputMethodID(const std::string& extension_id, | 33 std::string GetComponentInputMethodID(const std::string& extension_id, |
| 38 const std::string& engine_id) { | 34 const std::string& engine_id) { |
| 39 DCHECK(!extension_id.empty()); | 35 DCHECK(!extension_id.empty()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 51 } | 47 } |
| 52 if (IsComponentExtensionIME(input_method_id) && | 48 if (IsComponentExtensionIME(input_method_id) && |
| 53 input_method_id.size() >= kComponentExtensionIMEPrefixLength + | 49 input_method_id.size() >= kComponentExtensionIMEPrefixLength + |
| 54 kExtensionIdLength) { | 50 kExtensionIdLength) { |
| 55 return input_method_id.substr(kComponentExtensionIMEPrefixLength, | 51 return input_method_id.substr(kComponentExtensionIMEPrefixLength, |
| 56 kExtensionIdLength); | 52 kExtensionIdLength); |
| 57 } | 53 } |
| 58 return ""; | 54 return ""; |
| 59 } | 55 } |
| 60 | 56 |
| 61 std::string GetInputMethodIDByKeyboardLayout( | 57 std::string GetInputMethodIDByEngineID(const std::string& engine_id) { |
| 62 const std::string& keyboard_layout_id) { | 58 if (StartsWithASCII(engine_id, kComponentExtensionIMEPrefix, true) || |
| 63 const char* kExtensionXkbIdPrefix = | 59 StartsWithASCII(engine_id, kExtensionIMEPrefix, true)) |
| 64 #if defined(OFFICIAL_BUILD) | 60 return engine_id; |
| 65 kInternalExtensionXkbIdPrefix; | 61 if (StartsWithASCII(engine_id, "xkb:", true)) |
| 66 #else | 62 return GetComponentInputMethodID(kXkbExtensionId, engine_id); |
| 67 kPublicExtensionXkbIdPrefix; | 63 if (StartsWithASCII(engine_id, "vkd_", true)) |
| 68 #endif | 64 return GetComponentInputMethodID(kM17nExtensionId, engine_id); |
| 69 bool migrate = UseWrappedExtensionKeyboardLayouts(); | 65 if (StartsWithASCII(engine_id, "nacl_mozc_", true)) |
| 70 if (IsKeyboardLayoutExtension(keyboard_layout_id)) { | 66 return GetComponentInputMethodID(kMozcExtensionId, engine_id); |
| 71 std::string id = keyboard_layout_id.substr( | 67 if (StartsWithASCII(engine_id, "hangul_", true)) |
| 72 arraysize(kPublicExtensionXkbIdPrefix) - 1); | 68 return GetComponentInputMethodID(kHangulExtensionId, engine_id); |
| 73 if (migrate) | 69 |
| 74 return kExtensionXkbIdPrefix + id; | 70 if (StartsWithASCII(engine_id, "zh-", true) && |
| 75 return id; | 71 engine_id.find("pinyin") != std::string::npos) |
| 76 } | 72 return GetComponentInputMethodID(kChinesePinyinExtensionId, engine_id); |
| 77 if (migrate && StartsWithASCII(keyboard_layout_id, "xkb:", true)) | 73 if (StartsWithASCII(engine_id, "zh-", true) && |
| 78 return kExtensionXkbIdPrefix + keyboard_layout_id; | 74 engine_id.find("zhuyin") != std::string::npos) |
| 79 return keyboard_layout_id; | 75 return GetComponentInputMethodID(kChineseZhuyinExtensionId, engine_id); |
| 76 if (StartsWithASCII(engine_id, "zh-", true) && |
| 77 engine_id.find("cangjie") != std::string::npos) |
| 78 return GetComponentInputMethodID(kChineseCangjieExtensionId, engine_id); |
| 79 if (engine_id.find("-t-i0-") != std::string::npos) |
| 80 return GetComponentInputMethodID(kT13nExtensionId, engine_id); |
| 81 |
| 82 return engine_id; |
| 80 } | 83 } |
| 81 | 84 |
| 82 bool IsExtensionIME(const std::string& input_method_id) { | 85 bool IsExtensionIME(const std::string& input_method_id) { |
| 83 return StartsWithASCII(input_method_id, | 86 return StartsWithASCII(input_method_id, |
| 84 kExtensionIMEPrefix, | 87 kExtensionIMEPrefix, |
| 85 true); // Case sensitive. | 88 true); // Case sensitive. |
| 86 } | 89 } |
| 87 | 90 |
| 88 bool IsComponentExtensionIME(const std::string& input_method_id) { | 91 bool IsComponentExtensionIME(const std::string& input_method_id) { |
| 89 return StartsWithASCII(input_method_id, | 92 return StartsWithASCII(input_method_id, |
| 90 kComponentExtensionIMEPrefix, | 93 kComponentExtensionIMEPrefix, |
| 91 true); // Case sensitive. | 94 true); // Case sensitive. |
| 92 } | 95 } |
| 93 | 96 |
| 94 bool IsMemberOfExtension(const std::string& input_method_id, | 97 bool IsMemberOfExtension(const std::string& input_method_id, |
| 95 const std::string& extension_id) { | 98 const std::string& extension_id) { |
| 96 return StartsWithASCII(input_method_id, | 99 return StartsWithASCII(input_method_id, |
| 97 kExtensionIMEPrefix + extension_id, | 100 kExtensionIMEPrefix + extension_id, |
| 98 true); // Case sensitive. | 101 true); // Case sensitive. |
| 99 } | 102 } |
| 100 | 103 |
| 101 bool IsKeyboardLayoutExtension(const std::string& input_method_id) { | 104 bool IsKeyboardLayoutExtension(const std::string& input_method_id) { |
| 102 return StartsWithASCII(input_method_id, kPublicExtensionXkbIdPrefix, true) || | 105 std::string prefix = kComponentExtensionIMEPrefix; |
| 103 StartsWithASCII(input_method_id, kInternalExtensionXkbIdPrefix, true); | 106 return StartsWithASCII(input_method_id, prefix + kXkbExtensionId, true); |
| 104 } | |
| 105 | |
| 106 bool UseWrappedExtensionKeyboardLayouts() { | |
| 107 return g_use_wrapped_extension_keyboard_layouts; | |
| 108 } | 107 } |
| 109 | 108 |
| 110 std::string MaybeGetLegacyXkbId(const std::string& input_method_id) { | 109 std::string MaybeGetLegacyXkbId(const std::string& input_method_id) { |
| 111 if (IsKeyboardLayoutExtension(input_method_id)) { | 110 if (IsKeyboardLayoutExtension(input_method_id)) { |
| 112 size_t pos = input_method_id.find("xkb:"); | 111 size_t pos = input_method_id.find("xkb:"); |
| 113 if (pos != std::string::npos) | 112 if (pos != std::string::npos) |
| 114 return input_method_id.substr(pos); | 113 return input_method_id.substr(pos); |
| 115 } | 114 } |
| 116 return input_method_id; | 115 return input_method_id; |
| 117 } | 116 } |
| 118 | 117 |
| 119 ScopedUseExtensionKeyboardFlagForTesting:: | |
| 120 ScopedUseExtensionKeyboardFlagForTesting(bool new_flag) | |
| 121 : auto_reset_(&g_use_wrapped_extension_keyboard_layouts, new_flag) { | |
| 122 } | |
| 123 | |
| 124 ScopedUseExtensionKeyboardFlagForTesting:: | |
| 125 ~ScopedUseExtensionKeyboardFlagForTesting() { | |
| 126 } | |
| 127 | |
| 128 } // namespace extension_ime_util | 118 } // namespace extension_ime_util |
| 129 } // namespace chromeos | 119 } // namespace chromeos |
| OLD | NEW |