• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bundle_common_event.h"
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "device_manager_adapter.h"
25 #include "ipc_skeleton.h"
26 #include "log_print.h"
27 #include "metadata/meta_data_manager.h"
28 #include "metadata/store_meta_data.h"
29 #include "permit_delegate.h"
30 #include "cloud/cloud_info.h"
31 #include "utils/anonymous.h"
32 
33 namespace OHOS::DistributedKv {
34 using namespace OHOS::AppDistributedKv;
35 using namespace OHOS::AAFwk;
36 using namespace OHOS::AppExecFwk;
37 using namespace OHOS::DistributedData;
38 using namespace OHOS::EventFwk;
39 
UninstallEventSubscriber(const CommonEventSubscribeInfo & info,KvStoreDataService * kvStoreDataService)40 UninstallEventSubscriber::UninstallEventSubscriber(const CommonEventSubscribeInfo &info,
41     KvStoreDataService *kvStoreDataService)
42     : CommonEventSubscriber(info), kvStoreDataService_(kvStoreDataService)
43 {
44     callbacks_ = { { CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED, &UninstallEventSubscriber::OnUninstall },
45         { OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED, &UninstallEventSubscriber::OnUninstall },
46         { CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED, &UninstallEventSubscriber::OnUpdate } };
47 }
48 
OnReceiveEvent(const CommonEventData & event)49 void UninstallEventSubscriber::OnReceiveEvent(const CommonEventData &event)
50 {
51     ZLOGI("Intent Action Rec");
52     Want want = event.GetWant();
53     std::string action = want.GetAction();
54     auto it = callbacks_.find(action);
55     if (it != callbacks_.end()) {
56         std::string bundleName = want.GetElement().GetBundleName();
57         int32_t userId = want.GetIntParam(USER_ID, -1);
58         int32_t appIndex = want.GetIntParam(SANDBOX_APP_INDEX, 0);
59         ZLOGI("bundleName:%{public}s, user:%{public}d, appIndex:%{public}d", bundleName.c_str(), userId, appIndex);
60         (this->*(it->second))(bundleName, userId, appIndex);
61     }
62 }
63 
OnUninstall(const std::string & bundleName,int32_t userId,int32_t appIndex)64 void UninstallEventSubscriber::OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex)
65 {
66     kvStoreDataService_->OnUninstall(bundleName, userId, appIndex);
67     std::string prefix = StoreMetaData::GetPrefix(
68         { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName });
69     std::vector<StoreMetaData> storeMetaData;
70     if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData)) {
71         ZLOGE("load meta failed!");
72         return;
73     }
74     for (auto &meta : storeMetaData) {
75         if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) {
76             ZLOGI("uninstalled bundleName:%{public}s stordId:%{public}s", bundleName.c_str(),
77                 Anonymous::Change(meta.storeId).c_str());
78             MetaDataManager::GetInstance().DelMeta(meta.GetKey());
79             MetaDataManager::GetInstance().DelMeta(meta.GetSecretKey(), true);
80             MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey());
81             MetaDataManager::GetInstance().DelMeta(meta.appId, true);
82             MetaDataManager::GetInstance().DelMeta(meta.GetKeyLocal(), true);
83             PermitDelegate::GetInstance().DelCache(meta.GetKey());
84         }
85     }
86 }
87 
OnUpdate(const std::string & bundleName,int32_t userId,int32_t appIndex)88 void UninstallEventSubscriber::OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex)
89 {
90     kvStoreDataService_->OnUpdate(bundleName, userId, appIndex);
91     std::string prefix = StoreMetaData::GetPrefix(
92         { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName });
93     std::vector<StoreMetaData> storeMetaData;
94     if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData)) {
95         ZLOGE("load meta failed!");
96         return;
97     }
98     for (auto &meta : storeMetaData) {
99         if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) {
100             ZLOGI("updated bundleName:%{public}s, stordId:%{public}s", bundleName.c_str(),
101                 Anonymous::Change(meta.storeId).c_str());
102             MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true);
103         }
104     }
105 }
~UninstallerImpl()106 UninstallerImpl::~UninstallerImpl()
107 {
108     ZLOGD("destruct");
109     auto res = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
110     if (!res) {
111         ZLOGW("unsubscribe fail res:%d", res);
112     }
113 }
114 
UnsubscribeEvent()115 void UninstallerImpl::UnsubscribeEvent()
116 {
117     auto res = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
118     if (!res) {
119         ZLOGW("unsubscribe fail res:%d", res);
120     }
121 }
122 
Init(KvStoreDataService * kvStoreDataService,std::shared_ptr<ExecutorPool> executors)123 Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared_ptr<ExecutorPool> executors)
124 {
125     if (kvStoreDataService == nullptr) {
126         ZLOGW("kvStoreDataService is null.");
127         return Status::INVALID_ARGUMENT;
128     }
129     MatchingSkills matchingSkills;
130     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
131     matchingSkills.AddEvent(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
132     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
133     CommonEventSubscribeInfo info(matchingSkills);
134 
135     auto subscriber = std::make_shared<UninstallEventSubscriber>(info, kvStoreDataService);
136     subscriber_ = subscriber;
137     executors_ = executors;
138     executors_->Execute(GetTask());
139     return Status::SUCCESS;
140 }
141 
GetTask()142 ExecutorPool::Task UninstallerImpl::GetTask()
143 {
144     return [this] {
145         auto succ = CommonEventManager::SubscribeCommonEvent(subscriber_);
146         if (succ) {
147             ZLOGI("subscribe uninstall event success");
148             return;
149         }
150         ZLOGE("subscribe common event fail, try times:%{public}d", retryTime_);
151         if (retryTime_++ >= RETRY_TIME) {
152             return;
153         }
154         executors_->Schedule(std::chrono::milliseconds(RETRY_INTERVAL), GetTask());
155     };
156 }
157 } // namespace OHOS::DistributedKv
158