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 "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 
27 namespace OHOS::UpdateEngine {
GetInstance()28 UpdateServiceKits &UpdateServiceKits::GetInstance()
29 {
30     return DelayedRefSingleton<UpdateServiceKitsImpl>::GetInstance();
31 }
32 
UpdateServiceKitsImpl()33 UpdateServiceKitsImpl::UpdateServiceKitsImpl() : BaseServiceKitsImpl<IUpdateService>(UPDATE_DISTRIBUTED_SERVICE_ID) {}
34 
35 UpdateServiceKitsImpl::~UpdateServiceKitsImpl() = default;
36 
RegisterUpdateCallback(const UpgradeInfo & info,const UpdateCallbackInfo & cb)37 int32_t UpdateServiceKitsImpl::RegisterUpdateCallback(const UpgradeInfo &info, const UpdateCallbackInfo &cb)
38 {
39     auto updateService = GetService();
40     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
41 
42     std::lock_guard<std::recursive_mutex> lock(remoteServerLock_);
43 
44     // 以下代码中sptr<IUpdateCallback>不能修改为auto,否则在重注册时有概率出现Crash
45     sptr<IUpdateCallback> remoteUpdateCallback(new UpdateCallback(cb));
46     ENGINE_CHECK(remoteUpdateCallback != nullptr, return INT_PARAM_ERR, "Failed to create remote callback");
47     int32_t ret = updateService->RegisterUpdateCallback(info, remoteUpdateCallback);
48     remoteUpdateCallbackMap_[info] = remoteUpdateCallback;
49     ENGINE_LOGI("RegisterUpdateCallback %{public}s", ret == INT_CALL_SUCCESS ? "success" : "failure");
50     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "RegisterUpdateCallback ipc error");
51     return ret;
52 }
53 
UnregisterUpdateCallback(const UpgradeInfo & info)54 int32_t UpdateServiceKitsImpl::UnregisterUpdateCallback(const UpgradeInfo &info)
55 {
56     auto updateService = GetService();
57     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
58 
59     ENGINE_LOGI("UnregisterUpdateCallback");
60     std::lock_guard<std::recursive_mutex> lock(remoteServerLock_);
61     remoteUpdateCallbackMap_.erase(info);
62     int32_t ret = updateService->UnregisterUpdateCallback(info);
63     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "UnregisterUpdateCallback ipc error");
64     return ret;
65 }
66 
CheckNewVersion(const UpgradeInfo & info,BusinessError & businessError,CheckResult & checkResult)67 int32_t UpdateServiceKitsImpl::CheckNewVersion(const UpgradeInfo &info, BusinessError &businessError,
68     CheckResult &checkResult)
69 {
70     ENGINE_LOGI("UpdateServiceKitsImpl::CheckNewVersion");
71 
72     auto updateService = GetService();
73     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
74     int32_t ret = updateService->CheckNewVersion(info, businessError, checkResult);
75     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "CheckNewVersion ipc error");
76     return ret;
77 }
78 
Download(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const DownloadOptions & downloadOptions,BusinessError & businessError)79 int32_t UpdateServiceKitsImpl::Download(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
80     const DownloadOptions &downloadOptions, BusinessError &businessError)
81 {
82     ENGINE_LOGI("UpdateServiceKitsImpl::Download");
83     auto updateService = GetService();
84     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
85     int32_t ret = updateService->Download(info, versionDigestInfo, downloadOptions, businessError);
86     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "Download ipc error");
87     return ret;
88 }
89 
PauseDownload(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const PauseDownloadOptions & pauseDownloadOptions,BusinessError & businessError)90 int32_t UpdateServiceKitsImpl::PauseDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
91     const PauseDownloadOptions &pauseDownloadOptions, BusinessError &businessError)
92 {
93     ENGINE_LOGI("UpdateServiceKitsImpl::PauseDownload");
94     auto updateService = GetService();
95     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
96     int32_t ret = updateService->PauseDownload(info, versionDigestInfo, pauseDownloadOptions, businessError);
97     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "PauseDownload ipc error");
98     return ret;
99 }
100 
ResumeDownload(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const ResumeDownloadOptions & resumeDownloadOptions,BusinessError & businessError)101 int32_t UpdateServiceKitsImpl::ResumeDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
102     const ResumeDownloadOptions &resumeDownloadOptions, BusinessError &businessError)
103 {
104     ENGINE_LOGI("UpdateServiceKitsImpl::ResumeDownload");
105     auto updateService = GetService();
106     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
107     int32_t ret = updateService->ResumeDownload(info, versionDigestInfo, resumeDownloadOptions, businessError);
108     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "ResumeDownload ipc error");
109     return ret;
110 }
111 
Upgrade(const UpgradeInfo & info,const VersionDigestInfo & versionDigest,const UpgradeOptions & upgradeOptions,BusinessError & businessError)112 int32_t UpdateServiceKitsImpl::Upgrade(const UpgradeInfo &info, const VersionDigestInfo &versionDigest,
113     const UpgradeOptions &upgradeOptions, BusinessError &businessError)
114 {
115     ENGINE_LOGI("UpdateServiceKitsImpl::Upgrade");
116     auto updateService = GetService();
117     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
118     int32_t ret = updateService->Upgrade(info, versionDigest, upgradeOptions, businessError);
119     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "Upgrade ipc error");
120     return ret;
121 }
122 
ClearError(const UpgradeInfo & info,const VersionDigestInfo & versionDigest,const ClearOptions & clearOptions,BusinessError & businessError)123 int32_t UpdateServiceKitsImpl::ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigest,
124     const ClearOptions &clearOptions, BusinessError &businessError)
125 {
126     ENGINE_LOGI("UpdateServiceKitsImpl::ClearError");
127     auto updateService = GetService();
128     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
129     int32_t ret = updateService->ClearError(info, versionDigest, clearOptions, businessError);
130     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "ClearError ipc error");
131     return ret;
132 }
133 
TerminateUpgrade(const UpgradeInfo & info,BusinessError & businessError)134 int32_t UpdateServiceKitsImpl::TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError)
135 {
136     ENGINE_LOGI("UpdateServiceKitsImpl::TerminateUpgrade");
137     auto updateService = GetService();
138     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
139     int32_t ret = updateService->TerminateUpgrade(info, businessError);
140     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "TerminateUpgrade ipc error");
141     return ret;
142 }
143 
GetNewVersionInfo(const UpgradeInfo & info,NewVersionInfo & newVersionInfo,BusinessError & businessError)144 int32_t UpdateServiceKitsImpl::GetNewVersionInfo(const UpgradeInfo &info, NewVersionInfo &newVersionInfo,
145     BusinessError &businessError)
146 {
147     ENGINE_LOGI("UpdateServiceKitsImpl::GetNewVersionInfo");
148     auto updateService = GetService();
149     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
150     int32_t ret = updateService->GetNewVersionInfo(info, newVersionInfo, businessError);
151     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "GetNewVersionInfo ipc error");
152     return ret;
153 }
154 
GetNewVersionDescription(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & newVersionDescriptionInfo,BusinessError & businessError)155 int32_t UpdateServiceKitsImpl::GetNewVersionDescription(const UpgradeInfo &info,
156     const VersionDigestInfo &versionDigestInfo, const DescriptionOptions &descriptionOptions,
157     VersionDescriptionInfo &newVersionDescriptionInfo, BusinessError &businessError)
158 {
159     ENGINE_LOGI("UpdateServiceKitsImpl::GetNewVersionDescription");
160     auto updateService = GetService();
161     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
162     int32_t ret = updateService->GetNewVersionDescription(info, versionDigestInfo, descriptionOptions,
163         newVersionDescriptionInfo, businessError);
164     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "GetNewVersionDescription ipc error");
165     return ret;
166 }
167 
GetCurrentVersionInfo(const UpgradeInfo & info,CurrentVersionInfo & currentVersionInfo,BusinessError & businessError)168 int32_t UpdateServiceKitsImpl::GetCurrentVersionInfo(const UpgradeInfo &info, CurrentVersionInfo &currentVersionInfo,
169     BusinessError &businessError)
170 {
171     ENGINE_LOGI("UpdateServiceKitsImpl::GetCurrentVersionInfo");
172     auto updateService = GetService();
173     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
174     int32_t ret = updateService->GetCurrentVersionInfo(info, currentVersionInfo, businessError);
175     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "GetCurrentVersionInfo ipc error");
176     return ret;
177 }
178 
GetCurrentVersionDescription(const UpgradeInfo & info,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & currentVersionDescriptionInfo,BusinessError & businessError)179 int32_t UpdateServiceKitsImpl::GetCurrentVersionDescription(const UpgradeInfo &info,
180     const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &currentVersionDescriptionInfo,
181     BusinessError &businessError)
182 {
183     ENGINE_LOGI("UpdateServiceKitsImpl::GetCurrentVersionDescription");
184     auto updateService = GetService();
185     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
186     int32_t ret = updateService->GetCurrentVersionDescription(info, descriptionOptions, currentVersionDescriptionInfo,
187         businessError);
188     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "GetCurrentVersionDescription ipc error");
189     return ret;
190 }
191 
GetTaskInfo(const UpgradeInfo & info,TaskInfo & taskInfo,BusinessError & businessError)192 int32_t UpdateServiceKitsImpl::GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo, BusinessError &businessError)
193 {
194     ENGINE_LOGI("UpdateServiceKitsImpl::GetTaskInfo");
195     auto updateService = GetService();
196     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
197     int32_t ret = updateService->GetTaskInfo(info, taskInfo, businessError);
198     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "GetTaskInfo ipc error");
199     return ret;
200 }
201 
SetUpgradePolicy(const UpgradeInfo & info,const UpgradePolicy & policy,BusinessError & businessError)202 int32_t UpdateServiceKitsImpl::SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy,
203     BusinessError &businessError)
204 {
205     ENGINE_LOGI("UpdateServiceKitsImpl::SetUpgradePolicy");
206     auto updateService = GetService();
207     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
208     int32_t ret = updateService->SetUpgradePolicy(info, policy, businessError);
209     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "SetUpgradePolicy ipc error");
210     return ret;
211 }
212 
GetUpgradePolicy(const UpgradeInfo & info,UpgradePolicy & policy,BusinessError & businessError)213 int32_t UpdateServiceKitsImpl::GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy,
214     BusinessError &businessError)
215 {
216     ENGINE_LOGI("UpdateServiceKitsImpl::GetUpgradePolicy");
217     auto updateService = GetService();
218     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
219     int32_t ret = updateService->GetUpgradePolicy(info, policy, businessError);
220     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "GetUpgradePolicy ipc error");
221     return ret;
222 }
223 
Cancel(const UpgradeInfo & info,int32_t service,BusinessError & businessError)224 int32_t UpdateServiceKitsImpl::Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError)
225 {
226     ENGINE_LOGI("UpdateServiceKitsImpl::Cancel %d", service);
227     auto updateService = GetService();
228     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
229     int32_t ret = updateService->Cancel(info, service, businessError);
230     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "Cancel ipc error");
231     return ret;
232 }
233 
FactoryReset(BusinessError & businessError)234 int32_t UpdateServiceKitsImpl::FactoryReset(BusinessError &businessError)
235 {
236     ENGINE_LOGI("UpdateServiceKitsImpl::FactoryReset");
237     auto updateService = GetService();
238     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
239     int32_t ret = updateService->FactoryReset(businessError);
240     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "FactoryReset ipc error");
241     return ret;
242 }
243 
ApplyNewVersion(const UpgradeInfo & info,const std::string & miscFile,const std::vector<std::string> & packageNames,BusinessError & businessError)244 int32_t UpdateServiceKitsImpl::ApplyNewVersion(const UpgradeInfo &info, const std::string &miscFile,
245     const std::vector<std::string> &packageNames, BusinessError &businessError)
246 {
247     ENGINE_LOGI("UpdateServiceKitsImpl::ApplyNewVersion");
248     auto updateService = GetService();
249     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
250     int32_t ret = updateService->ApplyNewVersion(info, miscFile, packageNames, businessError);
251     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "ApplyNewVersion ipc error");
252     return ret;
253 }
254 
VerifyUpgradePackage(const std::string & packagePath,const std::string & keyPath,BusinessError & businessError)255 int32_t UpdateServiceKitsImpl::VerifyUpgradePackage(const std::string &packagePath, const std::string &keyPath,
256     BusinessError &businessError)
257 {
258     ENGINE_LOGI("UpdateServiceKitsImpl::VerifyUpgradePackage");
259     auto updateService = GetService();
260     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
261     int32_t ret = updateService->VerifyUpgradePackage(packagePath, keyPath, businessError);
262     ENGINE_CHECK((ret) == INT_CALL_SUCCESS, ResetRemoteService(), "VerifyUpgradePackage ipc error");
263     return ret;
264 }
265 
RegisterCallback()266 void UpdateServiceKitsImpl::RegisterCallback()
267 {
268     ENGINE_LOGI("RegisterUpdateCallback size %{public}zu", remoteUpdateCallbackMap_.size());
269     for (auto &iter : remoteUpdateCallbackMap_) {
270         remoteServer_->RegisterUpdateCallback(iter.first, iter.second);
271     }
272 }
273 
ResetService(const wptr<IRemoteObject> & remote)274 void UpdateServiceKitsImpl::ResetService(const wptr<IRemoteObject> &remote)
275 {
276     BaseServiceKitsImpl::ResetService(remote);
277     constexpr int32_t retryMaxTimes = 3;
278     ENGINE_LOGI("ResetService, remoteUpdateCallbackMap_: %{public}zu, retryTimes_: %{public}d",
279         remoteUpdateCallbackMap_.size(), retryTimes_);
280     if (!remoteUpdateCallbackMap_.empty() && retryTimes_ < retryMaxTimes) {
281         ENGINE_LOGI("ResetService, need resume register callback");
282         auto updateService = GetService(); // 重新连接注册回调
283         retryTimes_++;
284         ENGINE_LOGI("ResetService, reconnect service %{public}s", (updateService != nullptr) ? "success" : "fail");
285     }
286 }
287 } // namespace OHOS::UpdateEngine
288