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