1 /*
2 * Copyright (c) 2022 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 "sys_installer_kits_impl.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "log/log.h"
21 #include "securec.h"
22 #include "system_ability_definition.h"
23 #include "utils.h"
24
25 #include "isys_installer.h"
26 #include "isys_installer_callback.h"
27 #include "sys_installer_common.h"
28 #include "sys_installer_load_callback.h"
29 #include "sys_installer_proxy.h"
30
31 namespace OHOS {
32 namespace SysInstaller {
33 using namespace Updater;
34
35 constexpr int LOAD_SA_TIMEOUT_MS = 3000;
36
GetInstance()37 SysInstallerKitsImpl &SysInstallerKitsImpl::GetInstance()
38 {
39 static SysInstallerKitsImpl instance;
40 return instance;
41 }
42
ResetService(const wptr<IRemoteObject> & remote)43 void SysInstallerKitsImpl::ResetService(const wptr<IRemoteObject>& remote)
44 {
45 LOG(INFO) << "Remote is dead, reset service instance";
46
47 std::lock_guard<std::mutex> lock(sysInstallerLock_);
48 if (sysInstaller_ != nullptr) {
49 sptr<IRemoteObject> object = sysInstaller_->AsObject();
50 if ((object != nullptr) && (remote == object)) {
51 object->RemoveDeathRecipient(deathRecipient_);
52 sysInstaller_ = nullptr;
53 }
54 }
55 }
56
GetService()57 sptr<ISysInstaller> SysInstallerKitsImpl::GetService()
58 {
59 std::lock_guard<std::mutex> lock(sysInstallerLock_);
60 if (sysInstaller_ != nullptr) {
61 return sysInstaller_;
62 }
63
64 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65 if (samgr == nullptr) {
66 LOG(ERROR) << "Get samgr failed";
67 return nullptr;
68 }
69 sptr<IRemoteObject> object = samgr->GetSystemAbility(SYS_INSTALLER_DISTRIBUTED_SERVICE_ID);
70 if (object == nullptr) {
71 LOG(ERROR) << "Get update object from samgr failed";
72 return nullptr;
73 }
74
75 if (deathRecipient_ == nullptr) {
76 deathRecipient_ = new DeathRecipient();
77 }
78
79 if ((object->IsProxyObject()) && (!object->AddDeathRecipient(deathRecipient_))) {
80 LOG(ERROR) << "Failed to add death recipient";
81 }
82
83 LOG(INFO) << "get remote object ok";
84 sysInstaller_ = iface_cast<ISysInstaller>(object);
85 if (sysInstaller_ == nullptr) {
86 LOG(ERROR) << "account iface_cast failed";
87 }
88 return sysInstaller_;
89 }
90
OnRemoteDied(const wptr<IRemoteObject> & remote)91 void SysInstallerKitsImpl::DeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
92 {
93 SysInstallerKitsImpl::GetInstance().ResetService(remote);
94 }
95
Init()96 int32_t SysInstallerKitsImpl::Init()
97 {
98 std::lock_guard<std::mutex> lock(sysInstallerLock_);
99 if (sysInstaller_ != nullptr) {
100 LOG(INFO) << "already init";
101 return 0;
102 }
103 (void)Utils::MkdirRecursive(SYS_LOG_DIR, 0777); // 0777 : rwxrwxrwx
104 InitUpdaterLogger("SysInstallerClient", SYS_LOG_FILE, SYS_STAGE_FILE, SYS_ERROR_FILE);
105
106 // 构造步骤1的SystemAbilityLoadCallbackStub子类的实例
107 sptr<SysInstallerLoadCallback> loadCallback_ = new SysInstallerLoadCallback();
108 // 调用LoadSystemAbility方法
109 sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
110 if (sm == nullptr) {
111 LOG(ERROR) << "GetSystemAbilityManager samgr object null";
112 return -1;
113 }
114 int32_t result = sm->LoadSystemAbility(SYS_INSTALLER_DISTRIBUTED_SERVICE_ID, loadCallback_);
115 if (result != ERR_OK) {
116 LOG(ERROR) << "systemAbilityId " << SYS_INSTALLER_DISTRIBUTED_SERVICE_ID <<
117 " load failed, result code:" << result;
118 return -1;
119 }
120
121 std::unique_lock<std::mutex> callbackLock(getServiceMutex_);
122 getServiceCv_.wait_for(callbackLock, std::chrono::seconds(LOAD_SA_TIMEOUT_MS));
123 return 0;
124 }
125
SysInstallerInit()126 int32_t SysInstallerKitsImpl::SysInstallerInit()
127 {
128 LOG(INFO) << "SysInstallerInit";
129 int ret = Init();
130 if (ret != 0) {
131 LOG(ERROR) << "Init failed";
132 return ret;
133 }
134
135 auto updateService = GetService();
136 if (updateService == nullptr) {
137 LOG(ERROR) << "Get updateService failed";
138 return -1;
139 }
140 updateService->SysInstallerInit();
141 return 0;
142 }
143
StartUpdatePackageZip(const std::string & pkgPath)144 int32_t SysInstallerKitsImpl::StartUpdatePackageZip(const std::string &pkgPath)
145 {
146 LOG(INFO) << "StartUpdatePackageZip";
147 auto updateService = GetService();
148 if (updateService == nullptr) {
149 LOG(ERROR) << "Get updateService failed";
150 return false;
151 }
152 int32_t ret = updateService->StartUpdatePackageZip(pkgPath);
153 LOG(INFO) << "StartUpdatePackageZip ret:" << ret;
154 return ret;
155 }
156
SetUpdateCallback(const sptr<ISysInstallerCallback> & cb)157 int32_t SysInstallerKitsImpl::SetUpdateCallback(const sptr<ISysInstallerCallback> &cb)
158 {
159 LOG(INFO) << "SetUpdateCallback";
160 auto updateService = GetService();
161 if (updateService == nullptr) {
162 LOG(ERROR) << "Get updateService failed";
163 return -1;
164 }
165 updateCallBack_ = cb;
166 return updateService->SetUpdateCallback(cb);
167 }
168
GetUpdateStatus()169 int32_t SysInstallerKitsImpl::GetUpdateStatus()
170 {
171 LOG(INFO) << "GetUpdateStatus";
172 auto updateService = GetService();
173 if (updateService == nullptr) {
174 LOG(ERROR) << "Get updateService failed";
175 return -1;
176 }
177 return updateService->GetUpdateStatus();
178 }
179
StartUpdateParaZip(const std::string & pkgPath,const std::string & location,const std::string & cfgDir)180 int32_t SysInstallerKitsImpl::StartUpdateParaZip(const std::string &pkgPath,
181 const std::string &location, const std::string &cfgDir)
182 {
183 LOG(INFO) << "StartUpdateParaZip";
184 auto updateService = GetService();
185 if (updateService == nullptr) {
186 LOG(ERROR) << "Get updateService failed";
187 return false;
188 }
189 int32_t ret = updateService->StartUpdateParaZip(pkgPath, location, cfgDir);
190 LOG(INFO) << "StartUpdateParaZip ret:" << ret;
191 return ret;
192 }
193
LoadServiceSuccess()194 void SysInstallerKitsImpl::LoadServiceSuccess()
195 {
196 getServiceCv_.notify_all();
197 }
198
LoadServiceFail()199 void SysInstallerKitsImpl::LoadServiceFail()
200 {
201 getServiceCv_.notify_all();
202 }
203 }
204 } // namespace OHOS
205