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