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_resource_callback.h"
21 #include "bundle_resource_manager.h"
22 #include "bundle_resource_param.h"
23 #include "bundle_resource_parser.h"
24 #include "bundle_resource_register.h"
25 #include "bundle_system_state.h"
26 #include "resource_manager.h"
27 #endif
28
29 namespace OHOS {
30 namespace AppExecFwk {
BundleSystemStateInit()31 void BundleResourceHelper::BundleSystemStateInit()
32 {
33 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
34 APP_LOGI("system state init start");
35 // init language and colorMode
36 BundleSystemState::GetInstance().SetSystemLanguage(BundleResourceParam::GetSystemLanguage());
37 BundleSystemState::GetInstance().SetSystemColorMode(BundleResourceParam::GetSystemColorMode());
38 APP_LOGI("current system state: %{public}s", BundleSystemState::GetInstance().ToString().c_str());
39 // init resource manager
40 if (Global::Resource::GetSystemResourceManagerNoSandBox() == nullptr) {
41 APP_LOGE("init no sand box resource manager failed");
42 }
43 #endif
44 }
45
RegisterConfigurationObserver()46 void BundleResourceHelper::RegisterConfigurationObserver()
47 {
48 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
49 BundleResourceRegister::RegisterConfigurationObserver();
50 #endif
51 }
52
RegisterCommonEventSubscriber()53 void BundleResourceHelper::RegisterCommonEventSubscriber()
54 {
55 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
56 BundleResourceRegister::RegisterCommonEventSubscriber();
57 #endif
58 }
59
AddResourceInfoByBundleName(const std::string & bundleName,const int32_t userId)60 void BundleResourceHelper::AddResourceInfoByBundleName(const std::string &bundleName,
61 const int32_t userId)
62 {
63 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
64 APP_LOGI_NOFUNC("-n %{public}s -u %{public}d add resource start", bundleName.c_str(), userId);
65 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
66 if (manager == nullptr) {
67 APP_LOGE("failed, manager is nullptr");
68 return;
69 }
70 // add new resource info
71 if (!manager->AddResourceInfoByBundleName(bundleName, userId)) {
72 APP_LOGW("failed, bundleName:%{public}s", bundleName.c_str());
73 }
74 APP_LOGI_NOFUNC("-n %{public}s -u %{public}d add resource end", bundleName.c_str(), userId);
75 #endif
76 }
77
DeleteResourceInfo(const std::string & key,const int32_t userId)78 bool BundleResourceHelper::DeleteResourceInfo(const std::string &key, const int32_t userId)
79 {
80 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
81 APP_LOGI("start delete key %{public}s userId:%{public}d", key.c_str(), userId);
82 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
83 if (manager == nullptr) {
84 APP_LOGE("failed, manager is nullptr");
85 return false;
86 }
87
88 if (!manager->DeleteResourceInfo(key)) {
89 APP_LOGE("failed, key:%{public}s", key.c_str());
90 return false;
91 }
92
93 return true;
94 #else
95 return false;
96 #endif
97 }
98
GetAllBundleResourceName(std::vector<std::string> & resourceNames)99 void BundleResourceHelper::GetAllBundleResourceName(std::vector<std::string> &resourceNames)
100 {
101 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
102 APP_LOGI("start");
103 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
104 if (manager == nullptr) {
105 APP_LOGE("failed, manager is nullptr");
106 return;
107 }
108 if (!manager->GetAllResourceName(resourceNames)) {
109 APP_LOGE("failed");
110 }
111 #endif
112 }
113
ParseBundleName(const std::string & keyName)114 std::string BundleResourceHelper::ParseBundleName(const std::string &keyName)
115 {
116 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
117 ResourceInfo info;
118 info.ParseKey(keyName);
119 return info.bundleName_;
120 #else
121 return keyName;
122 #endif
123 }
124
SetOverlayEnabled(const std::string & bundleName,const std::string & moduleName,bool isEnabled,int32_t userId)125 void BundleResourceHelper::SetOverlayEnabled(const std::string &bundleName, const std::string &moduleName,
126 bool isEnabled, int32_t userId)
127 {
128 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
129 APP_LOGD("bundleName: %{public}s, moduleName: %{public}s,isEnabled: %{public}d, userId: %{public}d",
130 bundleName.c_str(), moduleName.c_str(), isEnabled, userId);
131 BundleResourceCallback callback;
132 callback.OnOverlayStatusChanged(bundleName, isEnabled, userId);
133 #endif
134 }
135
DeleteAllResourceInfo()136 bool BundleResourceHelper::DeleteAllResourceInfo()
137 {
138 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
139 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
140 if (manager == nullptr) {
141 APP_LOGE("failed, manager is nullptr");
142 return false;
143 }
144 if (!manager->DeleteAllResourceInfo()) {
145 APP_LOGE("delete all bundle resource failed");
146 return false;
147 }
148 return true;
149 #else
150 return false;
151 #endif
152 }
153
AddCloneBundleResourceInfo(const std::string & bundleName,const int32_t appIndex,const int32_t userId)154 bool BundleResourceHelper::AddCloneBundleResourceInfo(const std::string &bundleName,
155 const int32_t appIndex, const int32_t userId)
156 {
157 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
158 APP_LOGI("start add clone bundle:%{public}s appIndex:%{public}d userId:%{public}d",
159 bundleName.c_str(), appIndex, userId);
160 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
161 if (manager == nullptr) {
162 APP_LOGE("failed, manager is nullptr");
163 return false;
164 }
165 if (!manager->AddCloneBundleResourceInfo(bundleName, appIndex)) {
166 APP_LOGE("add clone bundle resource failed, bundleName:%{public}s, appIndex:%{public}d",
167 bundleName.c_str(), appIndex);
168 return false;
169 }
170 APP_LOGI("end add clone bundle:%{public}s appIndex:%{public}d userId:%{public}d",
171 bundleName.c_str(), appIndex, userId);
172 return true;
173 #else
174 APP_LOGI("bundle resource is not support");
175 return true;
176 #endif
177 }
178
DeleteCloneBundleResourceInfo(const std::string & bundleName,const int32_t appIndex,const int32_t userId)179 bool BundleResourceHelper::DeleteCloneBundleResourceInfo(const std::string &bundleName,
180 const int32_t appIndex, const int32_t userId)
181 {
182 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
183 APP_LOGI("start delete clone bundle:%{public}s appIndex:%{public}d userId:%{public}d",
184 bundleName.c_str(), appIndex, userId);
185 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
186 if (manager == nullptr) {
187 APP_LOGE("failed, manager is nullptr");
188 return false;
189 }
190
191 if (!manager->DeleteCloneBundleResourceInfo(bundleName, appIndex)) {
192 APP_LOGE("failed, key:%{public}s appIndex:%{public}d", bundleName.c_str(), appIndex);
193 return false;
194 }
195
196 return true;
197 #else
198 return false;
199 #endif
200 }
201
DeleteNotExistResourceInfo()202 void BundleResourceHelper::DeleteNotExistResourceInfo()
203 {
204 #ifdef BUNDLE_FRAMEWORK_BUNDLE_RESOURCE
205 APP_LOGI("start delete not exist resource");
206 auto manager = DelayedSingleton<BundleResourceManager>::GetInstance();
207 if (manager == nullptr) {
208 APP_LOGE("failed, manager is nullptr");
209 return;
210 }
211
212 if (!manager->DeleteNotExistResourceInfo()) {
213 APP_LOGE("delete not exist resource failed");
214 return;
215 }
216 #endif
217 }
218 } // AppExecFwk
219 } // OHOS
220