• 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 "set_allowed_kiosk_apps_plugin.h"
17 
18 #include "ability_manager_client.h"
19 #include "array_string_serializer.h"
20 #include "bundle_mgr_interface.h"
21 #include "edm_ipc_interface_code.h"
22 #include "edm_sys_manager.h"
23 #include "iplugin_manager.h"
24 #include "ipolicy_manager.h"
25 #include "session_manager_lite.h"
26 #include "system_ability_definition.h"
27 #include "notification_helper.h"
28 
29 namespace OHOS {
30 namespace EDM {
31 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(SetAllowedKioskAppsPlugin::GetPlugin());
32 const std::string KIOSK_APP_TRUST_LIST_KEY = "kiosk_app_trust_list";
33 
InitPlugin(std::shared_ptr<IPluginTemplate<SetAllowedKioskAppsPlugin,std::vector<std::string>>> ptr)34 void SetAllowedKioskAppsPlugin::InitPlugin(
35     std::shared_ptr<IPluginTemplate<SetAllowedKioskAppsPlugin, std::vector<std::string>>> ptr)
36 {
37     EDMLOGI("SetAllowedKioskAppsPlugin InitPlugin...");
38     ptr->InitAttribute(EdmInterfaceCode::ALLOWED_KIOSK_APPS, PolicyName::POLICY_ALLOWED_KIOSK_APPS,
39         EdmPermission::PERMISSION_ENTERPRISE_SET_KIOSK, IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
40     ptr->SetSerializer(ArrayStringSerializer::GetInstance());
41     ptr->SetOnHandlePolicyListener(&SetAllowedKioskAppsPlugin::OnSetPolicy, FuncOperateType::SET);
42     ptr->SetOnAdminRemoveListener(&SetAllowedKioskAppsPlugin::OnAdminRemove);
43     ptr->SetOtherServiceStartListener(&SetAllowedKioskAppsPlugin::OnOtherServiceStart);
44 }
45 
OnSetPolicy(std::vector<std::string> & data,std::vector<std::string> & currentData,std::vector<std::string> & mergeData,int32_t userId)46 ErrCode SetAllowedKioskAppsPlugin::OnSetPolicy(std::vector<std::string> &data,
47     std::vector<std::string> &currentData, std::vector<std::string> &mergeData, int32_t userId)
48 {
49     if (data.size() > EdmConstants::POLICIES_MAX_SIZE) {
50         EDMLOGE("SetAllowedKioskAppsPlugin OnSetPolicy data is too large.");
51         return EdmReturnErrCode::PARAM_ERROR;
52     }
53     std::vector<std::string> bundleNames;
54     ErrCode ret = convertAppIdentifierToBundleNames(data, bundleNames);
55     if (FAILED(ret)) {
56         EDMLOGE("SetAllowedKioskAppsPlugin convertAppIdentifierToBundleNames error.");
57         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
58     }
59     ret = SetKioskAppsToAms(bundleNames);
60     if (FAILED(ret)) {
61         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
62     }
63     ret = SetKioskAppsToWms(bundleNames);
64     if (FAILED(ret)) {
65         EDMLOGE("SetAllowedKioskAppsPlugin OnSetPolicy error clear");
66         std::vector<std::string> emptyBundleNames;
67         SetKioskAppsToAms(emptyBundleNames);
68         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
69     }
70     ret = SetKioskAppsToNotification(bundleNames);
71     if (FAILED(ret)) {
72         EDMLOGE("SetAllowedKioskAppsPlugin OnSetPolicy error clear");
73         std::vector<std::string> emptyBundleNames;
74         SetKioskAppsToAms(emptyBundleNames);
75         SetKioskAppsToWms(emptyBundleNames);
76         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
77     }
78     currentData = data;
79     mergeData = data;
80     return ERR_OK;
81 }
82 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)83 ErrCode SetAllowedKioskAppsPlugin::OnGetPolicy(
84     std::string &policyData, MessageParcel &data, MessageParcel &reply, int32_t userId)
85 {
86     std::vector<std::string> appIdentifiers;
87     ArrayStringSerializer::GetInstance()->Deserialize(policyData, appIdentifiers);
88     reply.WriteInt32(ERR_OK);
89     reply.WriteStringVector(appIdentifiers);
90     return ERR_OK;
91 }
92 
OnAdminRemove(const std::string & adminName,std::vector<std::string> & data,std::vector<std::string> & mergeData,int32_t userId)93 ErrCode SetAllowedKioskAppsPlugin::OnAdminRemove(
94     const std::string &adminName, std::vector<std::string> &data, std::vector<std::string> &mergeData, int32_t userId)
95 {
96     if (mergeData.empty()) {
97         // merge is empty
98         SetKioskAppsToAms(mergeData);
99         SetKioskAppsToWms(mergeData);
100         SetKioskAppsToNotification(mergeData);
101     }
102     return ERR_OK;
103 }
104 
OnOtherServiceStart(int32_t systemAbilityId)105 void SetAllowedKioskAppsPlugin::OnOtherServiceStart(int32_t systemAbilityId)
106 {
107     EDMLOGI("SetAllowedKioskAppsPlugin OnOtherServiceStart %{public}d", systemAbilityId);
108     std::string policyData;
109     IPolicyManager::GetInstance()->GetPolicy(
110         "", PolicyName::POLICY_ALLOWED_KIOSK_APPS, policyData, EdmConstants::DEFAULT_USER_ID);
111     if (policyData.empty()) {
112         EDMLOGI("SetAllowedKioskAppsPlugin OnOtherServiceStart policyData is empty");
113         return;
114     }
115     std::vector<std::string> appIdentifiers;
116     ArrayStringSerializer::GetInstance()->Deserialize(policyData, appIdentifiers);
117     std::vector<std::string> bundleNames;
118     int32_t ret = convertAppIdentifierToBundleNames(appIdentifiers, bundleNames);
119     if (FAILED(ret)) {
120         return;
121     }
122     if (systemAbilityId == ABILITY_MGR_SERVICE_ID) {
123         SetKioskAppsToAms(bundleNames);
124     } else if (systemAbilityId == WINDOW_MANAGER_SERVICE_ID) {
125         SetKioskAppsToWms(bundleNames);
126     } else if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) {
127         SetKioskAppsToAms(bundleNames);
128         SetKioskAppsToWms(bundleNames);
129     }
130 }
131 
SetKioskAppsToAms(const std::vector<std::string> & bundleNames)132 int32_t SetAllowedKioskAppsPlugin::SetKioskAppsToAms(const std::vector<std::string> &bundleNames)
133 {
134     EDMLOGI("SetAllowedKioskAppsPlugin SetKioskAppsToAms");
135     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(ABILITY_MGR_SERVICE_ID);
136     if (remoteObject == nullptr) {
137         EDMLOGE("SetAllowedKioskAppsPlugin SetKioskAppsToAms can not get remoteObject");
138         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
139     }
140     int32_t ret = OHOS::AAFwk::AbilityManagerClient::GetInstance()->UpdateKioskApplicationList(bundleNames);
141     if (FAILED(ret)) {
142         EDMLOGE("SetAllowedKioskAppsPlugin SetKioskAppsToAms failed %{public}d", ret);
143         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
144     }
145     return ERR_OK;
146 }
147 
SetKioskAppsToWms(const std::vector<std::string> & bundleNames)148 int32_t SetAllowedKioskAppsPlugin::SetKioskAppsToWms(const std::vector<std::string> &bundleNames)
149 {
150     EDMLOGI("SetAllowedKioskAppsPlugin SetKioskAppsToWms");
151     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(WINDOW_MANAGER_SERVICE_ID);
152     if (remoteObject == nullptr) {
153         EDMLOGE("SetAllowedKioskAppsPlugin SetKioskAppsToWms can not get remoteObject");
154         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
155     }
156     auto wmsProxy = OHOS::Rosen::SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy();
157     if (wmsProxy == nullptr) {
158         EDMLOGE("SetAllowedKioskAppsPlugin SetKioskAppsToWms proxy is nullptr");
159         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
160     }
161     auto ret = wmsProxy->UpdateKioskAppList(bundleNames);
162     if (ret != OHOS::Rosen::WMError::WM_OK) {
163         EDMLOGE("SetAllowedKioskAppsPlugin SetKioskAppsToWms failed %{public}d", ret);
164         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
165     }
166     return ERR_OK;
167 }
168 
SetKioskAppsToNotification(const std::vector<std::string> & bundleNames)169 int32_t SetAllowedKioskAppsPlugin::SetKioskAppsToNotification(const std::vector<std::string> &bundleNames)
170 {
171     EDMLOGI("SetAllowedKioskAppsPlugin SetKioskAppsToNotification");
172     std::string policyData;
173     ArrayStringSerializer::GetInstance()->Serialize(bundleNames, policyData);
174     int32_t ret = OHOS::Notification::NotificationHelper::SetAdditionConfig(KIOSK_APP_TRUST_LIST_KEY, policyData);
175     if (FAILED(ret)) {
176         EDMLOGE("SetAllowedKioskAppsPlugin SetKioskAppsToNotification failed %{public}d", ret);
177         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
178     }
179     return ERR_OK;
180 }
181 
convertAppIdentifierToBundleNames(const std::vector<std::string> & appIdentifiers,std::vector<std::string> & bundleNames)182 int32_t SetAllowedKioskAppsPlugin::convertAppIdentifierToBundleNames(
183     const std::vector<std::string> &appIdentifiers, std::vector<std::string> &bundleNames)
184 {
185     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
186     auto iBundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
187     if (iBundleMgr == nullptr) {
188         EDMLOGE("SetAllowedKioskAppsPlugin convertAppIdentifierToBundleNames can not get iBundleMgr");
189         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
190     }
191     bundleNames.clear();
192     bundleNames.reserve(appIdentifiers.size());
193     for (size_t i = 0; i < appIdentifiers.size(); ++i) {
194         std::string bundleName;
195         int32_t ret = iBundleMgr->GetBundleNameByAppId(appIdentifiers[i], bundleName);
196         if (FAILED(ret)) {
197             EDMLOGE("SetAllowedKioskAppsPlugin convertAppIdentifierToBundleNames failed %{public}d", ret);
198             continue;
199         }
200         bundleNames.push_back(bundleName);
201     }
202     return ERR_OK;
203 }
204 } // namespace EDM
205 } // namespace OHOS
206