1 /*
2 * Copyright (c) 2025 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 "disallowed_bluetooth_devices_plugin.h"
17
18 #include "array_string_serializer.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "edm_errors.h"
22 #include "edm_ipc_interface_code.h"
23 #include "parameters.h"
24 #include "iplugin_manager.h"
25
26 namespace OHOS {
27 namespace EDM {
28 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(DisallowedBluetoothDevicesPlugin::GetPlugin());
29 const char *const PERSIST_BLUETOOTH_CONTROL = "persist.edm.prohibit_bluetooth";
30 const char *const BLUETOOTH_DISALLOWED_LIST_CHANGED_EVENT = "com.ohos.edm.disallowedbluetoothdeviceschanged";
31
InitPlugin(std::shared_ptr<IPluginTemplate<DisallowedBluetoothDevicesPlugin,std::vector<std::string>>> ptr)32 void DisallowedBluetoothDevicesPlugin::InitPlugin(
33 std::shared_ptr<IPluginTemplate<DisallowedBluetoothDevicesPlugin, std::vector<std::string>>> ptr)
34 {
35 EDMLOGI("DisallowedBluetoothDevicesPlugin InitPlugin...");
36 ptr->InitAttribute(EdmInterfaceCode::DISALLOWED_BLUETOOTH_DEVICES, PolicyName::POLICY_DISALLOWED_BLUETOOTH_DEVICES,
37 EdmPermission::PERMISSION_ENTERPRISE_MANAGE_BLUETOOTH, IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
38 ptr->SetSerializer(ArrayStringSerializer::GetInstance());
39 ptr->SetOnHandlePolicyListener(&DisallowedBluetoothDevicesPlugin::OnBasicSetPolicy, FuncOperateType::SET);
40 ptr->SetOnHandlePolicyDoneListener(&DisallowedBluetoothDevicesPlugin::OnChangedPolicyDone, FuncOperateType::SET);
41 ptr->SetOnHandlePolicyListener(&DisallowedBluetoothDevicesPlugin::OnBasicRemovePolicy, FuncOperateType::REMOVE);
42 ptr->SetOnHandlePolicyDoneListener(&DisallowedBluetoothDevicesPlugin::OnChangedPolicyDone,
43 FuncOperateType::REMOVE);
44 ptr->SetOnAdminRemoveDoneListener(&DisallowedBluetoothDevicesPlugin::OnAdminRemoveDone);
45 maxListSize_ = EdmConstants::BLUETOOTH_LIST_MAX_SIZE;
46 }
47
SetOtherModulePolicy(const std::vector<std::string> & data,int32_t userId,std::vector<std::string> & failedData)48 ErrCode DisallowedBluetoothDevicesPlugin::SetOtherModulePolicy(const std::vector<std::string> &data, int32_t userId,
49 std::vector<std::string> &failedData)
50 {
51 EDMLOGI("DisallowedBluetoothDevicesPlugin OnSetPolicy");
52 bool isDisabled = system::GetBoolParameter(PERSIST_BLUETOOTH_CONTROL, false);
53 if (isDisabled) {
54 EDMLOGE("DisallowedBluetoothDevicesPlugin OnSetPolicy failed, because bluetooth disabled.");
55 return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
56 }
57 auto policyManager = IPolicyManager::GetInstance();
58 std::string bluetoothDevicesPolicy;
59 policyManager->GetPolicy("", PolicyName::POLICY_ALLOWED_BLUETOOTH_DEVICES, bluetoothDevicesPolicy);
60 if (!bluetoothDevicesPolicy.empty()) {
61 EDMLOGE("bluetoothDevices policy conflict! Has another bluetoothDevicesPolicy");
62 return EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED;
63 }
64 return ERR_OK;
65 }
66
OnChangedPolicyDone(bool isGlobalChanged)67 void DisallowedBluetoothDevicesPlugin::OnChangedPolicyDone(bool isGlobalChanged)
68 {
69 if (!isGlobalChanged) {
70 return;
71 }
72 NotifyBluetoothDevicesChanged();
73 }
74
NotifyBluetoothDevicesChanged()75 void DisallowedBluetoothDevicesPlugin::NotifyBluetoothDevicesChanged()
76 {
77 EDMLOGD("DisallowedBluetoothDevicesPlugin NotifyBluetoothDevicesChanged.");
78 AAFwk::Want want;
79 want.SetAction(BLUETOOTH_DISALLOWED_LIST_CHANGED_EVENT);
80 EventFwk::CommonEventData eventData;
81 eventData.SetWant(want);
82 if (!EventFwk::CommonEventManager::PublishCommonEvent(eventData)) {
83 EDMLOGE("NotifyBluetoothDevicesChanged failed.");
84 }
85 }
86
OnAdminRemoveDone()87 void DisallowedBluetoothDevicesPlugin::OnAdminRemoveDone()
88 {
89 EDMLOGI("DisallowedBluetoothDevicesPlugin OnAdminRemoveDone NotifyBluetoothDevicesChanged.");
90 NotifyBluetoothDevicesChanged();
91 }
92
93 } // namespace EDM
94 } // namespace OHOS