• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_wrapper.h"
20 #include "appexecfwk_errors.h"
21 #include "bundle_common_event.h"
22 #include "bundle_constants.h"
23 #include "bundle_util.h"
24 #include "common_event_manager.h"
25 #include "common_event_support.h"
26 #include "want.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 constexpr const char* ACCESS_TOKEN_ID = "accessTokenId";
32 constexpr const char* IS_AGING_UNINSTALL = "isAgingUninstall";
33 constexpr const char* APP_ID = "appId";
34 constexpr const char* IS_MODULE_UPDATE = "isModuleUpdate";
35 const std::string BUNDLE_RESOURCES_CHANGED = "usual.event.BUNDLE_RESOURCES_CHANGED";
36 }
37 
BundleCommonEventMgr()38 BundleCommonEventMgr::BundleCommonEventMgr()
39 {
40     APP_LOGI("enter BundleCommonEventMgr");
41     Init();
42 }
43 
Init()44 void BundleCommonEventMgr::Init()
45 {
46     commonEventMap_ = {
47         { NotifyType::INSTALL, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED },
48         { NotifyType::UNINSTALL_BUNDLE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED },
49         { NotifyType::UNINSTALL_MODULE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED },
50         { NotifyType::UPDATE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED },
51         { NotifyType::ABILITY_ENABLE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED },
52         { NotifyType::APPLICATION_ENABLE, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED },
53         { NotifyType::BUNDLE_DATA_CLEARED, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED },
54         { NotifyType::BUNDLE_CACHE_CLEARED, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CACHE_CLEARED },
55         { NotifyType::OVERLAY_INSTALL, OVERLAY_ADD_ACTION},
56         { NotifyType::OVERLAY_UPDATE, OVERLAY_CHANGED_ACTION},
57         { NotifyType::DISPOSED_RULE_ADDED, DISPOSED_RULE_ADDED},
58         { NotifyType::DISPOSED_RULE_DELETED, DISPOSED_RULE_DELETED},
59     };
60 }
61 
NotifyBundleStatus(const NotifyBundleEvents & installResult,const std::shared_ptr<BundleDataMgr> & dataMgr)62 void BundleCommonEventMgr::NotifyBundleStatus(const NotifyBundleEvents &installResult,
63     const std::shared_ptr<BundleDataMgr> &dataMgr)
64 {
65     APP_LOGD("notify type %{public}d with %{public}d for %{public}s-%{public}s in %{public}s",
66         static_cast<int32_t>(installResult.type), installResult.resultCode, installResult.modulePackage.c_str(),
67         installResult.abilityName.c_str(), installResult.bundleName.c_str());
68 
69     std::string eventData = GetCommonEventData(installResult.type);
70     APP_LOGD("will send event data %{public}s", eventData.c_str());
71     OHOS::AAFwk::Want want;
72     want.SetAction(eventData);
73     ElementName element;
74     element.SetBundleName(installResult.bundleName);
75     element.SetModuleName(installResult.modulePackage);
76     element.SetAbilityName(installResult.abilityName);
77     want.SetElement(element);
78     want.SetParam(Constants::UID, installResult.uid);
79     int32_t bundleUserId = BundleUtil::GetUserIdByUid(installResult.uid);
80     want.SetParam(Constants::USER_ID, bundleUserId);
81     want.SetParam(Constants::ABILITY_NAME, installResult.abilityName);
82     want.SetParam(ACCESS_TOKEN_ID, static_cast<int32_t>(installResult.accessTokenId));
83     want.SetParam(IS_AGING_UNINSTALL, installResult.isAgingUninstall);
84     want.SetParam(APP_ID, installResult.appId);
85     want.SetParam(IS_MODULE_UPDATE, installResult.isModuleUpdate);
86     EventFwk::CommonEventData commonData { want };
87     // trigger BundleEventCallback first
88     if (dataMgr != nullptr) {
89         dataMgr->NotifyBundleEventCallback(commonData);
90     }
91 
92     uint8_t installType = ((installResult.type == NotifyType::UNINSTALL_BUNDLE) ||
93             (installResult.type == NotifyType::UNINSTALL_MODULE)) ?
94             static_cast<uint8_t>(InstallType::UNINSTALL_CALLBACK) :
95             static_cast<uint8_t>(InstallType::INSTALL_CALLBACK);
96 
97     // trigger the status callback for status listening
98     if (dataMgr != nullptr) {
99         auto &callbackMutex = dataMgr->GetStatusCallbackMutex();
100         std::shared_lock<std::shared_mutex> lock(callbackMutex);
101         auto callbackList = dataMgr->GetCallBackList();
102         for (const auto& callback : callbackList) {
103             if (callback->GetBundleName() == installResult.bundleName) {
104                 // if the msg needed, it could convert in the proxy node
105                 callback->OnBundleStateChanged(installType, installResult.resultCode, Constants::EMPTY_STRING,
106                     installResult.bundleName);
107             }
108         }
109     }
110 
111     if (installResult.resultCode != ERR_OK || installResult.isBmsExtensionUninstalled) {
112         return;
113     }
114 
115     int32_t publishUserId = (bundleUserId == Constants::DEFAULT_USERID) ?
116         AccountHelper::GetCurrentActiveUserId() : bundleUserId;
117     EventFwk::CommonEventManager::PublishCommonEventAsUser(commonData, publishUserId);
118 }
119 
NotifySandboxAppStatus(const InnerBundleInfo & info,int32_t uid,int32_t userId,const SandboxInstallType & type)120 ErrCode BundleCommonEventMgr::NotifySandboxAppStatus(const InnerBundleInfo &info, int32_t uid, int32_t userId,
121     const SandboxInstallType &type)
122 {
123     OHOS::AAFwk::Want want;
124     if (type == SandboxInstallType::INSTALL) {
125         want.SetAction(COMMON_EVENT_SANDBOX_PACKAGE_ADDED);
126     } else if (type == SandboxInstallType::UNINSTALL) {
127         want.SetAction(COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
128     } else {
129         return ERR_APPEXECFWK_SANDBOX_INSTALL_UNKNOWN_INSTALL_TYPE;
130     }
131     ElementName element;
132     element.SetBundleName(info.GetBundleName());
133     element.SetAbilityName(info.GetMainAbility());
134     want.SetElement(element);
135     want.SetParam(Constants::UID, uid);
136     want.SetParam(Constants::USER_ID, userId);
137     want.SetParam(Constants::ABILITY_NAME, info.GetMainAbility());
138     want.SetParam(Constants::SANDBOX_APP_INDEX, info.GetAppIndex());
139     want.SetParam(ACCESS_TOKEN_ID, static_cast<int32_t>(info.GetAccessTokenId(userId)));
140     want.SetParam(APP_ID, info.GetAppId());
141     EventFwk::CommonEventData commonData { want };
142     EventFwk::CommonEventPublishInfo publishInfo;
143     std::vector<std::string> permissionVec { Constants::LISTEN_BUNDLE_CHANGE };
144     publishInfo.SetSubscriberPermissions(permissionVec);
145     EventFwk::CommonEventManager::PublishCommonEvent(commonData, publishInfo);
146     return ERR_OK;
147 }
148 
NotifyOverlayModuleStateStatus(const std::string & bundleName,const std::string & moduleName,bool isEnabled,int32_t userId,int32_t uid)149 void BundleCommonEventMgr::NotifyOverlayModuleStateStatus(const std::string &bundleName,
150     const std::string &moduleName, bool isEnabled, int32_t userId, int32_t uid)
151 {
152     OHOS::AAFwk::Want want;
153     want.SetAction(OVERLAY_STATE_CHANGED);
154     ElementName element;
155     element.SetBundleName(bundleName);
156     element.SetModuleName(moduleName);
157     want.SetElement(element);
158     want.SetParam(Constants::UID, uid);
159     want.SetParam(Constants::USER_ID, userId);
160     want.SetParam(Constants::OVERLAY_STATE, isEnabled);
161     EventFwk::CommonEventData commonData { want };
162     EventFwk::CommonEventPublishInfo publishInfo;
163     std::vector<std::string> permissionVec { Constants::LISTEN_BUNDLE_CHANGE };
164     publishInfo.SetSubscriberPermissions(permissionVec);
165     EventFwk::CommonEventManager::PublishCommonEvent(commonData, publishInfo);
166 }
167 
GetCommonEventData(const NotifyType & type)168 std::string BundleCommonEventMgr::GetCommonEventData(const NotifyType &type)
169 {
170     auto iter = commonEventMap_.find(type);
171     if (iter == commonEventMap_.end()) {
172         APP_LOGW("event type error");
173         return EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED;
174     }
175     return iter->second;
176 }
177 
NotifySetDiposedRule(const std::string & appId,int32_t userId,const std::string & data)178 void BundleCommonEventMgr::NotifySetDiposedRule(
179     const std::string &appId, int32_t userId, const std::string &data)
180 {
181     OHOS::AAFwk::Want want;
182     want.SetAction(DISPOSED_RULE_ADDED);
183 
184     want.SetParam(Constants::USER_ID, userId);
185     want.SetParam(Constants::APP_ID, appId);
186     EventFwk::CommonEventData commonData { want };
187     commonData.SetData(data);
188     EventFwk::CommonEventPublishInfo publishInfo;
189     std::vector<std::string> permissionVec { Constants::PERMISSION_MANAGE_DISPOSED_APP_STATUS };
190     publishInfo.SetSubscriberPermissions(permissionVec);
191     EventFwk::CommonEventManager::PublishCommonEvent(commonData, publishInfo);
192 }
193 
NotifyDeleteDiposedRule(const std::string & appId,int32_t userId)194 void BundleCommonEventMgr::NotifyDeleteDiposedRule(const std::string &appId, int32_t userId)
195 {
196     OHOS::AAFwk::Want want;
197     want.SetAction(DISPOSED_RULE_DELETED);
198     want.SetParam(Constants::USER_ID, userId);
199     want.SetParam(Constants::APP_ID, appId);
200     EventFwk::CommonEventData commonData { want };
201     EventFwk::CommonEventPublishInfo publishInfo;
202     std::vector<std::string> permissionVec { Constants::PERMISSION_MANAGE_DISPOSED_APP_STATUS };
203     publishInfo.SetSubscriberPermissions(permissionVec);
204     EventFwk::CommonEventManager::PublishCommonEvent(commonData, publishInfo);
205 }
206 
NotifyBundleResourcesChanged(int32_t userId)207 void BundleCommonEventMgr::NotifyBundleResourcesChanged(int32_t userId)
208 {
209     OHOS::AAFwk::Want want;
210     want.SetAction(BUNDLE_RESOURCES_CHANGED);
211     want.SetParam(Constants::USER_ID, userId);
212     EventFwk::CommonEventData commonData { want };
213     EventFwk::CommonEventPublishInfo publishInfo;
214     std::vector<std::string> permissionVec { Constants::PERMISSION_GET_BUNDLE_RESOURCES };
215     publishInfo.SetSubscriberPermissions(permissionVec);
216     EventFwk::CommonEventManager::PublishCommonEvent(commonData, publishInfo);
217 }
218 } // AppExecFwk
219 } // OHOS