• 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 "installd_client.h"
17 
18 #include "app_log_wrapper.h"
19 #include "bundle_constants.h"
20 #include "installd_death_recipient.h"
21 #include "system_ability_definition.h"
22 #include "system_ability_helper.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 
CreateBundleDir(const std::string & bundleDir)27 ErrCode InstalldClient::CreateBundleDir(const std::string &bundleDir)
28 {
29     if (!GetInstalldProxy()) {
30         return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR;
31     }
32     return installdProxy_->CreateBundleDir(bundleDir);
33 }
34 
RemoveBundleDir(const std::string & bundleDir)35 ErrCode InstalldClient::RemoveBundleDir(const std::string &bundleDir)
36 {
37     if (!GetInstalldProxy()) {
38         return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR;
39     }
40     return installdProxy_->RemoveBundleDir(bundleDir);
41 }
42 
RemoveBundleDataDir(const std::string & bundleDataDir)43 ErrCode InstalldClient::RemoveBundleDataDir(const std::string &bundleDataDir)
44 {
45     if (!GetInstalldProxy()) {
46         return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR;
47     }
48     return installdProxy_->RemoveBundleDir(bundleDataDir);
49 }
50 
ExtractModuleFiles(const std::string & srcModulePath,const std::string & targetPath)51 ErrCode InstalldClient::ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath)
52 {
53     if (!GetInstalldProxy()) {
54         return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR;
55     }
56     return installdProxy_->ExtractModuleFiles(srcModulePath, targetPath);
57 }
58 
RemoveModuleDir(const std::string & moduleDir)59 ErrCode InstalldClient::RemoveModuleDir(const std::string &moduleDir)
60 {
61     if (!GetInstalldProxy()) {
62         return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR;
63     }
64     return installdProxy_->RemoveModuleDir(moduleDir);
65 }
66 
RenameModuleDir(const std::string & oldPath,const std::string & newPath)67 ErrCode InstalldClient::RenameModuleDir(const std::string &oldPath, const std::string &newPath)
68 {
69     if (!GetInstalldProxy()) {
70         return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR;
71     }
72     return installdProxy_->RenameModuleDir(oldPath, newPath);
73 }
74 
CreateBundleDataDir(const std::string & bundleDir,const int uid,const int gid)75 ErrCode InstalldClient::CreateBundleDataDir(const std::string &bundleDir, const int uid, const int gid)
76 {
77     if (!GetInstalldProxy()) {
78         return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR;
79     }
80     return installdProxy_->CreateBundleDataDir(bundleDir, uid, gid);
81 }
82 
CreateModuleDataDir(const std::string & ModuleDir,const std::vector<std::string> & abilityDirs,const int uid,const int gid)83 ErrCode InstalldClient::CreateModuleDataDir(
84     const std::string &ModuleDir, const std::vector<std::string> &abilityDirs, const int uid, const int gid)
85 {
86     if (!GetInstalldProxy()) {
87         return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR;
88     }
89     return installdProxy_->CreateModuleDataDir(ModuleDir, abilityDirs, uid, gid);
90 }
91 
RemoveModuleDataDir(const std::string & moduleDataDir)92 ErrCode InstalldClient::RemoveModuleDataDir(const std::string &moduleDataDir)
93 {
94     if (!GetInstalldProxy()) {
95         return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR;
96     }
97     return installdProxy_->RemoveModuleDataDir(moduleDataDir);
98 }
99 
CleanBundleDataDir(const std::string & bundleDir)100 ErrCode InstalldClient::CleanBundleDataDir(const std::string &bundleDir)
101 {
102     if (!GetInstalldProxy()) {
103         return ERR_APPEXECFWK_INSTALLD_GET_PROXY_ERROR;
104     }
105     return installdProxy_->CleanBundleDataDir(bundleDir);
106 }
107 
ResetInstalldProxy()108 void InstalldClient::ResetInstalldProxy()
109 {
110     if ((installdProxy_ != nullptr) && (installdProxy_->AsObject() != nullptr)) {
111         installdProxy_->AsObject()->RemoveDeathRecipient(recipient_);
112     }
113     installdProxy_ = nullptr;
114 }
115 
GetInstalldProxy()116 bool InstalldClient::GetInstalldProxy()
117 {
118     if (!installdProxy_) {
119         APP_LOGD("try to get installd proxy");
120         std::lock_guard<std::mutex> lock(mutex_);
121         if (!installdProxy_) {
122             sptr<IInstalld> tempProxy =
123                 iface_cast<IInstalld>(SystemAbilityHelper::GetSystemAbility(INSTALLD_SERVICE_ID));
124             if ((!tempProxy) || (!tempProxy->AsObject())) {
125                 APP_LOGE("the installd proxy or remote object is null");
126                 return false;
127             }
128             recipient_ = new (std::nothrow) InstalldDeathRecipient();
129             tempProxy->AsObject()->AddDeathRecipient(recipient_);
130             installdProxy_ = tempProxy;
131         }
132     }
133     return true;
134 }
135 
136 }  // namespace AppExecFwk
137 }  // namespace OHOS
138