• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 <pthread.h>
19 #include <thread>
20 
21 #include "net_mgr_log_wrapper.h"
22 #include "net_policy_base.h"
23 #include "net_policy_event_handler.h"
24 #include "netmanager_base_common_utils.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 using namespace AppExecFwk;
29 namespace {
30 constexpr const char *DEVICE_IDLE_MODE_KEY = "0";
31 constexpr uint32_t AGAIN_REGISTER_CALLBACK_INTERVAL = 500;
32 constexpr uint32_t CORE_EVENT_PRIORITY = 1;
33 constexpr uint32_t MAX_RETRY_TIMES = 10;
34 } // namespace
35 
36 NetPolicyCore::NetPolicyCore() = default;
37 
~NetPolicyCore()38 NetPolicyCore::~NetPolicyCore()
39 {
40     cores_.clear();
41 }
42 
Init(std::shared_ptr<NetPolicyEventHandler> & handler)43 void NetPolicyCore::Init(std::shared_ptr<NetPolicyEventHandler> &handler)
44 {
45     handler_ = handler;
46     SubscribeCommonEvent();
47 
48     netAppStatusCallback_ = new (std::nothrow) AppStatus((std::static_pointer_cast<NetPolicyCore>(shared_from_this())));
49     if (netAppStatusCallback_ == nullptr) {
50         NETMGR_LOG_E("netAppStatusCallback is nullptr.");
51         return;
52     }
53     std::thread t([this]() {
54         auto appManager = std::make_unique<AppMgrClient>();
55         uint32_t count = 0;
56         int32_t connectResult = AppMgrResultCode::ERROR_SERVICE_NOT_READY;
57         while (connectResult != AppMgrResultCode::RESULT_OK && count <= MAX_RETRY_TIMES) {
58             std::this_thread::sleep_for(std::chrono::milliseconds(AGAIN_REGISTER_CALLBACK_INTERVAL));
59             connectResult = appManager->ConnectAppMgrService();
60             count++;
61         }
62         if (count > MAX_RETRY_TIMES && connectResult != AppMgrResultCode::RESULT_OK) {
63             NETMGR_LOG_E("Connect AppMgrService fail.");
64         } else {
65             appManager->RegisterAppStateCallback(netAppStatusCallback_);
66         }
67     });
68 
69     std::string threadName = "NetPolicyInit";
70     pthread_setname_np(t.native_handle(), threadName.c_str());
71     t.detach();
72 }
73 
HandleEvent(const AppExecFwk::InnerEvent::Pointer & event)74 void NetPolicyCore::HandleEvent(const AppExecFwk::InnerEvent::Pointer &event)
75 {
76     if (!event) {
77         NETMGR_LOG_E("HandleEvent event is null.");
78         return;
79     }
80 
81     for (const auto &core : cores_) {
82         auto eventId = event->GetInnerEventId();
83         auto eventData = event->GetSharedObject<PolicyEvent>();
84         if (eventData && core && core != eventData->sender) {
85             core->HandleEvent(eventId, eventData);
86         }
87     }
88 }
89 
SendEvent(int32_t eventId,std::shared_ptr<PolicyEvent> & eventData,int64_t delayTime)90 void NetPolicyCore::SendEvent(int32_t eventId, std::shared_ptr<PolicyEvent> &eventData, int64_t delayTime)
91 {
92     NETMGR_LOG_D("NetPolicyCore SendEvent: eventId[%{public}d]", eventId);
93     auto event = AppExecFwk::InnerEvent::Get(eventId, eventData);
94     if (handler_ == nullptr) {
95         NETMGR_LOG_E("handler is null");
96         return;
97     }
98 
99     handler_->SendEvent(event, delayTime);
100 }
101 
SubscribeCommonEvent()102 void NetPolicyCore::SubscribeCommonEvent()
103 {
104     NETMGR_LOG_D("SubscribeCommonEvent");
105     std::thread t([this]() {
106         EventFwk::MatchingSkills matchingSkills;
107         matchingSkills.AddEvent(COMMON_EVENT_POWER_SAVE_MODE_CHANGED);
108         matchingSkills.AddEvent(COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED);
109         matchingSkills.AddEvent(COMMON_EVENT_PACKAGE_REMOVED);
110         matchingSkills.AddEvent(COMMON_EVENT_NET_QUOTA_WARNING);
111         EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
112         subscribeInfo.SetPriority(CORE_EVENT_PRIORITY);
113         subscriber_ = std::make_shared<ReceiveMessage>(subscribeInfo, shared_from_this());
114         uint32_t count = 0;
115         bool result = false;
116         while (!result && count <= MAX_RETRY_TIMES) {
117             std::this_thread::sleep_for(std::chrono::milliseconds(AGAIN_REGISTER_CALLBACK_INTERVAL));
118             result = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
119             count++;
120         }
121         if (count > MAX_RETRY_TIMES || !result) {
122             NETMGR_LOG_E("SubscribeCommonEvent fail.");
123         } else {
124             NETMGR_LOG_D("SubscribeCommonEvent successful");
125         }
126     });
127     std::string threadName = "PolicyEvent";
128     pthread_setname_np(t.native_handle(), threadName.c_str());
129     t.detach();
130 }
131 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)132 void NetPolicyCore::ReceiveMessage::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
133 {
134     if (receiveMessage_ == nullptr) {
135         NETMGR_LOG_E("receive message is nullptr");
136         return;
137     }
138     const auto &action = eventData.GetWant().GetAction();
139     const auto &data = eventData.GetData();
140     const auto &code = eventData.GetCode();
141     if (action == COMMON_EVENT_POWER_SAVE_MODE_CHANGED) {
142         bool isPowerSave = (code == SAVE_MODE || code == LOWPOWER_MODE);
143         auto policyEvent = std::make_shared<PolicyEvent>();
144         policyEvent->powerSaveMode = isPowerSave;
145         receiveMessage_->SendEvent(NetPolicyEventHandler::MSG_POWER_SAVE_MODE_CHANGED, policyEvent);
146         return;
147     }
148 
149     if (action == COMMON_EVENT_DEVICE_IDLE_MODE_CHANGED) {
150         bool isDeviceIdle = eventData.GetWant().GetBoolParam(DEVICE_IDLE_MODE_KEY, false);
151         auto policyEvent = std::make_shared<PolicyEvent>();
152         policyEvent->deviceIdleMode = isDeviceIdle;
153         receiveMessage_->SendEvent(NetPolicyEventHandler::MSG_DEVICE_IDLE_MODE_CHANGED, policyEvent);
154         return;
155     }
156 
157     if (action == COMMON_EVENT_PACKAGE_REMOVED) {
158         if (eventData.GetWant().GetIntParam(AppExecFwk::Constants::UID, 0) < 0) {
159             NETMGR_LOG_E("error:deletedUid < 0!,return");
160             return;
161         }
162         uint32_t deletedUid = static_cast<uint32_t>(eventData.GetWant().GetIntParam(AppExecFwk::Constants::UID, 0));
163         auto policyEvent = std::make_shared<PolicyEvent>();
164         policyEvent->deletedUid = deletedUid;
165         receiveMessage_->SendEvent(NetPolicyEventHandler::MSG_UID_REMOVED, policyEvent);
166         return;
167     }
168     NETMGR_LOG_E("Unknow action:[%{public}s], data:[%{public}s], code:[%{public}d]", action.c_str(), data.c_str(),
169                  code);
170 }
171 
SendAppStatusMessage(const AppProcessData & appProcessData)172 void NetPolicyCore::SendAppStatusMessage(const AppProcessData &appProcessData)
173 {
174     for (const auto &appdata : appProcessData.appDatas) {
175         auto policyEvent = std::make_shared<PolicyEvent>();
176         NETMGR_LOG_D(
177             "SendAppStatusMessage : appProcessData.appState[%{public}d] appProcessName[%{public}s] uid[%{public}d]",
178             appProcessData.appState, appProcessData.processName.c_str(), appdata.uid);
179         policyEvent->uid = appdata.uid;
180         if (appProcessData.appState == ApplicationState::APP_STATE_FOREGROUND) {
181             SendEvent(NetPolicyEventHandler::MSG_UID_STATE_FOREGROUND, policyEvent);
182         }
183 
184         if (appProcessData.appState == ApplicationState::APP_STATE_BACKGROUND) {
185             SendEvent(NetPolicyEventHandler::MSG_UID_STATE_BACKGROUND, policyEvent);
186         }
187     }
188 }
189 
ReceiveMessage(const EventFwk::CommonEventSubscribeInfo & subscriberInfo,std::shared_ptr<NetPolicyCore> core)190 NetPolicyCore::ReceiveMessage::ReceiveMessage(const EventFwk::CommonEventSubscribeInfo &subscriberInfo,
191                                               std::shared_ptr<NetPolicyCore> core)
192     : EventFwk::CommonEventSubscriber(subscriberInfo), receiveMessage_(core)
193 {
194 }
195 } // namespace NetManagerStandard
196 } // namespace OHOS
197