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 namespace { | 10 namespace { |
11 const char kExtensionIMEPrefix[] = "_ext_ime_"; | 11 const char kExtensionIMEPrefix[] = "_ext_ime_"; |
12 const int kExtensionIMEPrefixLength = | 12 const int kExtensionIMEPrefixLength = |
13 sizeof(kExtensionIMEPrefix) / sizeof(kExtensionIMEPrefix[0]) - 1; | 13 sizeof(kExtensionIMEPrefix) / sizeof(kExtensionIMEPrefix[0]) - 1; |
14 const char kComponentExtensionIMEPrefix[] = "_comp_ime_"; | 14 const char kComponentExtensionIMEPrefix[] = "_comp_ime_"; |
15 const char kExtensionXkbIdPrefix[] = | 15 const char kExtensionXkbIdPrefix[] = |
16 "_comp_ime_fgoepimhcoialccpbmpnnblemnepkkao"; | 16 "_comp_ime_fgoepimhcoialccpbmpnnblemnepkkao"; |
17 const int kComponentExtensionIMEPrefixLength = | 17 const int kComponentExtensionIMEPrefixLength = |
18 sizeof(kComponentExtensionIMEPrefix) / | 18 sizeof(kComponentExtensionIMEPrefix) / |
19 sizeof(kComponentExtensionIMEPrefix[0]) - 1; | 19 sizeof(kComponentExtensionIMEPrefix[0]) - 1; |
20 const int kExtensionIdLength = 32; | 20 const int kExtensionIdLength = 32; |
| 21 // Hard coded to true. If the wrapped extension keyboards misbehaves, |
| 22 // we can easily change this to false to switch back to legacy xkb keyboards. |
| 23 bool g_use_wrapped_extension_keyboard_layouts = true; |
21 } // namespace | 24 } // namespace |
22 | 25 |
23 namespace extension_ime_util { | 26 namespace extension_ime_util { |
24 std::string GetInputMethodID(const std::string& extension_id, | 27 std::string GetInputMethodID(const std::string& extension_id, |
25 const std::string& engine_id) { | 28 const std::string& engine_id) { |
26 DCHECK(!extension_id.empty()); | 29 DCHECK(!extension_id.empty()); |
27 DCHECK(!engine_id.empty()); | 30 DCHECK(!engine_id.empty()); |
28 return kExtensionIMEPrefix + extension_id + engine_id; | 31 return kExtensionIMEPrefix + extension_id + engine_id; |
29 } | 32 } |
30 | 33 |
(...skipping 14 matching lines...) Expand all Loading... |
45 } | 48 } |
46 if (IsComponentExtensionIME(input_method_id) && | 49 if (IsComponentExtensionIME(input_method_id) && |
47 input_method_id.size() >= kComponentExtensionIMEPrefixLength + | 50 input_method_id.size() >= kComponentExtensionIMEPrefixLength + |
48 kExtensionIdLength) { | 51 kExtensionIdLength) { |
49 return input_method_id.substr(kComponentExtensionIMEPrefixLength, | 52 return input_method_id.substr(kComponentExtensionIMEPrefixLength, |
50 kExtensionIdLength); | 53 kExtensionIdLength); |
51 } | 54 } |
52 return ""; | 55 return ""; |
53 } | 56 } |
54 | 57 |
| 58 std::string GetInputMethodIDByKeyboardLayout( |
| 59 const std::string& keyboard_layout_id) { |
| 60 bool migrate = UseWrappedExtensionKeyboardLayouts(); |
| 61 if (IsKeyboardLayoutExtension(keyboard_layout_id)) { |
| 62 if (migrate) |
| 63 return keyboard_layout_id; |
| 64 return keyboard_layout_id.substr(arraysize(kExtensionXkbIdPrefix) - 1); |
| 65 } |
| 66 if (migrate && StartsWithASCII(keyboard_layout_id, "xkb:", true)) |
| 67 return kExtensionXkbIdPrefix + keyboard_layout_id; |
| 68 return keyboard_layout_id; |
| 69 } |
| 70 |
55 bool IsExtensionIME(const std::string& input_method_id) { | 71 bool IsExtensionIME(const std::string& input_method_id) { |
56 return StartsWithASCII(input_method_id, | 72 return StartsWithASCII(input_method_id, |
57 kExtensionIMEPrefix, | 73 kExtensionIMEPrefix, |
58 true); // Case sensitive. | 74 true); // Case sensitive. |
59 } | 75 } |
60 | 76 |
61 bool IsComponentExtensionIME(const std::string& input_method_id) { | 77 bool IsComponentExtensionIME(const std::string& input_method_id) { |
62 return StartsWithASCII(input_method_id, | 78 return StartsWithASCII(input_method_id, |
63 kComponentExtensionIMEPrefix, | 79 kComponentExtensionIMEPrefix, |
64 true); // Case sensitive. | 80 true); // Case sensitive. |
65 } | 81 } |
66 | 82 |
67 bool IsMemberOfExtension(const std::string& input_method_id, | 83 bool IsMemberOfExtension(const std::string& input_method_id, |
68 const std::string& extension_id) { | 84 const std::string& extension_id) { |
69 return StartsWithASCII(input_method_id, | 85 return StartsWithASCII(input_method_id, |
70 kExtensionIMEPrefix + extension_id, | 86 kExtensionIMEPrefix + extension_id, |
71 true); // Case sensitive. | 87 true); // Case sensitive. |
72 } | 88 } |
73 | 89 |
74 bool IsKeyboardLayoutExtension(const std::string& input_method_id) { | 90 bool IsKeyboardLayoutExtension(const std::string& input_method_id) { |
75 return StartsWithASCII(input_method_id, kExtensionXkbIdPrefix, true); | 91 return StartsWithASCII(input_method_id, kExtensionXkbIdPrefix, true); |
76 } | 92 } |
77 | 93 |
| 94 bool UseWrappedExtensionKeyboardLayouts() { |
| 95 return g_use_wrapped_extension_keyboard_layouts; |
| 96 } |
| 97 |
| 98 std::string MaybeGetLegacyXkbId(const std::string& input_method_id) { |
| 99 if (IsKeyboardLayoutExtension(input_method_id)) { |
| 100 size_t pos = input_method_id.find("xkb:"); |
| 101 if (pos != std::string::npos) |
| 102 return input_method_id.substr(pos); |
| 103 } |
| 104 return input_method_id; |
| 105 } |
| 106 |
| 107 ScopedUseExtensionKeyboardFlagForTesting:: |
| 108 ScopedUseExtensionKeyboardFlagForTesting(bool new_flag) |
| 109 : auto_reset_(&g_use_wrapped_extension_keyboard_layouts, new_flag) { |
| 110 } |
| 111 |
| 112 ScopedUseExtensionKeyboardFlagForTesting:: |
| 113 ~ScopedUseExtensionKeyboardFlagForTesting() { |
| 114 } |
| 115 |
78 } // namespace extension_ime_util | 116 } // namespace extension_ime_util |
79 } // namespace chromeos | 117 } // namespace chromeos |
OLD | NEW |