1 /*
2 * Copyright (c) 2021-2022 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 #include "net_policy_callback_proxy.h"
17
18 #include "net_mgr_log_wrapper.h"
19 #include "net_quota_policy.h"
20
21 namespace OHOS {
22 namespace NetManagerStandard {
NetPolicyCallbackProxy(const sptr<IRemoteObject> & impl)23 NetPolicyCallbackProxy::NetPolicyCallbackProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<INetPolicyCallback>(impl)
24 {
25 }
26
27 NetPolicyCallbackProxy::~NetPolicyCallbackProxy() = default;
28
NetUidPolicyChange(uint32_t uid,uint32_t policy)29 int32_t NetPolicyCallbackProxy::NetUidPolicyChange(uint32_t uid, uint32_t policy)
30 {
31 MessageParcel data;
32 if (!WriteInterfaceToken(data)) {
33 NETMGR_LOG_E("WriteInterfaceToken failed");
34 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
35 }
36
37 if (!data.WriteUint32(uid)) {
38 NETMGR_LOG_E("Write uint32 data failed");
39 return NETMANAGER_ERR_WRITE_DATA_FAIL;
40 }
41
42 if (!data.WriteUint32(policy)) {
43 NETMGR_LOG_E("Write uint32 data failed");
44 return NETMANAGER_ERR_WRITE_DATA_FAIL;
45 }
46
47 sptr<IRemoteObject> remote = Remote();
48 if (remote == nullptr) {
49 NETMGR_LOG_E("Remote is null");
50 return NETMANAGER_ERR_LOCAL_PTR_NULL;
51 }
52
53 MessageParcel reply;
54 MessageOption option;
55 int32_t ret = remote->SendRequest(static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_UID_POLICY_CHANGE),
56 data, reply, option);
57 if (ret != 0) {
58 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
59 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
60 }
61 return ret;
62 }
63
NetUidRuleChange(uint32_t uid,uint32_t rule)64 int32_t NetPolicyCallbackProxy::NetUidRuleChange(uint32_t uid, uint32_t rule)
65 {
66 MessageParcel data;
67 if (!WriteInterfaceToken(data)) {
68 NETMGR_LOG_E("WriteInterfaceToken failed");
69 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
70 }
71
72 if (!data.WriteUint32(uid)) {
73 NETMGR_LOG_E("Write uint32 data failed");
74 return NETMANAGER_ERR_WRITE_DATA_FAIL;
75 }
76
77 if (!data.WriteUint32(rule)) {
78 NETMGR_LOG_E("Write uint32 data failed");
79 return NETMANAGER_ERR_WRITE_DATA_FAIL;
80 }
81
82 sptr<IRemoteObject> remote = Remote();
83 if (remote == nullptr) {
84 NETMGR_LOG_E("Remote is null");
85 return NETMANAGER_ERR_LOCAL_PTR_NULL;
86 }
87
88 MessageParcel reply;
89 MessageOption option;
90 int32_t ret = remote->SendRequest(static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_UID_RULE_CHANGE),
91 data, reply, option);
92 if (ret != 0) {
93 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
94 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
95 }
96 return ret;
97 }
98
NetBackgroundPolicyChange(bool isBackgroundPolicyAllow)99 int32_t NetPolicyCallbackProxy::NetBackgroundPolicyChange(bool isBackgroundPolicyAllow)
100 {
101 MessageParcel data;
102 if (!WriteInterfaceToken(data)) {
103 NETMGR_LOG_E("WriteInterfaceToken failed");
104 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
105 }
106
107 if (!data.WriteBool(isBackgroundPolicyAllow)) {
108 NETMGR_LOG_E("Write Bool data failed");
109 return NETMANAGER_ERR_WRITE_DATA_FAIL;
110 }
111
112 sptr<IRemoteObject> remote = Remote();
113 if (remote == nullptr) {
114 NETMGR_LOG_E("Remote is null");
115 return NETMANAGER_ERR_LOCAL_PTR_NULL;
116 }
117
118 MessageParcel reply;
119 MessageOption option;
120 int32_t ret = remote->SendRequest(
121 static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_BACKGROUND_POLICY_CHANGE), data, reply, option);
122 if (ret != 0) {
123 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
124 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
125 }
126 return ret;
127 }
128
NetQuotaPolicyChange(const std::vector<NetQuotaPolicy> & quotaPolicies)129 int32_t NetPolicyCallbackProxy::NetQuotaPolicyChange(const std::vector<NetQuotaPolicy> "aPolicies)
130 {
131 if (quotaPolicies.empty()) {
132 NETMGR_LOG_E("NetQuotaPolicyChange proxy quotaPolicies empty");
133 return POLICY_ERR_QUOTA_POLICY_NOT_EXIST;
134 }
135
136 MessageParcel data;
137 if (!WriteInterfaceToken(data)) {
138 NETMGR_LOG_E("WriteInterfaceToken failed");
139 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
140 }
141
142 if (!NetQuotaPolicy::Marshalling(data, quotaPolicies)) {
143 NETMGR_LOG_E("Marshalling failed.");
144 return NETMANAGER_ERR_WRITE_DATA_FAIL;
145 }
146
147 sptr<IRemoteObject> remote = Remote();
148 if (remote == nullptr) {
149 NETMGR_LOG_E("Remote is null");
150 return NETMANAGER_ERR_LOCAL_PTR_NULL;
151 }
152
153 MessageParcel reply;
154 MessageOption option;
155 int32_t ret = remote->SendRequest(
156 static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_QUOTA_POLICY_CHANGE), data, reply, option);
157 if (ret != 0) {
158 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
159 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
160 }
161 return ret;
162 }
163
NetStrategySwitch(const std::string & simId,bool enable)164 int32_t NetPolicyCallbackProxy::NetStrategySwitch(const std::string &simId, bool enable)
165 {
166 MessageParcel data;
167 if (!WriteInterfaceToken(data)) {
168 NETMGR_LOG_E("WriteInterfaceToken failed");
169 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
170 }
171
172 if (!data.WriteString(simId)) {
173 NETMGR_LOG_E("WriteString String data failed");
174 return NETMANAGER_ERR_WRITE_DATA_FAIL;
175 }
176
177 if (!data.WriteBool(enable)) {
178 NETMGR_LOG_E("WriteBool Bool data failed");
179 return NETMANAGER_ERR_WRITE_DATA_FAIL;
180 }
181
182 sptr<IRemoteObject> remote = Remote();
183 if (remote == nullptr) {
184 NETMGR_LOG_E("Remote is null");
185 return NETMANAGER_ERR_LOCAL_PTR_NULL;
186 }
187
188 MessageParcel reply;
189 MessageOption option;
190 int32_t ret = remote->SendRequest(
191 static_cast<uint32_t>(PolicyCallbackInterfaceCode::NET_POLICY_STRATEGYSWITCH_CHANGE), data, reply, option);
192 if (ret != 0) {
193 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
194 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
195 }
196 return ret;
197 }
198
NetMeteredIfacesChange(std::vector<std::string> & ifaces)199 int32_t NetPolicyCallbackProxy::NetMeteredIfacesChange(std::vector<std::string> &ifaces)
200 {
201 MessageParcel data;
202 if (!WriteInterfaceToken(data)) {
203 NETMGR_LOG_E("WriteInterfaceToken failed");
204 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
205 }
206 uint32_t size = static_cast<uint32_t>(ifaces.size());
207 if (!data.WriteUint32(size)) {
208 NETMGR_LOG_E("Write uint32 data failed");
209 return NETMANAGER_ERR_WRITE_DATA_FAIL;
210 }
211
212 for (uint32_t i = 0; i < ifaces.size(); ++i) {
213 if (!data.WriteString(ifaces[i])) {
214 NETMGR_LOG_E("Write String data failed");
215 return NETMANAGER_ERR_WRITE_DATA_FAIL;
216 }
217 }
218
219 sptr<IRemoteObject> remote = Remote();
220 if (remote == nullptr) {
221 NETMGR_LOG_E("Remote is null");
222 return NETMANAGER_ERR_LOCAL_PTR_NULL;
223 }
224
225 MessageParcel reply;
226 MessageOption option;
227 int32_t ret = remote->SendRequest(
228 static_cast<uint32_t>(PolicyCallbackInterfaceCode::NOTIFY_NET_METERED_IFACES_CHANGE), data, reply, option);
229 if (ret != 0) {
230 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
231 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
232 }
233 return ret;
234 }
235
WriteInterfaceToken(MessageParcel & data)236 bool NetPolicyCallbackProxy::WriteInterfaceToken(MessageParcel &data)
237 {
238 if (!data.WriteInterfaceToken(NetPolicyCallbackProxy::GetDescriptor())) {
239 NETMGR_LOG_E("WriteInterfaceToken failed");
240 return false;
241 }
242 return true;
243 }
244 } // namespace NetManagerStandard
245 } // namespace OHOS
246