• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_core.h"
17 
18 #include "net_mgr_log_wrapper.h"
19 #include "net_policy_base.h"
20 #include "net_policy_event_handler.h"
21 #include "netmanager_base_common_utils.h"
22 
23 namespace OHOS {
24 namespace NetManagerStandard {
25 constexpr const char *EVENT_PARAM_DELETED_UID = "DeletedUid";
26 static constexpr uint32_t CORE_EVENT_PRIORITY = 1;
27 
28 NetPolicyCore::NetPolicyCore() = default;
29 
~NetPolicyCore()30 NetPolicyCore::~NetPolicyCore()
31 {
32     cores_.clear();
33 }
34 
Init(std::shared_ptr<NetPolicyEventHandler> & handler)35 void NetPolicyCore::Init(std::shared_ptr<NetPolicyEventHandler> &handler)
36 {
37     handler_ = handler;
38     SubscribeCommonEvent();
39 }
40 
HandleEvent(const AppExecFwk::InnerEvent::Pointer & event)41 void NetPolicyCore::HandleEvent(const AppExecFwk::InnerEvent::Pointer &event)
42 {
43     if (!event) {
44         NETMGR_LOG_E("HandleEvent event is null.");
45         return;
46     }
47 
48     for (const auto &core : cores_) {
49         auto eventId = event->GetInnerEventId();
50         auto eventData = event->GetSharedObject<PolicyEvent>();
51         if (eventData && core && core != eventData->sender) {
52             core->HandleEvent(eventId, eventData);
53         }
54     }
55 }
56 
SendEvent(int32_t eventId,std::shared_ptr<PolicyEvent> & eventData,int64_t delayTime)57 void NetPolicyCore::SendEvent(int32_t eventId, std::shared_ptr<PolicyEvent> &eventData, int64_t delayTime)
58 {
59     NETMGR_LOG_D("NetPolicyCore SendEvent: eventId[%{public}d]", eventId);
60     auto event = AppExecFwk::InnerEvent::Get(eventId, eventData);
61     if (handler_ == nullptr) {
62         NETMGR_LOG_E("handler is null");
63         return;
64     }
65 
66     handler_->SendEvent(event, delayTime);
67 }
68 
SubscribeCommonEvent()69 void NetPolicyCore::SubscribeCommonEvent()
70 {
71     NETMGR_LOG_D("SubscribeCommonEvent");
72     EventFwk::MatchingSkills matchingSkills;
73     matchingSkills.AddEvent(COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
74     matchingSkills.AddEvent(COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED);
75     matchingSkills.AddEvent(COMMON_EVENT_UID_REMOVED);
76     matchingSkills.AddEvent(COMMON_EVENT_NET_QUOTA_WARNING);
77     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
78     subscribeInfo.SetPriority(CORE_EVENT_PRIORITY);
79     EventFwk::CommonEventManager::SubscribeCommonEvent(shared_from_this());
80 }
81 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)82 void NetPolicyCore::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
83 {
84     const auto &action = eventData.GetWant().GetAction();
85     const auto &data = eventData.GetData();
86     const auto &code = eventData.GetCode();
87     NETMGR_LOG_I("OnReceiveEvent action:[%{public}s], data:[%{public}s], code:[%{public}d]", action.c_str(),
88                  data.c_str(), code);
89     if (action == COMMON_EVENT_POWER_SAVE_MODE_CHANGED) {
90         bool isPowerSave = (code == SAVE_MODE || code == LOWPOWER_MODE);
91         auto policyEvent = std::make_shared<PolicyEvent>();
92         policyEvent->powerSaveMode = isPowerSave;
93         SendEvent(NetPolicyEventHandler::MSG_POWER_SAVE_MODE_CHANGED, policyEvent);
94         return;
95     }
96 
97     if (action == COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED) {
98         bool isDeviceIdle = (code == EXTREME_MODE);
99         auto policyEvent = std::make_shared<PolicyEvent>();
100         policyEvent->deviceIdleMode = isDeviceIdle;
101         SendEvent(NetPolicyEventHandler::MSG_DEVICE_IDLE_MODE_CHANGED, policyEvent);
102         return;
103     }
104 
105     if (action == COMMON_EVENT_UID_REMOVED) {
106         uint32_t deletedUid = CommonUtils::StrToUint(eventData.GetWant().GetStringParam(EVENT_PARAM_DELETED_UID));
107         auto policyEvent = std::make_shared<PolicyEvent>();
108         policyEvent->deletedUid = deletedUid;
109         SendEvent(NetPolicyEventHandler::MSG_UID_REMOVED, policyEvent);
110         return;
111     }
112 }
113 } // namespace NetManagerStandard
114 } // namespace OHOS
115