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
22 namespace OHOS {
23 namespace EDM {
24 std::shared_ptr<ApplicationManagerProxy> ApplicationManagerProxy::instance_ = nullptr;
25 std::mutex ApplicationManagerProxy::mutexLock_;
26 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
27
GetApplicationManagerProxy()28 std::shared_ptr<ApplicationManagerProxy> ApplicationManagerProxy::GetApplicationManagerProxy()
29 {
30 if (instance_ == nullptr) {
31 std::lock_guard<std::mutex> lock(mutexLock_);
32 if (instance_ == nullptr) {
33 std::shared_ptr<ApplicationManagerProxy> temp = std::make_shared<ApplicationManagerProxy>();
34 instance_ = temp;
35 }
36 }
37 return instance_;
38 }
39
AddDisallowedRunningBundles(AppExecFwk::ElementName & admin,std::vector<std::string> & bundles,int32_t userId)40 int32_t ApplicationManagerProxy::AddDisallowedRunningBundles(AppExecFwk::ElementName &admin,
41 std::vector<std::string> &bundles, int32_t userId)
42 {
43 EDMLOGD("ApplicationManagerProxy::AddDisallowedRunningBundles");
44 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
45 if (proxy == nullptr) {
46 EDMLOGE("can not get EnterpriseDeviceMgrProxy");
47 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
48 }
49 MessageParcel data;
50 std::uint32_t funcCode =
51 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISALLOW_RUNNING_BUNDLES);
52 data.WriteInterfaceToken(DESCRIPTOR);
53 data.WriteInt32(HAS_USERID);
54 data.WriteInt32(userId);
55 data.WriteParcelable(&admin);
56 data.WriteStringVector(bundles);
57 return proxy->HandleDevicePolicy(funcCode, data);
58 }
59
RemoveDisallowedRunningBundles(AppExecFwk::ElementName & admin,std::vector<std::string> & bundles,int32_t userId)60 int32_t ApplicationManagerProxy::RemoveDisallowedRunningBundles(AppExecFwk::ElementName &admin,
61 std::vector<std::string> &bundles, int32_t userId)
62 {
63 EDMLOGD("ApplicationManagerProxy::RemoveDisallowedRunningBundles");
64 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
65 if (proxy == nullptr) {
66 EDMLOGE("can not get EnterpriseDeviceMgrProxy");
67 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
68 }
69 MessageParcel data;
70 std::uint32_t funcCode =
71 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::DISALLOW_RUNNING_BUNDLES);
72 data.WriteInterfaceToken(DESCRIPTOR);
73 data.WriteInt32(HAS_USERID);
74 data.WriteInt32(userId);
75 data.WriteParcelable(&admin);
76 data.WriteStringVector(bundles);
77 return proxy->HandleDevicePolicy(funcCode, data);
78 }
79
GetDisallowedRunningBundles(AppExecFwk::ElementName & admin,int32_t userId,std::vector<std::string> & bundles)80 int32_t ApplicationManagerProxy::GetDisallowedRunningBundles(AppExecFwk::ElementName &admin, int32_t userId,
81 std::vector<std::string> &bundles)
82 {
83 EDMLOGD("ApplicationManagerProxy::GetDisallowedRunningBundles");
84 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
85 if (proxy == nullptr) {
86 EDMLOGE("can not get EnterpriseDeviceMgrProxy");
87 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
88 }
89 MessageParcel data;
90 MessageParcel reply;
91 data.WriteInterfaceToken(DESCRIPTOR);
92 data.WriteInt32(HAS_USERID);
93 data.WriteInt32(userId);
94 data.WriteInt32(HAS_ADMIN);
95 data.WriteParcelable(&admin);
96 proxy->GetPolicy(EdmInterfaceCode::DISALLOW_RUNNING_BUNDLES, data, reply);
97 int32_t ret = ERR_INVALID_VALUE;
98 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
99 if (!blRes) {
100 EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
101 return ret;
102 }
103 int32_t size = reply.ReadInt32();
104 if (size > EdmConstants::APPID_MAX_SIZE) {
105 EDMLOGE("bundles size=[%{public}d] is too large", size);
106 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
107 }
108 reply.ReadStringVector(&bundles);
109 return ERR_OK;
110 }
111 } // namespace EDM
112 } // namespace OHOS
113