• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "full_ime_info_manager.h"
17 #include "common_timer_errors.h"
18 #include "ime_info_inquirer.h"
19 namespace OHOS {
20 namespace MiscServices {
21 constexpr uint32_t TIMER_TASK_INTERNAL = 3600000; // updated hourly
~FullImeInfoManager()22 FullImeInfoManager::~FullImeInfoManager()
23 {
24     timer_.Unregister(timerId_);
25     timer_.Shutdown();
26     fullImeInfos_.clear();
27 }
28 
FullImeInfoManager()29 FullImeInfoManager::FullImeInfoManager()
30 {
31     uint32_t ret = timer_.Setup();
32     if (ret != Utils::TIMER_ERR_OK) {
33         IMSA_HILOGE("failed to create timer!");
34         return;
35     }
36     timerId_ = timer_.Register([this]() { Init(); }, TIMER_TASK_INTERNAL, false);
37 }
38 
GetInstance()39 FullImeInfoManager &FullImeInfoManager::GetInstance()
40 {
41     static FullImeInfoManager instance;
42     return instance;
43 }
44 
Init()45 int32_t FullImeInfoManager::Init()
46 {
47     std::vector<std::pair<int32_t, std::vector<FullImeInfo>>> fullImeInfos;
48     auto ret = ImeInfoInquirer::GetInstance().QueryFullImeInfo(fullImeInfos);
49     if (ret != ErrorCode::NO_ERROR) {
50         IMSA_HILOGW("failed to QueryFullImeInfo, ret:%{public}d", ret);
51         return ret;
52     }
53     std::lock_guard<std::mutex> lock(lock_);
54     fullImeInfos_.clear();
55     for (const auto &infos : fullImeInfos) {
56         fullImeInfos_.insert_or_assign(infos.first, infos.second);
57     }
58     return ErrorCode::NO_ERROR;
59 }
60 
Add(int32_t userId)61 int32_t FullImeInfoManager::Add(int32_t userId)
62 {
63     {
64         std::lock_guard<std::mutex> lock(lock_);
65         auto it = fullImeInfos_.find(userId);
66         if (it != fullImeInfos_.end()) {
67             return ErrorCode::NO_ERROR;
68         }
69     }
70     std::vector<FullImeInfo> infos;
71     auto ret = ImeInfoInquirer::GetInstance().QueryFullImeInfo(userId, infos);
72     if (ret != ErrorCode::NO_ERROR) {
73         IMSA_HILOGE("failed to QueryFullImeInfo, userId:%{public}d, ret:%{public}d", userId, ret);
74         return ret;
75     }
76     std::lock_guard<std::mutex> lock(lock_);
77     fullImeInfos_.insert_or_assign(userId, infos);
78     return ErrorCode::NO_ERROR;
79 }
80 
Update()81 int32_t FullImeInfoManager::Update()
82 {
83     auto ret = Init();
84     if (ret != ErrorCode::NO_ERROR) {
85         std::lock_guard<std::mutex> lock(lock_);
86         fullImeInfos_.clear();
87     }
88     return ErrorCode::NO_ERROR;
89 }
90 
Delete(int32_t userId)91 int32_t FullImeInfoManager::Delete(int32_t userId)
92 {
93     std::lock_guard<std::mutex> lock(lock_);
94     fullImeInfos_.erase(userId);
95     return ErrorCode::NO_ERROR;
96 }
97 
Add(int32_t userId,const std::string & bundleName)98 int32_t FullImeInfoManager::Add(int32_t userId, const std::string &bundleName)
99 {
100     return Update(userId, bundleName);
101 }
102 
Delete(int32_t userId,const std::string & bundleName)103 int32_t FullImeInfoManager::Delete(int32_t userId, const std::string &bundleName)
104 {
105     std::lock_guard<std::mutex> lock(lock_);
106     auto it = fullImeInfos_.find(userId);
107     if (it == fullImeInfos_.end()) {
108         return ErrorCode::NO_ERROR;
109     }
110     auto iter = std::find_if(it->second.begin(), it->second.end(),
111         [&bundleName](const FullImeInfo &info) { return bundleName == info.prop.name; });
112     if (iter == it->second.end()) {
113         return ErrorCode::NO_ERROR;
114     }
115     it->second.erase(iter);
116     if (it->second.empty()) {
117         fullImeInfos_.erase(it->first);
118     }
119     return ErrorCode::NO_ERROR;
120 }
121 
Update(int32_t userId,const std::string & bundleName)122 int32_t FullImeInfoManager::Update(int32_t userId, const std::string &bundleName)
123 {
124     FullImeInfo info;
125     auto ret = ImeInfoInquirer::GetInstance().GetFullImeInfo(userId, bundleName, info);
126     if (ret != ErrorCode::NO_ERROR) {
127         IMSA_HILOGE("failed to GetFullImeInfo failed, userId:%{public}d, bundleName:%{public}s, ret:%{public}d",
128             userId, bundleName.c_str(), ret);
129         return ErrorCode::ERROR_PACKAGE_MANAGER;
130     }
131     std::lock_guard<std::mutex> lock(lock_);
132     auto it = fullImeInfos_.find(userId);
133     if (it == fullImeInfos_.end()) {
134         fullImeInfos_.insert({ userId, { info } });
135         return ErrorCode::NO_ERROR;
136     }
137     auto iter = std::find_if(it->second.begin(), it->second.end(),
138         [&bundleName](const FullImeInfo &info) { return bundleName == info.prop.name; });
139     if (iter != it->second.end()) {
140         it->second.erase(iter);
141     }
142     it->second.push_back(info);
143     return ErrorCode::NO_ERROR;
144 }
145 
Get(int32_t userId)146 std::vector<FullImeInfo> FullImeInfoManager::Get(int32_t userId)
147 {
148     std::lock_guard<std::mutex> lock(lock_);
149     auto it = fullImeInfos_.find(userId);
150     if (it == fullImeInfos_.end()) {
151         return {};
152     }
153     return it->second;
154 }
155 
Get(const std::string & bundleName,int32_t userId,FullImeInfo & fullImeInfo)156 bool FullImeInfoManager::Get(const std::string &bundleName, int32_t userId, FullImeInfo &fullImeInfo)
157 {
158     std::lock_guard<std::mutex> lock(lock_);
159     auto it = fullImeInfos_.find(userId);
160     if (it == fullImeInfos_.end()) {
161         IMSA_HILOGD("user %{public}d info", userId);
162         return false;
163     }
164     auto iter = std::find_if(it->second.begin(), it->second.end(),
165         [&bundleName](const FullImeInfo &info) { return bundleName == info.prop.name; });
166     if (iter == it->second.end()) {
167         IMSA_HILOGD("ime: %{public}s not in cache", bundleName.c_str());
168         return false;
169     }
170     fullImeInfo = *iter;
171     return true;
172 }
173 
Has(int32_t userId,const std::string & bundleName)174 bool FullImeInfoManager::Has(int32_t userId, const std::string &bundleName)
175 {
176     std::lock_guard<std::mutex> lock(lock_);
177     auto it = fullImeInfos_.find(userId);
178     if (it == fullImeInfos_.end()) {
179         return false;
180     }
181     auto iter = std::find_if(it->second.begin(), it->second.end(),
182         [&bundleName](const FullImeInfo &info) { return bundleName == info.prop.name; });
183     return iter != it->second.end();
184 }
185 
Get(int32_t userId,uint32_t tokenId)186 std::string FullImeInfoManager::Get(int32_t userId, uint32_t tokenId)
187 {
188     std::lock_guard<std::mutex> lock(lock_);
189     auto it = fullImeInfos_.find(userId);
190     if (it == fullImeInfos_.end()) {
191         return "";
192     }
193     auto iter = std::find_if(
194         it->second.begin(), it->second.end(), [&tokenId](const FullImeInfo &info) { return tokenId == info.tokenId; });
195     if (iter == it->second.end()) {
196         return "";
197     }
198     return (*iter).prop.name;
199 }
200 } // namespace MiscServices
201 } // namespace OHOS