• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "app_control_host.h"
17 
18 #include "app_control_constants.h"
19 #include "app_log_wrapper.h"
20 #include "appexecfwk_errors.h"
21 #include "ipc_types.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
AppControlHost()25 AppControlHost::AppControlHost()
26 {
27     APP_LOGD("create AppControlHost.");
28 }
29 
~AppControlHost()30 AppControlHost::~AppControlHost()
31 {
32     APP_LOGD("destroy AppControlHost.");
33 }
34 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int AppControlHost::OnRemoteRequest(
36     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
37 {
38     APP_LOGI("AppControlHost OnRemoteRequest, message code : %{public}u", code);
39     std::u16string descriptor = AppControlHost::GetDescriptor();
40     std::u16string remoteDescriptor = data.ReadInterfaceToken();
41     if (descriptor != remoteDescriptor) {
42         APP_LOGE("descriptor invalid.");
43         return OBJECT_NULL;
44     }
45 
46     switch (code) {
47         case IAppControlMgr::Message::ADD_APP_INSTALL_CONTROL_RULE:
48             return HandleAddAppInstallControlRule(data, reply);
49         case IAppControlMgr::Message::DELETE_APP_INSTALL_CONTROL_RULE:
50             return HandleDeleteAppInstallControlRule(data, reply);
51         case IAppControlMgr::Message::CLEAN_APP_INSTALL_CONTROL_RULE:
52             return HandleCleanAppInstallControlRule(data, reply);
53         case IAppControlMgr::Message::GET_APP_INSTALL_CONTROL_RULE:
54             return HandleGetAppInstallControlRule(data, reply);
55         case IAppControlMgr::Message::ADD_APP_RUNNING_CONTROL_RULE:
56             return HandleAddAppRunningControlRule(data, reply);
57         case IAppControlMgr::Message::DELETE_APP_RUNNING_CONTROL_RULE:
58             return HandleDeleteAppRunningControlRule(data, reply);
59         case IAppControlMgr::Message::CLEAN_APP_RUNNING_CONTROL_RULE:
60             return HandleCleanAppRunningControlRule(data, reply);
61         case IAppControlMgr::Message::GET_APP_RUNNING_CONTROL_RULE:
62             return HandleGetAppRunningControlRule(data, reply);
63         case IAppControlMgr::Message::GET_APP_RUNNING_CONTROL_RULE_RESULT:
64             return HandleGetAppRunningControlRuleResult(data, reply);
65         case IAppControlMgr::Message::SET_DISPOSED_STATUS:
66             return HandleSetDisposedStatus(data, reply);
67         case IAppControlMgr::Message::GET_DISPOSED_STATUS:
68             return HandleGetDisposedStatus(data, reply);
69         case IAppControlMgr::Message::DELETE_DISPOSED_STATUS:
70             return HandleDeleteDisposedStatus(data, reply);
71         default:
72             APP_LOGW("AppControlHost receive unknown code, code = %{public}d", code);
73             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74     }
75 }
76 
HandleAddAppInstallControlRule(MessageParcel & data,MessageParcel & reply)77 ErrCode AppControlHost::HandleAddAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
78 {
79     std::vector<std::string> appIds;
80     int32_t appIdSize = data.ReadInt32();
81     if (appIdSize > AppControlConstants::LIST_MAX_SIZE) {
82         APP_LOGE("HandleAddAppInstallControlRule parameter is invalid");
83         return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
84     }
85     for (int32_t i = 0; i < appIdSize; i++) {
86         appIds.emplace_back(data.ReadString());
87     }
88     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
89     int32_t userId = data.ReadInt32();
90     int32_t ret = AddAppInstallControlRule(appIds, controlRuleType, userId);
91     if (ret != ERR_OK) {
92         APP_LOGE("HandleAddAppInstallControlRule failed");
93     }
94     return ret;
95 }
96 
HandleDeleteAppInstallControlRule(MessageParcel & data,MessageParcel & reply)97 ErrCode AppControlHost::HandleDeleteAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
98 {
99     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
100     std::vector<std::string> appIds;
101     int32_t appIdSize = data.ReadInt32();
102     if (appIdSize > AppControlConstants::LIST_MAX_SIZE) {
103         APP_LOGE("HandleDeleteAppInstallControlRule parameter is invalid");
104         return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
105     }
106     for (int32_t i = 0; i < appIdSize; i++) {
107         appIds.emplace_back(data.ReadString());
108     }
109     int32_t userId = data.ReadInt32();
110     int32_t ret = DeleteAppInstallControlRule(controlRuleType, appIds, userId);
111     if (ret != ERR_OK) {
112         APP_LOGE("HandleDeleteAppInstallControlRule failed");
113     }
114     return ret;
115 }
116 
HandleCleanAppInstallControlRule(MessageParcel & data,MessageParcel & reply)117 ErrCode AppControlHost::HandleCleanAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
118 {
119     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
120     int32_t userId = data.ReadInt32();
121     int32_t ret = DeleteAppInstallControlRule(controlRuleType, userId);
122     if (ret != ERR_OK) {
123         APP_LOGE("HandleCleanAppInstallControlRule failed");
124     }
125     return ret;
126 }
127 
HandleGetAppInstallControlRule(MessageParcel & data,MessageParcel & reply)128 ErrCode AppControlHost::HandleGetAppInstallControlRule(MessageParcel& data, MessageParcel& reply)
129 {
130     AppInstallControlRuleType controlRuleType = static_cast<AppInstallControlRuleType>(data.ReadInt32());
131     int32_t userId = data.ReadInt32();
132     std::vector<std::string> appIds;
133     int32_t ret = GetAppInstallControlRule(controlRuleType, userId, appIds);
134     if (ret != ERR_OK) {
135         APP_LOGE("HandleGetAppInstallControlRule failed");
136         return ret;
137     }
138     if (!WriteParcelableVector(appIds, reply)) {
139         APP_LOGE("write appIds failed");
140         return ERR_APPEXECFWK_PARCEL_ERROR;
141     }
142     return ERR_OK;
143 }
144 
HandleAddAppRunningControlRule(MessageParcel & data,MessageParcel & reply)145 ErrCode AppControlHost::HandleAddAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
146 {
147     std::vector<AppRunningControlRule> controlRules;
148     auto ret = ReadParcelableVector(data, controlRules);
149     if (ret != ERR_OK) {
150         APP_LOGE("AddAppRunningControlRule read controlRuleParam failed");
151         return ret;
152     }
153     int32_t userId = data.ReadInt32();
154     return AddAppRunningControlRule(controlRules, userId);
155 }
156 
HandleDeleteAppRunningControlRule(MessageParcel & data,MessageParcel & reply)157 ErrCode AppControlHost::HandleDeleteAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
158 {
159     std::vector<AppRunningControlRule> controlRules;
160     auto ret = ReadParcelableVector(data, controlRules);
161     if (ret != ERR_OK) {
162         APP_LOGE("DeleteAppRunningControlRule read controlRuleParam failed");
163         return ret;
164     }
165     int32_t userId = data.ReadInt32();
166     return DeleteAppRunningControlRule(controlRules, userId);
167 }
168 
HandleCleanAppRunningControlRule(MessageParcel & data,MessageParcel & reply)169 ErrCode AppControlHost::HandleCleanAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
170 {
171     int32_t userId = data.ReadInt32();
172     int32_t ret = DeleteAppRunningControlRule(userId);
173     if (ret != ERR_OK) {
174         APP_LOGE("HandleCleanAppInstallControlRule failed");
175     }
176     return ret;
177 }
178 
HandleGetAppRunningControlRule(MessageParcel & data,MessageParcel & reply)179 ErrCode AppControlHost::HandleGetAppRunningControlRule(MessageParcel& data, MessageParcel& reply)
180 {
181     int32_t userId = data.ReadInt32();
182     std::vector<std::string> appIds;
183     int32_t ret = GetAppRunningControlRule(userId, appIds);
184     if (ret != ERR_OK) {
185         APP_LOGE("HandleGetAppRunningControlRule failed");
186         return ret;
187     }
188     if (!WriteParcelableVector(appIds, reply)) {
189         APP_LOGE("write appIds failed");
190         return ERR_APPEXECFWK_PARCEL_ERROR;
191     }
192     return ERR_OK;
193 }
194 
HandleGetAppRunningControlRuleResult(MessageParcel & data,MessageParcel & reply)195 ErrCode AppControlHost::HandleGetAppRunningControlRuleResult(MessageParcel& data, MessageParcel& reply)
196 {
197     std::string bundleName = data.ReadString();
198     int32_t userId = data.ReadInt32();
199     AppRunningControlRuleResult ruleResult;
200     int32_t ret = GetAppRunningControlRule(bundleName, userId, ruleResult);
201     if (ret != ERR_OK) {
202         APP_LOGE("HandleGetAppRunningControlRuleResult failed");
203     }
204     if (!reply.WriteInt32(ret)) {
205         APP_LOGE("write result failed");
206         return ERR_APPEXECFWK_PARCEL_ERROR;
207     }
208     if ((ret == ERR_OK) && !reply.WriteParcelable(&ruleResult)) {
209         APP_LOGE("write AppRunningControlRuleResult failed");
210         return ERR_APPEXECFWK_PARCEL_ERROR;
211     }
212     return ERR_OK;
213 }
214 
HandleSetDisposedStatus(MessageParcel & data,MessageParcel & reply)215 ErrCode AppControlHost::HandleSetDisposedStatus(MessageParcel& data, MessageParcel& reply)
216 {
217     std::string appId = data.ReadString();
218     std::unique_ptr<Want> want(data.ReadParcelable<Want>());
219     if (want == nullptr) {
220         APP_LOGE("ReadParcelable<Want> failed.");
221         return ERR_APPEXECFWK_PARCEL_ERROR;
222     }
223     ErrCode ret = SetDisposedStatus(appId, *want);
224     if (!reply.WriteInt32(ret)) {
225         APP_LOGE("write ret failed");
226         return ERR_APPEXECFWK_PARCEL_ERROR;
227     }
228     return ERR_OK;
229 }
230 
HandleDeleteDisposedStatus(MessageParcel & data,MessageParcel & reply)231 ErrCode AppControlHost::HandleDeleteDisposedStatus(MessageParcel& data, MessageParcel &reply)
232 {
233     std::string appId = data.ReadString();
234     ErrCode ret = DeleteDisposedStatus(appId);
235     if (!reply.WriteInt32(ret)) {
236         APP_LOGE("write ret failed");
237         return ERR_APPEXECFWK_PARCEL_ERROR;
238     }
239     return ERR_OK;
240 }
241 
HandleGetDisposedStatus(MessageParcel & data,MessageParcel & reply)242 ErrCode AppControlHost::HandleGetDisposedStatus(MessageParcel& data, MessageParcel &reply)
243 {
244     std::string appId = data.ReadString();
245     Want want;
246     ErrCode ret = GetDisposedStatus(appId, want);
247     if (!reply.WriteInt32(ret)) {
248         APP_LOGE("write ret failed");
249         return ERR_APPEXECFWK_PARCEL_ERROR;
250     }
251     if (ret == ERR_OK) {
252         if (!reply.WriteParcelable(&want)) {
253             APP_LOGE("write failed");
254             return ERR_APPEXECFWK_PARCEL_ERROR;
255         }
256     }
257     return ERR_OK;
258 }
259 
WriteParcelableVector(const std::vector<std::string> & stringVector,MessageParcel & reply)260 bool AppControlHost::WriteParcelableVector(const std::vector<std::string> &stringVector, MessageParcel &reply)
261 {
262     if (!reply.WriteInt32(stringVector.size())) {
263         APP_LOGE("write ParcelableVector failed");
264         return false;
265     }
266 
267     for (auto &string : stringVector) {
268         if (!reply.WriteString(string)) {
269             APP_LOGE("write string failed");
270             return false;
271         }
272     }
273     return true;
274 }
275 
276 template<typename T>
ReadParcelableVector(MessageParcel & data,std::vector<T> & parcelableInfos)277 ErrCode AppControlHost::ReadParcelableVector(MessageParcel &data, std::vector<T> &parcelableInfos)
278 {
279     int32_t infoSize = data.ReadInt32();
280     if (infoSize > AppControlConstants::LIST_MAX_SIZE) {
281         APP_LOGE("ReadParcelableVector elements num exceeds the limit %{public}d", AppControlConstants::LIST_MAX_SIZE);
282         return ERR_BUNDLE_MANAGER_INVALID_PARAMETER;
283     }
284     for (int32_t i = 0; i < infoSize; i++) {
285         std::unique_ptr<T> info(data.ReadParcelable<T>());
286         if (info == nullptr) {
287             APP_LOGE("read parcelable infos failed");
288             return ERR_APPEXECFWK_PARCEL_ERROR;
289         }
290         parcelableInfos.emplace_back(*info);
291     }
292     APP_LOGD("read parcelable infos success");
293     return ERR_OK;
294 }
295 } // AppExecFwk
296 } // OHOS
297