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