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
16 #include "net_policy_listener.h"
17
18 #include "net_manager_constants.h"
19 #include "net_mgr_log_wrapper.h"
20 #include "netmanager_base_common_utils.h"
21
22 namespace OHOS {
23 namespace NetManagerStandard {
24 namespace {
25 using namespace OHOS::EventFwk;
26 constexpr const char* UID = "uid";
27 constexpr const char* STATUS_FIELD = "rgmStatus";
28 const std::string STATUS_UNLOCKED = "rgm_user_unlocked";
29 };
30
NetPolicyListener(const EventFwk::CommonEventSubscribeInfo & sp,std::shared_ptr<NetPolicyService> NetPolicy)31 NetPolicyListener::NetPolicyListener(const EventFwk::CommonEventSubscribeInfo &sp,
32 std::shared_ptr<NetPolicyService> NetPolicy)
33 : CommonEventSubscriber(sp)
34 {
35 netPolicyService_ = NetPolicy;
36 }
37
OnReceiveEvent(const CommonEventData & data)38 void NetPolicyListener::OnReceiveEvent(const CommonEventData &data)
39 {
40 auto &want = data.GetWant();
41 std::string wantAction = want.GetAction();
42 if (wantAction == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
43 uint32_t uid = want.GetIntParam(UID, 0);
44 NETMGR_LOG_I("packet remove uid:[%{public}d]", uid);
45 netPolicyService_->GetNetAccessPolicyDBHandler().DeleteByUid(uid);
46 netPolicyService_->DeleteNetworkAccessPolicy(uid);
47 netPolicyService_->DelBrokerUidAccessPolicyMap(uid);
48 }
49 if (wantAction == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) {
50 uint32_t uid = want.GetIntParam(UID, 0);
51 NETMGR_LOG_I("packet add uid:[%{public}d]", uid);
52 netPolicyService_->SetBrokerUidAccessPolicyMap(uid);
53 netPolicyService_->RefreshNetworkAccessPolicyFromConfig();
54 }
55 if (wantAction == COMMON_EVENT_STATUS_CHANGED) {
56 std::string status = want.GetStringParam(STATUS_FIELD);
57 NETMGR_LOG_I("status changed, status:[%{public}s]", status.c_str());
58 if (status == STATUS_UNLOCKED) {
59 netPolicyService_->SetBrokerUidAccessPolicyMap(std::nullopt);
60 }
61 }
62 }
63 } // namespace NetManagerStandard
64 } // namespace OHOS
65