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 int32_t newAppIndex = want.GetIntParam(APP_INDEX, 0);
62 ZLOGI("bundleName:%{public}s, user:%{public}d, appIndex:%{public}d, newAppIndex:%{public}d",
63 bundleName.c_str(), userId, appIndex, newAppIndex);
64 // appIndex's key in want is "appIndex", the value of the elder key "sandbox_app_index" is unsure,
65 // to avoid effecting historical function, passing non-zero value to the function
66 if (appIndex == 0 && newAppIndex != 0) {
67 appIndex = newAppIndex;
68 }
69 (this->*(it->second))(bundleName, userId, appIndex);
70 }
71 }
72
OnUninstall(const std::string & bundleName,int32_t userId,int32_t appIndex)73 void InstallEventSubscriber::OnUninstall(const std::string &bundleName, int32_t userId, int32_t appIndex)
74 {
75 kvStoreDataService_->OnUninstall(bundleName, userId, appIndex);
76 std::string prefix = StoreMetaData::GetPrefix(
77 { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName });
78 std::vector<StoreMetaData> storeMetaData;
79 if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData, true)) {
80 ZLOGE("load meta failed! bundleName:%{public}s, userId:%{public}d, appIndex:%{public}d", bundleName.c_str(),
81 userId, appIndex);
82 return;
83 }
84 for (auto &meta : storeMetaData) {
85 if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) {
86 ZLOGI("uninstalled bundleName:%{public}s storeId:%{public}s, userId:%{public}d, appIndex:%{public}d",
87 bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), userId, appIndex);
88 MetaDataManager::GetInstance().DelMeta(meta.GetKey());
89 MetaDataManager::GetInstance().DelMeta(meta.GetKey(), true);
90 MetaDataManager::GetInstance().DelMeta(meta.GetKeyLocal(), true);
91 MetaDataManager::GetInstance().DelMeta(meta.GetSecretKey(), true);
92 MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey());
93 MetaDataManager::GetInstance().DelMeta(meta.appId, true);
94 MetaDataManager::GetInstance().DelMeta(meta.GetBackupSecretKey(), true);
95 MetaDataManager::GetInstance().DelMeta(meta.GetAutoLaunchKey(), true);
96 MetaDataManager::GetInstance().DelMeta(meta.GetDebugInfoKey(), true);
97 MetaDataManager::GetInstance().DelMeta(meta.GetDfxInfoKey(), true);
98 MetaDataManager::GetInstance().DelMeta(meta.GetCloneSecretKey(), true);
99 PermitDelegate::GetInstance().DelCache(meta.GetKey());
100 }
101 }
102 }
103
OnUpdate(const std::string & bundleName,int32_t userId,int32_t appIndex)104 void InstallEventSubscriber::OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex)
105 {
106 kvStoreDataService_->OnUpdate(bundleName, userId, appIndex);
107 std::string prefix = StoreMetaData::GetPrefix(
108 { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName });
109 std::vector<StoreMetaData> storeMetaData;
110 if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData, true)) {
111 ZLOGE("load meta failed! bundleName:%{public}s, userId:%{public}d, appIndex:%{public}d", bundleName.c_str(),
112 userId, appIndex);
113 return;
114 }
115 for (auto &meta : storeMetaData) {
116 if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) {
117 ZLOGI("updated bundleName:%{public}s, storeId:%{public}s, userId:%{public}d, appIndex:%{public}d",
118 bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), userId, appIndex);
119 MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true);
120 }
121 }
122 }
123
OnInstall(const std::string & bundleName,int32_t userId,int32_t appIndex)124 void InstallEventSubscriber::OnInstall(const std::string &bundleName, int32_t userId, int32_t appIndex)
125 {
126 kvStoreDataService_->OnInstall(bundleName, userId, appIndex);
127 }
128
~InstallerImpl()129 InstallerImpl::~InstallerImpl()
130 {
131 ZLOGD("destruct");
132 auto res = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
133 if (!res) {
134 ZLOGW("unsubscribe fail res:%d", res);
135 }
136 }
137
UnsubscribeEvent()138 void InstallerImpl::UnsubscribeEvent()
139 {
140 auto res = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
141 if (!res) {
142 ZLOGW("unsubscribe fail res:%d", res);
143 }
144 }
145
Init(KvStoreDataService * kvStoreDataService,std::shared_ptr<ExecutorPool> executors)146 Status InstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared_ptr<ExecutorPool> executors)
147 {
148 if (kvStoreDataService == nullptr) {
149 ZLOGW("kvStoreDataService is null.");
150 return Status::INVALID_ARGUMENT;
151 }
152 MatchingSkills matchingSkills;
153 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
154 matchingSkills.AddEvent(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
155 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
156 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
157 matchingSkills.AddEvent(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_ADDED);
158 CommonEventSubscribeInfo info(matchingSkills);
159
160 auto subscriber = std::make_shared<InstallEventSubscriber>(info, kvStoreDataService);
161 subscriber_ = subscriber;
162 executors_ = executors;
163 executors_->Execute(GetTask());
164 return Status::SUCCESS;
165 }
166
GetTask()167 ExecutorPool::Task InstallerImpl::GetTask()
168 {
169 return [this] {
170 auto succ = CommonEventManager::SubscribeCommonEvent(subscriber_);
171 if (succ) {
172 ZLOGI("subscribe install event success");
173 return;
174 }
175 ZLOGE("subscribe common event fail, try times:%{public}d", retryTime_);
176 if (retryTime_++ >= RETRY_TIME) {
177 return;
178 }
179 executors_->Schedule(std::chrono::milliseconds(RETRY_INTERVAL), GetTask());
180 };
181 }
182 } // namespace OHOS::DistributedKv