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 namespace { 11 const char kExtensionIMEPrefix[] = "_ext_ime_"; 12 const int kExtensionIMEPrefixLength = 13 sizeof(kExtensionIMEPrefix) / sizeof(kExtensionIMEPrefix[0]) - 1; 14 const char kComponentExtensionIMEPrefix[] = "_comp_ime_"; 15 const int kComponentExtensionIMEPrefixLength = 16 sizeof(kComponentExtensionIMEPrefix) / 17 sizeof(kComponentExtensionIMEPrefix[0]) - 1; 18 const int kExtensionIdLength = 32; 19 } // namespace 20 21 namespace extension_ime_util { 22 std::string GetInputMethodID(const std::string& extension_id, 23 const std::string& engine_id) { 24 DCHECK(!extension_id.empty()); 25 DCHECK(!engine_id.empty()); 26 return kExtensionIMEPrefix + extension_id + engine_id; 27 } 28 29 std::string GetComponentInputMethodID(const std::string& extension_id, 30 const std::string& engine_id) { 31 DCHECK(!extension_id.empty()); 32 DCHECK(!engine_id.empty()); 33 return kComponentExtensionIMEPrefix + extension_id + engine_id; 34 } 35 36 std::string GetExtensionIDFromInputMethodID( 37 const std::string& input_method_id) { 38 if (IsExtensionIME(input_method_id) && 39 input_method_id.size() >= kExtensionIMEPrefixLength + 40 kExtensionIdLength) { 41 return input_method_id.substr(kExtensionIMEPrefixLength, 42 kExtensionIdLength); 43 } 44 if (IsComponentExtensionIME(input_method_id) && 45 input_method_id.size() >= kComponentExtensionIMEPrefixLength + 46 kExtensionIdLength) { 47 return input_method_id.substr(kComponentExtensionIMEPrefixLength, 48 kExtensionIdLength); 49 } 50 return ""; 51 } 52 53 bool IsExtensionIME(const std::string& input_method_id) { 54 return StartsWithASCII(input_method_id, 55 kExtensionIMEPrefix, 56 true); // Case sensitive. 57 } 58 59 bool IsComponentExtensionIME(const std::string& input_method_id) { 60 return StartsWithASCII(input_method_id, 61 kComponentExtensionIMEPrefix, 62 true); // Case sensitive. 63 } 64 65 bool IsMemberOfExtension(const std::string& input_method_id, 66 const std::string& extension_id) { 67 return StartsWithASCII(input_method_id, 68 kExtensionIMEPrefix + extension_id, 69 true); // Case sensitive. 70 } 71 72 } // namespace extension_ime_util 73 } // namespace chromeos 74