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 #ifndef NET_FIREWALL_INTERCEPT_RECORDER_H
17 #define NET_FIREWALL_INTERCEPT_RECORDER_H
18 
19 #include <string>
20 #include <shared_mutex>
21 
22 #include "ffrt.h"
23 #include "netfirewall_common.h"
24 #include "netfirewall_callback_stub.h"
25 
26 namespace OHOS {
27 namespace NetManagerStandard {
28 class NetFirewallInterceptRecorder : public std::enable_shared_from_this<NetFirewallInterceptRecorder> {
29 public:
30     // Firewall interception log callback
31     class FirewallCallback : public OHOS::NetsysNative::NetFirewallCallbackStub {
32     public:
FirewallCallback(std::shared_ptr<NetFirewallInterceptRecorder> recorder)33         FirewallCallback(std::shared_ptr<NetFirewallInterceptRecorder> recorder) : recorder_(recorder)
34         {
35             ffrtQueue_ =
36                 std::make_shared<ffrt::queue>("FirewallCallbackQueue", ffrt::queue_attr().qos(ffrt::qos_utility));
37         }
38 
~FirewallCallback()39         ~FirewallCallback()
40         {
41             ffrtQueue_.reset();
42             ffrtQueue_ = nullptr;
43         }
44         virtual int32_t OnIntercept(sptr<InterceptRecord> &record) override;
45 
46     private:
47         std::shared_ptr<NetFirewallInterceptRecorder> recorder_ = nullptr;
48         ffrt::task_handle recordTaskHandle_;
49         std::shared_ptr<ffrt::queue> ffrtQueue_;
50     };
51 
52 public:
53     static std::shared_ptr<NetFirewallInterceptRecorder> GetInstance();
54     NetFirewallInterceptRecorder();
55     ~NetFirewallInterceptRecorder();
56 
57     /**
58      * Set current forground user Id
59      *
60      * @param userId User id
61      */
62     void SetCurrentUserId(int32_t userId);
63 
64     /**
65      * Get all interception records
66      *
67      * @param userId User ID
68      * @param requestParam Paging in parameter information
69      * @param info Paging data information
70      * @return Returns 0 success. Otherwise fail
71      */
72     int32_t GetInterceptRecords(const int32_t userId, const sptr<RequestParam> &requestParam,
73         sptr<InterceptRecordPage> &info);
74 
75     /**
76      * sync interception records and insert to db
77      */
78     void SyncRecordCache();
79 
80     /**
81      * add interception records in cache
82      *
83      * @param reocrd record object
84      */
85     void PutRecordCache(sptr<InterceptRecord> reocrd);
86 
87     /**
88      * get interception records size in cache
89      *
90      * @return Returns reocrds size
91      */
92     int32_t GetRecordCacheSize();
93 
94     /**
95      * Register callback for recevie intercept event
96      *
97      * @param callback implement of INetFirewallCallback
98      * @return 0 if success or -1 if an error occurred
99      */
100     int32_t RegisterInterceptCallback();
101 
102     /**
103      * Unregister callback for recevie intercept event
104      *
105      * @param callback register callback for recevie intercept event
106      * @return 0 if success or -1 if an error occurred
107      */
108     int32_t UnRegisterInterceptCallback();
109 
110 private:
111     std::shared_mutex setRecordMutex_;
112     std::atomic<int32_t> currentUserId_ = 0;
113     std::vector<sptr<InterceptRecord>> recordCache_;
114     sptr<OHOS::NetsysNative::INetFirewallCallback> callback_ = nullptr;
115     static std::shared_ptr<NetFirewallInterceptRecorder> instance_;
116 };
117 } // namespace NetManagerStandard
118 } // namespace OHOS
119 #endif /* NET_FIREWALL_INTERCEPT_RECORDER_H */
120