1 /*
2 * Copyright (c) 2023 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 #include "module_update_kits_impl.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "log/log.h"
21 #include "module_error_code.h"
22 #include "module_update_proxy.h"
23 #include "service_control.h"
24 #include "system_ability_definition.h"
25 #include "sys_installer_callback.h"
26
27 namespace OHOS {
28 namespace SysInstaller {
29 using namespace Updater;
30
31 namespace {
32 constexpr const char *SERVICE_NAME = "module_update_service";
33 }
34
GetInstance()35 ModuleUpdateKits &ModuleUpdateKits::GetInstance()
36 {
37 return DelayedRefSingleton<ModuleUpdateKitsImpl>::GetInstance();
38 }
39
ModuleUpdateKitsImpl()40 ModuleUpdateKitsImpl::ModuleUpdateKitsImpl() {}
41
~ModuleUpdateKitsImpl()42 ModuleUpdateKitsImpl::~ModuleUpdateKitsImpl() {}
43
ResetService(const wptr<IRemoteObject> & remote)44 void ModuleUpdateKitsImpl::ResetService(const wptr<IRemoteObject>& remote)
45 {
46 LOG(INFO) << "Remote is dead, reset service instance";
47
48 std::lock_guard<std::mutex> lock(moduleUpdateLock_);
49 if (moduleUpdate_ != nullptr) {
50 sptr<IRemoteObject> object = moduleUpdate_->AsObject();
51 if ((object != nullptr) && (remote == object)) {
52 object->RemoveDeathRecipient(deathRecipient_);
53 moduleUpdate_ = nullptr;
54 }
55 }
56 }
57
GetService()58 sptr<IModuleUpdate> ModuleUpdateKitsImpl::GetService()
59 {
60 std::lock_guard<std::mutex> lock(moduleUpdateLock_);
61 if (moduleUpdate_ != nullptr) {
62 return moduleUpdate_;
63 }
64
65 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
66 if (samgr == nullptr) {
67 LOG(ERROR) << "Get samgr object failed";
68 return nullptr;
69 }
70 sptr<IRemoteObject> object = samgr->GetSystemAbility(MODULE_UPDATE_SERVICE_ID);
71 if (object == nullptr) {
72 LOG(ERROR) << "Get module update object from samgr failed";
73 return nullptr;
74 }
75
76 if (deathRecipient_ == nullptr) {
77 deathRecipient_ = new DeathRecipient();
78 }
79
80 if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
81 LOG(ERROR) << "Failed to add death recipient";
82 }
83
84 LOG(INFO) << "get remote object ok";
85 moduleUpdate_ = iface_cast<IModuleUpdate>(object);
86 if (moduleUpdate_ == nullptr) {
87 LOG(ERROR) << "module update object iface_cast failed";
88 return nullptr;
89 }
90 return moduleUpdate_;
91 }
92
OnRemoteDied(const wptr<IRemoteObject> & remote)93 void ModuleUpdateKitsImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
94 {
95 DelayedRefSingleton<ModuleUpdateKitsImpl>::GetInstance().ResetService(remote);
96 }
97
InstallModulePackage(const std::string & pkgPath)98 int32_t ModuleUpdateKitsImpl::InstallModulePackage(const std::string &pkgPath)
99 {
100 LOG(INFO) << "InstallModulePackage " << pkgPath;
101 auto moduleUpdate = GetService();
102 if (moduleUpdate == nullptr) {
103 LOG(ERROR) << "Get moduleUpdate failed";
104 return ModuleErrorCode::ERR_SERVICE_NOT_FOUND;
105 }
106 return moduleUpdate->InstallModulePackage(pkgPath);
107 }
108
UninstallModulePackage(const std::string & hmpName)109 int32_t ModuleUpdateKitsImpl::UninstallModulePackage(const std::string &hmpName)
110 {
111 LOG(INFO) << "UninstallModulePackage " << hmpName;
112 auto moduleUpdate = GetService();
113 if (moduleUpdate == nullptr) {
114 LOG(ERROR) << "Get moduleUpdate failed";
115 return ModuleErrorCode::ERR_SERVICE_NOT_FOUND;
116 }
117 return moduleUpdate->UninstallModulePackage(hmpName);
118 }
119
GetModulePackageInfo(const std::string & hmpName,std::list<ModulePackageInfo> & modulePackageInfos)120 int32_t ModuleUpdateKitsImpl::GetModulePackageInfo(const std::string &hmpName,
121 std::list<ModulePackageInfo> &modulePackageInfos)
122 {
123 LOG(INFO) << "GetModulePackageInfo";
124 auto moduleUpdate = GetService();
125 if (moduleUpdate == nullptr) {
126 LOG(ERROR) << "Get moduleUpdate failed";
127 return ModuleErrorCode::ERR_SERVICE_NOT_FOUND;
128 }
129 return moduleUpdate->GetModulePackageInfo(hmpName, modulePackageInfos);
130 }
131
ReportModuleUpdateStatus(const ModuleUpdateStatus & status)132 int32_t ModuleUpdateKitsImpl::ReportModuleUpdateStatus(const ModuleUpdateStatus &status)
133 {
134 LOG(INFO) << "ReportModuleUpdateStatus process=" << status.process;
135 auto moduleUpdate = GetService();
136 if (moduleUpdate == nullptr) {
137 LOG(ERROR) << "Get moduleUpdate failed";
138 return ModuleErrorCode::ERR_SERVICE_NOT_FOUND;
139 }
140 return moduleUpdate->ReportModuleUpdateStatus(status);
141 }
142
ExitModuleUpdate()143 int32_t ModuleUpdateKitsImpl::ExitModuleUpdate()
144 {
145 LOG(INFO) << "ExitModuleUpdate";
146 auto moduleUpdate = GetService();
147 if (moduleUpdate == nullptr) {
148 LOG(ERROR) << "Get moduleUpdate failed";
149 return ModuleErrorCode::ERR_SERVICE_NOT_FOUND;
150 }
151 return moduleUpdate->ExitModuleUpdate();
152 }
153
InitModuleUpdate()154 int32_t ModuleUpdateKitsImpl::InitModuleUpdate()
155 {
156 LOG(INFO) << "InitModuleUpdate";
157 int ret = ServiceControl(SERVICE_NAME, ServiceAction::START);
158 if (ret != 0) {
159 LOG(ERROR) << "Failed to start service";
160 return ModuleErrorCode::ERR_SERVICE_NOT_FOUND;
161 }
162 return ModuleErrorCode::MODULE_UPDATE_SUCCESS;
163 }
164
GetHmpVersionInfo()165 std::vector<HmpVersionInfo> ModuleUpdateKitsImpl::GetHmpVersionInfo()
166 {
167 LOG(INFO) << "GetHmpVersionInfo";
168 std::vector<HmpVersionInfo> versionInfo {};
169 auto moduleUpdate = GetService();
170 if (moduleUpdate == nullptr) {
171 LOG(ERROR) << "Get moduleUpdate failed";
172 return versionInfo;
173 }
174 versionInfo = moduleUpdate->GetHmpVersionInfo();
175 return versionInfo;
176 }
177
StartUpdateHmpPackage(const std::string & path,sptr<ISysInstallerCallbackFunc> callback)178 int32_t ModuleUpdateKitsImpl::StartUpdateHmpPackage(const std::string &path,
179 sptr<ISysInstallerCallbackFunc> callback)
180 {
181 LOG(INFO) << "StartUpdateHmpPackage";
182 if (callback == nullptr) {
183 LOG(ERROR) << "callback null";
184 return ModuleErrorCode::ERR_SERVICE_PARA_ERROR;
185 }
186
187 auto moduleUpdate = GetService();
188 if (moduleUpdate == nullptr) {
189 LOG(ERROR) << "Get moduleUpdate failed";
190 return ModuleErrorCode::ERR_SERVICE_NOT_FOUND;
191 }
192
193 if (updateCallBack_ == nullptr) {
194 updateCallBack_ = new SysInstallerCallback;
195 }
196 static_cast<SysInstallerCallback *>(updateCallBack_.GetRefPtr())->RegisterCallback(callback);
197
198 return moduleUpdate->StartUpdateHmpPackage(path, updateCallBack_);
199 }
200
GetHmpUpdateResult()201 std::vector<HmpUpdateInfo> ModuleUpdateKitsImpl::GetHmpUpdateResult()
202 {
203 LOG(INFO) << "GetHmpUpdateResult";
204 std::vector<HmpUpdateInfo> updateInfo {};
205 auto moduleUpdate = GetService();
206 if (moduleUpdate == nullptr) {
207 LOG(ERROR) << "Get moduleUpdate failed";
208 return updateInfo;
209 }
210 updateInfo = moduleUpdate->GetHmpUpdateResult();
211 return updateInfo;
212 }
213 }
214 } // namespace OHOS
215