• 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 "securec.h"
23 #include "system_ability_definition.h"
24 
25 namespace OHOS {
26 namespace update_engine {
GetInstance()27 UpdateServiceKits& UpdateServiceKits::GetInstance()
28 {
29     return DelayedRefSingleton<UpdateServiceKitsImpl>::GetInstance();
30 }
31 
UpdateServiceKitsImpl()32 UpdateServiceKitsImpl::UpdateServiceKitsImpl() {}
33 
~UpdateServiceKitsImpl()34 UpdateServiceKitsImpl::~UpdateServiceKitsImpl() {}
35 
ResetService(const wptr<IRemoteObject> & remote)36 void UpdateServiceKitsImpl::ResetService(const wptr<IRemoteObject>& remote)
37 {
38     ENGINE_LOGI("Remote is dead, reset service instance");
39 
40     std::lock_guard<std::mutex> lock(updateServiceLock_);
41     if (updateService_ != nullptr) {
42         sptr<IRemoteObject> object = updateService_->AsObject();
43         if ((object != nullptr) && (remote == object)) {
44             object->RemoveDeathRecipient(deathRecipient_);
45             updateService_ = nullptr;
46         }
47     }
48 }
49 
GetService()50 sptr<IUpdateService> UpdateServiceKitsImpl::GetService()
51 {
52     std::lock_guard<std::mutex> lock(updateServiceLock_);
53     if (updateService_ != nullptr) {
54         return updateService_;
55     }
56 
57     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58     ENGINE_CHECK(samgr != nullptr, return nullptr, "Get samgr failed");
59     sptr<IRemoteObject> object = samgr->GetSystemAbility(UPDATE_DISTRIBUTED_SERVICE_ID);
60     ENGINE_CHECK(object != nullptr, return nullptr, "Get update object from samgr failed");
61 
62     if (deathRecipient_ == nullptr) {
63         deathRecipient_ = new DeathRecipient();
64     }
65 
66     if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
67         ENGINE_LOGE("Failed to add death recipient");
68     }
69 
70     ENGINE_LOGI("get remote object ok");
71     updateService_ = iface_cast<IUpdateService>(object);
72     if (updateService_ == nullptr) {
73         ENGINE_LOGE("account iface_cast failed");
74     }
75     return updateService_;
76 }
77 
OnRemoteDied(const wptr<IRemoteObject> & remote)78 void UpdateServiceKitsImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
79 {
80     DelayedRefSingleton<UpdateServiceKitsImpl>::GetInstance().ResetService(remote);
81 }
82 
RemoteUpdateCallback(const UpdateCallbackInfo & cb)83 UpdateServiceKitsImpl::RemoteUpdateCallback::RemoteUpdateCallback(const UpdateCallbackInfo &cb)
84     : UpdateCallback()
85 {
86     updateCallback_.checkNewVersionDone = cb.checkNewVersionDone;
87     updateCallback_.downloadProgress = cb.downloadProgress;
88     updateCallback_.upgradeProgress = cb.upgradeProgress;
89 }
90 
~RemoteUpdateCallback()91 UpdateServiceKitsImpl::RemoteUpdateCallback::~RemoteUpdateCallback()
92 {
93     updateCallback_.checkNewVersionDone = nullptr;
94     updateCallback_.downloadProgress = nullptr;
95     updateCallback_.upgradeProgress = nullptr;
96 }
97 
OnCheckVersionDone(const VersionInfo & info)98 void UpdateServiceKitsImpl::RemoteUpdateCallback::OnCheckVersionDone(const VersionInfo &info)
99 {
100     ENGINE_LOGE("OnCheckVersionDone VersionInfo status %d", info.status);
101     ENGINE_LOGE("OnCheckVersionDone VersionInfo errMsg %s", info.errMsg.c_str());
102     ENGINE_LOGE("OnCheckVersionDone VersionInfo versionName : %s", info.result[0].versionName.c_str());
103     ENGINE_LOGE("OnCheckVersionDone VersionInfo versionCode : %s", info.result[0].versionCode.c_str());
104     ENGINE_LOGE("OnCheckVersionDone VersionInfo verifyInfo : %s", info.result[0].verifyInfo.c_str());
105     ENGINE_LOGE("OnCheckVersionDone VersionInfo size : %zu", info.result[0].size);
106     if (updateCallback_.checkNewVersionDone != nullptr) {
107         updateCallback_.checkNewVersionDone(info);
108     }
109 }
110 
OnDownloadProgress(const Progress & progress)111 void UpdateServiceKitsImpl::RemoteUpdateCallback::OnDownloadProgress(const Progress &progress)
112 {
113     ENGINE_LOGE("OnDownloadProgress progress %u %d", progress.percent, progress.status);
114     if (updateCallback_.downloadProgress != nullptr) {
115         updateCallback_.downloadProgress(progress);
116     }
117 }
118 
OnUpgradeProgress(const Progress & progress)119 void UpdateServiceKitsImpl::RemoteUpdateCallback::OnUpgradeProgress(const Progress &progress)
120 {
121     ENGINE_LOGE("OnUpgradeProgress progress %u %d", progress.percent, progress.status);
122     if (updateCallback_.upgradeProgress != nullptr) {
123         updateCallback_.upgradeProgress(progress);
124     }
125 }
126 
RegisterUpdateCallback(const UpdateContext & ctx,const UpdateCallbackInfo & cb)127 int32_t UpdateServiceKitsImpl::RegisterUpdateCallback(const UpdateContext &ctx, const UpdateCallbackInfo &cb)
128 {
129     updateContext_.upgradeDevId = ctx.upgradeDevId;
130     updateContext_.controlDevId = ctx.controlDevId;
131     updateContext_.upgradeApp = ctx.upgradeApp;
132     updateContext_.type = ctx.type;
133     updateContext_.upgradeFile = ctx.upgradeFile;
134     if (remoteUpdateCallback_ == nullptr) {
135         remoteUpdateCallback_ = new RemoteUpdateCallback(cb);
136         ENGINE_CHECK(remoteUpdateCallback_ != nullptr, return -1, "Failed to create remote callback");
137         auto updateService = GetService();
138         ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed");
139         updateService->RegisterUpdateCallback(ctx, remoteUpdateCallback_);
140     }
141     return 0;
142 }
143 
UnregisterUpdateCallback()144 int32_t UpdateServiceKitsImpl::UnregisterUpdateCallback()
145 {
146     delete remoteUpdateCallback_;
147     remoteUpdateCallback_ = nullptr;
148     return 0;
149 }
150 
CheckNewVersion()151 int32_t UpdateServiceKitsImpl::CheckNewVersion()
152 {
153     ENGINE_LOGI("UpdateServiceKitsImpl::CheckNewVersion");
154 
155     auto updateService = GetService();
156     ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed");
157     return updateService->CheckNewVersion();
158 }
159 
DownloadVersion()160 int32_t UpdateServiceKitsImpl::DownloadVersion()
161 {
162     ENGINE_LOGI("UpdateServiceKitsImpl::DownloadVersion");
163     auto updateService = GetService();
164     ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed");
165     return updateService->DownloadVersion();
166 }
167 
DoUpdate()168 int32_t UpdateServiceKitsImpl::DoUpdate()
169 {
170     ENGINE_LOGI("UpdateServiceKitsImpl::DoUpdate");
171     auto updateService = GetService();
172     ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed");
173     return updateService->DoUpdate();
174 }
175 
GetNewVersion(VersionInfo & versionInfo)176 int32_t UpdateServiceKitsImpl::GetNewVersion(VersionInfo &versionInfo)
177 {
178     ENGINE_LOGI("UpdateServiceKitsImpl::GetNewversion");
179     auto updateService = GetService();
180     ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed");
181     return updateService->GetNewVersion(versionInfo);
182 }
183 
GetUpgradeStatus(UpgradeInfo & info)184 int32_t UpdateServiceKitsImpl::GetUpgradeStatus(UpgradeInfo &info)
185 {
186     ENGINE_LOGI("UpdateServiceKitsImpl::GetUpgradeStatus");
187     auto updateService = GetService();
188     ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed");
189     return updateService->GetUpgradeStatus(info);
190 }
191 
SetUpdatePolicy(const UpdatePolicy & policy)192 int32_t UpdateServiceKitsImpl::SetUpdatePolicy(const UpdatePolicy &policy)
193 {
194     ENGINE_LOGI("UpdateServiceKitsImpl::SetUpdatePolicy");
195     auto updateService = GetService();
196     ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed");
197     return updateService->SetUpdatePolicy(policy);
198 }
199 
GetUpdatePolicy(UpdatePolicy & policy)200 int32_t UpdateServiceKitsImpl::GetUpdatePolicy(UpdatePolicy &policy)
201 {
202     ENGINE_LOGI("UpdateServiceKitsImpl::GetUpdatePolicy");
203     auto updateService = GetService();
204     ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed");
205     return updateService->GetUpdatePolicy(policy);
206 }
207 
Cancel(int32_t service)208 int32_t UpdateServiceKitsImpl::Cancel(int32_t service)
209 {
210     ENGINE_LOGI("UpdateServiceKitsImpl::Cancel %d", service);
211     auto updateService = GetService();
212     ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed");
213     return updateService->Cancel(service);
214 }
215 
RebootAndClean(const std::string & miscFile,const std::string & cmd)216 int32_t UpdateServiceKitsImpl::RebootAndClean(const std::string &miscFile, const std::string &cmd)
217 {
218     ENGINE_LOGI("UpdateServiceKitsImpl::RebootAndCleanUserData");
219     auto updateService = GetService();
220     ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed");
221 #ifndef UPDATER_API_TEST
222     return updateService->RebootAndClean(miscFile, cmd);
223 #endif
224     return 1;
225 }
226 
RebootAndInstall(const std::string & miscFile,const std::string & packageName)227 int32_t UpdateServiceKitsImpl::RebootAndInstall(const std::string &miscFile, const std::string &packageName)
228 {
229     ENGINE_LOGI("UpdateServiceKitsImpl::RebootAndInstall");
230     auto updateService = GetService();
231     ENGINE_CHECK(updateService != nullptr, return -1, "Get updateService failed");
232     return updateService->RebootAndInstall(miscFile, packageName);
233 }
234 }
235 } // namespace OHOS
236