• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "enterprise_device_mgr_proxy.h"
17 
18 #include <iservice_registry.h>
19 #include <string_ex.h>
20 
21 #include "admin_type.h"
22 #include "edm_errors.h"
23 #include "edm_load_manager.h"
24 #include "edm_log.h"
25 #include "edm_sys_manager.h"
26 #include "func_code.h"
27 #include "parameters.h"
28 #include "system_ability_definition.h"
29 
30 namespace OHOS {
31 namespace EDM {
32 std::shared_ptr<EnterpriseDeviceMgrProxy> EnterpriseDeviceMgrProxy::instance_ = nullptr;
33 std::mutex EnterpriseDeviceMgrProxy::mutexLock_;
34 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
35 const uint32_t GET_ENABLE_ADMIN = 5;
36 
GetInstance()37 std::shared_ptr<EnterpriseDeviceMgrProxy> EnterpriseDeviceMgrProxy::GetInstance()
38 {
39     if (instance_ == nullptr) {
40         std::lock_guard<std::mutex> lock(mutexLock_);
41         if (instance_ == nullptr) {
42             std::shared_ptr<EnterpriseDeviceMgrProxy> temp = std::make_shared<EnterpriseDeviceMgrProxy>();
43             instance_ = temp;
44         }
45     }
46     return instance_;
47 }
48 
DestroyInstance()49 void EnterpriseDeviceMgrProxy::DestroyInstance()
50 {
51     std::lock_guard<std::mutex> lock(mutexLock_);
52     if (instance_ != nullptr) {
53         instance_.reset();
54         instance_ = nullptr;
55     }
56 }
57 
IsEdmEnabled()58 bool EnterpriseDeviceMgrProxy::IsEdmEnabled()
59 {
60     std::string edmParaValue = system::GetParameter("persist.edm.edm_enable", "false");
61     EDMLOGD("EnterpriseDeviceMgrProxy::GetParameter %{public}s", edmParaValue.c_str());
62     return edmParaValue == "true";
63 }
64 
EnableAdmin(AppExecFwk::ElementName & admin,EntInfo & entInfo,AdminType type,int32_t userId)65 ErrCode EnterpriseDeviceMgrProxy::EnableAdmin(AppExecFwk::ElementName &admin, EntInfo &entInfo, AdminType type,
66     int32_t userId)
67 {
68     EDMLOGD("EnterpriseDeviceMgrProxy::EnableAdmin");
69     sptr<IRemoteObject> remote = LoadAndGetEdmService();
70     if (!remote) {
71         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
72     }
73     MessageParcel data;
74     MessageParcel reply;
75     MessageOption option;
76     data.WriteInterfaceToken(DESCRIPTOR);
77     data.WriteParcelable(&admin);
78     entInfo.Marshalling(data);
79     data.WriteInt32(static_cast<int32_t>(type));
80     data.WriteInt32(userId);
81     ErrCode res = remote->SendRequest(EdmInterfaceCode::ADD_DEVICE_ADMIN, data, reply, option);
82     if (FAILED(res)) {
83         EDMLOGE("EnterpriseDeviceMgrProxy:EnableAdmin send request fail. %{public}d", res);
84         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
85     }
86     int32_t resCode = ERR_OK;
87     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
88         EDMLOGW("EnterpriseDeviceMgrProxy:EnableAdmin get result code fail. %{public}d", resCode);
89         return resCode;
90     }
91     return ERR_OK;
92 }
93 
DisableAdmin(AppExecFwk::ElementName & admin,int32_t userId)94 ErrCode EnterpriseDeviceMgrProxy::DisableAdmin(AppExecFwk::ElementName &admin, int32_t userId)
95 {
96     EDMLOGD("EnterpriseDeviceMgrProxy::DisableAdmin");
97     if (!IsEdmEnabled()) {
98         return EdmReturnErrCode::DISABLE_ADMIN_FAILED;
99     }
100     sptr<IRemoteObject> remote = LoadAndGetEdmService();
101     if (!remote) {
102         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
103     }
104     MessageParcel data;
105     MessageParcel reply;
106     MessageOption option;
107     data.WriteInterfaceToken(DESCRIPTOR);
108     data.WriteParcelable(&admin);
109     data.WriteInt32(userId);
110     ErrCode res = remote->SendRequest(EdmInterfaceCode::REMOVE_DEVICE_ADMIN, data, reply, option);
111     if (FAILED(res)) {
112         EDMLOGE("EnterpriseDeviceMgrProxy:DisableAdmin send request fail. %{public}d", res);
113         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
114     }
115     int32_t resCode = ERR_OK;
116     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
117         EDMLOGW("EnterpriseDeviceMgrProxy:DisableAdmin get result code fail. %{public}d", resCode);
118         return resCode;
119     }
120     return ERR_OK;
121 }
122 
DisableSuperAdmin(const std::string & bundleName)123 ErrCode EnterpriseDeviceMgrProxy::DisableSuperAdmin(const std::string &bundleName)
124 {
125     EDMLOGD("EnterpriseDeviceMgrProxy::DisableSuperAdmin");
126     if (!IsEdmEnabled()) {
127         return EdmReturnErrCode::DISABLE_ADMIN_FAILED;
128     }
129     sptr<IRemoteObject> remote = LoadAndGetEdmService();
130     if (!remote) {
131         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
132     }
133     MessageParcel data;
134     MessageParcel reply;
135     MessageOption option;
136     data.WriteInterfaceToken(DESCRIPTOR);
137     data.WriteString(bundleName);
138     ErrCode res = remote->SendRequest(EdmInterfaceCode::REMOVE_SUPER_ADMIN, data, reply, option);
139     if (FAILED(res)) {
140         EDMLOGE("EnterpriseDeviceMgrProxy:DisableSuperAdmin send request fail. %{public}d", res);
141         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
142     }
143     int32_t resCode = ERR_OK;
144     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
145         EDMLOGW("EnterpriseDeviceMgrProxy:DisableSuperAdmin get result code fail. %{public}d", resCode);
146         return resCode;
147     }
148     return ERR_OK;
149 }
150 
GetEnabledAdmin(AdminType type,std::vector<std::string> & enabledAdminList)151 ErrCode EnterpriseDeviceMgrProxy::GetEnabledAdmin(AdminType type, std::vector<std::string> &enabledAdminList)
152 {
153     EDMLOGD("EnterpriseDeviceMgrProxy::GetEnabledAdmin");
154     if (!IsEdmEnabled()) {
155         return EdmReturnErrCode::ADMIN_INACTIVE;
156     }
157     sptr<IRemoteObject> remote = LoadAndGetEdmService();
158     if (!remote) {
159         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
160     }
161     MessageParcel data;
162     MessageParcel reply;
163     MessageOption option;
164     data.WriteInterfaceToken(DESCRIPTOR);
165     data.WriteInt32(static_cast<int32_t>(type));
166     ErrCode res = remote->SendRequest(EdmInterfaceCode::GET_ENABLED_ADMIN, data, reply, option);
167     if (FAILED(res)) {
168         EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmin send request fail. %{public}d", res);
169         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
170     }
171     int32_t resCode = ERR_OK;
172     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
173         EDMLOGW("EnterpriseDeviceMgrProxy:GetEnabledAdmin get result code fail. %{public}d", resCode);
174         return resCode;
175     }
176     reply.ReadStringVector(&enabledAdminList);
177     return ERR_OK;
178 }
179 
GetEnterpriseInfo(AppExecFwk::ElementName & admin,EntInfo & entInfo)180 ErrCode EnterpriseDeviceMgrProxy::GetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo)
181 {
182     EDMLOGD("EnterpriseDeviceMgrProxy::GetEnterpriseInfo");
183     if (!IsEdmEnabled()) {
184         return EdmReturnErrCode::ADMIN_INACTIVE;
185     }
186     sptr<IRemoteObject> remote = LoadAndGetEdmService();
187     if (!remote) {
188         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
189     }
190     MessageParcel data;
191     MessageParcel reply;
192     MessageOption option;
193     data.WriteInterfaceToken(DESCRIPTOR);
194     data.WriteParcelable(&admin);
195     ErrCode res = remote->SendRequest(EdmInterfaceCode::GET_ENT_INFO, data, reply, option);
196     if (FAILED(res)) {
197         EDMLOGE("EnterpriseDeviceMgrProxy:GetEnterpriseInfo send request fail. %{public}d", res);
198         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
199     }
200     int32_t resCode = ERR_OK;
201     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
202         EDMLOGW("EnterpriseDeviceMgrProxy:GetEnterpriseInfo get result code fail. %{public}d", resCode);
203         return resCode;
204     }
205     if (!EntInfo::Unmarshalling(reply, entInfo)) {
206         EDMLOGE("EnterpriseDeviceMgrProxy::GetEnterpriseInfo read parcel fail");
207         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
208     }
209     return ERR_OK;
210 }
211 
SetEnterpriseInfo(AppExecFwk::ElementName & admin,EntInfo & entInfo)212 ErrCode EnterpriseDeviceMgrProxy::SetEnterpriseInfo(AppExecFwk::ElementName &admin, EntInfo &entInfo)
213 {
214     EDMLOGD("EnterpriseDeviceMgrProxy::SetEnterpriseInfo");
215     if (!IsEdmEnabled()) {
216         return EdmReturnErrCode::ADMIN_INACTIVE;
217     }
218     sptr<IRemoteObject> remote = LoadAndGetEdmService();
219     if (!remote) {
220         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
221     }
222     MessageParcel data;
223     MessageParcel reply;
224     MessageOption option;
225     data.WriteInterfaceToken(DESCRIPTOR);
226     data.WriteParcelable(&admin);
227     entInfo.Marshalling(data);
228     ErrCode res = remote->SendRequest(EdmInterfaceCode::SET_ENT_INFO, data, reply, option);
229     if (FAILED(res)) {
230         EDMLOGE("EnterpriseDeviceMgrProxy:SetEnterpriseInfo send request fail. %{public}d", res);
231         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
232     }
233     int32_t resCode = ERR_OK;
234     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
235         EDMLOGW("EnterpriseDeviceMgrProxy:SetEnterpriseInfo get result code fail. %{public}d", resCode);
236         return resCode;
237     }
238     return ERR_OK;
239 }
240 
HandleManagedEvent(const AppExecFwk::ElementName & admin,const std::vector<uint32_t> & events,bool subscribe)241 ErrCode EnterpriseDeviceMgrProxy::HandleManagedEvent(const AppExecFwk::ElementName &admin,
242     const std::vector<uint32_t> &events, bool subscribe)
243 {
244     EDMLOGD("EnterpriseDeviceMgrProxy::SubscribeManagedEvent: %{public}d", subscribe);
245     if (!IsEdmEnabled()) {
246         return EdmReturnErrCode::ADMIN_INACTIVE;
247     }
248     sptr<IRemoteObject> remote = LoadAndGetEdmService();
249     if (!remote) {
250         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
251     }
252     MessageParcel data;
253     MessageParcel reply;
254     MessageOption option;
255     data.WriteInterfaceToken(DESCRIPTOR);
256     data.WriteParcelable(&admin);
257     data.WriteUInt32Vector(events);
258     uint32_t policyCode = EdmInterfaceCode::SUBSCRIBE_MANAGED_EVENT;
259     if (!subscribe) {
260         policyCode = EdmInterfaceCode::UNSUBSCRIBE_MANAGED_EVENT;
261     }
262     ErrCode res = remote->SendRequest(policyCode, data, reply, option);
263     if (FAILED(res)) {
264         EDMLOGE("EnterpriseDeviceMgrProxy:SubscribeManagedEvent send request fail. %{public}d", res);
265         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
266     }
267     int32_t retCode;
268     reply.ReadInt32(retCode);
269     return retCode;
270 }
271 
IsSuperAdmin(const std::string & bundleName,bool & result)272 ErrCode EnterpriseDeviceMgrProxy::IsSuperAdmin(const std::string &bundleName, bool &result)
273 {
274     EDMLOGD("EnterpriseDeviceMgrProxy::IsSuperAdmin");
275     result = false;
276     if (!IsEdmEnabled()) {
277         return ERR_OK;
278     }
279     sptr<IRemoteObject> remote = LoadAndGetEdmService();
280     if (!remote) {
281         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
282     }
283     MessageParcel data;
284     MessageParcel reply;
285     MessageOption option;
286     data.WriteInterfaceToken(DESCRIPTOR);
287     data.WriteString(bundleName);
288     ErrCode res = remote->SendRequest(EdmInterfaceCode::IS_SUPER_ADMIN, data, reply, option);
289     if (FAILED(res)) {
290         EDMLOGE("EnterpriseDeviceMgrProxy:IsSuperAdmin send request fail. %{public}d", res);
291         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
292     }
293     int32_t resCode = ERR_OK;
294     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
295         EDMLOGW("EnterpriseDeviceMgrProxy:SetEnterpriseInfo get result code fail. %{public}d", resCode);
296         return resCode;
297     }
298     reply.ReadBool(result);
299     EDMLOGD("EnterpriseDeviceMgrProxy::IsSuperAdmin ret %{public}d", result);
300     return ERR_OK;
301 }
302 
IsAdminEnabled(AppExecFwk::ElementName & admin,int32_t userId,bool & result)303 ErrCode EnterpriseDeviceMgrProxy::IsAdminEnabled(AppExecFwk::ElementName &admin, int32_t userId, bool &result)
304 {
305     EDMLOGD("EnterpriseDeviceMgrProxy::IsAdminEnabled");
306     result = false;
307     if (!IsEdmEnabled()) {
308         return ERR_OK;
309     }
310     sptr<IRemoteObject> remote = LoadAndGetEdmService();
311     if (!remote) {
312         return false;
313     }
314     MessageParcel data;
315     MessageParcel reply;
316     MessageOption option;
317     data.WriteInterfaceToken(DESCRIPTOR);
318     data.WriteParcelable(&admin);
319     data.WriteInt32(userId);
320     ErrCode res = remote->SendRequest(EdmInterfaceCode::IS_ADMIN_ENABLED, data, reply, option);
321     if (FAILED(res)) {
322         EDMLOGE("EnterpriseDeviceMgrProxy:IsAdminEnabled send request fail. %{public}d", res);
323         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
324     }
325     int32_t resCode = ERR_OK;
326     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
327         EDMLOGW("EnterpriseDeviceMgrProxy:IsAdminEnabled get result code fail. %{public}d", resCode);
328         return resCode;
329     }
330     reply.ReadBool(result);
331     EDMLOGD("EnterpriseDeviceMgrProxy::IsAdminEnabled ret %{public}d", result);
332     return ERR_OK;
333 }
334 
IsPolicyDisabled(const AppExecFwk::ElementName * admin,int policyCode,bool & result)335 int32_t EnterpriseDeviceMgrProxy::IsPolicyDisabled(const AppExecFwk::ElementName *admin, int policyCode, bool &result)
336 {
337     MessageParcel data;
338     data.WriteInterfaceToken(DESCRIPTOR);
339     data.WriteInt32(WITHOUT_USERID);
340     if (admin != nullptr) {
341         data.WriteInt32(HAS_ADMIN);
342         data.WriteParcelable(admin);
343     } else {
344         if (!IsEdmEnabled()) {
345             result = false;
346             return ERR_OK;
347         }
348         data.WriteInt32(WITHOUT_ADMIN);
349     }
350     MessageParcel reply;
351     GetPolicy(policyCode, data, reply);
352     int32_t ret = ERR_INVALID_VALUE;
353     bool isSuccess = reply.ReadInt32(ret) && (ret == ERR_OK);
354     if (!isSuccess) {
355         EDMLOGE("IsPolicyDisabled:GetPolicy fail. %{public}d", ret);
356         return ret;
357     }
358     reply.ReadBool(result);
359     return ERR_OK;
360 }
361 
HandleDevicePolicy(int32_t policyCode,MessageParcel & data)362 int32_t EnterpriseDeviceMgrProxy::HandleDevicePolicy(int32_t policyCode, MessageParcel &data)
363 {
364     MessageParcel reply;
365     return HandleDevicePolicy(policyCode, data, reply);
366 }
367 
HandleDevicePolicy(int32_t policyCode,MessageParcel & data,MessageParcel & reply)368 int32_t EnterpriseDeviceMgrProxy::HandleDevicePolicy(int32_t policyCode, MessageParcel &data, MessageParcel &reply)
369 {
370     EDMLOGD("EnterpriseDeviceMgrProxy::HandleDevicePolicy");
371     if (!IsEdmEnabled()) {
372         return EdmReturnErrCode::ADMIN_INACTIVE;
373     }
374     sptr<IRemoteObject> remote = LoadAndGetEdmService();
375     if (!remote) {
376         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
377     }
378     MessageOption option;
379     EDMLOGD("EnterpriseDeviceMgrProxy::handleDevicePolicy::sendRequest %{public}d", policyCode);
380     ErrCode res = remote->SendRequest(policyCode, data, reply, option);
381     if (FAILED(res)) {
382         EDMLOGE("EnterpriseDeviceMgrProxy:HandleDevicePolicy send request fail. %{public}d", res);
383         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
384     }
385     int32_t ret = ERR_INVALID_VALUE;
386     reply.ReadInt32(ret);
387     return ret;
388 }
389 
AuthorizeAdmin(const AppExecFwk::ElementName & admin,const std::string & bundleName)390 ErrCode EnterpriseDeviceMgrProxy::AuthorizeAdmin(const AppExecFwk::ElementName &admin, const std::string &bundleName)
391 {
392     EDMLOGD("EnterpriseDeviceMgrProxy::AuthorizeAdmin");
393     if (!IsEdmEnabled()) {
394         return EdmReturnErrCode::ADMIN_INACTIVE;
395     }
396     sptr<IRemoteObject> remote = LoadAndGetEdmService();
397     if (!remote) {
398         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
399     }
400     MessageParcel data;
401     MessageParcel reply;
402     MessageOption option;
403     data.WriteInterfaceToken(DESCRIPTOR);
404     data.WriteParcelable(&admin);
405     data.WriteString(bundleName);
406     ErrCode res = remote->SendRequest(EdmInterfaceCode::AUTHORIZE_ADMIN, data, reply, option);
407     if (FAILED(res)) {
408         EDMLOGE("EnterpriseDeviceMgrProxy:AuthorizeAdmin send request fail. %{public}d", res);
409         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
410     }
411     int32_t resCode = ERR_INVALID_VALUE;
412     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
413         EDMLOGW("EnterpriseDeviceMgrProxy:AuthorizeAdmin get result code fail. %{public}d", resCode);
414         return resCode;
415     }
416     return ERR_OK;
417 }
418 
GetPolicyValue(AppExecFwk::ElementName * admin,int policyCode,std::string & policyData,int32_t userId)419 bool EnterpriseDeviceMgrProxy::GetPolicyValue(AppExecFwk::ElementName *admin, int policyCode, std::string &policyData,
420     int32_t userId)
421 {
422     MessageParcel reply;
423     if (!GetPolicyData(admin, policyCode, userId, reply)) {
424         return false;
425     }
426     reply.ReadString(policyData);
427     return true;
428 }
429 
GetPolicyArray(AppExecFwk::ElementName * admin,int policyCode,std::vector<std::string> & policyData,int32_t userId)430 bool EnterpriseDeviceMgrProxy::GetPolicyArray(AppExecFwk::ElementName *admin, int policyCode,
431     std::vector<std::string> &policyData, int32_t userId)
432 {
433     MessageParcel reply;
434     if (!GetPolicyData(admin, policyCode, userId, reply)) {
435         return false;
436     }
437     int32_t size = reply.ReadInt32();
438     EDMLOGD("EnterpriseDeviceMgrProxy::GetPolicyArray size: %{public}d.", size);
439     return reply.ReadStringVector(&policyData);
440 }
441 
GetPolicyMap(AppExecFwk::ElementName * admin,int policyCode,std::map<std::string,std::string> & policyData,int32_t userId)442 bool EnterpriseDeviceMgrProxy::GetPolicyMap(AppExecFwk::ElementName *admin, int policyCode,
443     std::map<std::string, std::string> &policyData, int32_t userId)
444 {
445     MessageParcel reply;
446     if (!GetPolicyData(admin, policyCode, userId, reply)) {
447         return false;
448     }
449     std::vector<std::string> keys;
450     if (!reply.ReadStringVector(&keys)) {
451         EDMLOGE("EnterpriseDeviceMgrProxy::read map keys fail.");
452         return false;
453     }
454     std::vector<std::string> values;
455     if (!reply.ReadStringVector(&values)) {
456         EDMLOGE("EnterpriseDeviceMgrProxy::read map values fail.");
457         return false;
458     }
459     if (keys.size() != values.size()) {
460         EDMLOGE("EnterpriseDeviceMgrProxy::read map fail.");
461         return false;
462     }
463     policyData.clear();
464     for (uint64_t i = 0; i < keys.size(); ++i) {
465         policyData.insert(std::make_pair(keys.at(i), values.at(i)));
466     }
467     return true;
468 }
469 
GetPolicyData(AppExecFwk::ElementName * admin,int policyCode,int32_t userId,MessageParcel & reply)470 bool EnterpriseDeviceMgrProxy::GetPolicyData(AppExecFwk::ElementName *admin, int policyCode, int32_t userId,
471     MessageParcel &reply)
472 {
473     if (!IsEdmEnabled()) {
474         return false;
475     }
476     MessageParcel data;
477     data.WriteInterfaceToken(DESCRIPTOR);
478     data.WriteInt32(HAS_USERID);
479     data.WriteInt32(userId);
480     if (admin != nullptr) {
481         data.WriteInt32(HAS_ADMIN);
482         data.WriteParcelable(admin);
483     } else {
484         data.WriteInt32(WITHOUT_ADMIN);
485     }
486     int32_t ret = ERR_INVALID_VALUE;
487     return GetPolicy(policyCode, data, reply) && reply.ReadInt32(ret) && (ret == ERR_OK);
488 }
489 
GetPolicy(int policyCode,MessageParcel & data,MessageParcel & reply)490 bool EnterpriseDeviceMgrProxy::GetPolicy(int policyCode, MessageParcel &data, MessageParcel &reply)
491 {
492     if (!IsEdmEnabled()) {
493         reply.WriteInt32(EdmReturnErrCode::ADMIN_INACTIVE);
494         return false;
495     }
496     if (policyCode < 0) {
497         EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy invalid policyCode:%{public}d", policyCode);
498         return false;
499     }
500     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::GET, (std::uint32_t)policyCode);
501     sptr<IRemoteObject> remote = LoadAndGetEdmService();
502     if (!remote) {
503         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
504         return false;
505     }
506     MessageOption option;
507     ErrCode res = remote->SendRequest(funcCode, data, reply, option);
508     if (FAILED(res)) {
509         EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy send request fail.");
510         return false;
511     }
512     return true;
513 }
514 
GetEnabledAdmins(std::vector<std::string> & enabledAdminList)515 void EnterpriseDeviceMgrProxy::GetEnabledAdmins(std::vector<std::string> &enabledAdminList)
516 {
517     GetEnabledAdmins(AdminType::NORMAL, enabledAdminList);
518 }
519 
GetEnabledSuperAdmin(std::string & enabledAdmin)520 void EnterpriseDeviceMgrProxy::GetEnabledSuperAdmin(std::string &enabledAdmin)
521 {
522     std::vector<std::string> enabledAdminList;
523     GetEnabledAdmins(AdminType::ENT, enabledAdminList);
524     if (!enabledAdminList.empty()) {
525         enabledAdmin = enabledAdminList[0];
526     }
527 }
528 
IsSuperAdminExist()529 bool EnterpriseDeviceMgrProxy::IsSuperAdminExist()
530 {
531     std::vector<std::string> enabledAdminList;
532     GetEnabledAdmins(AdminType::ENT, enabledAdminList);
533     return !enabledAdminList.empty();
534 }
535 
LoadAndGetEdmService()536 sptr<IRemoteObject> EnterpriseDeviceMgrProxy::LoadAndGetEdmService()
537 {
538     std::lock_guard<std::mutex> lock(mutexLock_);
539     sptr<ISystemAbilityManager> sysAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
540     if (sysAbilityMgr == nullptr) {
541         EDMLOGE("EnterpriseDeviceMgrProxy::failed to get SystemAbilityManager");
542         return nullptr;
543     }
544     auto objectSA = sysAbilityMgr->CheckSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
545     if (objectSA == nullptr) {
546         EDMLOGI("EnterpriseDeviceMgrProxy::load sa from remote");
547         return EdmLoadManager::GetInstance().LoadAndGetEdmService();
548     }
549     return EdmSysManager::GetRemoteObjectOfSystemAbility(ENTERPRISE_DEVICE_MANAGER_SA_ID);
550 }
551 
GetEnabledAdmins(AdminType type,std::vector<std::string> & enabledAdminList)552 void EnterpriseDeviceMgrProxy::GetEnabledAdmins(AdminType type, std::vector<std::string> &enabledAdminList)
553 {
554     if (!IsEdmEnabled()) {
555         return;
556     }
557     sptr<IRemoteObject> remote = LoadAndGetEdmService();
558     if (!remote) {
559         return;
560     }
561     MessageParcel data;
562     MessageParcel reply;
563     MessageOption option;
564     data.WriteInterfaceToken(DESCRIPTOR);
565     data.WriteInt32(static_cast<int32_t>(type));
566     ErrCode res = remote->SendRequest(GET_ENABLE_ADMIN, data, reply, option);
567     if (FAILED(res)) {
568         EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmins send request fail.");
569         return;
570     }
571     int32_t resCode = ERR_OK;
572     if (!reply.ReadInt32(resCode) || FAILED(resCode)) {
573         EDMLOGE("EnterpriseDeviceMgrProxy:GetEnabledAdmins get result code fail.");
574         return;
575     }
576     std::vector<std::string> readArray;
577     reply.ReadStringVector(&readArray);
578     for (const std::string &item : readArray) {
579         enabledAdminList.push_back(item);
580     }
581 }
582 
SetPolicyDisabled(const AppExecFwk::ElementName & admin,bool isDisabled,uint32_t policyCode)583 int32_t EnterpriseDeviceMgrProxy::SetPolicyDisabled(const AppExecFwk::ElementName &admin, bool isDisabled,
584     uint32_t policyCode)
585 {
586     MessageParcel data;
587     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, policyCode);
588     data.WriteInterfaceToken(DESCRIPTOR);
589     data.WriteInt32(WITHOUT_USERID);
590     data.WriteParcelable(&admin);
591     data.WriteBool(isDisabled);
592     return HandleDevicePolicy(funcCode, data);
593 }
594 } // namespace EDM
595 } // namespace OHOS