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 #include "update_service_kits_impl.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 #include "iupdate_service.h"
23 #include "iupdate_callback.h"
24 #include "update_define.h"
25 #include "update_log.h"
26 #include "update_service_ondemand.h"
27
28 namespace OHOS {
29 namespace UpdateEngine {
30 #define RETURN_FAIL_WHEN_SERVICE_NULL(updateService) \
31 ENGINE_CHECK((updateService) != nullptr, return INT_CALL_IPC_ERR, "Get updateService failed")
32
GetInstance()33 UpdateServiceKits& UpdateServiceKits::GetInstance()
34 {
35 return DelayedRefSingleton<UpdateServiceKitsImpl>::GetInstance();
36 }
37
UpdateServiceKitsImpl()38 UpdateServiceKitsImpl::UpdateServiceKitsImpl() {}
39
~UpdateServiceKitsImpl()40 UpdateServiceKitsImpl::~UpdateServiceKitsImpl() {}
41
ResetService(const wptr<IRemoteObject> & remote)42 void UpdateServiceKitsImpl::ResetService(const wptr<IRemoteObject>& remote)
43 {
44 ENGINE_LOGI("Remote is dead, reset service instance");
45
46 std::lock_guard<std::mutex> lock(updateServiceLock_);
47 if (updateService_ != nullptr) {
48 sptr<IRemoteObject> object = updateService_->AsObject();
49 if ((object != nullptr) && (remote == object)) {
50 object->RemoveDeathRecipient(deathRecipient_);
51 updateService_ = nullptr;
52 }
53 }
54 }
55
GetService()56 sptr<IUpdateService> UpdateServiceKitsImpl::GetService()
57 {
58 std::lock_guard<std::mutex> lock(updateServiceLock_);
59 if (updateService_ != nullptr) {
60 return updateService_;
61 }
62
63 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64 ENGINE_CHECK(samgr != nullptr, return nullptr, "Get samgr failed");
65 sptr<IRemoteObject> object = samgr->GetSystemAbility(UPDATE_DISTRIBUTED_SERVICE_ID);
66 if (object == nullptr) {
67 ENGINE_CHECK(UpdateServiceOnDemand::GetInstance()->TryLoadUpdaterSa(), return nullptr, "TryLoadUpdaterSa fail");
68 object = samgr->GetSystemAbility(UPDATE_DISTRIBUTED_SERVICE_ID);
69 ENGINE_CHECK(object != nullptr, return nullptr, "Get update object from samgr failed");
70 }
71
72 if (deathRecipient_ == nullptr) {
73 deathRecipient_ = new DeathRecipient();
74 }
75
76 if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
77 ENGINE_LOGE("Failed to add death recipient");
78 }
79
80 ENGINE_LOGI("get remote object ok");
81 updateService_ = iface_cast<IUpdateService>(object);
82 if (updateService_ == nullptr) {
83 ENGINE_LOGE("update service iface_cast failed");
84 return updateService_;
85 }
86
87 ENGINE_LOGI("RegisterUpdateCallback size %{public}zu", remoteUpdateCallbackMap_.size());
88 for (auto &iter : remoteUpdateCallbackMap_) {
89 updateService_->RegisterUpdateCallback(iter.first, iter.second);
90 }
91 return updateService_;
92 }
93
OnRemoteDied(const wptr<IRemoteObject> & remote)94 void UpdateServiceKitsImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
95 {
96 DelayedRefSingleton<UpdateServiceKitsImpl>::GetInstance().ResetService(remote);
97 }
98
RemoteUpdateCallback(const UpdateCallbackInfo & cb)99 UpdateServiceKitsImpl::RemoteUpdateCallback::RemoteUpdateCallback(const UpdateCallbackInfo &cb)
100 : UpdateCallback()
101 {
102 updateCallback_ = cb;
103 }
104
~RemoteUpdateCallback()105 UpdateServiceKitsImpl::RemoteUpdateCallback::~RemoteUpdateCallback()
106 {
107 updateCallback_.checkNewVersionDone = nullptr;
108 updateCallback_.onEvent = nullptr;
109 }
110
OnCheckVersionDone(const BusinessError & businessError,const CheckResult & checkResult)111 void UpdateServiceKitsImpl::RemoteUpdateCallback::OnCheckVersionDone(
112 const BusinessError &businessError, const CheckResult &checkResult)
113 {
114 ENGINE_LOGI("OnCheckVersionDone status %{public}d", checkResult.isExistNewVersion);
115 if (updateCallback_.checkNewVersionDone != nullptr) {
116 updateCallback_.checkNewVersionDone(businessError, checkResult);
117 }
118 }
119
OnEvent(const EventInfo & eventInfo)120 void UpdateServiceKitsImpl::RemoteUpdateCallback::OnEvent(const EventInfo &eventInfo)
121 {
122 ENGINE_LOGI("OnEvent eventId %{public}d", eventInfo.eventId);
123 if (updateCallback_.onEvent != nullptr) {
124 updateCallback_.onEvent(eventInfo);
125 }
126 }
127
RegisterUpdateCallback(const UpgradeInfo & info,const UpdateCallbackInfo & cb)128 int32_t UpdateServiceKitsImpl::RegisterUpdateCallback(const UpgradeInfo &info, const UpdateCallbackInfo &cb)
129 {
130 auto updateService = GetService();
131 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
132
133 std::lock_guard<std::mutex> lock(updateServiceLock_);
134 auto remoteUpdateCallback = new RemoteUpdateCallback(cb);
135 ENGINE_CHECK(remoteUpdateCallback != nullptr, return INT_PARAM_ERR, "Failed to create remote callback");
136 int32_t ret = updateService->RegisterUpdateCallback(info, remoteUpdateCallback);
137 if (ret == INT_CALL_SUCCESS) {
138 remoteUpdateCallbackMap_[info] = remoteUpdateCallback;
139 }
140 return ret;
141 }
142
UnregisterUpdateCallback(const UpgradeInfo & info)143 int32_t UpdateServiceKitsImpl::UnregisterUpdateCallback(const UpgradeInfo &info)
144 {
145 ENGINE_LOGI("UnregisterUpdateCallback");
146 std::lock_guard<std::mutex> lock(updateServiceLock_);
147 remoteUpdateCallbackMap_.erase(info);
148 return INT_CALL_SUCCESS;
149 }
150
CheckNewVersion(const UpgradeInfo & info)151 int32_t UpdateServiceKitsImpl::CheckNewVersion(const UpgradeInfo &info)
152 {
153 ENGINE_LOGI("UpdateServiceKitsImpl::CheckNewVersion");
154
155 auto updateService = GetService();
156 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
157 return updateService->CheckNewVersion(info);
158 }
159
Download(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const DownloadOptions & downloadOptions,BusinessError & businessError)160 int32_t UpdateServiceKitsImpl::Download(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
161 const DownloadOptions &downloadOptions, BusinessError &businessError)
162 {
163 ENGINE_LOGI("UpdateServiceKitsImpl::Download");
164 auto updateService = GetService();
165 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
166 return updateService->Download(info, versionDigestInfo, downloadOptions, businessError);
167 }
168
PauseDownload(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const PauseDownloadOptions & pauseDownloadOptions,BusinessError & businessError)169 int32_t UpdateServiceKitsImpl::PauseDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
170 const PauseDownloadOptions &pauseDownloadOptions, BusinessError &businessError)
171 {
172 ENGINE_LOGI("UpdateServiceKitsImpl::PauseDownload");
173 auto updateService = GetService();
174 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
175 return updateService->PauseDownload(info, versionDigestInfo, pauseDownloadOptions, businessError);
176 }
177
ResumeDownload(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const ResumeDownloadOptions & resumeDownloadOptions,BusinessError & businessError)178 int32_t UpdateServiceKitsImpl::ResumeDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
179 const ResumeDownloadOptions &resumeDownloadOptions, BusinessError &businessError)
180 {
181 ENGINE_LOGI("UpdateServiceKitsImpl::ResumeDownload");
182 auto updateService = GetService();
183 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
184 return updateService->ResumeDownload(info, versionDigestInfo, resumeDownloadOptions, businessError);
185 }
186
Upgrade(const UpgradeInfo & info,const VersionDigestInfo & versionDigest,const UpgradeOptions & upgradeOptions,BusinessError & businessError)187 int32_t UpdateServiceKitsImpl::Upgrade(const UpgradeInfo &info, const VersionDigestInfo &versionDigest,
188 const UpgradeOptions &upgradeOptions, BusinessError &businessError)
189 {
190 ENGINE_LOGI("UpdateServiceKitsImpl::Upgrade");
191 auto updateService = GetService();
192 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
193 return updateService->Upgrade(info, versionDigest, upgradeOptions, businessError);
194 }
195
ClearError(const UpgradeInfo & info,const VersionDigestInfo & versionDigest,const ClearOptions & clearOptions,BusinessError & businessError)196 int32_t UpdateServiceKitsImpl::ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigest,
197 const ClearOptions &clearOptions, BusinessError &businessError)
198 {
199 ENGINE_LOGI("UpdateServiceKitsImpl::ClearError");
200 auto updateService = GetService();
201 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
202 return updateService->ClearError(info, versionDigest, clearOptions, businessError);
203 }
204
TerminateUpgrade(const UpgradeInfo & info,BusinessError & businessError)205 int32_t UpdateServiceKitsImpl::TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError)
206 {
207 ENGINE_LOGI("UpdateServiceKitsImpl::TerminateUpgrade");
208 auto updateService = GetService();
209 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
210 return updateService->TerminateUpgrade(info, businessError);
211 }
212
GetNewVersionInfo(const UpgradeInfo & info,NewVersionInfo & newVersionInfo,BusinessError & businessError)213 int32_t UpdateServiceKitsImpl::GetNewVersionInfo(const UpgradeInfo &info, NewVersionInfo &newVersionInfo,
214 BusinessError &businessError)
215 {
216 ENGINE_LOGI("UpdateServiceKitsImpl::GetNewVersionInfo");
217 auto updateService = GetService();
218 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
219 return updateService->GetNewVersionInfo(info, newVersionInfo, businessError);
220 }
221
GetNewVersionDescription(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & newVersionDescriptionInfo,BusinessError & businessError)222 int32_t UpdateServiceKitsImpl::GetNewVersionDescription(const UpgradeInfo &info,
223 const VersionDigestInfo &versionDigestInfo, const DescriptionOptions &descriptionOptions,
224 VersionDescriptionInfo &newVersionDescriptionInfo, BusinessError &businessError)
225 {
226 ENGINE_LOGI("UpdateServiceKitsImpl::GetNewVersionDescription");
227 auto updateService = GetService();
228 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
229 return updateService->GetNewVersionDescription(info, versionDigestInfo, descriptionOptions,
230 newVersionDescriptionInfo, businessError);
231 }
232
GetCurrentVersionInfo(const UpgradeInfo & info,CurrentVersionInfo & currentVersionInfo,BusinessError & businessError)233 int32_t UpdateServiceKitsImpl::GetCurrentVersionInfo(const UpgradeInfo &info, CurrentVersionInfo ¤tVersionInfo,
234 BusinessError &businessError)
235 {
236 ENGINE_LOGI("UpdateServiceKitsImpl::GetCurrentVersionInfo");
237 auto updateService = GetService();
238 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
239 return updateService->GetCurrentVersionInfo(info, currentVersionInfo, businessError);
240 }
241
GetCurrentVersionDescription(const UpgradeInfo & info,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & currentVersionDescriptionInfo,BusinessError & businessError)242 int32_t UpdateServiceKitsImpl::GetCurrentVersionDescription(const UpgradeInfo &info,
243 const DescriptionOptions &descriptionOptions, VersionDescriptionInfo ¤tVersionDescriptionInfo,
244 BusinessError &businessError)
245 {
246 ENGINE_LOGI("UpdateServiceKitsImpl::GetCurrentVersionDescription");
247 auto updateService = GetService();
248 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
249 return updateService->GetCurrentVersionDescription(info, descriptionOptions, currentVersionDescriptionInfo,
250 businessError);
251 }
252
GetTaskInfo(const UpgradeInfo & info,TaskInfo & taskInfo,BusinessError & businessError)253 int32_t UpdateServiceKitsImpl::GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo, BusinessError &businessError)
254 {
255 ENGINE_LOGI("UpdateServiceKitsImpl::GetTaskInfo");
256 auto updateService = GetService();
257 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
258 return updateService->GetTaskInfo(info, taskInfo, businessError);
259 }
260
SetUpgradePolicy(const UpgradeInfo & info,const UpgradePolicy & policy,BusinessError & businessError)261 int32_t UpdateServiceKitsImpl::SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy,
262 BusinessError &businessError)
263 {
264 ENGINE_LOGI("UpdateServiceKitsImpl::SetUpgradePolicy");
265 auto updateService = GetService();
266 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
267 return updateService->SetUpgradePolicy(info, policy, businessError);
268 }
269
GetUpgradePolicy(const UpgradeInfo & info,UpgradePolicy & policy,BusinessError & businessError)270 int32_t UpdateServiceKitsImpl::GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy,
271 BusinessError &businessError)
272 {
273 ENGINE_LOGI("UpdateServiceKitsImpl::GetUpgradePolicy");
274 auto updateService = GetService();
275 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
276 return updateService->GetUpgradePolicy(info, policy, businessError);
277 }
278
Cancel(const UpgradeInfo & info,int32_t service,BusinessError & businessError)279 int32_t UpdateServiceKitsImpl::Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError)
280 {
281 ENGINE_LOGI("UpdateServiceKitsImpl::Cancel %d", service);
282 auto updateService = GetService();
283 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
284 return updateService->Cancel(info, service, businessError);
285 }
286
FactoryReset(BusinessError & businessError)287 int32_t UpdateServiceKitsImpl::FactoryReset(BusinessError &businessError)
288 {
289 ENGINE_LOGI("UpdateServiceKitsImpl::FactoryReset");
290 auto updateService = GetService();
291 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
292 #ifndef UPDATER_API_TEST
293 return updateService->FactoryReset(businessError);
294 #endif
295 return INT_CALL_SUCCESS;
296 }
297
ApplyNewVersion(const UpgradeInfo & info,const std::string & miscFile,const std::string & packageName,BusinessError & businessError)298 int32_t UpdateServiceKitsImpl::ApplyNewVersion(const UpgradeInfo &info, const std::string &miscFile,
299 const std::string &packageName, BusinessError &businessError)
300 {
301 ENGINE_LOGI("UpdateServiceKitsImpl::ApplyNewVersion");
302 auto updateService = GetService();
303 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
304 return updateService->ApplyNewVersion(info, miscFile, packageName, businessError);
305 }
306
VerifyUpgradePackage(const std::string & packagePath,const std::string & keyPath,BusinessError & businessError)307 int32_t UpdateServiceKitsImpl::VerifyUpgradePackage(const std::string &packagePath, const std::string &keyPath,
308 BusinessError &businessError)
309 {
310 ENGINE_LOGI("UpdateServiceKitsImpl::VerifyUpgradePackage");
311 auto updateService = GetService();
312 RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
313 return updateService->VerifyUpgradePackage(packagePath, keyPath, businessError);
314 }
315 } // namespace UpdateEngine
316 } // namespace OHOS
317