1 /*
2 * Copyright (c) 2021 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 #define LOG_TAG "UninstallerImpl"
17
18 #include "uninstaller_impl.h"
19 #include <thread>
20 #include <unistd.h>
21 #include "common_event_manager.h"
22 #include "common_event_support.h"
23 #include "communication_provider.h"
24 #include "ipc_skeleton.h"
25 #include "log_print.h"
26 #include "metadata/meta_data_manager.h"
27 #include "metadata/store_meta_data.h"
28 #include "permit_delegate.h"
29 #include "utils/block_integer.h"
30
31 namespace OHOS::DistributedKv {
32 using namespace OHOS::AppDistributedKv;
33 using namespace OHOS::AAFwk;
34 using namespace OHOS::AppExecFwk;
35 using namespace OHOS::DistributedData;
36 using namespace OHOS::EventFwk;
37
UninstallEventSubscriber(const CommonEventSubscribeInfo & info,UninstallEventCallback callback)38 UninstallEventSubscriber::UninstallEventSubscriber(const CommonEventSubscribeInfo &info,
39 UninstallEventCallback callback)
40 : CommonEventSubscriber(info), callback_(callback)
41 {}
42
OnReceiveEvent(const CommonEventData & event)43 void UninstallEventSubscriber::OnReceiveEvent(const CommonEventData &event)
44 {
45 ZLOGI("Intent Action Rec");
46 Want want = event.GetWant();
47 std::string action = want.GetAction();
48 if (action != CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED &&
49 action != CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) {
50 return;
51 }
52
53 std::string bundleName = want.GetElement().GetBundleName();
54 int32_t userId = want.GetIntParam(USER_ID, -1);
55 int32_t appIndex = want.GetIntParam(SANDBOX_APP_INDEX, 0);
56 ZLOGI("bundleName:%s, user:%d, appIndex:%d", bundleName.c_str(), userId, appIndex);
57 callback_(bundleName, userId, appIndex);
58 }
59
~UninstallerImpl()60 UninstallerImpl::~UninstallerImpl()
61 {
62 ZLOGD("destruct");
63 auto res = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
64 if (!res) {
65 ZLOGW("unsubscribe fail res:%d", res);
66 }
67 }
68
UnsubscribeEvent()69 void UninstallerImpl::UnsubscribeEvent()
70 {
71 auto res = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
72 if (!res) {
73 ZLOGW("unsubscribe fail res:%d", res);
74 }
75 }
76
Init(KvStoreDataService * kvStoreDataService)77 Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService)
78 {
79 if (kvStoreDataService == nullptr) {
80 ZLOGW("kvStoreDataService is null.");
81 return Status::INVALID_ARGUMENT;
82 }
83 MatchingSkills matchingSkills;
84 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
85 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
86 CommonEventSubscribeInfo info(matchingSkills);
87 auto callback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) {
88 kvStoreDataService->OnUninstall(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID());
89 std::string prefix = StoreMetaData::GetPrefix({ CommunicationProvider::GetInstance().GetLocalDevice().uuid,
90 std::to_string(userId), "default", bundleName });
91 std::vector<StoreMetaData> storeMetaData;
92 if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData)) {
93 ZLOGE("load meta failed!");
94 return;
95 }
96 for (auto &meta : storeMetaData) {
97 if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) {
98 ZLOGI("uninstalled bundleName:%s, stordId:%s", bundleName.c_str(), meta.storeId.c_str());
99 MetaDataManager::GetInstance().DelMeta(meta.GetKey());
100 MetaDataManager::GetInstance().DelMeta(meta.GetSecretKey(), true);
101 MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey());
102 MetaDataManager::GetInstance().DelMeta(meta.appId, true);
103 MetaDataManager::GetInstance().DelMeta(meta.GetKeyLocal(), true);
104 PermitDelegate::GetInstance().DelCache(meta.GetKey());
105 }
106 }
107 };
108 auto subscriber = std::make_shared<UninstallEventSubscriber>(info, callback);
109 subscriber_ = subscriber;
110 std::thread th = std::thread([subscriber] {
111 constexpr int32_t RETRY_TIME = 300;
112 constexpr int32_t RETRY_INTERVAL = 100 * 1000;
113 for (BlockInteger retry(RETRY_INTERVAL); retry < RETRY_TIME; ++retry) {
114 if (CommonEventManager::SubscribeCommonEvent(subscriber)) {
115 ZLOGI("subscribe uninstall event success");
116 break;
117 }
118 ZLOGE("subscribe uninstall event fail, try times:%d", static_cast<int>(retry));
119 }
120 });
121 th.detach();
122 return Status::SUCCESS;
123 }
124 } // namespace OHOS::DistributedKv
125