1 /*
2 * Copyright (c) 2022 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 #include "ime_cfg_manager.h"
17
18 #include <fcntl.h>
19
20 #include <algorithm>
21 #include <ios>
22 #include <string>
23
24 #include "file_operator.h"
25 #include "global.h"
26 namespace OHOS {
27 namespace MiscServices {
28 namespace {
29 constexpr const char *IME_CFG_FILE_PATH = "/data/service/el1/public/imf/ime_cfg.json";
30 } // namespace
GetInstance()31 ImeCfgManager &ImeCfgManager::GetInstance()
32 {
33 static ImeCfgManager instance;
34 return instance;
35 }
36
Init()37 void ImeCfgManager::Init()
38 {
39 ReadImeCfg();
40 }
41
ReadImeCfg()42 void ImeCfgManager::ReadImeCfg()
43 {
44 if (!FileOperator::IsExist(IME_CFG_FILE_PATH)) {
45 IMSA_HILOGD("ime cfg file not found.");
46 return;
47 }
48 std::string cfg;
49 bool ret = FileOperator::Read(IME_CFG_FILE_PATH, cfg);
50 if (!ret) {
51 IMSA_HILOGE("failed to ReadJsonFile!");
52 return;
53 }
54 ParseImeCfg(cfg);
55 }
56
WriteImeCfg()57 void ImeCfgManager::WriteImeCfg()
58 {
59 auto content = PackageImeCfg();
60 if (content.empty()) {
61 IMSA_HILOGE("failed to Package imeCfg!");
62 return;
63 }
64 if (!FileOperator::Write(IME_CFG_FILE_PATH, content, O_CREAT | O_WRONLY | O_SYNC | O_TRUNC)) {
65 IMSA_HILOGE("failed to WriteJsonFile!");
66 }
67 }
68
ParseImeCfg(const std::string & content)69 bool ImeCfgManager::ParseImeCfg(const std::string &content)
70 {
71 IMSA_HILOGD("content: %{public}s", content.c_str());
72 ImePersistCfg cfg;
73 auto ret = cfg.Unmarshall(content);
74 if (!ret) {
75 IMSA_HILOGE("Unmarshall failed!");
76 return false;
77 }
78 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
79 imeConfigs_ = cfg.imePersistInfo;
80 return true;
81 }
82
PackageImeCfg()83 std::string ImeCfgManager::PackageImeCfg()
84 {
85 ImePersistCfg cfg;
86 {
87 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
88 cfg.imePersistInfo = imeConfigs_;
89 }
90 std::string content;
91 auto ret = cfg.Marshall(content);
92 IMSA_HILOGD("ret: %{public}d, content: %{public}s, size: %{public}zu", ret, content.c_str(),
93 cfg.imePersistInfo.size());
94 return content;
95 }
96
AddImeCfg(const ImePersistInfo & cfg)97 void ImeCfgManager::AddImeCfg(const ImePersistInfo &cfg)
98 {
99 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
100 imeConfigs_.push_back(cfg);
101 WriteImeCfg();
102 }
103
ModifyImeCfg(const ImePersistInfo & cfg)104 void ImeCfgManager::ModifyImeCfg(const ImePersistInfo &cfg)
105 {
106 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
107 auto it = std::find_if(imeConfigs_.begin(), imeConfigs_.end(),
108 [&cfg](const ImePersistInfo &imeCfg) { return imeCfg.userId == cfg.userId && !cfg.currentIme.empty(); });
109 if (it != imeConfigs_.end()) {
110 *it = cfg;
111 }
112
113 WriteImeCfg();
114 }
115
DeleteImeCfg(int32_t userId)116 void ImeCfgManager::DeleteImeCfg(int32_t userId)
117 {
118 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
119 for (auto iter = imeConfigs_.begin(); iter != imeConfigs_.end(); iter++) {
120 if (iter->userId == userId) {
121 imeConfigs_.erase(iter);
122 break;
123 }
124 }
125 WriteImeCfg();
126 }
127
GetImeCfg(int32_t userId)128 ImePersistInfo ImeCfgManager::GetImeCfg(int32_t userId)
129 {
130 std::lock_guard<std::recursive_mutex> lock(imeCfgLock_);
131 auto it = std::find_if(
132 imeConfigs_.begin(), imeConfigs_.end(), [userId](const ImePersistInfo &cfg) { return cfg.userId == userId; });
133 if (it != imeConfigs_.end()) {
134 return *it;
135 }
136 return {};
137 }
138
GetCurrentImeCfg(int32_t userId)139 std::shared_ptr<ImeNativeCfg> ImeCfgManager::GetCurrentImeCfg(int32_t userId)
140 {
141 auto cfg = GetImeCfg(userId);
142 ImeNativeCfg info;
143 info.subName = cfg.currentSubName;
144 info.imeId = cfg.currentIme;
145 auto pos = info.imeId.find('/');
146 if (pos != std::string::npos && pos + 1 < info.imeId.size()) {
147 info.bundleName = info.imeId.substr(0, pos);
148 info.extName = info.imeId.substr(pos + 1);
149 }
150 return std::make_shared<ImeNativeCfg>(info);
151 }
152 } // namespace MiscServices
153 } // namespace OHOS