1 /*
2 * Copyright (c) 2021 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 #include "net_policy_callback.h"
16 #include "net_policy_constants.h"
17 #include "net_policy_define.h"
18 #include "net_mgr_log_wrapper.h"
19
20 namespace OHOS {
21 namespace NetManagerStandard {
RegisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)22 void NetPolicyCallback::RegisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
23 {
24 if (callback == nullptr) {
25 NETMGR_LOG_E("The parameter callback is null");
26 return;
27 }
28
29 uint32_t callBackNum = netPolicyCallback_.size();
30 NETMGR_LOG_D("netPolicyCallback_ callback num [%{public}d]", callBackNum);
31 if (callBackNum >= LIMIT_CALLBACK_NUM) {
32 NETMGR_LOG_E("netPolicyCallback_ callback num cannot more than [%{public}d]", LIMIT_CALLBACK_NUM);
33 return;
34 }
35
36 for (uint32_t i = 0; i < callBackNum; i++) {
37 if (callback->AsObject().GetRefPtr() == netPolicyCallback_[i]->AsObject().GetRefPtr()) {
38 NETMGR_LOG_I("netPolicyCallback_ had this callback");
39 return;
40 }
41 }
42
43 netPolicyCallback_.emplace_back(callback);
44 }
45
UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> & callback)46 void NetPolicyCallback::UnregisterNetPolicyCallback(const sptr<INetPolicyCallback> &callback)
47 {
48 if (callback == nullptr) {
49 NETMGR_LOG_E("The parameter of callback is null");
50 return;
51 }
52
53 for (auto iter = netPolicyCallback_.begin(); iter != netPolicyCallback_.end(); ++iter) {
54 if (callback->AsObject().GetRefPtr() == (*iter)->AsObject().GetRefPtr()) {
55 netPolicyCallback_.erase(iter);
56 return;
57 }
58 }
59 }
60
NotifyNetUidPolicyChanged(uint32_t uid,NetUidPolicy policy)61 int32_t NetPolicyCallback::NotifyNetUidPolicyChanged(uint32_t uid, NetUidPolicy policy)
62 {
63 NETMGR_LOG_I("NotifyNetUidPolicyChanged uid[%{public}d] policy[%{public}d] netPolicyCallback_[%{public}zd]",
64 uid, static_cast<uint32_t>(policy), netPolicyCallback_.size());
65
66 for (const auto &callback : netPolicyCallback_) {
67 if (callback != nullptr) {
68 callback->NetUidPolicyChanged(uid, policy);
69 }
70 }
71
72 return static_cast<int32_t>(NetPolicyResultCode::ERR_NONE);
73 }
74
NotifyNetBackgroundPolicyChanged(bool isBackgroundPolicyAllow)75 int32_t NetPolicyCallback::NotifyNetBackgroundPolicyChanged(bool isBackgroundPolicyAllow)
76 {
77 NETMGR_LOG_I("NotifyNetBackgroundPolicyChanged backgroundPolicy[%{public}d] netPolicyCallback_[%{public}d]",
78 isBackgroundPolicyAllow, static_cast<uint32_t>(netPolicyCallback_.size()));
79
80 for (const auto &callback : netPolicyCallback_) {
81 if (callback != nullptr) {
82 callback->NetBackgroundPolicyChanged(isBackgroundPolicyAllow);
83 }
84 }
85
86 return static_cast<int32_t>(NetPolicyResultCode::ERR_NONE);
87 }
88
NotifyNetCellularPolicyChanged(const std::vector<NetPolicyCellularPolicy> & cellularPolicies)89 int32_t NetPolicyCallback::NotifyNetCellularPolicyChanged(const std::vector<NetPolicyCellularPolicy> &cellularPolicies)
90 {
91 if (cellularPolicies.empty()) {
92 NETMGR_LOG_E("NotifyNetCellularPolicyChanged cellularPolicies empty");
93 return static_cast<int32_t>(NetPolicyResultCode::ERR_INTERNAL_ERROR);
94 }
95 NETMGR_LOG_I("NotifyNetCellularPolicyChanged cellularPolicies.size[%{public}zd]", cellularPolicies.size());
96
97 for (const auto &callback : netPolicyCallback_) {
98 if (callback != nullptr) {
99 callback->NetCellularPolicyChanged(cellularPolicies);
100 }
101 }
102
103 return static_cast<int32_t>(NetPolicyResultCode::ERR_NONE);
104 }
105
NotifyNetStrategySwitch(const std::string & simId,bool enable)106 int32_t NetPolicyCallback::NotifyNetStrategySwitch(const std::string &simId, bool enable)
107 {
108 NETMGR_LOG_I("NotifyNetStrategySwitch simId[%{public}s] enable[%{public}d]",
109 simId.c_str(), static_cast<int32_t>(enable));
110
111 for (const auto &callback : netPolicyCallback_) {
112 if (callback != nullptr) {
113 callback->NetStrategySwitch(simId, enable);
114 }
115 }
116
117 return static_cast<int32_t>(NetPolicyResultCode::ERR_NONE);
118 }
119 } // namespace NetManagerStandard
120 } // namespace OHOS
121