1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17 #include "bundle_name_parser.h"
18
19 #include "util.h"
20
21 #include <cJSON.h>
22
23 #undef MMI_LOG_DOMAIN
24 #define MMI_LOG_DOMAIN MMI_LOG_SERVER
25 #undef MMI_LOG_TAG
26 #define MMI_LOG_TAG "BundleNameParser"
27
28 namespace OHOS {
29 namespace MMI {
30 namespace {
31 constexpr std::string_view bundleNameConfigDir { "/etc/multimodalinput/bundle_name_config.json" };
32 constexpr int32_t maxJsonArraySize { 100 };
33 } // namespace
34
GetInstance()35 BundleNameParser& BundleNameParser::GetInstance()
36 {
37 static BundleNameParser instance;
38 return instance;
39 }
40
Init()41 int32_t BundleNameParser::Init()
42 {
43 CALL_INFO_TRACE;
44 if (isInitialized_.load()) {
45 return RET_OK;
46 }
47 std::string jsonStr = ReadJsonFile(std::string(bundleNameConfigDir));
48 if (jsonStr.empty()) {
49 MMI_HILOGE("Read bundleName failed");
50 return RET_ERR;
51 }
52 JsonParser parser(jsonStr.c_str());
53 if (!cJSON_IsObject(parser.Get())) {
54 MMI_HILOGE("Not valid object");
55 return RET_ERR;
56 }
57 if (ParseBundleNameMap(parser) != RET_OK) {
58 MMI_HILOGE("ParseBundleNameMap failed");
59 return RET_ERR;
60 }
61 PrintBundleNames();
62 isInitialized_.store(true);
63 return RET_OK;
64 }
65
GetBundleName(const std::string & key)66 std::string BundleNameParser::GetBundleName(const std::string &key)
67 {
68 if (Init() != RET_OK) {
69 MMI_HILOGE("Init failed");
70 return "";
71 }
72 std::shared_lock<std::shared_mutex> lock(lock_);
73 if (bundleNames_.find(key) != bundleNames_.end()) {
74 return bundleNames_[key];
75 }
76 MMI_HILOGW("No %{public}s matched.", key.c_str());
77 return "";
78 }
79
ParseBundleNameMap(const JsonParser & jsonParser)80 int32_t BundleNameParser::ParseBundleNameMap(const JsonParser &jsonParser)
81 {
82 cJSON *bundleNameMapJson = cJSON_GetObjectItemCaseSensitive(jsonParser.Get(), "bundle_name_map");
83 if (!cJSON_IsArray(bundleNameMapJson)) {
84 MMI_HILOGE("bundleNameMapJson is not array");
85 return RET_ERR;
86 }
87 int32_t arraySize = cJSON_GetArraySize(bundleNameMapJson);
88 if (arraySize > maxJsonArraySize) {
89 MMI_HILOGW("arraySize is too much, truncate it");
90 }
91 for (int32_t i = 0; i < std::min(arraySize, maxJsonArraySize); i++) {
92 cJSON* bundleNameItemJson = cJSON_GetArrayItem(bundleNameMapJson, i);
93 CHKPC(bundleNameItemJson);
94 BundleNameItem bundleNameItem;
95 if (ParseBundleNameItem(bundleNameItemJson, bundleNameItem) != RET_OK) {
96 MMI_HILOGE("ParseBundleNameItem failed");
97 continue;
98 }
99 std::unique_lock<std::shared_mutex> lock(lock_);
100 bundleNames_.insert({ bundleNameItem.placeHolder, bundleNameItem.bundleName });
101 }
102 return RET_OK;
103 }
104
ParseBundleNameItem(const cJSON * json,BundleNameItem & bundleNameItem)105 int32_t BundleNameParser::ParseBundleNameItem(const cJSON *json, BundleNameItem &bundleNameItem)
106 {
107 if (JsonParser::ParseString(json, "placeholder", bundleNameItem.placeHolder) != RET_OK) {
108 MMI_HILOGW("Parse placeholder failed");
109 return RET_ERR;
110 }
111 if (JsonParser::ParseString(json, "bundle_name", bundleNameItem.bundleName) != RET_OK) {
112 MMI_HILOGW("Parse bundle_name failed");
113 return RET_ERR;
114 }
115 return RET_OK;
116 }
117
PrintBundleNames()118 void BundleNameParser::PrintBundleNames()
119 {
120 CALL_INFO_TRACE;
121 std::shared_lock<std::shared_mutex> lock(lock_);
122 for (const auto &bundleName: bundleNames_) {
123 MMI_HILOGI("key:%{public}s -> value:%{public}s", bundleName.first.c_str(), bundleName.second.c_str());
124 }
125 }
126
127 } // namespace MMI
128 } // namespace OHOS