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 "ipc/installd_proxy.h"
17
18 #include "ipc_types.h"
19
20 #include "app_log_wrapper.h"
21 #include "bundle_constants.h"
22 #include "parcel_macro.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26
InstalldProxy(const sptr<IRemoteObject> & object)27 InstalldProxy::InstalldProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IInstalld>(object)
28 {
29 APP_LOGI("installd proxy instance is created");
30 }
31
~InstalldProxy()32 InstalldProxy::~InstalldProxy()
33 {
34 APP_LOGI("installd proxy instance is destroyed");
35 }
36
CreateBundleDir(const std::string & bundleDir)37 ErrCode InstalldProxy::CreateBundleDir(const std::string &bundleDir)
38 {
39 MessageParcel data;
40 MessageParcel reply;
41 if (!data.WriteInterfaceToken(GetDescriptor())) {
42 APP_LOGE("fail to send cmd to service due to data.WriteInterfaceToken(GetDescriptor())");
43 return false;
44 }
45 if (!data.WriteString(bundleDir)) {
46 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
47 }
48 if (!TransactInstalldCmd(IInstalld::Message::CREATE_BUNDLE_DIR, data, reply)) {
49 return false;
50 }
51 return reply.ReadInt32();
52 }
53
RemoveBundleDir(const std::string & bundleDir)54 ErrCode InstalldProxy::RemoveBundleDir(const std::string &bundleDir)
55 {
56 MessageParcel data;
57 MessageParcel reply;
58 if (!data.WriteInterfaceToken(GetDescriptor())) {
59 APP_LOGE("fail to send cmd to service due to data.WriteInterfaceToken(GetDescriptor())");
60 return false;
61 }
62 if (!data.WriteString(bundleDir)) {
63 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
64 }
65 if (!TransactInstalldCmd(IInstalld::Message::REMOVE_BUNDLE_DIR, data, reply)) {
66 return false;
67 }
68 return reply.ReadInt32();
69 }
70
ExtractModuleFiles(const std::string & srcModulePath,const std::string & targetPath)71 ErrCode InstalldProxy::ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath)
72 {
73 MessageParcel data;
74 MessageParcel reply;
75 if (!data.WriteInterfaceToken(GetDescriptor())) {
76 APP_LOGE("fail to send cmd to service due to data.WriteInterfaceToken(GetDescriptor())");
77 return false;
78 }
79 if (!data.WriteString(srcModulePath)) {
80 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
81 }
82 if (!data.WriteString(targetPath)) {
83 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
84 }
85 if (!TransactInstalldCmd(IInstalld::Message::EXTRACT_MODULE_FILES, data, reply)) {
86 return false;
87 }
88 return reply.ReadInt32();
89 }
90
RemoveModuleDir(const std::string & moduleDir)91 ErrCode InstalldProxy::RemoveModuleDir(const std::string &moduleDir)
92 {
93 MessageParcel data;
94 MessageParcel reply;
95 if (!data.WriteInterfaceToken(GetDescriptor())) {
96 APP_LOGE("fail to send cmd to service due to data.WriteInterfaceToken(GetDescriptor())");
97 return false;
98 }
99 if (!data.WriteString(moduleDir)) {
100 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
101 }
102 if (!TransactInstalldCmd(IInstalld::Message::REMOVE_MODULE_DIR, data, reply)) {
103 return false;
104 }
105 return reply.ReadInt32();
106 }
107
RenameModuleDir(const std::string & oldPath,const std::string & newPath)108 ErrCode InstalldProxy::RenameModuleDir(const std::string &oldPath, const std::string &newPath)
109 {
110 MessageParcel data;
111 MessageParcel reply;
112 if (!data.WriteInterfaceToken(GetDescriptor())) {
113 APP_LOGE("fail to send cmd to service due to data.WriteInterfaceToken(GetDescriptor())");
114 return false;
115 }
116 if (!data.WriteString(oldPath)) {
117 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
118 }
119 if (!data.WriteString(newPath)) {
120 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
121 }
122 if (!TransactInstalldCmd(IInstalld::Message::RENAME_MODULE_DIR, data, reply)) {
123 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
124 }
125 return reply.ReadInt32();
126 }
127
CreateBundleDataDir(const std::string & bundleDir,const int uid,const int gid)128 ErrCode InstalldProxy::CreateBundleDataDir(const std::string &bundleDir, const int uid, const int gid)
129 {
130 MessageParcel data;
131 MessageParcel reply;
132 if (!data.WriteInterfaceToken(GetDescriptor())) {
133 APP_LOGE("fail to send cmd to service due to data.WriteInterfaceToken(GetDescriptor())");
134 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
135 }
136 if (!data.WriteString(bundleDir)) {
137 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
138 }
139 if (!data.WriteInt32(uid)) {
140 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
141 }
142 if (!data.WriteInt32(gid)) {
143 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
144 }
145 if (!TransactInstalldCmd(IInstalld::Message::CREATE_BUNDLE_DATA_DIR, data, reply)) {
146 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
147 }
148 return reply.ReadInt32();
149 }
150
RemoveBundleDataDir(const std::string & bundleDataPath)151 ErrCode InstalldProxy::RemoveBundleDataDir(const std::string &bundleDataPath)
152 {
153 MessageParcel data;
154 MessageParcel reply;
155 if (!data.WriteInterfaceToken(GetDescriptor())) {
156 APP_LOGE("fail to send cmd to service due to data.WriteInterfaceToken(GetDescriptor())");
157 return false;
158 }
159 if (!data.WriteString(bundleDataPath)) {
160 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
161 }
162 if (!TransactInstalldCmd(IInstalld::Message::REMOVE_BUNDLE_DATA_DIR, data, reply)) {
163 return false;
164 }
165 return reply.ReadInt32();
166 }
167
CreateModuleDataDir(const std::string & ModuleDir,const std::vector<std::string> & abilityDirs,const int uid,const int gid)168 ErrCode InstalldProxy::CreateModuleDataDir(
169 const std::string &ModuleDir, const std::vector<std::string> &abilityDirs, const int uid, const int gid)
170 {
171 MessageParcel data;
172 MessageParcel reply;
173 if (!data.WriteInterfaceToken(GetDescriptor())) {
174 APP_LOGE("fail to send cmd to service due to data.WriteInterfaceToken(GetDescriptor())");
175 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
176 }
177 if (!data.WriteString(ModuleDir)) {
178 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
179 }
180 if (!data.WriteStringVector(abilityDirs)) {
181 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
182 }
183 if (!data.WriteInt32(uid)) {
184 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
185 }
186 if (!data.WriteInt32(gid)) {
187 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
188 }
189 if (!TransactInstalldCmd(IInstalld::Message::CREATE_MODULE_DATA_DIR, data, reply)) {
190 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
191 }
192 return reply.ReadInt32();
193 }
194
RemoveModuleDataDir(const std::string & moduleDataDir)195 ErrCode InstalldProxy::RemoveModuleDataDir(const std::string &moduleDataDir)
196 {
197 MessageParcel data;
198 MessageParcel reply;
199 if (!data.WriteInterfaceToken(GetDescriptor())) {
200 APP_LOGE("fail to send cmd to service due to data.WriteInterfaceToken(GetDescriptor())");
201 return false;
202 }
203 if (!data.WriteString(moduleDataDir)) {
204 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
205 }
206 if (!TransactInstalldCmd(IInstalld::Message::REMOVE_MODULE_DATA_DIR, data, reply)) {
207 return false;
208 }
209 return reply.ReadInt32();
210 }
211
CleanBundleDataDir(const std::string & bundleDir)212 ErrCode InstalldProxy::CleanBundleDataDir(const std::string &bundleDir)
213 {
214 MessageParcel data;
215 MessageParcel reply;
216 if (!data.WriteInterfaceToken(GetDescriptor())) {
217 APP_LOGE("fail to send cmd to service due to data.WriteInterfaceToken(GetDescriptor())");
218 return false;
219 }
220 if (!data.WriteString(bundleDir)) {
221 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
222 }
223 if (!TransactInstalldCmd(IInstalld::Message::CLEAN_BUNDLE_DATA_DIR, data, reply)) {
224 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
225 }
226 return reply.ReadInt32();
227 }
228
TransactInstalldCmd(IInstalld::Message code,MessageParcel & data,MessageParcel & reply)229 bool InstalldProxy::TransactInstalldCmd(IInstalld::Message code, MessageParcel &data, MessageParcel &reply)
230 {
231 sptr<IRemoteObject> remote = Remote();
232 MessageOption option(MessageOption::TF_SYNC);
233 if (remote == nullptr) {
234 APP_LOGE("fail to send %{public}d cmd to service due to remote object is null", code);
235 return false;
236 }
237 int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
238 if (result != OHOS::NO_ERROR) {
239 APP_LOGE("fail to send %{public}d cmd to service due to transact error", code);
240 return false;
241 }
242 return true;
243 }
244
245 } // namespace AppExecFwk
246 } // namespace OHOS