1/*
2 * Copyright (c) 2021-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_CALLBACK_H
17#define NET_POLICY_CALLBACK_H
18
19#include <mutex>
20#include <string>
21#include <vector>
22
23#include "singleton.h"
24
25#include "ffrt.h"
26#include "event_handler.h"
27#include "i_net_policy_callback.h"
28#include "net_quota_policy.h"
29
30namespace OHOS {
31namespace NetManagerStandard {
32class NetPolicyCallback : public std::enable_shared_from_this<NetPolicyCallback> {
33    DECLARE_DELAYED_SINGLETON(NetPolicyCallback);
34
35public:
36    /**
37     * Register net policy callback.
38     * @param callback Interface type pointer.
39     * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
40     */
41    int32_t RegisterNetPolicyCallbackAsync(const sptr<INetPolicyCallback> &callback);
42
43    /**
44     * Unregister net policy callback.
45     * @param callback Interface type pointer.
46     * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
47     */
48    int32_t UnregisterNetPolicyCallbackAsync(const sptr<INetPolicyCallback> &callback);
49
50    /**
51     * Notify this uid's policy is changed.
52     * @param uid The UID of application.
53     * @param policy See {@link NetUidPolicy}.
54     * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
55     */
56    int32_t NotifyNetUidPolicyChangeAsync(uint32_t uid, uint32_t policy);
57
58    /**
59     * Notify this uid's rule is changed.
60     * @param uid The UID of application.
61     * @param rule See {@link NetUidRule}.
62     * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
63     */
64    int32_t NotifyNetUidRuleChangeAsync(uint32_t uid, uint32_t rule);
65
66    /**
67     * Notify the quota policy is changed.
68     * @param quotaPolicies The struct vector of quotaPolicies.
69     * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
70     */
71    int32_t NotifyNetQuotaPolicyChangeAsync(const std::vector<NetQuotaPolicy> &quotaPolicies);
72
73    /**
74     * Notify when metered ifaces is changed.
75     * @param ifaces The string vector of ifaces.
76     * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
77     */
78    int32_t NotifyNetMeteredIfacesChangeAsync(std::vector<std::string> &ifaces);
79
80    /**
81     * Notify when background policy is changed.
82     * @param isAllow When isAllow is true,it means background policy is true,
83     * when isAllow is false,it means background policy is false.
84     * @return int32_t Returns 0 success. Otherwise fail, {@link NetPolicyResultCode}.
85     */
86    int32_t NotifyNetBackgroundPolicyChangeAsync(bool isAllowed);
87
88private:
89    int32_t RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback);
90    int32_t UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback);
91    int32_t NotifyNetUidPolicyChange(uint32_t uid, uint32_t policy);
92    int32_t NotifyNetUidRuleChange(uint32_t uid, uint32_t rule);
93    int32_t NotifyNetQuotaPolicyChange(const std::vector<NetQuotaPolicy> &quotaPolicies);
94    int32_t NotifyNetMeteredIfacesChange(std::vector<std::string> &ifaces);
95    int32_t NotifyNetBackgroundPolicyChange(bool isAllowed);
96
97private:
98    std::vector<sptr<INetPolicyCallback>> callbacks_;
99    std::shared_ptr<ffrt::queue> netPolicyCallbackFfrtQueue_ = nullptr;
100};
101} // namespace NetManagerStandard
102} // namespace OHOS
103#endif // NET_POLICY_CALLBACK_H