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