• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 "ability_bundle_event_callback.h"
17 
18 #include "insight_intent_event_mgr.h"
19 #include "ability_manager_service.h"
20 #include "ability_util.h"
21 #include "parameters.h"
22 #ifdef SUPPORT_UPMS
23 #include "uri_permission_manager_client.h"
24 #endif // SUPPORT_UPMS
25 
26 namespace OHOS {
27 namespace AAFwk {
28 namespace {
29 constexpr const char* KEY_TOKEN = "accessTokenId";
30 constexpr const char* KEY_UID = "uid";
31 constexpr const char* KEY_USER_ID = "userId";
32 constexpr const char* KEY_APP_INDEX = "appIndex";
33 constexpr const char* OLD_WEB_BUNDLE_NAME = "com.ohos.nweb";
34 constexpr const char* NEW_WEB_BUNDLE_NAME = "com.ohos.arkwebcore";
35 constexpr const char* ARKWEB_CORE_PACKAGE_NAME = "persist.arkwebcore.package_name";
36 constexpr const char* BUNDLE_TYPE = "bundleType";
37 constexpr const char* IS_RECOVER = "isRecover";
38 }
AbilityBundleEventCallback(std::shared_ptr<TaskHandlerWrap> taskHandler,std::shared_ptr<AbilityAutoStartupService> abilityAutoStartupService)39 AbilityBundleEventCallback::AbilityBundleEventCallback(
40     std::shared_ptr<TaskHandlerWrap> taskHandler, std::shared_ptr<AbilityAutoStartupService> abilityAutoStartupService)
41     : taskHandler_(taskHandler), abilityAutoStartupService_(abilityAutoStartupService) {}
42 
OnReceiveEvent(const EventFwk::CommonEventData eventData)43 void AbilityBundleEventCallback::OnReceiveEvent(const EventFwk::CommonEventData eventData)
44 {
45     // env check
46     if (taskHandler_ == nullptr) {
47         TAG_LOGE(AAFwkTag::ABILITYMGR, "OnReceiveEvent failed, taskHandler is nullptr");
48         return;
49     }
50     const Want& want = eventData.GetWant();
51     // action contains the change type of haps.
52     std::string action = want.GetAction();
53     std::string bundleName = want.GetElement().GetBundleName();
54     std::string moduleName = want.GetElement().GetModuleName();
55     auto tokenId = static_cast<uint32_t>(want.GetIntParam(KEY_TOKEN, 0));
56     int uid = want.GetIntParam(KEY_UID, 0);
57     auto bundleType = want.GetIntParam(BUNDLE_TYPE, 0);
58     int userId = want.GetIntParam(KEY_USER_ID, 0);
59     int appIndex = want.GetIntParam(KEY_APP_INDEX, 0);
60     // verify data
61     if (action.empty() || bundleName.empty()) {
62         TAG_LOGE(AAFwkTag::ABILITYMGR, "OnReceiveEvent failed, empty action/bundleName");
63         return;
64     }
65     TAG_LOGD(AAFwkTag::ABILITYMGR, "OnReceiveEvent, action:%{public}s.", action.c_str());
66     if (bundleType == static_cast<int32_t>(AppExecFwk::BundleType::APP_PLUGIN)) {
67         if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) {
68             TAG_LOGI(AAFwkTag::ABILITYMGR, "plugin add:%{public}s", bundleName.c_str());
69             HandleUpdatedModuleInfo(bundleName, uid, moduleName, true);
70         }
71         return;
72     }
73 
74     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
75         IN_PROCESS_CALL_WITHOUT_RET(DelayedSingleton<AppExecFwk::AppMgrClient>::
76             GetInstance()->NotifyUninstallOrUpgradeAppEnd(uid));
77         // uninstall bundle
78         HandleRemoveUriPermission(tokenId);
79         HandleUpdatedModuleInfo(bundleName, uid, moduleName, false);
80         if (abilityAutoStartupService_ == nullptr) {
81             TAG_LOGE(AAFwkTag::ABILITYMGR, "OnReceiveEvent failed, abilityAutoStartupService is nullptr");
82             return;
83         }
84         abilityAutoStartupService_->DeleteAutoStartupData(bundleName, tokenId);
85         AbilityRuntime::InsightIntentEventMgr::DeleteInsightIntentEvent(want.GetElement(), userId, appIndex);
86     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) {
87         // install or uninstall module/bundle
88         HandleUpdatedModuleInfo(bundleName, uid, moduleName, false);
89         AbilityRuntime::InsightIntentEventMgr::UpdateInsightIntentEvent(want.GetElement(), userId);
90         bool isRecover = want.GetBoolParam(IS_RECOVER, false);
91         TAG_LOGI(AAFwkTag::ABILITYMGR, "COMMON_EVENT_PACKAGE_ADDED, isRecover:%{public}d", isRecover);
92         if (isRecover) {
93             HandleAppUpgradeCompleted(uid);
94         }
95     } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
96         IN_PROCESS_CALL_WITHOUT_RET(DelayedSingleton<AppExecFwk::AppMgrClient>::
97             GetInstance()->NotifyUninstallOrUpgradeAppEnd(uid));
98         if (bundleName == NEW_WEB_BUNDLE_NAME || bundleName == OLD_WEB_BUNDLE_NAME ||
99             bundleName == system::GetParameter(ARKWEB_CORE_PACKAGE_NAME, "false")) {
100             HandleRestartResidentProcessDependedOnWeb();
101         }
102         HandleUpdatedModuleInfo(bundleName, uid, moduleName, false);
103         HandleAppUpgradeCompleted(uid);
104         if (abilityAutoStartupService_ == nullptr) {
105             TAG_LOGE(AAFwkTag::ABILITYMGR, "OnReceiveEvent failed, abilityAutoStartupService is nullptr");
106             return;
107         }
108         abilityAutoStartupService_->CheckAutoStartupData(bundleName, uid);
109         AbilityRuntime::InsightIntentEventMgr::UpdateInsightIntentEvent(want.GetElement(), userId);
110     }
111 }
112 
HandleRemoveUriPermission(uint32_t tokenId)113 void AbilityBundleEventCallback::HandleRemoveUriPermission(uint32_t tokenId)
114 {
115     TAG_LOGD(AAFwkTag::ABILITYMGR, "HandleRemoveUriPermission: %{public}i", tokenId);
116 #ifdef SUPPORT_UPMS
117     auto ret = IN_PROCESS_CALL(AAFwk::UriPermissionManagerClient::GetInstance().RevokeAllUriPermissions(tokenId));
118     if (!ret) {
119         TAG_LOGE(AAFwkTag::ABILITYMGR, "Revoke all uri permissions failed.");
120     }
121 #endif // SUPPORT_UPMS
122 }
123 
HandleUpdatedModuleInfo(const std::string & bundleName,int32_t uid,const std::string & moduleName,bool isPlugin)124 void AbilityBundleEventCallback::HandleUpdatedModuleInfo(const std::string &bundleName, int32_t uid,
125     const std::string &moduleName, bool isPlugin)
126 {
127     wptr<AbilityBundleEventCallback> weakThis = this;
128     auto task = [weakThis, bundleName, uid, moduleName, isPlugin]() {
129         sptr<AbilityBundleEventCallback> sharedThis = weakThis.promote();
130         if (sharedThis == nullptr) {
131             TAG_LOGE(AAFwkTag::ABILITYMGR, "sharedThis is nullptr.");
132             return;
133         }
134         sharedThis->abilityEventHelper_.HandleModuleInfoUpdated(bundleName, uid, moduleName, isPlugin);
135     };
136     taskHandler_->SubmitTask(task);
137 }
138 
HandleAppUpgradeCompleted(int32_t uid)139 void AbilityBundleEventCallback::HandleAppUpgradeCompleted(int32_t uid)
140 {
141     wptr<AbilityBundleEventCallback> weakThis = this;
142     auto task = [weakThis, uid]() {
143         sptr<AbilityBundleEventCallback> sharedThis = weakThis.promote();
144         if (sharedThis == nullptr) {
145             TAG_LOGE(AAFwkTag::ABILITYMGR, "sharedThis is nullptr.");
146             return;
147         }
148 
149         auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
150         if (abilityMgr == nullptr) {
151             TAG_LOGE(AAFwkTag::ABILITYMGR, "abilityMgr is nullptr.");
152             return;
153         }
154         abilityMgr->AppUpgradeCompleted(uid);
155     };
156     taskHandler_->SubmitTask(task);
157 }
158 
HandleRestartResidentProcessDependedOnWeb()159 void AbilityBundleEventCallback::HandleRestartResidentProcessDependedOnWeb()
160 {
161     auto task = []() {
162         auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
163         if (abilityMgr == nullptr) {
164             TAG_LOGE(AAFwkTag::ABILITYMGR, "abilityMgr is nullptr.");
165             return;
166         }
167         abilityMgr->HandleRestartResidentProcessDependedOnWeb();
168     };
169     taskHandler_->SubmitTask(task);
170 }
171 } // namespace AAFwk
172 } // namespace OHOS
173