• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "bundle_resource_helper.h"
17 
18 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
19 #include "account_helper.h"
20 #include "bundle_parser.h"
21 #include "bundle_resource_callback.h"
22 #include "bundle_resource_constants.h"
23 #include "bundle_resource_manager.h"
24 #include "bundle_resource_observer.h"
25 #include "bundle_resource_param.h"
26 #include "bundle_resource_parser.h"
27 #include "bundle_resource_register.h"
28 #include "bundle_system_state.h"
29 #include "resource_manager.h"
30 #endif
31 
32 namespace OHOS {
33 namespace AppExecFwk {
BundleSystemStateInit()34 void BundleResourceHelper::BundleSystemStateInit()
35 {
36 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
37     APP_LOGI("system state init start");
38     // init language and colorMode
39     BundleSystemState::GetInstance().SetSystemLanguage(BundleResourceParam::GetSystemLanguage());
40     BundleSystemState::GetInstance().SetSystemColorMode(BundleResourceParam::GetSystemColorMode());
41     APP_LOGI("current system state: %{public}s", BundleSystemState::GetInstance().ToString().c_str());
42 #endif
43 }
44 
RegisterConfigurationObserver()45 void BundleResourceHelper::RegisterConfigurationObserver()
46 {
47 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
48     BundleResourceRegister::RegisterConfigurationObserver();
49 #endif
50 }
51 
RegisterCommonEventSubscriber()52 void BundleResourceHelper::RegisterCommonEventSubscriber()
53 {
54 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
55     BundleResourceRegister::RegisterCommonEventSubscriber();
56 #endif
57 }
58 
AddResourceInfoByBundleName(const std::string & bundleName,const int32_t userId,const ADD_RESOURCE_TYPE type,const bool isBundleFirstInstall)59 void BundleResourceHelper::AddResourceInfoByBundleName(const std::string &bundleName,
60     const int32_t userId, const ADD_RESOURCE_TYPE type, const bool isBundleFirstInstall)
61 {
62 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
63     APP_LOGI_NOFUNC("-n %{public}s -u %{public}d type %{public}d add resource start", bundleName.c_str(), userId,
64         static_cast<int32_t>(type));
65     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
66     if (manager == nullptr) {
67         APP_LOGE("failed, manager is nullptr");
68         return;
69     }
70     switch (type) {
71         case ADD_RESOURCE_TYPE::INSTALL_BUNDLE : {
72             // add new resource info
73             if (!manager->AddResourceInfoByBundleNameWhenInstall(bundleName, userId, isBundleFirstInstall)) {
74                 APP_LOGW("add failed, bundleName:%{public}s", bundleName.c_str());
75             }
76             break;
77         }
78         case ADD_RESOURCE_TYPE::UPDATE_BUNDLE : {
79             if (!manager->AddResourceInfoByBundleNameWhenUpdate(bundleName, userId)) {
80                 APP_LOGW("update failed, bundleName:%{public}s", bundleName.c_str());
81             }
82             break;
83         }
84         case ADD_RESOURCE_TYPE::CREATE_USER : {
85             if (!manager->AddResourceInfoByBundleNameWhenCreateUser(bundleName, userId)) {
86                 APP_LOGW("update failed, bundleName:%{public}s", bundleName.c_str());
87             }
88             break;
89         }
90         default:
91             break;
92     }
93     APP_LOGI_NOFUNC("-n %{public}s -u %{public}d type %{public}d add resource end", bundleName.c_str(), userId,
94         static_cast<int32_t>(type));
95 #endif
96 }
97 
DeleteBundleResourceInfo(const std::string & bundleName,const int32_t userId,const bool isExistInOtherUser)98 bool BundleResourceHelper::DeleteBundleResourceInfo(
99     const std::string &bundleName, const int32_t userId, const bool isExistInOtherUser)
100 {
101 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
102     APP_LOGI("start delete resource -n %{public}s -u %{public}d", bundleName.c_str(), userId);
103     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
104     if (manager == nullptr) {
105         APP_LOGE("failed, manager is nullptr");
106         return false;
107     }
108 
109     if (!manager->DeleteBundleResourceInfo(bundleName, userId, isExistInOtherUser)) {
110         APP_LOGE("delete resource failed -n %{public}s -u %{public}d", bundleName.c_str(), userId);
111         return false;
112     }
113 
114     return true;
115 #else
116     return false;
117 #endif
118 }
119 
GetAllBundleResourceName(std::vector<std::string> & resourceNames)120 void BundleResourceHelper::GetAllBundleResourceName(std::vector<std::string> &resourceNames)
121 {
122 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
123     APP_LOGI("start");
124     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
125     if (manager == nullptr) {
126         APP_LOGE("failed, manager is nullptr");
127         return;
128     }
129     if (!manager->GetAllResourceName(resourceNames)) {
130         APP_LOGE("failed");
131     }
132 #endif
133 }
134 
ParseBundleName(const std::string & keyName)135 std::string BundleResourceHelper::ParseBundleName(const std::string &keyName)
136 {
137 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
138     ResourceInfo info;
139     info.ParseKey(keyName);
140     return info.bundleName_;
141 #else
142     return keyName;
143 #endif
144 }
145 
SetOverlayEnabled(const std::string & bundleName,const std::string & moduleName,bool isEnabled,int32_t userId)146 void BundleResourceHelper::SetOverlayEnabled(const std::string &bundleName, const std::string &moduleName,
147     bool isEnabled, int32_t userId)
148 {
149 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
150     APP_LOGD("bundleName: %{public}s, moduleName: %{public}s,isEnabled: %{public}d, userId: %{public}d",
151         bundleName.c_str(), moduleName.c_str(), isEnabled, userId);
152     BundleResourceCallback callback;
153     callback.OnOverlayStatusChanged(bundleName, isEnabled, userId);
154 #endif
155 }
156 
DeleteAllResourceInfo()157 bool BundleResourceHelper::DeleteAllResourceInfo()
158 {
159 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
160     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
161     if (manager == nullptr) {
162         APP_LOGE("failed, manager is nullptr");
163         return false;
164     }
165     if (!manager->DeleteAllResourceInfo()) {
166         APP_LOGE("delete all bundle resource failed");
167         return false;
168     }
169     return true;
170 #else
171     return false;
172 #endif
173 }
174 
AddCloneBundleResourceInfo(const std::string & bundleName,const int32_t userId,const int32_t appIndex,const bool isExistInOtherUser)175 bool BundleResourceHelper::AddCloneBundleResourceInfo(const std::string &bundleName,
176     const int32_t userId, const int32_t appIndex, const bool isExistInOtherUser)
177 {
178 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
179     APP_LOGI("start add clone bundle:%{public}s appIndex:%{public}d userId:%{public}d",
180         bundleName.c_str(), appIndex, userId);
181     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
182     if (manager == nullptr) {
183         APP_LOGE("failed, manager is nullptr");
184         return false;
185     }
186     if (!manager->AddCloneBundleResourceInfoWhenInstall(bundleName, userId, appIndex, isExistInOtherUser)) {
187         APP_LOGE("add clone bundle resource failed, bundleName:%{public}s, appIndex:%{public}d",
188             bundleName.c_str(), appIndex);
189         return false;
190     }
191     APP_LOGI("end add clone bundle:%{public}s appIndex:%{public}d userId:%{public}d",
192         bundleName.c_str(), appIndex, userId);
193     return true;
194 #else
195     APP_LOGI("bundle resource is not support");
196     return true;
197 #endif
198 }
199 
DeleteCloneBundleResourceInfo(const std::string & bundleName,const int32_t userId,const int32_t appIndex,const bool isExistInOtherUser)200 bool BundleResourceHelper::DeleteCloneBundleResourceInfo(const std::string &bundleName,
201     const int32_t userId, const int32_t appIndex, const bool isExistInOtherUser)
202 {
203 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
204     APP_LOGI("start delete clone bundle:%{public}s appIndex:%{public}d userId:%{public}d",
205         bundleName.c_str(), appIndex, userId);
206     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
207     if (manager == nullptr) {
208         APP_LOGE("failed, manager is nullptr");
209         return false;
210     }
211 
212     if (!manager->DeleteCloneBundleResourceInfoWhenUninstall(bundleName, userId, appIndex, isExistInOtherUser)) {
213         APP_LOGE("failed, key:%{public}s appIndex:%{public}d", bundleName.c_str(), appIndex);
214         return false;
215     }
216 
217     return true;
218 #else
219     return false;
220 #endif
221 }
222 
DeleteNotExistResourceInfo()223 void BundleResourceHelper::DeleteNotExistResourceInfo()
224 {
225 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
226     APP_LOGI_NOFUNC("start delete not exist resource");
227     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
228     if (manager == nullptr) {
229         APP_LOGE("failed, manager is nullptr");
230         return;
231     }
232 
233     if (!manager->DeleteNotExistResourceInfo()) {
234         APP_LOGE("delete not exist resource failed");
235         return;
236     }
237 #endif
238 }
239 
ProcessThemeAndDynamicIconWhenOta(const std::set<std::string> updateBundleNames)240 bool BundleResourceHelper::ProcessThemeAndDynamicIconWhenOta(const std::set<std::string> updateBundleNames)
241 {
242 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
243     APP_LOGI("ProcessThemeAndDynamicIconWhenOta start");
244     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
245     if (manager == nullptr) {
246         APP_LOGE("failed, manager is nullptr");
247         return false;
248     }
249 
250     if (!manager->ProcessThemeAndDynamicIconWhenOta(updateBundleNames)) {
251         APP_LOGE("ProcessThemeAndDynamicIconWhenOta failed");
252         return false;
253     }
254     APP_LOGI("ProcessThemeAndDynamicIconWhenOta end");
255     return true;
256 #else
257     return false;
258 #endif
259 }
260 
GetBundleResourceInfo(const std::string & bundleName,const uint32_t flags,BundleResourceInfo & bundleResourceInfo,int32_t appIndex)261 bool BundleResourceHelper::GetBundleResourceInfo(const std::string &bundleName,
262     const uint32_t flags, BundleResourceInfo &bundleResourceInfo, int32_t appIndex)
263 {
264 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
265     APP_LOGI_NOFUNC("-n %{public}s -u get bundle resource info start", bundleName.c_str());
266     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
267     if (manager == nullptr) {
268         APP_LOGE("failed, manager is nullptr");
269         return false;
270     }
271     if (!manager->GetBundleResourceInfo(bundleName, flags, bundleResourceInfo, appIndex)) {
272         APP_LOGE("failed, bundleName:%{public}s", bundleName.c_str());
273         return false;
274     }
275     return true;
276 #else
277     return false;
278 #endif
279 }
280 
GetLauncherAbilityResourceInfo(const std::string & bundleName,const uint32_t flags,std::vector<LauncherAbilityResourceInfo> & launcherAbilityResourceInfo,int32_t appIndex)281 bool BundleResourceHelper::GetLauncherAbilityResourceInfo(const std::string &bundleName,
282     const uint32_t flags, std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfo, int32_t appIndex)
283 {
284 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
285     APP_LOGI_NOFUNC("-n %{public}s -u get launcher ability resource info start", bundleName.c_str());
286     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
287     if (manager == nullptr) {
288         APP_LOGE("failed, manager is nullptr");
289         return false;
290     }
291     if (!manager->GetLauncherAbilityResourceInfo(bundleName, flags, launcherAbilityResourceInfo, appIndex)) {
292         APP_LOGE("failed, bundleName:%{public}s", bundleName.c_str());
293         return false;
294     }
295     return true;
296 #else
297     return false;
298 #endif
299 }
300 
ProcessBundleResourceChange()301 void BundleResourceHelper::ProcessBundleResourceChange()
302 {
303 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
304     std::string path = std::string(BundleResourceConstants::BUNDLE_RESOURCE_RDB_PATH) +
305         std::string(BundleResourceConstants::USER_FILE_NAME);
306     nlohmann::json jsonBuf;
307     if (!BundleParser::ReadFileIntoJson(path, jsonBuf)) {
308         APP_LOGW("read user file failed, errno %{public}d", errno);
309         return;
310     }
311 #ifdef ABILITY_RUNTIME_ENABLE
312     BundleResourceObserver observer;
313     int32_t parseResult = ERR_OK;
314     std::string language;
315     std::string theme;
316     int32_t id;
317     int32_t themeIcon;
318     uint32_t type;
319     for (const auto &[key, value] : jsonBuf.items()) {
320         if (key.find(BundleResourceConstants::THEME) == 0) {
321             const auto &jsonBufEnd = value.end();
322             GetValueIfFindKey<int32_t>(value, jsonBufEnd, BundleResourceConstants::THEME_ID, id,
323                 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
324             GetValueIfFindKey<int32_t>(value, jsonBufEnd, BundleResourceConstants::THEME_ICON, themeIcon,
325                 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
326             GetValueIfFindKey<uint32_t>(value, jsonBufEnd, BundleResourceConstants::TYPE, type,
327                 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
328             BMSJsonUtil::GetStrValueIfFindKey(value, jsonBufEnd, BundleResourceConstants::THEME,
329                 theme, false, parseResult);
330             observer.ProcessResourceChangeByType("", theme, id, themeIcon, type);
331             APP_LOGI("-t %{public}d start when reboot", type);
332         }
333         if (key.find(BundleResourceConstants::LANGUAGE) == 0) {
334             const auto &jsonBufEnd = value.end();
335             GetValueIfFindKey<uint32_t>(value, jsonBufEnd, BundleResourceConstants::TYPE, type,
336                 JsonType::NUMBER, false, parseResult, ArrayType::NOT_ARRAY);
337             BMSJsonUtil::GetStrValueIfFindKey(value, jsonBufEnd, BundleResourceConstants::LANGUAGE,
338                 language, false, parseResult);
339             observer.ProcessResourceChangeByType(language, "", 0, 0, type);
340             APP_LOGI("-t %{public}d start when reboot", type);
341         }
342     }
343 #else
344     APP_LOGW("ability runtime is disable");
345 #endif
346 #else
347     return;
348 #endif
349 }
350 
SetIsOnlineThemeWhenBoot()351 void BundleResourceHelper::SetIsOnlineThemeWhenBoot()
352 {
353 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
354     auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
355     if (manager == nullptr) {
356         APP_LOGE("failed, manager is nullptr");
357         return;
358     }
359     manager->SetIsOnlineThemeWhenBoot();
360 #endif
361 }
362 } // AppExecFwk
363 } // OHOS
364