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