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 #ifndef NET_POLICY_CORE_H 17 #define NET_POLICY_CORE_H 18 19 #include <string> 20 #include <vector> 21 22 #include "app_mgr_client.h" 23 #include "app_state_callback_host.h" 24 #include "application_state_observer_stub.h" 25 #include "event_handler.h" 26 27 #include "net_manager_center.h" 28 #include "net_policy_callback.h" 29 #include "net_policy_file.h" 30 #include "net_policy_inner_define.h" 31 #include "netsys_policy_wrapper.h" 32 33 namespace OHOS { 34 namespace NetManagerStandard { 35 class NetPolicyEventHandler; 36 class NetPolicyBase; 37 38 struct PolicyEvent { 39 int32_t eventId = 0; 40 std::shared_ptr<NetPolicyBase> sender = nullptr; 41 bool deviceIdleMode = false; 42 std::set<uint32_t> deviceIdleList; 43 bool powerSaveMode = false; 44 std::set<uint32_t> powerSaveList; 45 uint32_t deletedUid = 0; 46 uint32_t uid = 0; 47 }; 48 49 class NetPolicyCore : public std::enable_shared_from_this<NetPolicyCore> { 50 DECLARE_DELAYED_SINGLETON(NetPolicyCore); 51 52 public: CreateCore()53 template <typename NetPolicyBase> std::shared_ptr<NetPolicyBase> CreateCore() 54 { 55 std::shared_ptr<NetPolicyBase> core = std::make_shared<NetPolicyBase>(); 56 core->Init(); 57 cores_.push_back(core); 58 return core; 59 } 60 void Init(std::shared_ptr<NetPolicyEventHandler> &handler); 61 62 /** 63 * Handle the event from NetPolicyCore 64 * 65 * @param eventId The event id 66 * @param eventData The event data ptr 67 */ 68 void HandleEvent(int32_t eventId, std::shared_ptr<PolicyEvent> eventData); 69 70 /** 71 * Send events to other policy cores. 72 * 73 * @param eventId The event id 74 * @param eventData The event data 75 * @param delayTime The delay time, if need the message send delay 76 */ 77 void SendEvent(int32_t eventId, std::shared_ptr<PolicyEvent> &eventData, int64_t delayTime = 0); 78 79 private: 80 void SubscribeCommonEvent(); 81 void SendAppStatusMessage(const AppExecFwk::AppProcessData &appProcessData); 82 83 private: 84 class AppStatus : public AppExecFwk::AppStateCallbackHost { 85 public: AppStatus(std::shared_ptr<NetPolicyCore> core)86 AppStatus(std::shared_ptr<NetPolicyCore> core) 87 { 88 appStatus_ = core; 89 } 90 OnAppStateChanged(const AppExecFwk::AppProcessData & appProcessData)91 inline void OnAppStateChanged(const AppExecFwk::AppProcessData &appProcessData) override 92 { 93 if (appStatus_ != nullptr) { 94 appStatus_->SendAppStatusMessage(appProcessData); 95 } 96 } 97 OnAbilityRequestDone(const sptr<IRemoteObject> & token,const AppExecFwk::AbilityState state)98 inline void OnAbilityRequestDone(const sptr<IRemoteObject> &token, 99 const AppExecFwk::AbilityState state) override 100 { 101 return; 102 } 103 104 private: 105 std::shared_ptr<NetPolicyCore> appStatus_ = nullptr; 106 }; 107 108 class ReceiveMessage : public EventFwk::CommonEventSubscriber { 109 public: 110 ReceiveMessage(const EventFwk::CommonEventSubscribeInfo &subscriberInfo, std::shared_ptr<NetPolicyCore> core); 111 112 virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override; 113 114 private: 115 std::shared_ptr<NetPolicyCore> receiveMessage_ = nullptr; 116 }; 117 118 private: 119 std::vector<std::shared_ptr<NetPolicyBase>> cores_; 120 std::shared_ptr<AppExecFwk::EventRunner> runner_; 121 std::shared_ptr<NetPolicyEventHandler> handler_; 122 sptr<AppExecFwk::IAppStateCallback> netAppStatusCallback_ = nullptr; 123 std::shared_ptr<ReceiveMessage> subscriber_ = nullptr; 124 }; 125 } // namespace NetManagerStandard 126 } // namespace OHOS 127 #endif // NET_POLICY_CORE_H