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 "InstallerImpl"
17
18 #include "installer_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
InstallEventSubscriber(const CommonEventSubscribeInfo & info,KvStoreDataService * kvStoreDataService)40 InstallEventSubscriber::InstallEventSubscriber(const CommonEventSubscribeInfo &info,
41 KvStoreDataService *kvStoreDataService)
42 : CommonEventSubscriber(info), kvStoreDataService_(kvStoreDataService)
43 {
44 callbacks_ = { { CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED, &InstallEventSubscriber::OnUninstall },
45 { OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED, &InstallEventSubscriber::OnUninstall },
46 { CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED, &InstallEventSubscriber::OnUpdate },
47 { CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED, &InstallEventSubscriber::OnInstall },
48 { OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_ADDED, &InstallEventSubscriber::OnInstall }};
49 }
50
OnReceiveEvent(const CommonEventData & event)51 void InstallEventSubscriber::OnReceiveEvent(const CommonEventData &event)
52 {
53 ZLOGI("Action Rec");
54 Want want = event.GetWant();
55 std::string action = want.GetAction();
56 auto it = callbacks_.find(action);
57 if (it != callbacks_.end()) {
58 std::string bundleName = want.GetElement().GetBundleName();
59 int32_t userId = want.GetIntParam(USER_ID, -1);
60 int32_t appIndex = want.GetIntParam(SANDBOX_APP_INDEX, 0);
61 ZLOGI("bundleName:%{public}s, user:%{public}d, appIndex:%{public}d", bundleName.c_str(), userId, appIndex);
62 (this->*(it->second))(bundleName, userId, appIndex);
63 }
64 }
65
OnUninstall(const std::string & bundleName,int32_t userId,int32_t appIndex)66 void InstallEventSubscriber::OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex)
67 {
68 kvStoreDataService_->OnUninstall(bundleName, userId, appIndex);
69 std::string prefix = StoreMetaData::GetPrefix(
70 { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName });
71 std::vector<StoreMetaData> storeMetaData;
72 if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData, true)) {
73 ZLOGE("load meta failed! bundleName:%{public}s, userId:%{public}d, appIndex:%{public}d", bundleName.c_str(),
74 userId, appIndex);
75 return;
76 }
77 for (auto &meta : storeMetaData) {
78 if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) {
79 ZLOGI("uninstalled bundleName:%{public}s storeId:%{public}s, userId:%{public}d, appIndex:%{public}d",
80 bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), userId, appIndex);
81 MetaDataManager::GetInstance().DelMeta(meta.GetKey());
82 MetaDataManager::GetInstance().DelMeta(meta.GetKey(), true);
83 MetaDataManager::GetInstance().DelMeta(meta.GetKeyLocal(), true);
84 MetaDataManager::GetInstance().DelMeta(meta.GetSecretKey(), true);
85 MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey());
86 MetaDataManager::GetInstance().DelMeta(meta.appId, true);
87 MetaDataManager::GetInstance().DelMeta(meta.GetBackupSecretKey(), true);
88 MetaDataManager::GetInstance().DelMeta(meta.GetAutoLaunchKey(), true);
89 MetaDataManager::GetInstance().DelMeta(meta.GetDebugInfoKey(), true);
90 PermitDelegate::GetInstance().DelCache(meta.GetKey());
91 }
92 }
93 }
94
OnUpdate(const std::string & bundleName,int32_t userId,int32_t appIndex)95 void InstallEventSubscriber::OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex)
96 {
97 kvStoreDataService_->OnUpdate(bundleName, userId, appIndex);
98 std::string prefix = StoreMetaData::GetPrefix(
99 { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName });
100 std::vector<StoreMetaData> storeMetaData;
101 if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData, true)) {
102 ZLOGE("load meta failed! bundleName:%{public}s, userId:%{public}d, appIndex:%{public}d", bundleName.c_str(),
103 userId, appIndex);
104 return;
105 }
106 for (auto &meta : storeMetaData) {
107 if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) {
108 ZLOGI("updated bundleName:%{public}s, storeId:%{public}s, userId:%{public}d, appIndex:%{public}d",
109 bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), userId, appIndex);
110 MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true);
111 }
112 }
113 }
114
OnInstall(const std::string & bundleName,int32_t userId,int32_t appIndex)115 void InstallEventSubscriber::OnInstall(const std::string &bundleName, int32_t userId, int32_t appIndex)
116 {
117 kvStoreDataService_->OnInstall(bundleName, userId, appIndex);
118 }
119
~InstallerImpl()120 InstallerImpl::~InstallerImpl()
121 {
122 ZLOGD("destruct");
123 auto res = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
124 if (!res) {
125 ZLOGW("unsubscribe fail res:%d", res);
126 }
127 }
128
UnsubscribeEvent()129 void InstallerImpl::UnsubscribeEvent()
130 {
131 auto res = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
132 if (!res) {
133 ZLOGW("unsubscribe fail res:%d", res);
134 }
135 }
136
Init(KvStoreDataService * kvStoreDataService,std::shared_ptr<ExecutorPool> executors)137 Status InstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared_ptr<ExecutorPool> executors)
138 {
139 if (kvStoreDataService == nullptr) {
140 ZLOGW("kvStoreDataService is null.");
141 return Status::INVALID_ARGUMENT;
142 }
143 MatchingSkills matchingSkills;
144 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
145 matchingSkills.AddEvent(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
146 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
147 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
148 matchingSkills.AddEvent(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_ADDED);
149 CommonEventSubscribeInfo info(matchingSkills);
150
151 auto subscriber = std::make_shared<InstallEventSubscriber>(info, kvStoreDataService);
152 subscriber_ = subscriber;
153 executors_ = executors;
154 executors_->Execute(GetTask());
155 return Status::SUCCESS;
156 }
157
GetTask()158 ExecutorPool::Task InstallerImpl::GetTask()
159 {
160 return [this] {
161 auto succ = CommonEventManager::SubscribeCommonEvent(subscriber_);
162 if (succ) {
163 ZLOGI("subscribe install event success");
164 return;
165 }
166 ZLOGE("subscribe common event fail, try times:%{public}d", retryTime_);
167 if (retryTime_++ >= RETRY_TIME) {
168 return;
169 }
170 executors_->Schedule(std::chrono::milliseconds(RETRY_INTERVAL), GetTask());
171 };
172 }
173 } // namespace OHOS::DistributedKv
174