1 /*
2  * Copyright (c) 2021-2024 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 "installd_client.h"
17 
18 #include "installd/installd_load_callback.h"
19 #include "installd_death_recipient.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include "system_ability_helper.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const int LOAD_SA_TIMEOUT_MS = 4 * 1000;
28 } // namespace
29 
CreateBundleDir(const std::string & bundleDir)30 ErrCode InstalldClient::CreateBundleDir(const std::string &bundleDir)
31 {
32     if (bundleDir.empty()) {
33         APP_LOGE("bundle dir is empty");
34         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
35     }
36 
37     return CallService(&IInstalld::CreateBundleDir, bundleDir);
38 }
39 
ExtractModuleFiles(const std::string & srcModulePath,const std::string & targetPath,const std::string & targetSoPath,const std::string & cpuAbi)40 ErrCode InstalldClient::ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath,
41     const std::string &targetSoPath, const std::string &cpuAbi)
42 {
43     if (srcModulePath.empty() || targetPath.empty()) {
44         APP_LOGE("src module path or target path is empty");
45         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
46     }
47 
48     return CallService(&IInstalld::ExtractModuleFiles, srcModulePath, targetPath, targetSoPath, cpuAbi);
49 }
50 
ExtractFiles(const ExtractParam & extractParam)51 ErrCode InstalldClient::ExtractFiles(const ExtractParam &extractParam)
52 {
53     if (extractParam.srcPath.empty() || extractParam.targetPath.empty()) {
54         APP_LOGE("src path or target path is empty");
55         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
56     }
57     return CallService(&IInstalld::ExtractFiles, extractParam);
58 }
59 
ExtractHnpFiles(const std::string & hnpPackageInfo,const ExtractParam & extractParam)60 ErrCode InstalldClient::ExtractHnpFiles(const std::string &hnpPackageInfo, const ExtractParam &extractParam)
61 {
62     if (extractParam.srcPath.empty() || extractParam.targetPath.empty() || hnpPackageInfo.empty()) {
63         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
64     }
65     return CallService(&IInstalld::ExtractHnpFiles, hnpPackageInfo, extractParam);
66 }
67 
ProcessBundleInstallNative(const std::string & userId,const std::string & hnpRootPath,const std::string & hapPath,const std::string & cpuAbi,const std::string & packageName)68 ErrCode InstalldClient::ProcessBundleInstallNative(const std::string &userId, const std::string &hnpRootPath,
69     const std::string &hapPath, const std::string &cpuAbi, const std::string &packageName)
70 {
71     return CallService(&IInstalld::ProcessBundleInstallNative, userId, hnpRootPath,
72         hapPath, cpuAbi, packageName);
73 }
74 
ProcessBundleUnInstallNative(const std::string & userId,const std::string & packageName)75 ErrCode InstalldClient::ProcessBundleUnInstallNative(const std::string &userId, const std::string &packageName)
76 {
77     return CallService(&IInstalld::ProcessBundleUnInstallNative, userId, packageName);
78 }
79 
ExecuteAOT(const AOTArgs & aotArgs,std::vector<uint8_t> & pendSignData)80 ErrCode InstalldClient::ExecuteAOT(const AOTArgs &aotArgs, std::vector<uint8_t> &pendSignData)
81 {
82     return CallService(&IInstalld::ExecuteAOT, aotArgs, pendSignData);
83 }
84 
PendSignAOT(const std::string & anFileName,const std::vector<uint8_t> & signData)85 ErrCode InstalldClient::PendSignAOT(const std::string &anFileName, const std::vector<uint8_t> &signData)
86 {
87     return CallService(&IInstalld::PendSignAOT, anFileName, signData);
88 }
89 
StopAOT()90 ErrCode InstalldClient::StopAOT()
91 {
92     return CallService(&IInstalld::StopAOT);
93 }
94 
DeleteUninstallTmpDirs(const std::vector<std::string> & dirs)95 ErrCode InstalldClient::DeleteUninstallTmpDirs(const std::vector<std::string> &dirs)
96 {
97     if (dirs.empty()) {
98         APP_LOGE("dirs empty");
99         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
100     }
101     return CallService(&IInstalld::DeleteUninstallTmpDirs, dirs);
102 }
103 
RenameModuleDir(const std::string & oldPath,const std::string & newPath)104 ErrCode InstalldClient::RenameModuleDir(const std::string &oldPath, const std::string &newPath)
105 {
106     if (oldPath.empty() || newPath.empty()) {
107         APP_LOGE("rename path is empty");
108         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
109     }
110 
111     return CallService(&IInstalld::RenameModuleDir, oldPath, newPath);
112 }
113 
CreateBundleDataDir(const CreateDirParam & createDirParam)114 ErrCode InstalldClient::CreateBundleDataDir(const CreateDirParam &createDirParam)
115 {
116     if (createDirParam.bundleName.empty() || createDirParam.userId < 0
117         || createDirParam.uid < 0 || createDirParam.gid < 0) {
118         APP_LOGE("params are invalid");
119         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
120     }
121 
122     return CallService(&IInstalld::CreateBundleDataDir, createDirParam);
123 }
124 
CreateBundleDataDirWithVector(const std::vector<CreateDirParam> & createDirParams)125 ErrCode InstalldClient::CreateBundleDataDirWithVector(const std::vector<CreateDirParam> &createDirParams)
126 {
127     return CallService(&IInstalld::CreateBundleDataDirWithVector, createDirParams);
128 }
129 
RemoveBundleDataDir(const std::string & bundleName,const int32_t userId,bool isAtomicService,const bool async)130 ErrCode InstalldClient::RemoveBundleDataDir(
131     const std::string &bundleName, const int32_t userId, bool isAtomicService, const bool async)
132 {
133     if (bundleName.empty() || userId < 0) {
134         APP_LOGE("params are invalid");
135         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
136     }
137 
138     return CallService(&IInstalld::RemoveBundleDataDir, bundleName, userId, isAtomicService, async);
139 }
140 
RemoveModuleDataDir(const std::string & ModuleName,const int userid)141 ErrCode InstalldClient::RemoveModuleDataDir(const std::string &ModuleName, const int userid)
142 {
143     if (ModuleName.empty() || userid < 0) {
144         APP_LOGE("params are invalid");
145         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
146     }
147 
148     return CallService(&IInstalld::RemoveModuleDataDir, ModuleName, userid);
149 }
150 
RemoveDir(const std::string & dir)151 ErrCode InstalldClient::RemoveDir(const std::string &dir)
152 {
153     if (dir.empty()) {
154         APP_LOGE("dir removed is empty");
155         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
156     }
157 
158     return CallService(&IInstalld::RemoveDir, dir);
159 }
160 
GetDiskUsage(const std::string & dir,bool isRealPath)161 int64_t InstalldClient::GetDiskUsage(const std::string &dir, bool isRealPath)
162 {
163     if (dir.empty()) {
164         APP_LOGE("bundle dir is empty");
165         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
166     }
167 
168     return CallService(&IInstalld::GetDiskUsage, dir, isRealPath);
169 }
170 
CleanBundleDataDir(const std::string & bundleDir)171 ErrCode InstalldClient::CleanBundleDataDir(const std::string &bundleDir)
172 {
173     if (bundleDir.empty()) {
174         APP_LOGE("bundle dir is empty");
175         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
176     }
177 
178     return CallService(&IInstalld::CleanBundleDataDir, bundleDir);
179 }
180 
CleanBundleDataDirByName(const std::string & bundleName,const int userid,const int appIndex)181 ErrCode InstalldClient::CleanBundleDataDirByName(const std::string &bundleName, const int userid, const int appIndex)
182 {
183     if (bundleName.empty() || userid < 0 || appIndex < 0 || appIndex > Constants::INITIAL_SANDBOX_APP_INDEX) {
184         APP_LOGE("params are invalid");
185         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
186     }
187 
188     return CallService(&IInstalld::CleanBundleDataDirByName, bundleName, userid, appIndex);
189 }
190 
GetBundleStats(const std::string & bundleName,const int32_t userId,std::vector<int64_t> & bundleStats,const int32_t uid,const int32_t appIndex,const uint32_t statFlag,const std::vector<std::string> & moduleNameList)191 ErrCode InstalldClient::GetBundleStats(const std::string &bundleName, const int32_t userId,
192     std::vector<int64_t> &bundleStats, const int32_t uid, const int32_t appIndex,
193     const uint32_t statFlag, const std::vector<std::string> &moduleNameList)
194 {
195     if (bundleName.empty()) {
196         APP_LOGE("bundleName is empty");
197         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
198     }
199 
200     return CallService(&IInstalld::GetBundleStats, bundleName, userId, bundleStats,
201         uid, appIndex, statFlag, moduleNameList);
202 }
203 
GetAllBundleStats(const int32_t userId,std::vector<int64_t> & bundleStats,const std::vector<int32_t> & uids)204 ErrCode InstalldClient::GetAllBundleStats(const int32_t userId,
205     std::vector<int64_t> &bundleStats, const std::vector<int32_t> &uids)
206 {
207     if (uids.empty()) {
208         APP_LOGE("uids is empty");
209         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
210     }
211 
212     return CallService(&IInstalld::GetAllBundleStats, userId, bundleStats, uids);
213 }
214 
SetDirApl(const std::string & dir,const std::string & bundleName,const std::string & apl,bool isPreInstallApp,bool debug)215 ErrCode InstalldClient::SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl,
216     bool isPreInstallApp, bool debug)
217 {
218     if (dir.empty() || bundleName.empty() || apl.empty()) {
219         APP_LOGE("params are invalid");
220         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
221     }
222 
223     return CallService(&IInstalld::SetDirApl, dir, bundleName, apl, isPreInstallApp, debug);
224 }
225 
GetBundleCachePath(const std::string & dir,std::vector<std::string> & cachePath)226 ErrCode InstalldClient::GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath)
227 {
228     if (dir.empty()) {
229         APP_LOGE("params are invalid");
230         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
231     }
232 
233     return CallService(&IInstalld::GetBundleCachePath, dir, cachePath);
234 }
235 
ResetInstalldProxy()236 void InstalldClient::ResetInstalldProxy()
237 {
238     std::lock_guard<std::mutex> lock(mutex_);
239     if ((installdProxy_ != nullptr) && (installdProxy_->AsObject() != nullptr)) {
240         installdProxy_->AsObject()->RemoveDeathRecipient(recipient_);
241     }
242     SystemAbilityHelper::UnloadSystemAbility(INSTALLD_SERVICE_ID);
243     installdProxy_ = nullptr;
244 }
245 
LoadInstalldService()246 bool InstalldClient::LoadInstalldService()
247 {
248     {
249         std::unique_lock<std::mutex> lock(loadSaMutex_);
250         loadSaFinished_ = false;
251     }
252     auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
253     if (systemAbilityMgr == nullptr) {
254         APP_LOGE("Failed to get SystemAbilityManager");
255         return false;
256     }
257     sptr<InstalldLoadCallback> loadCallback = new (std::nothrow) InstalldLoadCallback();
258     if (loadCallback == nullptr) {
259         APP_LOGE("Create load callback failed");
260         return false;
261     }
262     auto ret = systemAbilityMgr->LoadSystemAbility(INSTALLD_SERVICE_ID, loadCallback);
263     if (ret != 0) {
264         APP_LOGE("Load system ability %{public}d failed with %{public}d", INSTALLD_SERVICE_ID, ret);
265         return false;
266     }
267 
268     {
269         std::unique_lock<std::mutex> lock(loadSaMutex_);
270         auto waitStatus = loadSaCondition_.wait_for(lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
271             [this]() {
272                 return loadSaFinished_;
273             });
274         if (!waitStatus) {
275             APP_LOGE("Wait for load sa timeout");
276             return false;
277         }
278     }
279     return true;
280 }
281 
GetInstalldProxy()282 sptr<IInstalld> InstalldClient::GetInstalldProxy()
283 {
284     std::lock_guard<std::mutex> lock(getProxyMutex_);
285     if (installdProxy_ != nullptr) {
286         APP_LOGD("installd ready");
287         return installdProxy_;
288     }
289 
290     APP_LOGI("try to get installd proxy");
291     if (!LoadInstalldService()) {
292         APP_LOGE("load installd service failed");
293         return nullptr;
294     }
295     if ((installdProxy_ == nullptr) || (installdProxy_->AsObject() == nullptr)) {
296         APP_LOGE("the installd proxy or remote object is null");
297         return nullptr;
298     }
299 
300     recipient_ = new (std::nothrow) InstalldDeathRecipient();
301     if (recipient_ == nullptr) {
302         APP_LOGE("the death recipient is nullptr");
303         return nullptr;
304     }
305     installdProxy_->AsObject()->AddDeathRecipient(recipient_);
306     return installdProxy_;
307 }
308 
ScanDir(const std::string & dir,ScanMode scanMode,ResultMode resultMode,std::vector<std::string> & paths)309 ErrCode InstalldClient::ScanDir(
310     const std::string &dir, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths)
311 {
312     if (dir.empty()) {
313         APP_LOGE("params are invalid");
314         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
315     }
316 
317     return CallService(&IInstalld::ScanDir, dir, scanMode, resultMode, paths);
318 }
319 
MoveFile(const std::string & oldPath,const std::string & newPath)320 ErrCode InstalldClient::MoveFile(const std::string &oldPath, const std::string &newPath)
321 {
322     if (oldPath.empty() || newPath.empty()) {
323         APP_LOGE("params are invalid");
324         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
325     }
326 
327     return CallService(&IInstalld::MoveFile, oldPath, newPath);
328 }
329 
CopyFile(const std::string & oldPath,const std::string & newPath,const std::string & signatureFilePath)330 ErrCode InstalldClient::CopyFile(const std::string &oldPath, const std::string &newPath,
331     const std::string &signatureFilePath)
332 {
333     if (oldPath.empty() || newPath.empty()) {
334         APP_LOGE("params are invalid");
335         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
336     }
337 
338     return CallService(&IInstalld::CopyFile, oldPath, newPath, signatureFilePath);
339 }
340 
Mkdir(const std::string & dir,const int32_t mode,const int32_t uid,const int32_t gid)341 ErrCode InstalldClient::Mkdir(
342     const std::string &dir, const int32_t mode, const int32_t uid, const int32_t gid)
343 {
344     if (dir.empty()) {
345         APP_LOGE("params are invalid");
346         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
347     }
348 
349     return CallService(&IInstalld::Mkdir, dir, mode, uid, gid);
350 }
351 
GetFileStat(const std::string & file,FileStat & fileStat)352 ErrCode InstalldClient::GetFileStat(const std::string &file, FileStat &fileStat)
353 {
354     if (file.empty()) {
355         APP_LOGE("params are invalid");
356         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
357     }
358 
359     return CallService(&IInstalld::GetFileStat, file, fileStat);
360 }
361 
ExtractDiffFiles(const std::string & filePath,const std::string & targetPath,const std::string & cpuAbi)362 ErrCode InstalldClient::ExtractDiffFiles(const std::string &filePath, const std::string &targetPath,
363     const std::string &cpuAbi)
364 {
365     if (filePath.empty() || targetPath.empty() || cpuAbi.empty()) {
366         APP_LOGE("file path or target path or cpuAbi is empty");
367         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
368     }
369     return CallService(&IInstalld::ExtractDiffFiles, filePath, targetPath, cpuAbi);
370 }
371 
ApplyDiffPatch(const std::string & oldSoPath,const std::string & diffFilePath,const std::string & newSoPath,int32_t uid)372 ErrCode InstalldClient::ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath,
373     const std::string &newSoPath, int32_t uid)
374 {
375     if (oldSoPath.empty() || diffFilePath.empty() || newSoPath.empty()) {
376         APP_LOGE("old path or diff file path or new so path is empty");
377         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
378     }
379     return CallService(&IInstalld::ApplyDiffPatch, oldSoPath, diffFilePath, newSoPath, uid);
380 }
381 
IsExistDir(const std::string & dir,bool & isExist)382 ErrCode InstalldClient::IsExistDir(const std::string &dir, bool &isExist)
383 {
384     return CallService(&IInstalld::IsExistDir, dir, isExist);
385 }
386 
IsExistFile(const std::string & path,bool & isExist)387 ErrCode InstalldClient::IsExistFile(const std::string &path, bool &isExist)
388 {
389     return CallService(&IInstalld::IsExistFile, path, isExist);
390 }
391 
IsExistApFile(const std::string & path,bool & isExist)392 ErrCode InstalldClient::IsExistApFile(const std::string &path, bool &isExist)
393 {
394     return CallService(&IInstalld::IsExistApFile, path, isExist);
395 }
396 
IsDirEmpty(const std::string & dir,bool & isDirEmpty)397 ErrCode InstalldClient::IsDirEmpty(const std::string &dir, bool &isDirEmpty)
398 {
399     return CallService(&IInstalld::IsDirEmpty, dir, isDirEmpty);
400 }
401 
ObtainQuickFixFileDir(const std::string & dir,std::vector<std::string> & dirVec)402 ErrCode InstalldClient::ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &dirVec)
403 {
404     return CallService(&IInstalld::ObtainQuickFixFileDir, dir, dirVec);
405 }
406 
CopyFiles(const std::string & sourceDir,const std::string & destinationDir)407 ErrCode InstalldClient::CopyFiles(const std::string &sourceDir, const std::string &destinationDir)
408 {
409     return CallService(&IInstalld::CopyFiles, sourceDir, destinationDir);
410 }
411 
GetNativeLibraryFileNames(const std::string & filePath,const std::string & cpuAbi,std::vector<std::string> & fileNames)412 ErrCode InstalldClient::GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi,
413     std::vector<std::string> &fileNames)
414 {
415     return CallService(&IInstalld::GetNativeLibraryFileNames, filePath, cpuAbi, fileNames);
416 }
417 
VerifyCodeSignature(const CodeSignatureParam & codeSignatureParam)418 ErrCode InstalldClient::VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam)
419 {
420     if (codeSignatureParam.modulePath.empty()) {
421         APP_LOGE("module path is empty");
422         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
423     }
424     return CallService(&IInstalld::VerifyCodeSignature, codeSignatureParam);
425 }
426 
CheckEncryption(const CheckEncryptionParam & checkEncryptionParam,bool & isEncryption)427 ErrCode InstalldClient::CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption)
428 {
429     if (checkEncryptionParam.modulePath.empty()) {
430         APP_LOGE("module path is empty");
431         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
432     }
433     return CallService(&IInstalld::CheckEncryption, checkEncryptionParam, isEncryption);
434 }
435 
MoveFiles(const std::string & srcDir,const std::string & desDir)436 ErrCode InstalldClient::MoveFiles(const std::string &srcDir, const std::string &desDir)
437 {
438     if (srcDir.empty() || desDir.empty()) {
439         APP_LOGE("src dir or des dir is empty");
440         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
441     }
442     return CallService(&IInstalld::MoveFiles, srcDir, desDir);
443 }
444 
445 
ExtractDriverSoFiles(const std::string & srcPath,const std::unordered_multimap<std::string,std::string> & dirMap)446 ErrCode InstalldClient::ExtractDriverSoFiles(const std::string &srcPath,
447     const std::unordered_multimap<std::string, std::string> &dirMap)
448 {
449     if (srcPath.empty() || dirMap.empty()) {
450         APP_LOGE("src path or dir map is empty");
451         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
452     }
453     return CallService(&IInstalld::ExtractDriverSoFiles, srcPath, dirMap);
454 }
455 
VerifyCodeSignatureForHap(const CodeSignatureParam & codeSignatureParam)456 ErrCode InstalldClient::VerifyCodeSignatureForHap(const CodeSignatureParam &codeSignatureParam)
457 {
458     if (codeSignatureParam.modulePath.empty()) {
459         APP_LOGE("module path is empty");
460         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
461     }
462     return CallService(&IInstalld::VerifyCodeSignatureForHap, codeSignatureParam);
463 }
464 
DeliverySignProfile(const std::string & bundleName,int32_t profileBlockLength,const unsigned char * profileBlock)465 ErrCode InstalldClient::DeliverySignProfile(const std::string &bundleName, int32_t profileBlockLength,
466     const unsigned char *profileBlock)
467 {
468     if (bundleName.empty() || profileBlock == nullptr) {
469         APP_LOGE("bundle name or profile block is empty");
470         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
471     }
472     return CallService(&IInstalld::DeliverySignProfile, bundleName, profileBlockLength, profileBlock);
473 }
474 
RemoveSignProfile(const std::string & bundleName)475 ErrCode InstalldClient::RemoveSignProfile(const std::string &bundleName)
476 {
477     if (bundleName.empty()) {
478         APP_LOGE("bundle name is empty");
479         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
480     }
481     return CallService(&IInstalld::RemoveSignProfile, bundleName);
482 }
483 
OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> & remoteObject)484 void InstalldClient::OnLoadSystemAbilitySuccess(const sptr<IRemoteObject> &remoteObject)
485 {
486     {
487         std::lock_guard<std::mutex> lock(mutex_);
488         installdProxy_ = iface_cast<IInstalld>(remoteObject);
489     }
490 
491     {
492         std::lock_guard<std::mutex> lock(loadSaMutex_);
493         loadSaFinished_ = true;
494         loadSaCondition_.notify_one();
495     }
496 }
497 
OnLoadSystemAbilityFail()498 void InstalldClient::OnLoadSystemAbilityFail()
499 {
500     {
501         std::lock_guard<std::mutex> lock(mutex_);
502         installdProxy_ = nullptr;
503     }
504 
505     {
506         std::lock_guard<std::mutex> lock(loadSaMutex_);
507         loadSaFinished_ = true;
508         loadSaCondition_.notify_one();
509     }
510 }
511 
StartInstalldService()512 bool InstalldClient::StartInstalldService()
513 {
514     return GetInstalldProxy() != nullptr;
515 }
516 
ExtractEncryptedSoFiles(const std::string & hapPath,const std::string & realSoFilesPath,const std::string & cpuAbi,const std::string & tmpSoPath,int32_t uid)517 ErrCode InstalldClient::ExtractEncryptedSoFiles(const std::string &hapPath, const std::string &realSoFilesPath,
518     const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid)
519 {
520     if (hapPath.empty() || tmpSoPath.empty() || cpuAbi.empty()) {
521         APP_LOGE("params are invalid");
522         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
523     }
524     return CallService(&IInstalld::ExtractEncryptedSoFiles, hapPath, realSoFilesPath, cpuAbi, tmpSoPath, uid);
525 }
526 
SetEncryptionPolicy(int32_t uid,const std::string & bundleName,const int32_t userId,std::string & keyId)527 ErrCode InstalldClient::SetEncryptionPolicy(int32_t uid, const std::string &bundleName,
528     const int32_t userId, std::string &keyId)
529 {
530     if (bundleName.empty()) {
531         APP_LOGE("bundleName is empty");
532         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
533     }
534     return CallService(&IInstalld::SetEncryptionPolicy, uid, bundleName, userId, keyId);
535 }
536 
DeleteEncryptionKeyId(const std::string & bundleName,const int32_t userId)537 ErrCode InstalldClient::DeleteEncryptionKeyId(const std::string &bundleName, const int32_t userId)
538 {
539     if (bundleName.empty()) {
540         APP_LOGE("bundleName is empty");
541         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
542     }
543     return CallService(&IInstalld::DeleteEncryptionKeyId, bundleName, userId);
544 }
545 
RemoveExtensionDir(int32_t userId,const std::vector<std::string> & extensionBundleDirs)546 ErrCode InstalldClient::RemoveExtensionDir(int32_t userId, const std::vector<std::string> &extensionBundleDirs)
547 {
548     if (extensionBundleDirs.empty() || userId < 0) {
549         APP_LOGI("extensionBundleDirs empty or userId invalid");
550         return ERR_OK;
551     }
552     return CallService(&IInstalld::RemoveExtensionDir, userId, extensionBundleDirs);
553 }
554 
IsExistExtensionDir(int32_t userId,const std::string & extensionBundleDir,bool & isExist)555 ErrCode InstalldClient::IsExistExtensionDir(int32_t userId, const std::string &extensionBundleDir, bool &isExist)
556 {
557     if (extensionBundleDir.empty() || userId < 0) {
558         APP_LOGE("extensionBundleDir is empty or userId is invalid");
559         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
560     }
561     return CallService(&IInstalld::IsExistExtensionDir, userId, extensionBundleDir, isExist);
562 }
563 
CreateExtensionDataDir(const CreateDirParam & createDirParam)564 ErrCode InstalldClient::CreateExtensionDataDir(const CreateDirParam &createDirParam)
565 {
566     if (createDirParam.bundleName.empty() || createDirParam.userId < 0
567         || createDirParam.uid < 0 || createDirParam.gid < 0 || createDirParam.extensionDirs.empty()) {
568         APP_LOGE("params are invalid");
569         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
570     }
571 
572     return CallService(&IInstalld::CreateExtensionDataDir, createDirParam);
573 }
574 
GetExtensionSandboxTypeList(std::vector<std::string> & typeList)575 ErrCode InstalldClient::GetExtensionSandboxTypeList(std::vector<std::string> &typeList)
576 {
577     return CallService(&IInstalld::GetExtensionSandboxTypeList, typeList);
578 }
579 
MoveHapToCodeDir(const std::string & originPath,const std::string & targetPath)580 ErrCode InstalldClient::MoveHapToCodeDir(const std::string &originPath, const std::string &targetPath)
581 {
582     if (originPath.empty() || targetPath.empty()) {
583         APP_LOGE("params are invalid");
584         return ERR_APPEXECFWK_INSTALLD_PARAM_ERROR;
585     }
586 
587     return CallService(&IInstalld::MoveHapToCodeDir, originPath, targetPath);
588 }
589 }  // namespace AppExecFwk
590 }  // namespace OHOS
591