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