• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_nearlink_protocols_plugin.h"
17 
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 
21 #include "array_int_serializer.h"
22 #include "edm_constants.h"
23 #include "edm_errors.h"
24 #include "edm_ipc_interface_code.h"
25 #include "edm_os_account_manager_impl.h"
26 #include "func_code_utils.h"
27 #include "nearlink_config_utils.h"
28 #include "nearlink_protocol_utils.h"
29 #include "iplugin_manager.h"
30 
31 namespace OHOS {
32 namespace EDM {
33 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(DisallowedNearlinkProtocolsPlugin::GetPlugin());
34 constexpr int32_t NEARLINK_UID = 7030;
35 
InitPlugin(std::shared_ptr<IPluginTemplate<DisallowedNearlinkProtocolsPlugin,std::vector<int32_t>>> ptr)36 void DisallowedNearlinkProtocolsPlugin::InitPlugin(
37     std::shared_ptr<IPluginTemplate<DisallowedNearlinkProtocolsPlugin, std::vector<int32_t>>> ptr)
38 {
39     EDMLOGI("DisallowedNearlinkProtocolsPlugin InitPlugin...");
40     ptr->InitAttribute(EdmInterfaceCode::DISALLOWED_NEARLINK_PROTOCOLS,
41         PolicyName::POLICY_DISALLOWED_NEARLINK_PROTOCOLS,
42         EdmPermission::PERMISSION_ENTERPRISE_MANAGE_SYSTEM,
43         IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
44         true);
45     ptr->SetSerializer(ArrayIntSerializer::GetInstance());
46     ptr->SetOnHandlePolicyListener(&DisallowedNearlinkProtocolsPlugin::OnSetPolicy, FuncOperateType::SET);
47     ptr->SetOnHandlePolicyDoneListener(&DisallowedNearlinkProtocolsPlugin::OnChangedPolicyDone, FuncOperateType::SET);
48     ptr->SetOnHandlePolicyListener(&DisallowedNearlinkProtocolsPlugin::OnRemovePolicy, FuncOperateType::REMOVE);
49     ptr->SetOnHandlePolicyDoneListener(
50         &DisallowedNearlinkProtocolsPlugin::OnChangedPolicyDone, FuncOperateType::REMOVE);
51     ptr->SetOnAdminRemoveListener(&DisallowedNearlinkProtocolsPlugin::OnAdminRemove);
52 }
53 
OnSetPolicy(std::vector<int32_t> & data,std::vector<int32_t> & currentData,std::vector<int32_t> & mergeData,int32_t userId)54 ErrCode DisallowedNearlinkProtocolsPlugin::OnSetPolicy(
55     std::vector<int32_t> &data, std::vector<int32_t> &currentData, std::vector<int32_t> &mergeData, int32_t userId)
56 {
57     EDMLOGI("DisallowedNearlinkProtocolsPlugin OnSetPolicy");
58     if (data.empty()) {
59         EDMLOGE("DisallowedNearlinkProtocolsPlugin OnSetPolicy data is empty");
60         return EdmReturnErrCode::PARAMETER_VERIFICATION_FAILED;
61     }
62     if (data.size() > EdmConstants::DISALLOWED_NEARLINK_PROTOCOLS_MAX_SIZE) {
63         EDMLOGE("DisallowedNearlinkProtocolsPlugin OnSetPolicy size is over limit");
64         return EdmReturnErrCode::PARAMETER_VERIFICATION_FAILED;
65     }
66     NearlinkConfigUtils nearlinkConfigUtils;
67     for (size_t i = 0; i < data.size(); ++i) {
68         std::string protocol;
69         if (!NearlinkProtocolUtils::IntToProtocolStr(data[i], protocol)) {
70             return EdmReturnErrCode::PARAMETER_VERIFICATION_FAILED;
71         }
72         if (!nearlinkConfigUtils.UpdateProtocol(std::to_string(userId), protocol, true)) {
73             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
74         }
75     }
76     currentData = data;
77     mergeData = currentData;
78     NotifyNearlinkProtocolsChanged();
79     return ERR_OK;
80 }
81 
OnRemovePolicy(std::vector<int32_t> & data,std::vector<int32_t> & currentData,std::vector<int32_t> & mergeData,int32_t userId)82 ErrCode DisallowedNearlinkProtocolsPlugin::OnRemovePolicy(
83     std::vector<int32_t> &data, std::vector<int32_t> &currentData, std::vector<int32_t> &mergeData, int32_t userId)
84 {
85     EDMLOGI("DisallowedNearlinkProtocolsPlugin OnRemovePolicy");
86     if (data.empty()) {
87         EDMLOGE("DisallowedNearlinkProtocolsPlugin OnRemovePolicy data is empty");
88         return EdmReturnErrCode::PARAMETER_VERIFICATION_FAILED;
89     }
90     if (data.size() > EdmConstants::DEFAULT_LOOP_MAX_SIZE) {
91         EDMLOGE("DisallowedNearlinkProtocolsPlugin OnRemovePolicy size is over limit");
92         return EdmReturnErrCode::PARAMETER_VERIFICATION_FAILED;
93     }
94     NearlinkConfigUtils nearlinkConfigUtils;
95     for (size_t i = 0; i < data.size(); ++i) {
96         std::string protocol;
97         if (!NearlinkProtocolUtils::IntToProtocolStr(data[i], protocol)) {
98             return EdmReturnErrCode::PARAMETER_VERIFICATION_FAILED;
99         }
100         if (!nearlinkConfigUtils.UpdateProtocol(std::to_string(userId), protocol, false)) {
101             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
102         }
103     }
104     currentData = data;
105     mergeData = currentData;
106     NotifyNearlinkProtocolsChanged();
107     return ERR_OK;
108 }
109 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)110 ErrCode DisallowedNearlinkProtocolsPlugin::OnGetPolicy(
111     std::string &policyData, MessageParcel &data, MessageParcel &reply, int32_t userId)
112 {
113     EDMLOGI("DisallowedNearlinkProtocolsPlugin OnGetPolicy");
114     std::vector<int32_t> protocols;
115     NearlinkConfigUtils nearlinkConfigUtils;
116     if (!nearlinkConfigUtils.QueryProtocols(std::to_string(userId), protocols)) {
117         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
118         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
119     }
120     reply.WriteInt32(ERR_OK);
121     reply.WriteInt32Vector(protocols);
122     return ERR_OK;
123 }
124 
OnAdminRemove(const std::string & adminName,std::vector<int32_t> & policyData,std::vector<int32_t> & mergeData,int32_t userId)125 ErrCode DisallowedNearlinkProtocolsPlugin::OnAdminRemove(
126     const std::string &adminName, std::vector<int32_t> &policyData, std::vector<int32_t> &mergeData, int32_t userId)
127 {
128     EDMLOGI("DisallowedNearlinkProtocolsPlugin OnAdminRemove");
129     NearlinkConfigUtils nearlinkConfigUtils;
130     if (!nearlinkConfigUtils.RemoveProtocolDenyList()) {
131         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
132     }
133     NotifyNearlinkProtocolsChanged();
134     return ERR_OK;
135 }
136 
OnChangedPolicyDone(bool isGlobalChanged)137 void DisallowedNearlinkProtocolsPlugin::OnChangedPolicyDone(bool isGlobalChanged)
138 {
139     if (!isGlobalChanged) {
140         return;
141     }
142     NotifyNearlinkProtocolsChanged();
143 }
144 
NotifyNearlinkProtocolsChanged()145 void DisallowedNearlinkProtocolsPlugin::NotifyNearlinkProtocolsChanged()
146 {
147     EDMLOGD("DisallowedNearlinkProtocolsPlugin NotifyNearlinkProtocolsChanged.");
148     AAFwk::Want want;
149     want.SetAction(EdmConstants::EDM_CONFIG_CHANGED_EVENT);
150     EventFwk::CommonEventData eventData;
151     eventData.SetWant(want);
152     EventFwk::CommonEventPublishInfo eventInfo;
153     std::vector<int32_t> subscriberUids;
154     subscriberUids.push_back(NEARLINK_UID);
155     eventInfo.SetSubscriberUid(subscriberUids);
156     if (!EventFwk::CommonEventManager::NewPublishCommonEvent(eventData, eventInfo)) {
157         EDMLOGE("NotifyNearlinkProtocolsChanged failed.");
158     }
159 }
160 }  // namespace EDM
161 }  // namespace OHOS
162