• 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::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::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     return ret;
51 }
52 
UnregisterUpdateCallback(const UpgradeInfo & info)53 int32_t UpdateServiceKitsImpl::UnregisterUpdateCallback(const UpgradeInfo &info)
54 {
55     auto updateService = GetService();
56     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
57 
58     ENGINE_LOGI("UnregisterUpdateCallback");
59     std::lock_guard<std::mutex> lock(remoteServerLock_);
60     remoteUpdateCallbackMap_.erase(info);
61     return updateService->UnregisterUpdateCallback(info);
62 }
63 
CheckNewVersion(const UpgradeInfo & info,BusinessError & businessError,CheckResult & checkResult)64 int32_t UpdateServiceKitsImpl::CheckNewVersion(const UpgradeInfo &info, BusinessError &businessError,
65     CheckResult &checkResult)
66 {
67     ENGINE_LOGI("UpdateServiceKitsImpl::CheckNewVersion");
68 
69     auto updateService = GetService();
70     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
71     return updateService->CheckNewVersion(info, businessError, checkResult);
72 }
73 
Download(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const DownloadOptions & downloadOptions,BusinessError & businessError)74 int32_t UpdateServiceKitsImpl::Download(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
75     const DownloadOptions &downloadOptions, BusinessError &businessError)
76 {
77     ENGINE_LOGI("UpdateServiceKitsImpl::Download");
78     auto updateService = GetService();
79     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
80     return updateService->Download(info, versionDigestInfo, downloadOptions, businessError);
81 }
82 
PauseDownload(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const PauseDownloadOptions & pauseDownloadOptions,BusinessError & businessError)83 int32_t UpdateServiceKitsImpl::PauseDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
84     const PauseDownloadOptions &pauseDownloadOptions, BusinessError &businessError)
85 {
86     ENGINE_LOGI("UpdateServiceKitsImpl::PauseDownload");
87     auto updateService = GetService();
88     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
89     return updateService->PauseDownload(info, versionDigestInfo, pauseDownloadOptions, businessError);
90 }
91 
ResumeDownload(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const ResumeDownloadOptions & resumeDownloadOptions,BusinessError & businessError)92 int32_t UpdateServiceKitsImpl::ResumeDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
93     const ResumeDownloadOptions &resumeDownloadOptions, BusinessError &businessError)
94 {
95     ENGINE_LOGI("UpdateServiceKitsImpl::ResumeDownload");
96     auto updateService = GetService();
97     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
98     return updateService->ResumeDownload(info, versionDigestInfo, resumeDownloadOptions, businessError);
99 }
100 
Upgrade(const UpgradeInfo & info,const VersionDigestInfo & versionDigest,const UpgradeOptions & upgradeOptions,BusinessError & businessError)101 int32_t UpdateServiceKitsImpl::Upgrade(const UpgradeInfo &info, const VersionDigestInfo &versionDigest,
102     const UpgradeOptions &upgradeOptions, BusinessError &businessError)
103 {
104     ENGINE_LOGI("UpdateServiceKitsImpl::Upgrade");
105     auto updateService = GetService();
106     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
107     return updateService->Upgrade(info, versionDigest, upgradeOptions, businessError);
108 }
109 
ClearError(const UpgradeInfo & info,const VersionDigestInfo & versionDigest,const ClearOptions & clearOptions,BusinessError & businessError)110 int32_t UpdateServiceKitsImpl::ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigest,
111     const ClearOptions &clearOptions, BusinessError &businessError)
112 {
113     ENGINE_LOGI("UpdateServiceKitsImpl::ClearError");
114     auto updateService = GetService();
115     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
116     return updateService->ClearError(info, versionDigest, clearOptions, businessError);
117 }
118 
TerminateUpgrade(const UpgradeInfo & info,BusinessError & businessError)119 int32_t UpdateServiceKitsImpl::TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError)
120 {
121     ENGINE_LOGI("UpdateServiceKitsImpl::TerminateUpgrade");
122     auto updateService = GetService();
123     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
124     return updateService->TerminateUpgrade(info, businessError);
125 }
126 
GetNewVersionInfo(const UpgradeInfo & info,NewVersionInfo & newVersionInfo,BusinessError & businessError)127 int32_t UpdateServiceKitsImpl::GetNewVersionInfo(const UpgradeInfo &info, NewVersionInfo &newVersionInfo,
128     BusinessError &businessError)
129 {
130     ENGINE_LOGI("UpdateServiceKitsImpl::GetNewVersionInfo");
131     auto updateService = GetService();
132     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
133     return updateService->GetNewVersionInfo(info, newVersionInfo, businessError);
134 }
135 
GetNewVersionDescription(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & newVersionDescriptionInfo,BusinessError & businessError)136 int32_t UpdateServiceKitsImpl::GetNewVersionDescription(const UpgradeInfo &info,
137     const VersionDigestInfo &versionDigestInfo, const DescriptionOptions &descriptionOptions,
138     VersionDescriptionInfo &newVersionDescriptionInfo, BusinessError &businessError)
139 {
140     ENGINE_LOGI("UpdateServiceKitsImpl::GetNewVersionDescription");
141     auto updateService = GetService();
142     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
143     return updateService->GetNewVersionDescription(info, versionDigestInfo, descriptionOptions,
144         newVersionDescriptionInfo, businessError);
145 }
146 
GetCurrentVersionInfo(const UpgradeInfo & info,CurrentVersionInfo & currentVersionInfo,BusinessError & businessError)147 int32_t UpdateServiceKitsImpl::GetCurrentVersionInfo(const UpgradeInfo &info, CurrentVersionInfo &currentVersionInfo,
148     BusinessError &businessError)
149 {
150     ENGINE_LOGI("UpdateServiceKitsImpl::GetCurrentVersionInfo");
151     auto updateService = GetService();
152     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
153     return updateService->GetCurrentVersionInfo(info, currentVersionInfo, businessError);
154 }
155 
GetCurrentVersionDescription(const UpgradeInfo & info,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & currentVersionDescriptionInfo,BusinessError & businessError)156 int32_t UpdateServiceKitsImpl::GetCurrentVersionDescription(const UpgradeInfo &info,
157     const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &currentVersionDescriptionInfo,
158     BusinessError &businessError)
159 {
160     ENGINE_LOGI("UpdateServiceKitsImpl::GetCurrentVersionDescription");
161     auto updateService = GetService();
162     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
163     return updateService->GetCurrentVersionDescription(info, descriptionOptions, currentVersionDescriptionInfo,
164         businessError);
165 }
166 
GetTaskInfo(const UpgradeInfo & info,TaskInfo & taskInfo,BusinessError & businessError)167 int32_t UpdateServiceKitsImpl::GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo, BusinessError &businessError)
168 {
169     ENGINE_LOGI("UpdateServiceKitsImpl::GetTaskInfo");
170     auto updateService = GetService();
171     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
172     return updateService->GetTaskInfo(info, taskInfo, businessError);
173 }
174 
SetUpgradePolicy(const UpgradeInfo & info,const UpgradePolicy & policy,BusinessError & businessError)175 int32_t UpdateServiceKitsImpl::SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy,
176     BusinessError &businessError)
177 {
178     ENGINE_LOGI("UpdateServiceKitsImpl::SetUpgradePolicy");
179     auto updateService = GetService();
180     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
181     return updateService->SetUpgradePolicy(info, policy, businessError);
182 }
183 
GetUpgradePolicy(const UpgradeInfo & info,UpgradePolicy & policy,BusinessError & businessError)184 int32_t UpdateServiceKitsImpl::GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy,
185     BusinessError &businessError)
186 {
187     ENGINE_LOGI("UpdateServiceKitsImpl::GetUpgradePolicy");
188     auto updateService = GetService();
189     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
190     return updateService->GetUpgradePolicy(info, policy, businessError);
191 }
192 
Cancel(const UpgradeInfo & info,int32_t service,BusinessError & businessError)193 int32_t UpdateServiceKitsImpl::Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError)
194 {
195     ENGINE_LOGI("UpdateServiceKitsImpl::Cancel %d", service);
196     auto updateService = GetService();
197     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
198     return updateService->Cancel(info, service, businessError);
199 }
200 
FactoryReset(BusinessError & businessError)201 int32_t UpdateServiceKitsImpl::FactoryReset(BusinessError &businessError)
202 {
203     ENGINE_LOGI("UpdateServiceKitsImpl::FactoryReset");
204     auto updateService = GetService();
205     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
206     return updateService->FactoryReset(businessError);
207 }
208 
ApplyNewVersion(const UpgradeInfo & info,const std::string & miscFile,const std::string & packageName,BusinessError & businessError)209 int32_t UpdateServiceKitsImpl::ApplyNewVersion(const UpgradeInfo &info, const std::string &miscFile,
210     const std::string &packageName, BusinessError &businessError)
211 {
212     ENGINE_LOGI("UpdateServiceKitsImpl::ApplyNewVersion");
213     auto updateService = GetService();
214     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
215     return updateService->ApplyNewVersion(info, miscFile, packageName, businessError);
216 }
217 
VerifyUpgradePackage(const std::string & packagePath,const std::string & keyPath,BusinessError & businessError)218 int32_t UpdateServiceKitsImpl::VerifyUpgradePackage(const std::string &packagePath, const std::string &keyPath,
219     BusinessError &businessError)
220 {
221     ENGINE_LOGI("UpdateServiceKitsImpl::VerifyUpgradePackage");
222     auto updateService = GetService();
223     RETURN_FAIL_WHEN_SERVICE_NULL(updateService);
224     return updateService->VerifyUpgradePackage(packagePath, keyPath, businessError);
225 }
226 
RegisterCallback()227 void UpdateServiceKitsImpl::RegisterCallback()
228 {
229     ENGINE_LOGI("RegisterUpdateCallback size %{public}zu", remoteUpdateCallbackMap_.size());
230     for (auto &iter : remoteUpdateCallbackMap_) {
231         remoteServer_->RegisterUpdateCallback(iter.first, iter.second);
232     }
233 }
234 } // namespace OHOS::UpdateEngine
235