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