1 /*
2 * Copyright (c) 2022-2023 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 "app_control_host.h"
17
18 #include "app_control_constants.h"
19 #include "app_log_wrapper.h"
20 #include "appexecfwk_errors.h"
21 #include "bundle_framework_core_ipc_interface_code.h"
22 #include "bundle_memory_guard.h"
23 #include "ipc_types.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
AppControlHost()27 AppControlHost::AppControlHost()
28 {
29 APP_LOGD("create AppControlHost.");
30 }
31
~AppControlHost()32 AppControlHost::~AppControlHost()
33 {
34 APP_LOGD("destroy AppControlHost.");
35 }
36
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int AppControlHost::OnRemoteRequest(
38 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
39 {
40 BundleMemoryGuard memoryGuard;
41 APP_LOGI("AppControlHost OnRemoteRequest, message code : %{public}u", code);
42 std::u16string descriptor = AppControlHost::GetDescriptor();
43 std::u16string remoteDescriptor = data.ReadInterfaceToken();
44 if (descriptor != remoteDescriptor) {
45 APP_LOGE("descriptor invalid.");
46 return OBJECT_NULL;
47 }
48
49 switch (code) {
50 case static_cast<uint32_t>(AppControlManagerInterfaceCode::ADD_APP_INSTALL_CONTROL_RULE):
51 return HandleAddAppInstallControlRule(data, reply);
52 case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_INSTALL_CONTROL_RULE):
53 return HandleDeleteAppInstallControlRule(data, reply);
54 case static_cast<uint32_t>(AppControlManagerInterfaceCode::CLEAN_APP_INSTALL_CONTROL_RULE):
55 return HandleCleanAppInstallControlRule(data, reply);
56 case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_INSTALL_CONTROL_RULE):
57 return HandleGetAppInstallControlRule(data, reply);
58 case static_cast<uint32_t>(AppControlManagerInterfaceCode::ADD_APP_RUNNING_CONTROL_RULE):
59 return HandleAddAppRunningControlRule(data, reply);
60 case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_RUNNING_CONTROL_RULE):
61 return HandleDeleteAppRunningControlRule(data, reply);
62 case static_cast<uint32_t>(AppControlManagerInterfaceCode::CLEAN_APP_RUNNING_CONTROL_RULE):
63 return HandleCleanAppRunningControlRule(data, reply);
64 case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_RUNNING_CONTROL_RULE):
65 return HandleGetAppRunningControlRule(data, reply);
66 case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_RUNNING_CONTROL_RULE_RESULT):
67 return HandleGetAppRunningControlRuleResult(data, reply);
68 case static_cast<uint32_t>(AppControlManagerInterfaceCode::CONFIRM_APP_JUMP_CONTROL_RULE):
69 return HandleConfirmAppJumpControlRule(data, reply);
70 case static_cast<uint32_t>(AppControlManagerInterfaceCode::ADD_APP_JUMP_CONTROL_RULE):
71 return HandleAddAppJumpControlRule(data, reply);
72 case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_JUMP_CONTROL_RULE):
73 return HandleDeleteAppJumpControlRule(data, reply);
74 case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_JUMP_CONTROL_RULE_BY_CALLER):
75 return HandleDeleteRuleByCallerBundleName(data, reply);
76 case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_APP_JUMP_CONTROL_RULE_BY_TARGET):
77 return HandleDeleteRuleByTargetBundleName(data, reply);
78 case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_APP_JUMP_CONTROL_RULE):
79 return HandleGetAppJumpControlRule(data, reply);
80 case static_cast<uint32_t>(AppControlManagerInterfaceCode::SET_DISPOSED_STATUS):
81 return HandleSetDisposedStatus(data, reply);
82 case static_cast<uint32_t>(AppControlManagerInterfaceCode::GET_DISPOSED_STATUS):
83 return HandleGetDisposedStatus(data, reply);
84 case static_cast<uint32_t>(AppControlManagerInterfaceCode::DELETE_DISPOSED_STATUS):
85 return HandleDeleteDisposedStatus(data, reply);
86 default:
87 APP_LOGW("AppControlHost receive unknown code, code = %{public}d", code);
88 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
89 }
90 }
91
HandleAddAppInstallControlRule(MessageParcel & data,MessageParcel & reply)92 ErrCode AppControlHost::HandleAddAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
93 {
94 std::vector<std::string> appIds;
95 int32_t appIdSize = data.ReadInt32();
96 if (appIdSize > AppControlConstants::LIST_MAX_SIZE) {
97 APP_LOGE("HandleAddAppInstallControlRule parameter is invalid");
98 return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
99 }
100 for (int32_t i = 0; i < appIdSize; i++) {
101 appIds.emplace_back(data.ReadString());
102 }
103 AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
104 int32_t userId = data.ReadInt32();
105 int32_t ret = AddAppInstallControlRule(appIds, controlRuleType, userId);
106 if (ret != ERR_OK) {
107 APP_LOGE("HandleAddAppInstallControlRule failed");
108 }
109 return ret;
110 }
111
HandleDeleteAppInstallControlRule(MessageParcel & data,MessageParcel & reply)112 ErrCode AppControlHost::HandleDeleteAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
113 {
114 AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
115 std::vector<std::string> appIds;
116 int32_t appIdSize = data.ReadInt32();
117 if (appIdSize > AppControlConstants::LIST_MAX_SIZE) {
118 APP_LOGE("HandleDeleteAppInstallControlRule parameter is invalid");
119 return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
120 }
121 for (int32_t i = 0; i < appIdSize; i++) {
122 appIds.emplace_back(data.ReadString());
123 }
124 int32_t userId = data.ReadInt32();
125 int32_t ret = DeleteAppInstallControlRule(controlRuleType, appIds, userId);
126 if (ret != ERR_OK) {
127 APP_LOGE("HandleDeleteAppInstallControlRule failed");
128 }
129 return ret;
130 }
131
HandleCleanAppInstallControlRule(MessageParcel & data,MessageParcel & reply)132 ErrCode AppControlHost::HandleCleanAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
133 {
134 AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
135 int32_t userId = data.ReadInt32();
136 int32_t ret = DeleteAppInstallControlRule(controlRuleType, userId);
137 if (ret != ERR_OK) {
138 APP_LOGE("HandleCleanAppInstallControlRule failed");
139 }
140 return ret;
141 }
142
HandleGetAppInstallControlRule(MessageParcel & data,MessageParcel & reply)143 ErrCode AppControlHost::HandleGetAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
144 {
145 AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
146 int32_t userId = data.ReadInt32();
147 std::vector<std::string> appIds;
148 int32_t ret = GetAppInstallControlRule(controlRuleType, userId, appIds);
149 if (ret != ERR_OK) {
150 APP_LOGE("HandleGetAppInstallControlRule failed");
151 return ret;
152 }
153 if (!WriteParcelableVector(appIds, reply)) {
154 APP_LOGE("write appIds failed");
155 return ERR_APPEXECFWK_PARCEL_ERROR;
156 }
157 return ERR_OK;
158 }
159
HandleAddAppRunningControlRule(MessageParcel & data,MessageParcel & reply)160 ErrCode AppControlHost::HandleAddAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
161 {
162 std::vector<AppRunningControlRule> controlRules;
163 auto ret = ReadParcelableVector(data, controlRules);
164 if (ret != ERR_OK) {
165 APP_LOGE("AddAppRunningControlRule read controlRuleParam failed");
166 return ret;
167 }
168 int32_t userId = data.ReadInt32();
169 return AddAppRunningControlRule(controlRules, userId);
170 }
171
HandleDeleteAppRunningControlRule(MessageParcel & data,MessageParcel & reply)172 ErrCode AppControlHost::HandleDeleteAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
173 {
174 std::vector<AppRunningControlRule> controlRules;
175 auto ret = ReadParcelableVector(data, controlRules);
176 if (ret != ERR_OK) {
177 APP_LOGE("DeleteAppRunningControlRule read controlRuleParam failed");
178 return ret;
179 }
180 int32_t userId = data.ReadInt32();
181 return DeleteAppRunningControlRule(controlRules, userId);
182 }
183
HandleCleanAppRunningControlRule(MessageParcel & data,MessageParcel & reply)184 ErrCode AppControlHost::HandleCleanAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
185 {
186 int32_t userId = data.ReadInt32();
187 int32_t ret = DeleteAppRunningControlRule(userId);
188 if (ret != ERR_OK) {
189 APP_LOGE("HandleCleanAppInstallControlRule failed");
190 }
191 return ret;
192 }
193
HandleGetAppRunningControlRule(MessageParcel & data,MessageParcel & reply)194 ErrCode AppControlHost::HandleGetAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
195 {
196 int32_t userId = data.ReadInt32();
197 std::vector<std::string> appIds;
198 int32_t ret = GetAppRunningControlRule(userId, appIds);
199 if (ret != ERR_OK) {
200 APP_LOGE("HandleGetAppRunningControlRule failed");
201 return ret;
202 }
203 if (!WriteParcelableVector(appIds, reply)) {
204 APP_LOGE("write appIds failed");
205 return ERR_APPEXECFWK_PARCEL_ERROR;
206 }
207 return ERR_OK;
208 }
209
HandleGetAppRunningControlRuleResult(MessageParcel & data,MessageParcel & reply)210 ErrCode AppControlHost::HandleGetAppRunningControlRuleResult(MessageParcel& data, MessageParcel& reply)
211 {
212 std::string bundleName = data.ReadString();
213 int32_t userId = data.ReadInt32();
214 AppRunningControlRuleResult ruleResult;
215 int32_t ret = GetAppRunningControlRule(bundleName, userId, ruleResult);
216 if (ret != ERR_OK) {
217 APP_LOGE("HandleGetAppRunningControlRuleResult failed");
218 }
219 if (!reply.WriteInt32(ret)) {
220 APP_LOGE("write result failed");
221 return ERR_APPEXECFWK_PARCEL_ERROR;
222 }
223 if ((ret == ERR_OK) && !reply.WriteParcelable(&ruleResult)) {
224 APP_LOGE("write AppRunningControlRuleResult failed");
225 return ERR_APPEXECFWK_PARCEL_ERROR;
226 }
227 return ERR_OK;
228 }
229
HandleConfirmAppJumpControlRule(MessageParcel & data,MessageParcel & reply)230 ErrCode AppControlHost::HandleConfirmAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
231 {
232 std::string callerBundleName = data.ReadString();
233 std::string targetBundleName = data.ReadString();
234 int32_t userId = data.ReadInt32();
235 int32_t ret = ConfirmAppJumpControlRule(callerBundleName, targetBundleName, userId);
236 if (ret != ERR_OK) {
237 APP_LOGE("HandleConfirmAppJumpControlRule failed");
238 }
239 return ret;
240 }
241
HandleAddAppJumpControlRule(MessageParcel & data,MessageParcel & reply)242 ErrCode AppControlHost::HandleAddAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
243 {
244 std::vector<AppJumpControlRule> controlRules;
245 auto ret = ReadParcelableVector(data, controlRules);
246 if (ret != ERR_OK) {
247 APP_LOGE("HandleAddAppJumpControlRule read controlRuleParam failed");
248 return ret;
249 }
250 int32_t userId = data.ReadInt32();
251 return AddAppJumpControlRule(controlRules, userId);
252 }
253
HandleDeleteAppJumpControlRule(MessageParcel & data,MessageParcel & reply)254 ErrCode AppControlHost::HandleDeleteAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
255 {
256 std::vector<AppJumpControlRule> controlRules;
257 auto ret = ReadParcelableVector(data, controlRules);
258 if (ret != ERR_OK) {
259 APP_LOGE("HandleDeleteAppJumpControlRule read controlRuleParam failed");
260 return ret;
261 }
262 int32_t userId = data.ReadInt32();
263 return DeleteAppJumpControlRule(controlRules, userId);
264 }
265
HandleDeleteRuleByCallerBundleName(MessageParcel & data,MessageParcel & reply)266 ErrCode AppControlHost::HandleDeleteRuleByCallerBundleName(MessageParcel& data, MessageParcel& reply)
267 {
268 std::string callerBundleName = data.ReadString();
269 int32_t userId = data.ReadInt32();
270 int32_t ret = DeleteRuleByCallerBundleName(callerBundleName, userId);
271 if (ret != ERR_OK) {
272 APP_LOGE("HandleDeleteRuleByCallerBundleName failed");
273 }
274 return ret;
275 }
276
HandleDeleteRuleByTargetBundleName(MessageParcel & data,MessageParcel & reply)277 ErrCode AppControlHost::HandleDeleteRuleByTargetBundleName(MessageParcel& data, MessageParcel& reply)
278 {
279 std::string targetBundleName = data.ReadString();
280 int32_t userId = data.ReadInt32();
281 int32_t ret = DeleteRuleByTargetBundleName(targetBundleName, userId);
282 if (ret != ERR_OK) {
283 APP_LOGE("HandleDeleteRuleByTargetBundleName failed");
284 }
285 return ret;
286 }
287
HandleGetAppJumpControlRule(MessageParcel & data,MessageParcel & reply)288 ErrCode AppControlHost::HandleGetAppJumpControlRule(MessageParcel& data, MessageParcel& reply)
289 {
290 std::string callerBundleName = data.ReadString();
291 std::string targetBundleName = data.ReadString();
292 int32_t userId = data.ReadInt32();
293 AppJumpControlRule rule;
294 int32_t ret = GetAppJumpControlRule(callerBundleName, targetBundleName, userId, rule);
295 if (ret != ERR_OK) {
296 APP_LOGE("HandleGetAppJumpControlRule failed");
297 }
298 if (!reply.WriteInt32(ret)) {
299 APP_LOGE("write result failed");
300 return ERR_APPEXECFWK_PARCEL_ERROR;
301 }
302 if ((ret == ERR_OK) && !reply.WriteParcelable(&rule)) {
303 APP_LOGE("write AppJumpControlRule failed");
304 return ERR_APPEXECFWK_PARCEL_ERROR;
305 }
306 return ERR_OK;
307 }
308
HandleSetDisposedStatus(MessageParcel & data,MessageParcel & reply)309 ErrCode AppControlHost::HandleSetDisposedStatus(MessageParcel& data, MessageParcel& reply)
310 {
311 std::string appId = data.ReadString();
312 std::unique_ptr<Want> want(data.ReadParcelable<Want>());
313 if (want == nullptr) {
314 APP_LOGE("ReadParcelable<Want> failed.");
315 return ERR_APPEXECFWK_PARCEL_ERROR;
316 }
317 ErrCode ret = SetDisposedStatus(appId, *want);
318 if (!reply.WriteInt32(ret)) {
319 APP_LOGE("write ret failed");
320 return ERR_APPEXECFWK_PARCEL_ERROR;
321 }
322 return ERR_OK;
323 }
324
HandleDeleteDisposedStatus(MessageParcel & data,MessageParcel & reply)325 ErrCode AppControlHost::HandleDeleteDisposedStatus(MessageParcel& data, MessageParcel &reply)
326 {
327 std::string appId = data.ReadString();
328 ErrCode ret = DeleteDisposedStatus(appId);
329 if (!reply.WriteInt32(ret)) {
330 APP_LOGE("write ret failed");
331 return ERR_APPEXECFWK_PARCEL_ERROR;
332 }
333 return ERR_OK;
334 }
335
HandleGetDisposedStatus(MessageParcel & data,MessageParcel & reply)336 ErrCode AppControlHost::HandleGetDisposedStatus(MessageParcel& data, MessageParcel &reply)
337 {
338 std::string appId = data.ReadString();
339 Want want;
340 ErrCode ret = GetDisposedStatus(appId, want);
341 if (!reply.WriteInt32(ret)) {
342 APP_LOGE("write ret failed");
343 return ERR_APPEXECFWK_PARCEL_ERROR;
344 }
345 if (ret == ERR_OK) {
346 if (!reply.WriteParcelable(&want)) {
347 APP_LOGE("write failed");
348 return ERR_APPEXECFWK_PARCEL_ERROR;
349 }
350 }
351 return ERR_OK;
352 }
353
WriteParcelableVector(const std::vector<std::string> & stringVector,MessageParcel & reply)354 bool AppControlHost::WriteParcelableVector(const std::vector<std::string> &stringVector, MessageParcel &reply)
355 {
356 if (!reply.WriteInt32(stringVector.size())) {
357 APP_LOGE("write ParcelableVector failed");
358 return false;
359 }
360
361 for (auto &string : stringVector) {
362 if (!reply.WriteString(string)) {
363 APP_LOGE("write string failed");
364 return false;
365 }
366 }
367 return true;
368 }
369
370 template<typename T>
ReadParcelableVector(MessageParcel & data,std::vector<T> & parcelableInfos)371 ErrCode AppControlHost::ReadParcelableVector(MessageParcel &data, std::vector<T> &parcelableInfos)
372 {
373 int32_t infoSize = data.ReadInt32();
374 if (infoSize > AppControlConstants::LIST_MAX_SIZE) {
375 APP_LOGE("ReadParcelableVector elements num exceeds the limit %{public}d", AppControlConstants::LIST_MAX_SIZE);
376 return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
377 }
378 for (int32_t i = 0; i < infoSize; i++) {
379 std::unique_ptr<T> info(data.ReadParcelable<T>());
380 if (info == nullptr) {
381 APP_LOGE("read parcelable infos failed");
382 return ERR_APPEXECFWK_PARCEL_ERROR;
383 }
384 parcelableInfos.emplace_back(*info);
385 }
386 APP_LOGD("read parcelable infos success");
387 return ERR_OK;
388 }
389 } // AppExecFwk
390 } // OHOS
391