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,bool isSync)40 int32_t ApplicationManagerProxy::AddDisallowedRunningBundles(AppExecFwk::ElementName &admin,
41     std::vector<std::string> &bundles, int32_t userId, bool isSync)
42 {
43     EDMLOGD("ApplicationManagerProxy::AddDisallowedRunningBundles");
44     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
45     MessageParcel data;
46     std::uint32_t funcCode =
47         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISALLOW_RUNNING_BUNDLES);
48     data.WriteInterfaceToken(DESCRIPTOR);
49     data.WriteInt32(HAS_USERID);
50     data.WriteInt32(userId);
51     data.WriteParcelable(&admin);
52     data.WriteString(isSync ? EdmConstants::PERMISSION_TAG_VERSION_12 : EdmConstants::PERMISSION_TAG_VERSION_11);
53     data.WriteStringVector(bundles);
54     return proxy->HandleDevicePolicy(funcCode, data);
55 }
56 
RemoveDisallowedRunningBundles(AppExecFwk::ElementName & admin,std::vector<std::string> & bundles,int32_t userId,bool isSync)57 int32_t ApplicationManagerProxy::RemoveDisallowedRunningBundles(AppExecFwk::ElementName &admin,
58     std::vector<std::string> &bundles, int32_t userId, bool isSync)
59 {
60     EDMLOGD("ApplicationManagerProxy::RemoveDisallowedRunningBundles");
61     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
62     MessageParcel data;
63     std::uint32_t funcCode =
64         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::DISALLOW_RUNNING_BUNDLES);
65     data.WriteInterfaceToken(DESCRIPTOR);
66     data.WriteInt32(HAS_USERID);
67     data.WriteInt32(userId);
68     data.WriteParcelable(&admin);
69     data.WriteString(isSync ? EdmConstants::PERMISSION_TAG_VERSION_12 : EdmConstants::PERMISSION_TAG_VERSION_11);
70     data.WriteStringVector(bundles);
71     return proxy->HandleDevicePolicy(funcCode, data);
72 }
73 
GetDisallowedRunningBundles(AppExecFwk::ElementName & admin,int32_t userId,std::vector<std::string> & bundles,bool isSync)74 int32_t ApplicationManagerProxy::GetDisallowedRunningBundles(AppExecFwk::ElementName &admin, int32_t userId,
75     std::vector<std::string> &bundles, bool isSync)
76 {
77     EDMLOGD("ApplicationManagerProxy::GetDisallowedRunningBundles");
78     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
79     MessageParcel data;
80     MessageParcel reply;
81     data.WriteInterfaceToken(DESCRIPTOR);
82     data.WriteInt32(HAS_USERID);
83     data.WriteInt32(userId);
84     data.WriteString(isSync ? EdmConstants::PERMISSION_TAG_VERSION_12 : EdmConstants::PERMISSION_TAG_VERSION_11);
85     data.WriteInt32(HAS_ADMIN);
86     data.WriteParcelable(&admin);
87     proxy->GetPolicy(EdmInterfaceCode::DISALLOW_RUNNING_BUNDLES, data, reply);
88     int32_t ret = ERR_INVALID_VALUE;
89     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
90     if (!blRes) {
91         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
92         return ret;
93     }
94     int32_t size = reply.ReadInt32();
95     if (size > EdmConstants::APPID_MAX_SIZE) {
96         EDMLOGE("bundles size=[%{public}d] is too large", size);
97         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
98     }
99     reply.ReadStringVector(&bundles);
100     return ERR_OK;
101 }
102 
AddAutoStartApps(const AppExecFwk::ElementName & admin,const std::vector<AppExecFwk::ElementName> & autoStartApps)103 int32_t ApplicationManagerProxy::AddAutoStartApps(const AppExecFwk::ElementName &admin,
104     const std::vector<AppExecFwk::ElementName> &autoStartApps)
105 {
106     EDMLOGD("ApplicationManagerProxy::AddAutoStartApps");
107     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
108     MessageParcel data;
109     std::uint32_t funcCode =
110         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::MANAGE_AUTO_START_APPS);
111     std::vector<std::string> autoStartAppsString;
112     for (size_t i = 0; i < autoStartApps.size(); i++) {
113         std::string appWant = autoStartApps[i].GetBundleName() + "/" + autoStartApps[i].GetAbilityName();
114         autoStartAppsString.push_back(appWant);
115     }
116     data.WriteInterfaceToken(DESCRIPTOR);
117     data.WriteInt32(WITHOUT_USERID);
118     data.WriteParcelable(&admin);
119     data.WriteString(WITHOUT_PERMISSION_TAG);
120     data.WriteStringVector(autoStartAppsString);
121     return proxy->HandleDevicePolicy(funcCode, data);
122 }
123 
RemoveAutoStartApps(const AppExecFwk::ElementName & admin,const std::vector<AppExecFwk::ElementName> & autoStartApps)124 int32_t ApplicationManagerProxy::RemoveAutoStartApps(const AppExecFwk::ElementName &admin,
125     const std::vector<AppExecFwk::ElementName> &autoStartApps)
126 {
127     EDMLOGD("ApplicationManagerProxy::RemoveAutoStartApps");
128     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
129     MessageParcel data;
130     std::uint32_t funcCode =
131         POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, EdmInterfaceCode::MANAGE_AUTO_START_APPS);
132     std::vector<std::string> autoStartAppsString;
133     for (size_t i = 0; i < autoStartApps.size(); i++) {
134         std::string appWant = autoStartApps[i].GetBundleName() + "/" + autoStartApps[i].GetAbilityName();
135         autoStartAppsString.push_back(appWant);
136     }
137     data.WriteInterfaceToken(DESCRIPTOR);
138     data.WriteInt32(WITHOUT_USERID);
139     data.WriteParcelable(&admin);
140     data.WriteString(WITHOUT_PERMISSION_TAG);
141     data.WriteStringVector(autoStartAppsString);
142     return proxy->HandleDevicePolicy(funcCode, data);
143 }
144 
GetAutoStartApps(const AppExecFwk::ElementName & admin,std::vector<AppExecFwk::ElementName> & autoStartApps)145 int32_t ApplicationManagerProxy::GetAutoStartApps(const AppExecFwk::ElementName &admin,
146     std::vector<AppExecFwk::ElementName> &autoStartApps)
147 {
148     EDMLOGD("ApplicationManagerProxy::GetAutoStartApps");
149     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
150     MessageParcel data;
151     MessageParcel reply;
152     data.WriteInterfaceToken(DESCRIPTOR);
153     data.WriteInt32(WITHOUT_USERID);
154     data.WriteString(WITHOUT_PERMISSION_TAG);
155     data.WriteInt32(HAS_ADMIN);
156     data.WriteParcelable(&admin);
157     proxy->GetPolicy(EdmInterfaceCode::MANAGE_AUTO_START_APPS, data, reply);
158     int32_t ret = ERR_INVALID_VALUE;
159     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
160     if (!blRes) {
161         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
162         return ret;
163     }
164     std::vector<std::string> autoStartAppsString;
165     reply.ReadStringVector(&autoStartAppsString);
166     for (size_t i = 0; i < autoStartAppsString.size(); i++) {
167         size_t index = autoStartAppsString[i].find("/");
168         std::string bundleName;
169         std::string abilityName;
170         if (index != autoStartAppsString[i].npos) {
171             bundleName = autoStartAppsString[i].substr(0, index);
172             abilityName = autoStartAppsString[i].substr(index + 1);
173         } else {
174             EDMLOGE("GetAutoStartApps parse auto start app want failed");
175             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
176         }
177         AppExecFwk::ElementName element;
178         element.SetBundleName(bundleName);
179         element.SetAbilityName(abilityName);
180         autoStartApps.push_back(element);
181     }
182     return ERR_OK;
183 }
184 } // namespace EDM
185 } // namespace OHOS
186