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