• 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 #ifndef NET_POLICY_CORE_H
17 #define NET_POLICY_CORE_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "event_handler.h"
23 
24 #include "net_manager_center.h"
25 #include "net_policy_callback.h"
26 #include "net_policy_file.h"
27 #include "net_policy_inner_define.h"
28 #include "netsys_policy_wrapper.h"
29 
30 namespace OHOS {
31 namespace NetManagerStandard {
32 class NetPolicyEventHandler;
33 class NetPolicyBase;
34 
35 struct PolicyEvent {
36     int32_t eventId = 0;
37     std::shared_ptr<NetPolicyBase> sender = nullptr;
38     bool deviceIdleMode = false;
39     std::vector<uint32_t> deviceIdleList;
40     bool powerSaveMode = false;
41     uint32_t deletedUid = 0;
42 };
43 
44 class NetPolicyCore : public EventFwk::CommonEventSubscriber, public std::enable_shared_from_this<NetPolicyCore> {
45     DECLARE_DELAYED_SINGLETON(NetPolicyCore);
46 
47 public:
CreateCore()48     template <typename NetPolicyBase> std::shared_ptr<NetPolicyBase> CreateCore()
49     {
50         std::shared_ptr<NetPolicyBase> core = std::make_shared<NetPolicyBase>();
51         core->Init();
52         cores_.push_back(core);
53         return core;
54     }
55     void Init(std::shared_ptr<NetPolicyEventHandler> &handler);
56 
57     /**
58      * Handle the event from NetPolicyCore
59      *
60      * @param eventId The event id
61      * @param policyEvent The infomations passed from other core
62      */
63     void HandleEvent(const AppExecFwk::InnerEvent::Pointer &event);
64 
65     /**
66      * Send events to other policy cores.
67      *
68      * @param eventId The event id
69      * @param eventData The event data
70      * @param delayTime The delay time, if need the message send delay
71      */
72     void SendEvent(int32_t eventId, std::shared_ptr<PolicyEvent> &eventData, int64_t delayTime = 0);
73 
74     virtual void OnReceiveEvent(const EventFwk::CommonEventData &data);
75 
76 private:
77     void SubscribeCommonEvent();
78     std::vector<std::shared_ptr<NetPolicyBase>> cores_;
79     std::shared_ptr<AppExecFwk::EventRunner> runner_;
80     std::shared_ptr<NetPolicyEventHandler> handler_;
81 };
82 } // namespace NetManagerStandard
83 } // namespace OHOS
84 #endif // NET_POLICY_CORE_H