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 "bundle_common_event_mgr.h"
17
18 #include "account_helper.h"
19 #include "app_log_tag_wrapper.h"
20 #include "bundle_common_event.h"
21 #include "bundle_util.h"
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "ipc_skeleton.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 constexpr const char* ACCESS_TOKEN_ID = "accessTokenId";
30 constexpr const char* IS_AGING_UNINSTALL = "isAgingUninstall";
31 constexpr const char* APP_ID = "appId";
32 constexpr const char* IS_MODULE_UPDATE = "isModuleUpdate";
33 constexpr const char* IS_ENABLE_DYNAMIC_ICON = "isEnableDynamicIcon";
34 constexpr const char* BUNDLE_RESOURCES_CHANGED = "usual.event.BUNDLE_RESOURCES_CHANGED";
35 constexpr const char* APP_IDENTIFIER = "appIdentifier";
36 constexpr const char* APP_DISTRIBUTION_TYPE = "appDistributionType";
37 constexpr const char* BUNDLE_TYPE = "bundleType";
38 constexpr const char* ATOMIC_SERVICE_MODULE_UPGRADE = "atomicServiceModuleUpgrade";
39 constexpr const char* UID = "uid";
40 constexpr const char* SANDBOX_APP_INDEX = "sandbox_app_index";
41 constexpr const char* BUNDLE_RESOURCE_CHANGE_TYPE = "bundleResourceChangeType";
42 constexpr const char* APP_INDEX = "appIndex";
43 constexpr const char* TYPE = "type";
44 constexpr const char* RESULT_CODE = "resultCode";
45 constexpr const char* PERMISSION_GET_DISPOSED_STATUS = "ohos.permission.GET_DISPOSED_APP_STATUS";
46 constexpr const char* ASSET_ACCESS_GROUPS = "assetAccessGroups";
47 constexpr const char* DEVELOPERID = "developerId";
48 }
49
BundleCommonEventMgr()50 BundleCommonEventMgr::BundleCommonEventMgr()
51 {
52 APP_LOGI_NOFUNC("enter BundleCommonEventMgr");
53 Init();
54 }
55
Init()56 void BundleCommonEventMgr::Init()
57 {
58 commonEventMap_ = {
59 { NotifyType::INSTALL, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED },
60 { NotifyType::UNINSTALL_BUNDLE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED },
61 { NotifyType::UNINSTALL_MODULE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED },
62 { NotifyType::UPDATE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED },
63 { NotifyType::ABILITY_ENABLE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED },
64 { NotifyType::UNINSTALL_STATE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED },
65 { NotifyType::APPLICATION_ENABLE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED },
66 { NotifyType::BUNDLE_DATA_CLEARED, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED },
67 { NotifyType::BUNDLE_CACHE_CLEARED, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CACHE_CLEARED },
68 { NotifyType::OVERLAY_INSTALL, OVERLAY_ADD_ACTION},
69 { NotifyType::OVERLAY_UPDATE, OVERLAY_CHANGED_ACTION},
70 { NotifyType::DISPOSED_RULE_ADDED, DISPOSED_RULE_ADDED},
71 { NotifyType::DISPOSED_RULE_DELETED, DISPOSED_RULE_DELETED},
72 { NotifyType::START_INSTALL, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_INSTALLATION_STARTED },
73 };
74 }
75
NotifyBundleStatus(const NotifyBundleEvents & installResult,const std::shared_ptr<BundleDataMgr> & dataMgr)76 void BundleCommonEventMgr::NotifyBundleStatus(const NotifyBundleEvents &installResult,
77 const std::shared_ptr<BundleDataMgr> &dataMgr)
78 {
79 APP_LOGD("notify type %{public}d with %{public}d for %{public}s-%{public}s in %{public}s",
80 static_cast<int32_t>(installResult.type), installResult.resultCode, installResult.modulePackage.c_str(),
81 installResult.abilityName.c_str(), installResult.bundleName.c_str());
82 OHOS::AAFwk::Want want;
83 SetNotifyWant(want, installResult);
84 EventFwk::CommonEventData commonData { want };
85 // trigger BundleEventCallback first
86 if (dataMgr != nullptr && !(want.GetAction() == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED &&
87 installResult.resultCode != ERR_OK)) {
88 LOG_I(BMS_TAG_DEFAULT, "eventBack begin");
89 dataMgr->NotifyBundleEventCallback(commonData);
90 LOG_I(BMS_TAG_DEFAULT, "eventBack end");
91 }
92
93 uint8_t installType = ((installResult.type == NotifyType::UNINSTALL_BUNDLE) ||
94 (installResult.type == NotifyType::UNINSTALL_MODULE)) ?
95 static_cast<uint8_t>(InstallType::UNINSTALL_CALLBACK) :
96 static_cast<uint8_t>(InstallType::INSTALL_CALLBACK);
97 int32_t bundleUserId = BundleUtil::GetUserIdByUid(installResult.uid);
98 int32_t publishUserId = (bundleUserId == Constants::DEFAULT_USERID) ?
99 AccountHelper::GetCurrentActiveUserId() : bundleUserId;
100
101 // trigger the status callback for status listening
102 if ((dataMgr != nullptr) && (installResult.type != NotifyType::START_INSTALL)) {
103 auto &callbackMutex = dataMgr->GetStatusCallbackMutex();
104 std::shared_lock<std::shared_mutex> lock(callbackMutex);
105 auto callbackList = dataMgr->GetCallBackList();
106 for (const auto& callback : callbackList) {
107 int32_t callbackUserId = callback->GetUserId();
108 if (callbackUserId != Constants::UNSPECIFIED_USERID && callbackUserId != publishUserId) {
109 LOG_W(BMS_TAG_DEFAULT, "not callback userId %{public}d incorrect", callbackUserId);
110 continue;
111 }
112 if (callback->GetBundleName() == installResult.bundleName) {
113 // if the msg needed, it could convert in the proxy node
114 callback->OnBundleStateChanged(installType, installResult.resultCode, Constants::EMPTY_STRING,
115 installResult.bundleName);
116 }
117 }
118 }
119
120 if (installResult.resultCode != ERR_OK || installResult.isBmsExtensionUninstalled) {
121 APP_LOGI("install ret: %{public}d, extension: %{public}d",
122 installResult.resultCode, installResult.isBmsExtensionUninstalled);
123 return;
124 }
125
126 std::string identity = IPCSkeleton::ResetCallingIdentity();
127 if (!EventFwk::CommonEventManager::PublishCommonEventAsUser(commonData, publishUserId)) {
128 APP_LOGE("PublishCommonEventAsUser failed");
129 }
130 IPCSkeleton::SetCallingIdentity(identity);
131 }
132
SetNotifyWant(OHOS::AAFwk::Want & want,const NotifyBundleEvents & installResult)133 void BundleCommonEventMgr::SetNotifyWant(OHOS::AAFwk::Want& want, const NotifyBundleEvents &installResult)
134 {
135 std::string eventData = GetCommonEventData(installResult.type);
136 APP_LOGD("will send event data %{public}s", eventData.c_str());
137 want.SetAction(eventData);
138 ElementName element;
139 element.SetBundleName(installResult.bundleName);
140 element.SetModuleName(installResult.modulePackage);
141 element.SetAbilityName(installResult.abilityName);
142 want.SetElement(element);
143 want.SetParam(Constants::BUNDLE_NAME, installResult.bundleName);
144 want.SetParam(Constants::UID, installResult.uid);
145 want.SetParam(Constants::USER_ID, BundleUtil::GetUserIdByUid(installResult.uid));
146 want.SetParam(Constants::ABILITY_NAME, installResult.abilityName);
147 want.SetParam(ACCESS_TOKEN_ID, static_cast<int32_t>(installResult.accessTokenId));
148 want.SetParam(IS_AGING_UNINSTALL, installResult.isAgingUninstall);
149 want.SetParam(APP_ID, installResult.appId);
150 want.SetParam(IS_MODULE_UPDATE, installResult.isModuleUpdate);
151 want.SetParam(APP_IDENTIFIER, installResult.appIdentifier);
152 want.SetParam(APP_DISTRIBUTION_TYPE, installResult.appDistributionType);
153 want.SetParam(BUNDLE_TYPE, installResult.bundleType);
154 want.SetParam(ATOMIC_SERVICE_MODULE_UPGRADE, installResult.atomicServiceModuleUpgrade);
155 want.SetParam(APP_INDEX, installResult.appIndex);
156 want.SetParam(TYPE, static_cast<int32_t>(installResult.type));
157 want.SetParam(RESULT_CODE, installResult.resultCode);
158 if (want.GetAction() == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED
159 && !installResult.assetAccessGroups.empty()) {
160 want.SetParam(ASSET_ACCESS_GROUPS, installResult.assetAccessGroups);
161 want.SetParam(DEVELOPERID, installResult.developerId);
162 }
163 }
164
NotifySandboxAppStatus(const InnerBundleInfo & info,int32_t uid,int32_t userId,const SandboxInstallType & type)165 ErrCode BundleCommonEventMgr::NotifySandboxAppStatus(const InnerBundleInfo &info, int32_t uid, int32_t userId,
166 const SandboxInstallType &type)
167 {
168 OHOS::AAFwk::Want want;
169 if (type == SandboxInstallType::INSTALL) {
170 want.SetAction(COMMON_EVENT_SANDBOX_PACKAGE_ADDED);
171 } else if (type == SandboxInstallType::UNINSTALL) {
172 want.SetAction(COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
173 } else {
174 return ERR_APPEXECFWK_SANDBOX_INSTALL_UNKNOWN_INSTALL_TYPE;
175 }
176 ElementName element;
177 element.SetBundleName(info.GetBundleName());
178 element.SetAbilityName(info.GetMainAbility());
179 want.SetElement(element);
180 want.SetParam(UID, uid);
181 want.SetParam(Constants::USER_ID, userId);
182 want.SetParam(Constants::ABILITY_NAME, info.GetMainAbility());
183 want.SetParam(SANDBOX_APP_INDEX, info.GetAppIndex());
184 want.SetParam(ACCESS_TOKEN_ID, static_cast<int32_t>(info.GetAccessTokenId(userId)));
185 want.SetParam(APP_ID, info.GetAppId());
186 EventFwk::CommonEventData commonData { want };
187 EventFwk::CommonEventPublishInfo publishInfo;
188 std::vector<std::string> permissionVec { Constants::LISTEN_BUNDLE_CHANGE };
189 publishInfo.SetSubscriberPermissions(permissionVec);
190 std::string identity = IPCSkeleton::ResetCallingIdentity();
191 if (!EventFwk::CommonEventManager::PublishCommonEvent(commonData, publishInfo)) {
192 APP_LOGE("PublishCommonEvent failed");
193 }
194 IPCSkeleton::SetCallingIdentity(identity);
195 return ERR_OK;
196 }
197
NotifyOverlayModuleStateStatus(const std::string & bundleName,const std::string & moduleName,bool isEnabled,int32_t userId,int32_t uid)198 void BundleCommonEventMgr::NotifyOverlayModuleStateStatus(const std::string &bundleName,
199 const std::string &moduleName, bool isEnabled, int32_t userId, int32_t uid)
200 {
201 OHOS::AAFwk::Want want;
202 want.SetAction(OVERLAY_STATE_CHANGED);
203 ElementName element;
204 element.SetBundleName(bundleName);
205 element.SetModuleName(moduleName);
206 want.SetElement(element);
207 want.SetParam(UID, uid);
208 want.SetParam(Constants::USER_ID, userId);
209 want.SetParam(Constants::OVERLAY_STATE, isEnabled);
210 EventFwk::CommonEventData commonData { want };
211 EventFwk::CommonEventPublishInfo publishInfo;
212 std::vector<std::string> permissionVec { Constants::LISTEN_BUNDLE_CHANGE };
213 publishInfo.SetSubscriberPermissions(permissionVec);
214 std::string identity = IPCSkeleton::ResetCallingIdentity();
215 if (!EventFwk::CommonEventManager::PublishCommonEvent(commonData, publishInfo)) {
216 APP_LOGE("PublishCommonEvent failed");
217 }
218 IPCSkeleton::SetCallingIdentity(identity);
219 }
220
GetCommonEventData(const NotifyType & type)221 std::string BundleCommonEventMgr::GetCommonEventData(const NotifyType &type)
222 {
223 auto iter = commonEventMap_.find(type);
224 if (iter == commonEventMap_.end()) {
225 APP_LOGW("event type error");
226 return EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED;
227 }
228 return iter->second;
229 }
230
NotifySetDiposedRule(const std::string & appId,int32_t userId,const std::string & data,int32_t appIndex)231 void BundleCommonEventMgr::NotifySetDiposedRule(
232 const std::string &appId, int32_t userId, const std::string &data, int32_t appIndex)
233 {
234 OHOS::AAFwk::Want want;
235 want.SetAction(DISPOSED_RULE_ADDED);
236 want.SetParam(Constants::USER_ID, userId);
237 want.SetParam(APP_ID, appId);
238 want.SetParam(APP_INDEX, appIndex);
239 EventFwk::CommonEventData commonData { want };
240 commonData.SetData(data);
241 EventFwk::CommonEventPublishInfo publishInfo;
242 std::vector<std::string> permissionVec { PERMISSION_GET_DISPOSED_STATUS };
243 publishInfo.SetSubscriberPermissions(permissionVec);
244 std::string identity = IPCSkeleton::ResetCallingIdentity();
245 if (!EventFwk::CommonEventManager::PublishCommonEvent(commonData, publishInfo)) {
246 APP_LOGE("PublishCommonEvent failed");
247 }
248 IPCSkeleton::SetCallingIdentity(identity);
249 }
250
NotifyDeleteDiposedRule(const std::string & appId,int32_t userId,int32_t appIndex)251 void BundleCommonEventMgr::NotifyDeleteDiposedRule(const std::string &appId, int32_t userId, int32_t appIndex)
252 {
253 OHOS::AAFwk::Want want;
254 want.SetAction(DISPOSED_RULE_DELETED);
255 want.SetParam(Constants::USER_ID, userId);
256 want.SetParam(APP_ID, appId);
257 want.SetParam(APP_INDEX, appIndex);
258 EventFwk::CommonEventData commonData { want };
259 EventFwk::CommonEventPublishInfo publishInfo;
260 std::vector<std::string> permissionVec { PERMISSION_GET_DISPOSED_STATUS };
261 publishInfo.SetSubscriberPermissions(permissionVec);
262 std::string identity = IPCSkeleton::ResetCallingIdentity();
263 if (!EventFwk::CommonEventManager::PublishCommonEvent(commonData, publishInfo)) {
264 APP_LOGE("PublishCommonEvent failed");
265 }
266 IPCSkeleton::SetCallingIdentity(identity);
267 }
268
NotifyDynamicIconEvent(const std::string & bundleName,bool isEnableDynamicIcon)269 void BundleCommonEventMgr::NotifyDynamicIconEvent(
270 const std::string &bundleName, bool isEnableDynamicIcon)
271 {
272 APP_LOGI("NotifyDynamicIconEvent bundleName: %{public}s, %{public}d",
273 bundleName.c_str(), isEnableDynamicIcon);
274 OHOS::AAFwk::Want want;
275 want.SetAction(DYNAMIC_ICON_CHANGED);
276 ElementName element;
277 element.SetBundleName(bundleName);
278 want.SetElement(element);
279 want.SetParam(IS_ENABLE_DYNAMIC_ICON, isEnableDynamicIcon);
280 EventFwk::CommonEventData commonData { want };
281 EventFwk::CommonEventPublishInfo publishInfo;
282 std::string identity = IPCSkeleton::ResetCallingIdentity();
283 if (!EventFwk::CommonEventManager::PublishCommonEvent(commonData, publishInfo)) {
284 APP_LOGE("PublishCommonEvent failed");
285 }
286 IPCSkeleton::SetCallingIdentity(identity);
287 }
288
NotifyBundleResourcesChanged(const int32_t userId,const uint32_t type)289 void BundleCommonEventMgr::NotifyBundleResourcesChanged(const int32_t userId, const uint32_t type)
290 {
291 OHOS::AAFwk::Want want;
292 want.SetAction(BUNDLE_RESOURCES_CHANGED);
293 want.SetParam(Constants::USER_ID, userId);
294 want.SetParam(BUNDLE_RESOURCE_CHANGE_TYPE, static_cast<int32_t>(type));
295 EventFwk::CommonEventData commonData { want };
296 EventFwk::CommonEventPublishInfo publishInfo;
297 std::vector<std::string> permissionVec { ServiceConstants::PERMISSION_GET_BUNDLE_RESOURCES };
298 publishInfo.SetSubscriberPermissions(permissionVec);
299 std::string identity = IPCSkeleton::ResetCallingIdentity();
300 if (!EventFwk::CommonEventManager::PublishCommonEvent(commonData, publishInfo)) {
301 APP_LOGE("PublishCommonEvent failed");
302 }
303 IPCSkeleton::SetCallingIdentity(identity);
304 }
305 } // AppExecFwk
306 } // OHOS