1 /*
2 * Copyright (c) 2021-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 "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 #include "string_ex.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 constexpr int32_t WAIT_TIME = 3000;
29 }
30
InstalldProxy(const sptr<IRemoteObject> & object)31 InstalldProxy::InstalldProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IInstalld>(object)
32 {
33 APP_LOGI("installd proxy instance is created");
34 }
35
~InstalldProxy()36 InstalldProxy::~InstalldProxy()
37 {
38 APP_LOGI("installd proxy instance is destroyed");
39 }
40
CreateBundleDir(const std::string & bundleDir)41 ErrCode InstalldProxy::CreateBundleDir(const std::string &bundleDir)
42 {
43 MessageParcel data;
44 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
45 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleDir));
46
47 MessageParcel reply;
48 MessageOption option;
49 return TransactInstalldCmd(IInstalld::Message::CREATE_BUNDLE_DIR, data, reply, option);
50 }
51
ExtractModuleFiles(const std::string & srcModulePath,const std::string & targetPath,const std::string & targetSoPath,const std::string & cpuAbi)52 ErrCode InstalldProxy::ExtractModuleFiles(const std::string &srcModulePath, const std::string &targetPath,
53 const std::string &targetSoPath, const std::string &cpuAbi)
54 {
55 MessageParcel data;
56 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
57 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(srcModulePath));
58 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(targetPath));
59 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(targetSoPath));
60 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(cpuAbi));
61
62 MessageParcel reply;
63 MessageOption option;
64 return TransactInstalldCmd(IInstalld::Message::EXTRACT_MODULE_FILES, data, reply, option);
65 }
66
RenameModuleDir(const std::string & oldPath,const std::string & newPath)67 ErrCode InstalldProxy::RenameModuleDir(const std::string &oldPath, const std::string &newPath)
68 {
69 MessageParcel data;
70 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
71 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(oldPath));
72 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(newPath));
73
74 MessageParcel reply;
75 MessageOption option;
76 return TransactInstalldCmd(IInstalld::Message::RENAME_MODULE_DIR, data, reply, option);
77 }
78
CreateBundleDataDir(const std::string & bundleDir,const int userid,const int uid,const int gid,const std::string & apl,bool onlyOneUser)79 ErrCode InstalldProxy::CreateBundleDataDir(const std::string &bundleDir,
80 const int userid, const int uid, const int gid, const std::string &apl, bool onlyOneUser)
81 {
82 MessageParcel data;
83 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
84 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleDir));
85 INSTALLD_PARCEL_WRITE(data, Int32, userid);
86 INSTALLD_PARCEL_WRITE(data, Int32, uid);
87 INSTALLD_PARCEL_WRITE(data, Int32, gid);
88 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(apl));
89 INSTALLD_PARCEL_WRITE(data, Bool, onlyOneUser);
90
91 MessageParcel reply;
92 MessageOption option;
93 return TransactInstalldCmd(IInstalld::Message::CREATE_BUNDLE_DATA_DIR, data, reply, option);
94 }
95
RemoveBundleDataDir(const std::string & bundleName,const int userid)96 ErrCode InstalldProxy::RemoveBundleDataDir(const std::string &bundleName, const int userid)
97 {
98 MessageParcel data;
99 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
100 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleName));
101 INSTALLD_PARCEL_WRITE(data, Int32, userid);
102
103 MessageParcel reply;
104 MessageOption option;
105 return TransactInstalldCmd(IInstalld::Message::REMOVE_BUNDLE_DATA_DIR, data, reply, option);
106 }
107
CreateModuleDataDir(const std::string & ModuleDir,const std::vector<std::string> & abilityDirs,const int uid,const int gid)108 ErrCode InstalldProxy::CreateModuleDataDir(
109 const std::string &ModuleDir, const std::vector<std::string> &abilityDirs, const int uid, const int gid)
110 {
111 MessageParcel data;
112 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
113 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(ModuleDir));
114 INSTALLD_PARCEL_WRITE(data, StringVector, abilityDirs);
115 INSTALLD_PARCEL_WRITE(data, Int32, uid);
116 INSTALLD_PARCEL_WRITE(data, Int32, gid);
117
118 MessageParcel reply;
119 MessageOption option;
120 return TransactInstalldCmd(IInstalld::Message::CREATE_MODULE_DATA_DIR, data, reply, option);
121 }
122
RemoveModuleDataDir(const std::string & ModuleName,const int userid)123 ErrCode InstalldProxy::RemoveModuleDataDir(const std::string &ModuleName, const int userid)
124 {
125 MessageParcel data;
126 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
127 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(ModuleName));
128 INSTALLD_PARCEL_WRITE(data, Int32, userid);
129
130 MessageParcel reply;
131 MessageOption option;
132 return TransactInstalldCmd(IInstalld::Message::REMOVE_MODULE_DATA_DIR, data, reply, option);
133 }
134
RemoveDir(const std::string & dir)135 ErrCode InstalldProxy::RemoveDir(const std::string &dir)
136 {
137 MessageParcel data;
138 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
139 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(dir));
140
141 MessageParcel reply;
142 MessageOption option(MessageOption::TF_SYNC);
143 return TransactInstalldCmd(IInstalld::Message::REMOVE_DIR, data, reply, option);
144 }
145
CleanBundleDataDir(const std::string & bundleDir)146 ErrCode InstalldProxy::CleanBundleDataDir(const std::string &bundleDir)
147 {
148 MessageParcel data;
149 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
150 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleDir));
151
152 MessageParcel reply;
153 MessageOption option(MessageOption::TF_SYNC, WAIT_TIME);
154 return TransactInstalldCmd(IInstalld::Message::CLEAN_BUNDLE_DATA_DIR, data, reply, option);
155 }
156
GetBundleStats(const std::string & bundleName,const int32_t userId,std::vector<int64_t> & bundleStats)157 ErrCode InstalldProxy::GetBundleStats(
158 const std::string &bundleName, const int32_t userId, std::vector<int64_t> &bundleStats)
159 {
160 MessageParcel data;
161 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
162 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleName));
163 INSTALLD_PARCEL_WRITE(data, Int32, userId);
164 MessageParcel reply;
165 MessageOption option(MessageOption::TF_SYNC);
166 auto ret = TransactInstalldCmd(IInstalld::Message::GET_BUNDLE_STATS, data, reply, option);
167 if (ret == ERR_OK) {
168 if (reply.ReadInt64Vector(&bundleStats)) {
169 return ERR_OK;
170 } else {
171 return ERR_APPEXECFWK_PARCEL_ERROR;
172 }
173 }
174 return ret;
175 }
176
SetDirApl(const std::string & dir,const std::string & bundleName,const std::string & apl)177 ErrCode InstalldProxy::SetDirApl(const std::string &dir, const std::string &bundleName, const std::string &apl)
178 {
179 MessageParcel data;
180 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
181 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(dir));
182 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(bundleName));
183 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(apl));
184
185 MessageParcel reply;
186 MessageOption option(MessageOption::TF_SYNC);
187 return TransactInstalldCmd(IInstalld::Message::SET_DIR_APL, data, reply, option);
188 }
189
GetBundleCachePath(const std::string & dir,std::vector<std::string> & cachePath)190 ErrCode InstalldProxy::GetBundleCachePath(const std::string &dir, std::vector<std::string> &cachePath)
191 {
192 MessageParcel data;
193 INSTALLD_PARCEL_WRITE_INTERFACE_TOKEN(data, (GetDescriptor()));
194 INSTALLD_PARCEL_WRITE(data, String16, Str8ToStr16(dir));
195 MessageParcel reply;
196 MessageOption option(MessageOption::TF_SYNC);
197 auto ret = TransactInstalldCmd(IInstalld::Message::GET_BUNDLE_CACHE_PATH, data, reply, option);
198 if (ret == ERR_OK) {
199 if (reply.ReadStringVector(&cachePath)) {
200 return ERR_OK;
201 } else {
202 return ERR_APPEXECFWK_PARCEL_ERROR;
203 }
204 }
205 return ret;
206 }
207
TransactInstalldCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)208 ErrCode InstalldProxy::TransactInstalldCmd(uint32_t code, MessageParcel &data, MessageParcel &reply,
209 MessageOption &option)
210 {
211 sptr<IRemoteObject> remote = Remote();
212 if (remote == nullptr) {
213 APP_LOGE("fail to send %{public}u cmd to service due to remote object is null", code);
214 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
215 }
216
217 if (remote->SendRequest(code, data, reply, option) != OHOS::NO_ERROR) {
218 APP_LOGE("fail to send %{public}u request to service due to transact error", code);
219 return ERR_APPEXECFWK_INSTALL_INSTALLD_SERVICE_ERROR;
220 }
221 return reply.ReadInt32();
222 }
223 } // namespace AppExecFwk
224 } // namespace OHOS