• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_service_stub.h"
16 
17 #include "net_policy_cellular_policy.h"
18 #include "net_policy_quota_policy.h"
19 #include "net_mgr_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
NetPolicyServiceStub()23 NetPolicyServiceStub::NetPolicyServiceStub()
24 {
25     memberFuncMap_[CMD_NSM_SET_UID_POLICY] = &NetPolicyServiceStub::OnSetPolicyByUid;
26     memberFuncMap_[CMD_NSM_GET_UID_POLICY] = &NetPolicyServiceStub::OnGetPolicyByUid;
27     memberFuncMap_[CMD_NSM_GET_UIDS] = &NetPolicyServiceStub::OnGetUidsByPolicy;
28     memberFuncMap_[CMD_NSM_IS_NET_ACCESS_METERED] = &NetPolicyServiceStub::OnIsUidNetAccessMetered;
29     memberFuncMap_[CMD_NSM_IS_NET_ACCESS_IFACENAME] = &NetPolicyServiceStub::OnIsUidNetAccessIfaceName;
30     memberFuncMap_[CMD_NSM_REGISTER_NET_POLICY_CALLBACK] = &NetPolicyServiceStub::OnRegisterNetPolicyCallback;
31     memberFuncMap_[CMD_NSM_UNREGISTER_NET_POLICY_CALLBACK] = &NetPolicyServiceStub::OnUnregisterNetPolicyCallback;
32     memberFuncMap_[CMD_NSM_NET_SET_QUOTA_POLICY] = &NetPolicyServiceStub::OnSetNetQuotaPolicies;
33     memberFuncMap_[CMD_NSM_NET_GET_QUOTA_POLICY] = &NetPolicyServiceStub::OnGetNetQuotaPolicies;
34     memberFuncMap_[CMD_NSM_NET_SET_CELLULAR_POLICY] = &NetPolicyServiceStub::OnSetCellularPolicies;
35     memberFuncMap_[CMD_NSM_NET_GET_CELLULAR_POLICY] = &NetPolicyServiceStub::OnGetCellularPolicies;
36     memberFuncMap_[CMD_NSM_FACTORY_RESET] = &NetPolicyServiceStub::OnSetFactoryPolicy;
37     memberFuncMap_[CMD_NSM_SNOOZE_POLICY] = &NetPolicyServiceStub::OnSnoozePolicy;
38     memberFuncMap_[CMD_NSM_SET_IDLE_TRUSTLIST] = &NetPolicyServiceStub::OnSetIdleTrustlist;
39     memberFuncMap_[CMD_NSM_GET_IDLE_TRUSTLIST] = &NetPolicyServiceStub::OnGetIdleTrustlist;
40     memberFuncMap_[CMD_NSM_SET_BACKGROUND_POLICY] = &NetPolicyServiceStub::OnSetBackgroundPolicy;
41     memberFuncMap_[CMD_NSM_GET_BACKGROUND_POLICY] = &NetPolicyServiceStub::OnGetBackgroundPolicy;
42     memberFuncMap_[CMD_NSM_GET_BACKGROUND_POLICY_BY_UID] = &NetPolicyServiceStub::OnGetBackgroundPolicyByUid;
43     memberFuncMap_[CMD_NSM_GET_BACKGROUND_POLICY_BY_CURRENT] = &NetPolicyServiceStub::OnGetCurrentBackgroundPolicy;
44 }
45 
~NetPolicyServiceStub()46 NetPolicyServiceStub::~NetPolicyServiceStub() {}
47 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int32_t NetPolicyServiceStub::OnRemoteRequest(
49     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
50 {
51     std::u16string myDescripter = NetPolicyServiceStub::GetDescriptor();
52     std::u16string remoteDescripter = data.ReadInterfaceToken();
53     if (myDescripter != remoteDescripter) {
54         NETMGR_LOG_E("descriptor checked fail");
55         return ERR_FLATTEN_OBJECT;
56     }
57 
58     auto itFunc = memberFuncMap_.find(code);
59     if (itFunc != memberFuncMap_.end()) {
60         auto requestFunc = itFunc->second;
61         if (requestFunc != nullptr) {
62             return (this->*requestFunc)(data, reply);
63         }
64     }
65 
66     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
67 }
68 
OnSetPolicyByUid(MessageParcel & data,MessageParcel & reply)69 int32_t NetPolicyServiceStub::OnSetPolicyByUid(MessageParcel &data, MessageParcel &reply)
70 {
71     uint32_t uid;
72     if (!data.ReadUint32(uid)) {
73         return ERR_FLATTEN_OBJECT;
74     }
75 
76     uint32_t netPolicy;
77     if (!data.ReadUint32(netPolicy)) {
78         return ERR_FLATTEN_OBJECT;
79     }
80 
81     if (!reply.WriteInt32(static_cast<int32_t>(SetPolicyByUid(uid, static_cast<NetUidPolicy>(netPolicy))))) {
82         return ERR_FLATTEN_OBJECT;
83     }
84 
85     return ERR_NONE;
86 }
87 
OnGetPolicyByUid(MessageParcel & data,MessageParcel & reply)88 int32_t NetPolicyServiceStub::OnGetPolicyByUid(MessageParcel &data, MessageParcel &reply)
89 {
90     uint32_t uid;
91     if (!data.ReadUint32(uid)) {
92         return ERR_FLATTEN_OBJECT;
93     }
94 
95     if (!reply.WriteInt32(static_cast<int32_t>(GetPolicyByUid(uid)))) {
96         return ERR_FLATTEN_OBJECT;
97     }
98 
99     return ERR_NONE;
100 }
101 
OnGetUidsByPolicy(MessageParcel & data,MessageParcel & reply)102 int32_t NetPolicyServiceStub::OnGetUidsByPolicy(MessageParcel &data, MessageParcel &reply)
103 {
104     uint32_t policy;
105     if (!data.ReadUint32(policy)) {
106         return ERR_FLATTEN_OBJECT;
107     }
108 
109     if (!reply.WriteUInt32Vector(GetUidsByPolicy(static_cast<NetUidPolicy>(policy)))) {
110         return ERR_FLATTEN_OBJECT;
111     }
112 
113     return ERR_NONE;
114 }
115 
OnIsUidNetAccessMetered(MessageParcel & data,MessageParcel & reply)116 int32_t NetPolicyServiceStub::OnIsUidNetAccessMetered(MessageParcel &data, MessageParcel &reply)
117 {
118     uint32_t uid = 0;
119     bool metered = false;
120     if (!data.ReadUint32(uid)) {
121         return ERR_FLATTEN_OBJECT;
122     }
123 
124     if (!data.ReadBool(metered)) {
125         return ERR_FLATTEN_OBJECT;
126     }
127 
128     bool ret = IsUidNetAccess(uid, metered);
129     if (!reply.WriteBool(ret)) {
130         return ERR_FLATTEN_OBJECT;
131     }
132 
133     return ERR_NONE;
134 }
135 
OnIsUidNetAccessIfaceName(MessageParcel & data,MessageParcel & reply)136 int32_t NetPolicyServiceStub::OnIsUidNetAccessIfaceName(MessageParcel &data, MessageParcel &reply)
137 {
138     uint32_t uid = 0;
139     std::string ifaceName;
140     if (!data.ReadUint32(uid)) {
141         return ERR_FLATTEN_OBJECT;
142     }
143 
144     if (!data.ReadString(ifaceName)) {
145         return ERR_FLATTEN_OBJECT;
146     }
147     bool ret = IsUidNetAccess(uid, ifaceName);
148     if (!reply.WriteBool(ret)) {
149         return ERR_FLATTEN_OBJECT;
150     }
151 
152     return ERR_NONE;
153 }
154 
OnRegisterNetPolicyCallback(MessageParcel & data,MessageParcel & reply)155 int32_t NetPolicyServiceStub::OnRegisterNetPolicyCallback(MessageParcel &data, MessageParcel &reply)
156 {
157     int32_t result = ERR_FLATTEN_OBJECT;
158     sptr<IRemoteObject> remote = data.ReadRemoteObject();
159     if (remote == nullptr) {
160         NETMGR_LOG_E("Callback ptr is nullptr.");
161         reply.WriteInt32(result);
162         return result;
163     }
164 
165     sptr<INetPolicyCallback> callback = iface_cast<INetPolicyCallback>(remote);
166     result = RegisterNetPolicyCallback(callback);
167     reply.WriteInt32(result);
168     return result;
169 }
170 
OnUnregisterNetPolicyCallback(MessageParcel & data,MessageParcel & reply)171 int32_t NetPolicyServiceStub::OnUnregisterNetPolicyCallback(MessageParcel &data, MessageParcel &reply)
172 {
173     int32_t result = ERR_FLATTEN_OBJECT;
174     sptr<IRemoteObject> remote = data.ReadRemoteObject();
175     if (remote == nullptr) {
176         NETMGR_LOG_E("callback ptr is nullptr.");
177         reply.WriteInt32(result);
178         return result;
179     }
180     sptr<INetPolicyCallback> callback = iface_cast<INetPolicyCallback>(remote);
181     result = UnregisterNetPolicyCallback(callback);
182     reply.WriteInt32(result);
183     return result;
184 }
185 
OnSetNetQuotaPolicies(MessageParcel & data,MessageParcel & reply)186 int32_t NetPolicyServiceStub::OnSetNetQuotaPolicies(MessageParcel &data, MessageParcel &reply)
187 {
188     std::vector<NetPolicyQuotaPolicy> quotaPolicies;
189     if (!NetPolicyQuotaPolicy::Unmarshalling(data, quotaPolicies)) {
190         NETMGR_LOG_E("Unmarshalling failed.");
191         return ERR_FLATTEN_OBJECT;
192     }
193 
194     if (!reply.WriteInt32(static_cast<int32_t>(SetNetQuotaPolicies(quotaPolicies)))) {
195         return ERR_FLATTEN_OBJECT;
196     }
197 
198     return ERR_NONE;
199 }
200 
OnGetNetQuotaPolicies(MessageParcel & data,MessageParcel & reply)201 int32_t NetPolicyServiceStub::OnGetNetQuotaPolicies(MessageParcel &data, MessageParcel &reply)
202 {
203     std::vector<NetPolicyQuotaPolicy> quotaPolicies;
204 
205     if (GetNetQuotaPolicies(quotaPolicies) != NetPolicyResultCode::ERR_NONE) {
206         NETMGR_LOG_E("GetNetQuotaPolicies failed.");
207         return ERR_FLATTEN_OBJECT;
208     }
209 
210     if (!NetPolicyQuotaPolicy::Marshalling(reply, quotaPolicies)) {
211         NETMGR_LOG_E("Marshalling failed");
212         return ERR_FLATTEN_OBJECT;
213     }
214 
215     return ERR_NONE;
216 }
217 
OnSetCellularPolicies(MessageParcel & data,MessageParcel & reply)218 int32_t NetPolicyServiceStub::OnSetCellularPolicies(MessageParcel &data, MessageParcel &reply)
219 {
220     std::vector<NetPolicyCellularPolicy> cellularPolicies;
221     if (!NetPolicyCellularPolicy::Unmarshalling(data, cellularPolicies)) {
222         NETMGR_LOG_E("Unmarshalling failed.");
223         return ERR_FLATTEN_OBJECT;
224     }
225 
226     if (!reply.WriteInt32(static_cast<int32_t>(SetCellularPolicies(cellularPolicies)))) {
227         NETMGR_LOG_E("WriteInt32 SetCellularPolicies return failed.");
228         return ERR_FLATTEN_OBJECT;
229     }
230 
231     return ERR_NONE;
232 }
233 
OnGetCellularPolicies(MessageParcel & data,MessageParcel & reply)234 int32_t NetPolicyServiceStub::OnGetCellularPolicies(MessageParcel &data, MessageParcel &reply)
235 {
236     std::vector<NetPolicyCellularPolicy> cellularPolicies;
237 
238     if (GetCellularPolicies(cellularPolicies) != NetPolicyResultCode::ERR_NONE) {
239         NETMGR_LOG_E("GetNetQuotaPolicies failed.");
240         return ERR_FLATTEN_OBJECT;
241     }
242 
243     if (!NetPolicyCellularPolicy::Marshalling(reply, cellularPolicies)) {
244         NETMGR_LOG_E("Marshalling failed");
245         return ERR_FLATTEN_OBJECT;
246     }
247 
248     return ERR_NONE;
249 }
250 
OnSetFactoryPolicy(MessageParcel & data,MessageParcel & reply)251 int32_t NetPolicyServiceStub::OnSetFactoryPolicy(MessageParcel &data, MessageParcel &reply)
252 {
253     std::string subscrberId;
254     if (!data.ReadString(subscrberId)) {
255         return ERR_FLATTEN_OBJECT;
256     }
257 
258     if (!reply.WriteInt32(static_cast<int32_t>(SetFactoryPolicy(subscrberId)))) {
259         return ERR_FLATTEN_OBJECT;
260     }
261 
262     return ERR_NONE;
263 }
264 
OnSetBackgroundPolicy(MessageParcel & data,MessageParcel & reply)265 int32_t NetPolicyServiceStub::OnSetBackgroundPolicy(MessageParcel &data, MessageParcel &reply)
266 {
267     bool isBackgroundPolicyAllow = false;
268     if (!data.ReadBool(isBackgroundPolicyAllow)) {
269         return ERR_FLATTEN_OBJECT;
270     }
271 
272     if (!reply.WriteInt32(static_cast<int32_t>(SetBackgroundPolicy(isBackgroundPolicyAllow)))) {
273         return ERR_FLATTEN_OBJECT;
274     }
275 
276     return ERR_NONE;
277 }
278 
OnGetBackgroundPolicy(MessageParcel & data,MessageParcel & reply)279 int32_t NetPolicyServiceStub::OnGetBackgroundPolicy(MessageParcel &data, MessageParcel &reply)
280 {
281     bool ret = GetBackgroundPolicy();
282     if (!reply.WriteBool(ret)) {
283         return ERR_FLATTEN_OBJECT;
284     }
285 
286     return ERR_NONE;
287 }
288 
OnGetBackgroundPolicyByUid(MessageParcel & data,MessageParcel & reply)289 int32_t NetPolicyServiceStub::OnGetBackgroundPolicyByUid(MessageParcel &data, MessageParcel &reply)
290 {
291     uint32_t uid = 0;
292     if (!data.ReadUint32(uid)) {
293         return ERR_FLATTEN_OBJECT;
294     }
295 
296     if (!reply.WriteBool(GetBackgroundPolicyByUid(uid))) {
297         return ERR_FLATTEN_OBJECT;
298     }
299 
300     return ERR_NONE;
301 }
302 
OnGetCurrentBackgroundPolicy(MessageParcel & data,MessageParcel & reply)303 int32_t NetPolicyServiceStub::OnGetCurrentBackgroundPolicy(MessageParcel &data, MessageParcel &reply)
304 {
305     if (!reply.WriteInt32(static_cast<int32_t>(GetCurrentBackgroundPolicy()))) {
306         return ERR_FLATTEN_OBJECT;
307     }
308 
309     return ERR_NONE;
310 }
311 
OnSnoozePolicy(MessageParcel & data,MessageParcel & reply)312 int32_t NetPolicyServiceStub::OnSnoozePolicy(MessageParcel &data, MessageParcel &reply)
313 {
314     int8_t netType = 0;
315     if (!data.ReadInt8(netType)) {
316         return ERR_FLATTEN_OBJECT;
317     }
318 
319     std::string simId;
320     if (!data.ReadString(simId)) {
321         return ERR_FLATTEN_OBJECT;
322     }
323 
324     if (!reply.WriteInt32(static_cast<int32_t>(SetSnoozePolicy(netType, simId)))) {
325         return ERR_FLATTEN_OBJECT;
326     }
327 
328     return ERR_NONE;
329 }
330 
OnSetIdleTrustlist(MessageParcel & data,MessageParcel & reply)331 int32_t NetPolicyServiceStub::OnSetIdleTrustlist(MessageParcel &data, MessageParcel &reply)
332 {
333     uint32_t uid;
334     if (!data.ReadUint32(uid)) {
335         return ERR_FLATTEN_OBJECT;
336     }
337 
338     bool isTrustlist = false;
339     if (!data.ReadBool(isTrustlist)) {
340         return ERR_FLATTEN_OBJECT;
341     }
342 
343     if (!reply.WriteInt32(static_cast<int32_t>(SetIdleTrustlist(uid, isTrustlist)))) {
344         return ERR_FLATTEN_OBJECT;
345     }
346 
347     return ERR_NONE;
348 }
349 
OnGetIdleTrustlist(MessageParcel & data,MessageParcel & reply)350 int32_t NetPolicyServiceStub::OnGetIdleTrustlist(MessageParcel &data, MessageParcel &reply)
351 {
352     std::vector<uint32_t> uids;
353     if (GetIdleTrustlist(uids) != NetPolicyResultCode::ERR_NONE) {
354         return ERR_FLATTEN_OBJECT;
355     }
356 
357     if (!reply.WriteUInt32Vector(uids)) {
358         return ERR_FLATTEN_OBJECT;
359     }
360 
361     return ERR_NONE;
362 }
363 } // namespace NetManagerStandard
364 } // namespace OHOS
365