• 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 "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.GetSecretKey(), true);
84             MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey());
85             MetaDataManager::GetInstance().DelMeta(meta.appId, true);
86             MetaDataManager::GetInstance().DelMeta(meta.GetKeyLocal(), true);
87             PermitDelegate::GetInstance().DelCache(meta.GetKey());
88         }
89     }
90 }
91 
OnUpdate(const std::string & bundleName,int32_t userId,int32_t appIndex)92 void InstallEventSubscriber::OnUpdate(const std::string &bundleName, int32_t userId, int32_t appIndex)
93 {
94     kvStoreDataService_->OnUpdate(bundleName, userId, appIndex);
95     std::string prefix = StoreMetaData::GetPrefix(
96         { DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName });
97     std::vector<StoreMetaData> storeMetaData;
98     if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData, true)) {
99         ZLOGE("load meta failed! bundleName:%{public}s, userId:%{public}d, appIndex:%{public}d", bundleName.c_str(),
100             userId, appIndex);
101         return;
102     }
103     for (auto &meta : storeMetaData) {
104         if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) {
105             ZLOGI("updated bundleName:%{public}s, storeId:%{public}s, userId:%{public}d, appIndex:%{public}d",
106                 bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), userId, appIndex);
107             MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true);
108         }
109     }
110 }
111 
OnInstall(const std::string & bundleName,int32_t userId,int32_t appIndex)112 void InstallEventSubscriber::OnInstall(const std::string &bundleName, int32_t userId, int32_t appIndex)
113 {
114     kvStoreDataService_->OnInstall(bundleName, userId, appIndex);
115 }
116 
~InstallerImpl()117 InstallerImpl::~InstallerImpl()
118 {
119     ZLOGD("destruct");
120     auto res = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
121     if (!res) {
122         ZLOGW("unsubscribe fail res:%d", res);
123     }
124 }
125 
UnsubscribeEvent()126 void InstallerImpl::UnsubscribeEvent()
127 {
128     auto res = CommonEventManager::UnSubscribeCommonEvent(subscriber_);
129     if (!res) {
130         ZLOGW("unsubscribe fail res:%d", res);
131     }
132 }
133 
Init(KvStoreDataService * kvStoreDataService,std::shared_ptr<ExecutorPool> executors)134 Status InstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared_ptr<ExecutorPool> executors)
135 {
136     if (kvStoreDataService == nullptr) {
137         ZLOGW("kvStoreDataService is null.");
138         return Status::INVALID_ARGUMENT;
139     }
140     MatchingSkills matchingSkills;
141     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
142     matchingSkills.AddEvent(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED);
143     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
144     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
145     matchingSkills.AddEvent(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_ADDED);
146     CommonEventSubscribeInfo info(matchingSkills);
147 
148     auto subscriber = std::make_shared<InstallEventSubscriber>(info, kvStoreDataService);
149     subscriber_ = subscriber;
150     executors_ = executors;
151     executors_->Execute(GetTask());
152     return Status::SUCCESS;
153 }
154 
GetTask()155 ExecutorPool::Task InstallerImpl::GetTask()
156 {
157     return [this] {
158         auto succ = CommonEventManager::SubscribeCommonEvent(subscriber_);
159         if (succ) {
160             ZLOGI("subscribe install event success");
161             return;
162         }
163         ZLOGE("subscribe common event fail, try times:%{public}d", retryTime_);
164         if (retryTime_++ >= RETRY_TIME) {
165             return;
166         }
167         executors_->Schedule(std::chrono::milliseconds(RETRY_INTERVAL), GetTask());
168     };
169 }
170 } // namespace OHOS::DistributedKv
171