1 /*
2 * Copyright (c) 2023 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 "uri_permission_manager_client.h"
20
21 namespace OHOS {
22 namespace AAFwk {
23 namespace {
24 const std::string KEY_TOKEN = "accessTokenId";
25 }
AbilityBundleEventCallback(std::shared_ptr<TaskHandlerWrap> taskHandler,std::shared_ptr<AbilityAutoStartupService> abilityAutoStartupService)26 AbilityBundleEventCallback::AbilityBundleEventCallback(
27 std::shared_ptr<TaskHandlerWrap> taskHandler, std::shared_ptr<AbilityAutoStartupService> abilityAutoStartupService)
28 : taskHandler_(taskHandler), abilityAutoStartupService_(abilityAutoStartupService) {}
29
OnReceiveEvent(const EventFwk::CommonEventData eventData)30 void AbilityBundleEventCallback::OnReceiveEvent(const EventFwk::CommonEventData eventData)
31 {
32 // env check
33 if (taskHandler_ == nullptr) {
34 HILOG_ERROR("OnReceiveEvent failed, taskHandler is nullptr");
35 return;
36 }
37 const Want& want = eventData.GetWant();
38 // action contains the change type of haps.
39 std::string action = want.GetAction();
40 std::string bundleName = want.GetElement().GetBundleName();
41 auto tokenId = static_cast<uint32_t>(want.GetIntParam(KEY_TOKEN, 0));
42 int uid = want.GetIntParam(KEY_UID, 0);
43 // verify data
44 if (action.empty() || bundleName.empty()) {
45 HILOG_ERROR("OnReceiveEvent failed, empty action/bundleName");
46 return;
47 }
48 HILOG_DEBUG("OnReceiveEvent, action:%{public}s.", action.c_str());
49
50 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
51 // uninstall bundle
52 HandleRemoveUriPermission(tokenId);
53 HandleUpdatedModuleInfo(bundleName, uid);
54 if (abilityAutoStartupService_ == nullptr) {
55 HILOG_ERROR("OnReceiveEvent failed, abilityAutoStartupService is nullptr");
56 return;
57 }
58 abilityAutoStartupService_->DeleteAutoStartupData(bundleName);
59 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) {
60 // install or uninstall module/bundle
61 HandleUpdatedModuleInfo(bundleName, uid);
62 } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
63 HandleUpdatedModuleInfo(bundleName, uid);
64 HandleAppUpgradeCompleted(bundleName, uid);
65 if (abilityAutoStartupService_ == nullptr) {
66 HILOG_ERROR("OnReceiveEvent failed, abilityAutoStartupService is nullptr");
67 return;
68 }
69 abilityAutoStartupService_->CheckAutoStartupData(bundleName, uid);
70 }
71 }
72
HandleRemoveUriPermission(uint32_t tokenId)73 void AbilityBundleEventCallback::HandleRemoveUriPermission(uint32_t tokenId)
74 {
75 HILOG_DEBUG("HandleRemoveUriPermission: %{public}i", tokenId);
76 auto ret = IN_PROCESS_CALL(AAFwk::UriPermissionManagerClient::GetInstance().RevokeAllUriPermissions(tokenId));
77 if (!ret) {
78 HILOG_ERROR("Revoke all uri permissions failed.");
79 }
80 }
81
HandleUpdatedModuleInfo(const std::string & bundleName,int32_t uid)82 void AbilityBundleEventCallback::HandleUpdatedModuleInfo(const std::string &bundleName, int32_t uid)
83 {
84 wptr<AbilityBundleEventCallback> weakThis = this;
85 auto task = [weakThis, bundleName, uid]() {
86 sptr<AbilityBundleEventCallback> sharedThis = weakThis.promote();
87 if (sharedThis == nullptr) {
88 HILOG_ERROR("sharedThis is nullptr.");
89 return;
90 }
91 sharedThis->abilityEventHelper_.HandleModuleInfoUpdated(bundleName, uid);
92 };
93 taskHandler_->SubmitTask(task);
94 }
95
HandleAppUpgradeCompleted(const std::string & bundleName,int32_t uid)96 void AbilityBundleEventCallback::HandleAppUpgradeCompleted(const std::string &bundleName, int32_t uid)
97 {
98 wptr<AbilityBundleEventCallback> weakThis = this;
99 auto task = [weakThis, bundleName, uid]() {
100 sptr<AbilityBundleEventCallback> sharedThis = weakThis.promote();
101 if (sharedThis == nullptr) {
102 HILOG_ERROR("sharedThis is nullptr.");
103 return;
104 }
105
106 auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
107 if (abilityMgr == nullptr) {
108 HILOG_ERROR("abilityMgr is nullptr.");
109 return;
110 }
111 abilityMgr->AppUpgradeCompleted(bundleName, uid);
112 };
113 taskHandler_->SubmitTask(task);
114 }
115 } // namespace AAFwk
116 } // namespace OHOS