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 NETFIREWALL_CLIENT_H
17#define NETFIREWALL_CLIENT_H
18
19#include <cstdint>
20
21#include "i_netfirewall_service.h"
22#include "netfirewall_common.h"
23#include "system_ability_load_callback_stub.h"
24
25namespace OHOS {
26namespace NetManagerStandard {
27// Firewall load callback
28class NetFirewallLoadCallback : public SystemAbilityLoadCallbackStub {
29public:
30    void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject) override;
31
32    void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
33
34    bool IsFailed();
35
36    const sptr<IRemoteObject> &GetRemoteObject() const;
37
38private:
39    bool loadSAFailed_ = false;
40    sptr<IRemoteObject> remoteObject_ = nullptr;
41};
42
43class NetFirewallClient {
44public:
45    NetFirewallClient() = default;
46    ~NetFirewallClient() = default;
47    NetFirewallClient(const NetFirewallClient &) = delete;
48    NetFirewallClient &operator = (const NetFirewallClient &) = delete;
49
50public:
51    static NetFirewallClient &GetInstance();
52
53public:
54    int32_t SetNetFirewallPolicy(const int32_t userId, const sptr<NetFirewallPolicy> &status);
55
56    int32_t GetNetFirewallPolicy(const int32_t userId, sptr<NetFirewallPolicy> &status);
57
58    int32_t AddNetFirewallRule(const sptr<NetFirewallRule> &rule, int32_t &result);
59
60    int32_t UpdateNetFirewallRule(const sptr<NetFirewallRule> &rule);
61
62    int32_t DeleteNetFirewallRule(const int32_t userId, const int32_t ruleId);
63
64    int32_t GetNetFirewallRules(const int32_t userId, const sptr<RequestParam> &requestParam,
65        sptr<FirewallRulePage> &info);
66
67    int32_t GetNetFirewallRule(const int32_t userId, const int32_t ruleId, sptr<NetFirewallRule> &rule);
68
69    int32_t GetInterceptRecords(const int32_t userId, const sptr<RequestParam> &requestParam,
70        sptr<InterceptRecordPage> &info);
71
72private:
73    class MonitorPcfirewallServiceDead : public IRemoteObject::DeathRecipient {
74    public:
75        explicit MonitorPcfirewallServiceDead(NetFirewallClient &client) : client_(client) {}
76        ~MonitorPcfirewallServiceDead() override = default;
77        void OnRemoteDied(const wptr<IRemoteObject> &remote) override
78        {
79            client_.OnRemoteDied(remote);
80        }
81
82    private:
83        NetFirewallClient &client_;
84    };
85
86    sptr<INetFirewallService> GetProxy();
87
88    sptr<IRemoteObject> LoadSaOnDemand();
89
90    bool RestartNetFirewallManagerSysAbility();
91
92    void OnRemoteDied(const wptr<IRemoteObject> &remote);
93
94private:
95    std::mutex mutex_;
96    sptr<INetFirewallService> netfirewallService_ = nullptr;
97    sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
98    sptr<NetFirewallLoadCallback> loadCallback_ = nullptr;
99};
100} // namespace NetManagerStandard
101} // namespace OHOS
102#endif // NETFIREWALL_CLIENT_H
103