• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "application_manager_proxy.h"
17 
18 #include "edm_constants.h"
19 #include "edm_log.h"
20 #include "func_code.h"
21 #include "message_parcel_utils.h"
22 
23 namespace OHOS {
24 namespace EDM {
25 std::shared_ptr<ApplicationManagerProxy> ApplicationManagerProxy::instance_ = nullptr;
26 std::once_flag ApplicationManagerProxy::flag_;
27 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
28 
GetApplicationManagerProxy()29 std::shared_ptr<ApplicationManagerProxy> ApplicationManagerProxy::GetApplicationManagerProxy()
30 {
31     std::call_once(flag_, []() {
32         if (instance_ == nullptr) {
33             instance_ = std::make_shared<ApplicationManagerProxy>();
34         }
35     });
36     return instance_;
37 }
38 
AddDisallowedRunningBundles(AppExecFwk::ElementName & admin,std::vector<std::string> & bundles,int32_t userId,bool isSync)39 int32_t ApplicationManagerProxy::AddDisallowedRunningBundles(AppExecFwk::ElementName &admin,
40     std::vector<std::string> &bundles, int32_t userId, bool isSync)
41 {
42     EDMLOGD("ApplicationManagerProxy::AddDisallowedRunningBundles");
43     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
44     MessageParcel data;
45     std::uint32_t funcCode =
46         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISALLOW_RUNNING_BUNDLES);
47     data.WriteInterfaceToken(DESCRIPTOR);
48     data.WriteInt32(HAS_USERID);
49     data.WriteInt32(userId);
50     data.WriteParcelable(&admin);
51     data.WriteString(isSync ? EdmConstants::PERMISSION_TAG_VERSION_12 : EdmConstants::PERMISSION_TAG_VERSION_11);
52     data.WriteStringVector(bundles);
53     return proxy->HandleDevicePolicy(funcCode, data);
54 }
55 
RemoveDisallowedRunningBundles(AppExecFwk::ElementName & admin,std::vector<std::string> & bundles,int32_t userId,bool isSync)56 int32_t ApplicationManagerProxy::RemoveDisallowedRunningBundles(AppExecFwk::ElementName &admin,
57     std::vector<std::string> &bundles, int32_t userId, bool isSync)
58 {
59     EDMLOGD("ApplicationManagerProxy::RemoveDisallowedRunningBundles");
60     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
61     MessageParcel data;
62     std::uint32_t funcCode =
63         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::DISALLOW_RUNNING_BUNDLES);
64     data.WriteInterfaceToken(DESCRIPTOR);
65     data.WriteInt32(HAS_USERID);
66     data.WriteInt32(userId);
67     data.WriteParcelable(&admin);
68     data.WriteString(isSync ? EdmConstants::PERMISSION_TAG_VERSION_12 : EdmConstants::PERMISSION_TAG_VERSION_11);
69     data.WriteStringVector(bundles);
70     return proxy->HandleDevicePolicy(funcCode, data);
71 }
72 
GetDisallowedRunningBundles(AppExecFwk::ElementName & admin,int32_t userId,std::vector<std::string> & bundles,bool isSync)73 int32_t ApplicationManagerProxy::GetDisallowedRunningBundles(AppExecFwk::ElementName &admin, int32_t userId,
74     std::vector<std::string> &bundles, bool isSync)
75 {
76     EDMLOGD("ApplicationManagerProxy::GetDisallowedRunningBundles");
77     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
78     MessageParcel data;
79     MessageParcel reply;
80     data.WriteInterfaceToken(DESCRIPTOR);
81     data.WriteInt32(HAS_USERID);
82     data.WriteInt32(userId);
83     data.WriteString(isSync ? EdmConstants::PERMISSION_TAG_VERSION_12 : EdmConstants::PERMISSION_TAG_VERSION_11);
84     data.WriteInt32(HAS_ADMIN);
85     data.WriteParcelable(&admin);
86     proxy->GetPolicy(EdmInterfaceCode::DISALLOW_RUNNING_BUNDLES, data, reply);
87     int32_t ret = ERR_INVALID_VALUE;
88     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
89     if (!blRes) {
90         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
91         return ret;
92     }
93     reply.ReadStringVector(&bundles);
94     return ERR_OK;
95 }
96 
AddOrRemoveAutoStartApps(MessageParcel & data,bool isAdd)97 int32_t ApplicationManagerProxy::AddOrRemoveAutoStartApps(MessageParcel &data, bool isAdd)
98 {
99     EDMLOGD("ApplicationManagerProxy::AddOrRemoveAutoStartApps");
100     FuncOperateType operateType = isAdd ? FuncOperateType::SET : FuncOperateType::REMOVE;
101     std::uint32_t funcCode =
102         POLICY_FUNC_CODE((std::uint32_t)operateType, EdmInterfaceCode::MANAGE_AUTO_START_APPS);
103     return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
104 }
105 
GetAutoStartApps(MessageParcel & data,std::vector<AppExecFwk::ElementName> & autoStartApps)106 int32_t ApplicationManagerProxy::GetAutoStartApps(MessageParcel &data,
107     std::vector<AppExecFwk::ElementName> &autoStartApps)
108 {
109     EDMLOGD("ApplicationManagerProxy::GetAutoStartApps");
110     MessageParcel reply;
111     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::MANAGE_AUTO_START_APPS, data, reply);
112     int32_t ret = ERR_INVALID_VALUE;
113     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
114     if (!blRes) {
115         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
116         return ret;
117     }
118     std::vector<std::string> autoStartAppsString;
119     reply.ReadStringVector(&autoStartAppsString);
120     for (size_t i = 0; i < autoStartAppsString.size(); i++) {
121         size_t index = autoStartAppsString[i].find("/");
122         std::string bundleName;
123         std::string abilityName;
124         if (index != autoStartAppsString[i].npos) {
125             bundleName = autoStartAppsString[i].substr(0, index);
126             abilityName = autoStartAppsString[i].substr(index + 1);
127         } else {
128             EDMLOGE("GetAutoStartApps parse auto start app want failed");
129             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
130         }
131         AppExecFwk::ElementName element;
132         element.SetBundleName(bundleName);
133         element.SetAbilityName(abilityName);
134         autoStartApps.push_back(element);
135     }
136     return ERR_OK;
137 }
138 
IsModifyAutoStartAppsDisallowed(MessageParcel & data,bool & isDisallowModify)139 int32_t ApplicationManagerProxy::IsModifyAutoStartAppsDisallowed(MessageParcel &data, bool &isDisallowModify)
140 {
141     EDMLOGD("ApplicationManagerProxy::IsModifyAutoStartAppsDisallowed");
142     MessageParcel reply;
143     EnterpriseDeviceMgrProxy::GetInstance()->GetPolicy(EdmInterfaceCode::MANAGE_AUTO_START_APPS, data, reply);
144     int32_t ret = ERR_INVALID_VALUE;
145     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
146     if (!blRes) {
147         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
148         return ret;
149     }
150     reply.ReadBool(isDisallowModify);
151     return ERR_OK;
152 }
153 
AddKeepAliveApps(const AppExecFwk::ElementName & admin,const std::vector<std::string> & keepAliveApps,bool disallowModify,int32_t userId,std::string & retMessage)154 int32_t ApplicationManagerProxy::AddKeepAliveApps(const AppExecFwk::ElementName &admin,
155     const std::vector<std::string> &keepAliveApps, bool disallowModify, int32_t userId, std::string &retMessage)
156 {
157     EDMLOGD("ApplicationManagerProxy::AddKeepAliveAppsWithDisallowModify");
158     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
159     MessageParcel data;
160     MessageParcel reply;
161     std::uint32_t funcCode =
162         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::MANAGE_KEEP_ALIVE_APPS);
163     data.WriteInterfaceToken(DESCRIPTOR);
164     data.WriteInt32(HAS_USERID);
165     data.WriteInt32(userId);
166     data.WriteParcelable(&admin);
167     data.WriteString(WITHOUT_PERMISSION_TAG);
168     data.WriteStringVector(keepAliveApps);
169     data.WriteBool(disallowModify);
170     ErrCode ret = proxy->HandleDevicePolicy(funcCode, data, reply);
171     if (ret != ERR_OK) {
172         retMessage = reply.ReadString();
173         return ret;
174     }
175     return ERR_OK;
176 }
177 
RemoveKeepAliveApps(const AppExecFwk::ElementName & admin,const std::vector<std::string> & keepAliveApps,int32_t userId)178 int32_t ApplicationManagerProxy::RemoveKeepAliveApps(const AppExecFwk::ElementName &admin,
179     const std::vector<std::string> &keepAliveApps, int32_t userId)
180 {
181     EDMLOGD("ApplicationManagerProxy::RemoveKeepAliveApps");
182     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
183     MessageParcel data;
184     std::uint32_t funcCode =
185         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::MANAGE_KEEP_ALIVE_APPS);
186     data.WriteInterfaceToken(DESCRIPTOR);
187     data.WriteInt32(HAS_USERID);
188     data.WriteInt32(userId);
189     data.WriteParcelable(&admin);
190     data.WriteString(WITHOUT_PERMISSION_TAG);
191     data.WriteStringVector(keepAliveApps);
192     return proxy->HandleDevicePolicy(funcCode, data);
193 }
194 
GetKeepAliveApps(const AppExecFwk::ElementName & admin,std::vector<std::string> & keepAliveApps,int32_t userId)195 int32_t ApplicationManagerProxy::GetKeepAliveApps(const AppExecFwk::ElementName &admin,
196     std::vector<std::string> &keepAliveApps, int32_t userId)
197 {
198     EDMLOGD("ApplicationManagerProxy::GetKeepAliveApps");
199     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
200     MessageParcel data;
201     MessageParcel reply;
202     data.WriteInterfaceToken(DESCRIPTOR);
203     data.WriteInt32(HAS_USERID);
204     data.WriteInt32(userId);
205     data.WriteString(WITHOUT_PERMISSION_TAG);
206     data.WriteInt32(HAS_ADMIN);
207     data.WriteParcelable(&admin);
208     data.WriteString(EdmConstants::KeepAlive::GET_MANAGE_KEEP_ALIVE_APPS_BUNDLE_NAME);
209     proxy->GetPolicy(EdmInterfaceCode::MANAGE_KEEP_ALIVE_APPS, data, reply);
210 
211     int32_t ret = ERR_INVALID_VALUE;
212     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
213     if (!blRes) {
214         EDMLOGW("EnterpriseDeviceMgrProxy::GetPolicy fail. %{public}d", ret);
215         return ret;
216     }
217     reply.ReadStringVector(&keepAliveApps);
218     return ERR_OK;
219 }
220 
SetKioskFeatures(MessageParcel & data)221 int32_t ApplicationManagerProxy::SetKioskFeatures(MessageParcel &data)
222 {
223     EDMLOGD("ApplicationManagerProxy::SetKioskFeatures");
224     MessageParcel reply;
225     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::SET_KIOSK_FEATURE);
226     return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data);
227 }
228 
SetAllowedKioskApps(const AppExecFwk::ElementName & admin,const std::vector<std::string> & appIdentifiers)229 int32_t ApplicationManagerProxy::SetAllowedKioskApps(
230     const AppExecFwk::ElementName &admin, const std::vector<std::string> &appIdentifiers)
231 {
232     EDMLOGI("ApplicationManagerProxy::SetAllowedKioskApps");
233     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
234     MessageParcel data;
235     std::uint32_t funcCode =
236             POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::ALLOWED_KIOSK_APPS);
237     data.WriteInterfaceToken(DESCRIPTOR);
238     data.WriteInt32(WITHOUT_USERID);
239     data.WriteParcelable(&admin);
240     data.WriteString(WITHOUT_PERMISSION_TAG);
241     data.WriteStringVector(appIdentifiers);
242     return proxy->HandleDevicePolicy(funcCode, data);
243 }
244 
GetAllowedKioskApps(const AppExecFwk::ElementName & admin,std::vector<std::string> & appIdentifiers)245 int32_t ApplicationManagerProxy::GetAllowedKioskApps(
246     const AppExecFwk::ElementName &admin, std::vector<std::string> &appIdentifiers)
247 {
248     EDMLOGI("ApplicationManagerProxy::GetAllowedKioskApps");
249     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
250     MessageParcel data;
251     MessageParcel reply;
252     data.WriteInterfaceToken(DESCRIPTOR);
253     data.WriteInt32(WITHOUT_USERID);
254     data.WriteString(WITHOUT_PERMISSION_TAG);
255     data.WriteInt32(HAS_ADMIN);
256     data.WriteParcelable(&admin);
257     proxy->GetPolicy(EdmInterfaceCode::ALLOWED_KIOSK_APPS, data, reply);
258 
259     int32_t ret = ERR_INVALID_VALUE;
260     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
261     if (!blRes) {
262         EDMLOGW("EnterpriseDeviceMgrProxy::GetPolicy fail. %{public}d", ret);
263         return ret;
264     }
265     reply.ReadStringVector(&appIdentifiers);
266     return ERR_OK;
267 }
268 
IsAppKioskAllowed(const std::string & appIdentifier,bool & isAllowed)269 int32_t ApplicationManagerProxy::IsAppKioskAllowed(const std::string &appIdentifier, bool &isAllowed)
270 {
271     EDMLOGI("ApplicationManagerProxy::IsAppKioskAllowed");
272     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
273     MessageParcel data;
274     MessageParcel reply;
275     data.WriteInterfaceToken(DESCRIPTOR);
276     data.WriteInt32(WITHOUT_USERID);
277     data.WriteString(WITHOUT_PERMISSION_TAG);
278     data.WriteInt32(WITHOUT_ADMIN);
279     data.WriteString(appIdentifier);
280     proxy->GetPolicy(EdmInterfaceCode::IS_APP_KIOSK_ALLOWED, data, reply);
281     int32_t ret = ERR_INVALID_VALUE;
282     int32_t readRet = reply.ReadInt32(ret);
283     if (readRet && (ret == EdmReturnErrCode::ADMIN_INACTIVE)) {
284         isAllowed = false;
285         return ERR_OK;
286     }
287     bool blRes = readRet && (ret == ERR_OK);
288     if (!blRes) {
289         EDMLOGW("ApplicationManagerProxy::IsAppKioskAllowed fail. %{public}d", ret);
290         return ret;
291     }
292     reply.ReadBool(isAllowed);
293     return ERR_OK;
294 }
295 
296 
ClearUpApplicationData(const AppExecFwk::ElementName & admin,const ClearUpApplicationDataParam & param)297 int32_t ApplicationManagerProxy::ClearUpApplicationData(
298     const AppExecFwk::ElementName &admin, const ClearUpApplicationDataParam &param)
299 {
300     EDMLOGI("ApplicationManagerProxy::ClearUpApplicationData");
301     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
302     MessageParcel data;
303     std::uint32_t funcCode =
304         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::CLEAR_UP_APPLICATION_DATA);
305     data.WriteInterfaceToken(DESCRIPTOR);
306     data.WriteInt32(WITHOUT_USERID);
307     data.WriteParcelable(&admin);
308     data.WriteString(WITHOUT_PERMISSION_TAG);
309     MessageParcelUtils::WriteClearUpApplicationDataParam(param, data);
310     return proxy->HandleDevicePolicy(funcCode, data);
311 }
312 
IsModifyKeepAliveAppsDisallowed(const AppExecFwk::ElementName & admin,std::string & keepAliveApp,int32_t userId,bool & disallowModify)313 int32_t ApplicationManagerProxy::IsModifyKeepAliveAppsDisallowed(const AppExecFwk::ElementName &admin,
314     std::string &keepAliveApp, int32_t userId, bool &disallowModify)
315 {
316     EDMLOGD("ApplicationManagerProxy::IsModifyKeepAliveAppsDisallowed");
317     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
318     MessageParcel data;
319     MessageParcel reply;
320     data.WriteInterfaceToken(DESCRIPTOR);
321     data.WriteInt32(HAS_USERID);
322     data.WriteInt32(userId);
323     data.WriteString(WITHOUT_PERMISSION_TAG);
324     data.WriteInt32(HAS_ADMIN);
325     data.WriteParcelable(&admin);
326     data.WriteString(EdmConstants::KeepAlive::GET_MANAGE_KEEP_ALIVE_APP_DISALLOW_MODIFY);
327     data.WriteString(keepAliveApp);
328     proxy->GetPolicy(EdmInterfaceCode::MANAGE_KEEP_ALIVE_APPS, data, reply);
329 
330     int32_t ret = ERR_INVALID_VALUE;
331     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
332     if (!blRes) {
333         EDMLOGW("EnterpriseDeviceMgrProxy::GetPolicy fail. %{public}d", ret);
334         return ret;
335     }
336     reply.ReadBool(disallowModify);
337     return ERR_OK;
338 }
339 } // namespace EDM
340 } // namespace OHOS
341