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
AddAutoStartApps(const AppExecFwk::ElementName & admin,const std::vector<AppExecFwk::ElementName> & autoStartApps)112 int32_t ApplicationManagerProxy::AddAutoStartApps(const AppExecFwk::ElementName &admin,
113 const std::vector<AppExecFwk::ElementName> &autoStartApps)
114 {
115 EDMLOGD("ApplicationManagerProxy::AddAutoStartApps");
116 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
117 if (proxy == nullptr) {
118 EDMLOGE("can not get EnterpriseDeviceMgrProxy");
119 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
120 }
121 MessageParcel data;
122 std::uint32_t funcCode =
123 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::MANAGE_AUTO_START_APPS);
124 std::vector<std::string> autoStartAppsString;
125 for (size_t i = 0; i < autoStartApps.size(); i++) {
126 std::string appWant = autoStartApps[i].GetBundleName() + "/" + autoStartApps[i].GetAbilityName();
127 autoStartAppsString.push_back(appWant);
128 }
129 data.WriteInterfaceToken(DESCRIPTOR);
130 data.WriteInt32(WITHOUT_USERID);
131 data.WriteParcelable(&admin);
132 data.WriteStringVector(autoStartAppsString);
133 return proxy->HandleDevicePolicy(funcCode, data);
134 }
135
RemoveAutoStartApps(const AppExecFwk::ElementName & admin,const std::vector<AppExecFwk::ElementName> & autoStartApps)136 int32_t ApplicationManagerProxy::RemoveAutoStartApps(const AppExecFwk::ElementName &admin,
137 const std::vector<AppExecFwk::ElementName> &autoStartApps)
138 {
139 EDMLOGD("ApplicationManagerProxy::RemoveAutoStartApps");
140 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
141 if (proxy == nullptr) {
142 EDMLOGE("can not get EnterpriseDeviceMgrProxy");
143 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
144 }
145 MessageParcel data;
146 std::uint32_t funcCode =
147 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::MANAGE_AUTO_START_APPS);
148 std::vector<std::string> autoStartAppsString;
149 for (size_t i = 0; i < autoStartApps.size(); i++) {
150 std::string appWant = autoStartApps[i].GetBundleName() + "/" + autoStartApps[i].GetAbilityName();
151 autoStartAppsString.push_back(appWant);
152 }
153 data.WriteInterfaceToken(DESCRIPTOR);
154 data.WriteInt32(WITHOUT_USERID);
155 data.WriteParcelable(&admin);
156 data.WriteStringVector(autoStartAppsString);
157 return proxy->HandleDevicePolicy(funcCode, data);
158 }
159
GetAutoStartApps(const AppExecFwk::ElementName & admin,std::vector<AppExecFwk::ElementName> & autoStartApps)160 int32_t ApplicationManagerProxy::GetAutoStartApps(const AppExecFwk::ElementName &admin,
161 std::vector<AppExecFwk::ElementName> &autoStartApps)
162 {
163 EDMLOGD("ApplicationManagerProxy::GetAutoStartApps");
164 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
165 if (proxy == nullptr) {
166 EDMLOGE("can not get EnterpriseDeviceMgrProxy");
167 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
168 }
169 MessageParcel data;
170 MessageParcel reply;
171 data.WriteInterfaceToken(DESCRIPTOR);
172 data.WriteInt32(WITHOUT_USERID);
173 data.WriteInt32(HAS_ADMIN);
174 data.WriteParcelable(&admin);
175 proxy->GetPolicy(EdmInterfaceCode::MANAGE_AUTO_START_APPS, data, reply);
176 int32_t ret = ERR_INVALID_VALUE;
177 bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
178 if (!blRes) {
179 EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
180 return ret;
181 }
182 std::vector<std::string> autoStartAppsString;
183 reply.ReadStringVector(&autoStartAppsString);
184 for (size_t i = 0; i < autoStartAppsString.size(); i++) {
185 size_t index = autoStartAppsString[i].find("/");
186 std::string bundleName;
187 std::string abilityName;
188 if (index != autoStartAppsString[i].npos) {
189 bundleName = autoStartAppsString[i].substr(0, index);
190 abilityName = autoStartAppsString[i].substr(index + 1);
191 } else {
192 EDMLOGE("GetAutoStartApps parse auto start app want failed");
193 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
194 }
195 AppExecFwk::ElementName element;
196 element.SetBundleName(bundleName);
197 element.SetAbilityName(abilityName);
198 autoStartApps.push_back(element);
199 }
200 return ERR_OK;
201 }
202 } // namespace EDM
203 } // namespace OHOS
204