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