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