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 "app_log_wrapper.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_constants.h"
21 #include "bundle_util.h"
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "want.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
BundleCommonEventMgr()28 BundleCommonEventMgr::BundleCommonEventMgr()
29 {
30 APP_LOGI("enter BundleCommonEventMgr");
31 Init();
32 }
33
Init()34 void BundleCommonEventMgr::Init()
35 {
36 commonEventMap_ = {
37 { NotifyType::INSTALL, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED },
38 { NotifyType::UNINSTALL_BUNDLE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED },
39 { NotifyType::UNINSTALL_MODULE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED },
40 { NotifyType::UPDATE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED },
41 { NotifyType::ABILITY_ENABLE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED },
42 { NotifyType::APPLICATION_ENABLE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED },
43 { NotifyType::BUNDLE_DATA_CLEARED, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED },
44 { NotifyType::BUNDLE_CACHE_CLEARED, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CACHE_CLEARED },
45 };
46 }
47
NotifyBundleStatus(const NotifyBundleEvents & installResult,const std::shared_ptr<BundleDataMgr> & dataMgr)48 void BundleCommonEventMgr::NotifyBundleStatus(const NotifyBundleEvents &installResult,
49 const std::shared_ptr<BundleDataMgr> &dataMgr)
50 {
51 APP_LOGD("notify type %{public}d with %{public}d for %{public}s-%{public}s in %{public}s",
52 static_cast<int32_t>(installResult.type), installResult.resultCode, installResult.modulePackage.c_str(),
53 installResult.abilityName.c_str(), installResult.bundleName.c_str());
54
55 std::string eventData = GetCommonEventData(installResult.type);
56 APP_LOGD("will send event data %{public}s", eventData.c_str());
57 OHOS::AAFwk::Want want;
58 want.SetAction(eventData);
59 ElementName element;
60 element.SetBundleName(installResult.bundleName);
61 element.SetAbilityName(installResult.abilityName);
62 want.SetElement(element);
63 want.SetParam(Constants::UID, installResult.uid);
64 want.SetParam(Constants::USER_ID, BundleUtil::GetUserIdByUid(installResult.uid));
65 want.SetParam(Constants::ABILITY_NAME, installResult.abilityName);
66 want.SetParam(Constants::ACCESS_TOKEN_ID, static_cast<int32_t>(installResult.accessTokenId));
67 want.SetParam(Constants::IS_AGING_UNINSTALL, installResult.isAgingUninstall);
68 EventFwk::CommonEventData commonData { want };
69 // trigger BundleEventCallback first
70 if (dataMgr != nullptr) {
71 dataMgr->NotifyBundleEventCallback(commonData);
72 }
73
74 uint8_t installType = ((installResult.type == NotifyType::UNINSTALL_BUNDLE) ||
75 (installResult.type == NotifyType::UNINSTALL_MODULE)) ?
76 static_cast<uint8_t>(InstallType::UNINSTALL_CALLBACK) :
77 static_cast<uint8_t>(InstallType::INSTALL_CALLBACK);
78
79 // trigger the status callback for status listenning
80 if (dataMgr != nullptr) {
81 auto &callbackMutex = dataMgr->GetStatusCallbackMutex();
82 std::shared_lock<std::shared_mutex> lock(callbackMutex);
83 auto callbackList = dataMgr->GetCallBackList();
84 for (const auto& callback : callbackList) {
85 if (callback->GetBundleName() == installResult.bundleName) {
86 // if the msg needed, it could convert in the proxy node
87 callback->OnBundleStateChanged(installType, installResult.resultCode, Constants::EMPTY_STRING,
88 installResult.bundleName);
89 }
90 }
91 }
92
93 if (installResult.resultCode != ERR_OK) {
94 return;
95 }
96 EventFwk::CommonEventManager::PublishCommonEvent(commonData);
97 }
98
NotifySandboxAppStatus(const InnerBundleInfo & info,int32_t uid,int32_t userId,const SandboxInstallType & type)99 ErrCode BundleCommonEventMgr::NotifySandboxAppStatus(const InnerBundleInfo &info, int32_t uid, int32_t userId,
100 const SandboxInstallType &type)
101 {
102 OHOS::AAFwk::Want want;
103 if (type == SandboxInstallType::INSTALL) {
104 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_ADDED);
105 } else if (type == SandboxInstallType::UNINSTALL) {
106 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
107 } else {
108 return ERR_APPEXECFWK_SANDBOX_INSTALL_UNKNOWN_INSTALL_TYPE;
109 }
110 ElementName element;
111 element.SetBundleName(info.GetBundleName());
112 element.SetAbilityName(info.GetMainAbility());
113 want.SetElement(element);
114 want.SetParam(Constants::UID, uid);
115 want.SetParam(Constants::USER_ID, userId);
116 want.SetParam(Constants::ABILITY_NAME, info.GetMainAbility());
117 want.SetParam(Constants::SANDBOX_APP_INDEX, info.GetAppIndex());
118 want.SetParam(Constants::ACCESS_TOKEN_ID, static_cast<int32_t>(info.GetAccessTokenId(userId)));
119 EventFwk::CommonEventData commonData { want };
120 EventFwk::CommonEventManager::PublishCommonEvent(commonData);
121 return ERR_OK;
122 }
123
GetCommonEventData(const NotifyType & type)124 std::string BundleCommonEventMgr::GetCommonEventData(const NotifyType &type)
125 {
126 auto iter = commonEventMap_.find(type);
127 if (iter == commonEventMap_.end()) {
128 APP_LOGW("event type error");
129 return EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED;
130 }
131 return iter->second;
132 }
133 } // AppExecFwk
134 } // OHOS