• 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 #include "net_policy_service_stub.h"
17 
18 #include "net_mgr_log_wrapper.h"
19 #include "net_policy_core.h"
20 #include "net_quota_policy.h"
21 #include "netmanager_base_permission.h"
22 
23 namespace OHOS {
24 namespace NetManagerStandard {
25 namespace {
26 std::map<uint32_t, const char *> g_codeNPS = {
27     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POLICY_BY_UID), Permission::MANAGE_NET_STRATEGY},
28     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_POLICY_BY_UID), Permission::MANAGE_NET_STRATEGY},
29     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_UIDS_BY_POLICY), Permission::MANAGE_NET_STRATEGY},
30     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_IS_NET_ALLOWED_BY_METERED), Permission::MANAGE_NET_STRATEGY},
31     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_IS_NET_ALLOWED_BY_IFACE), Permission::MANAGE_NET_STRATEGY},
32     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_REGISTER_NET_POLICY_CALLBACK), Permission::MANAGE_NET_STRATEGY},
33     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_UNREGISTER_NET_POLICY_CALLBACK),
34      Permission::MANAGE_NET_STRATEGY},
35     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_NET_QUOTA_POLICIES), Permission::MANAGE_NET_STRATEGY},
36     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_NET_QUOTA_POLICIES), Permission::MANAGE_NET_STRATEGY},
37     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_UPDATE_REMIND_POLICY), Permission::MANAGE_NET_STRATEGY},
38     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_IDLE_TRUSTLIST), Permission::MANAGE_NET_STRATEGY},
39     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_IDLE_TRUSTLIST), Permission::MANAGE_NET_STRATEGY},
40     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_DEVICE_IDLE_POLICY), Permission::MANAGE_NET_STRATEGY},
41     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_RESET_POLICIES), Permission::MANAGE_NET_STRATEGY},
42     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_BACKGROUND_POLICY), Permission::MANAGE_NET_STRATEGY},
43     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_BACKGROUND_POLICY), Permission::MANAGE_NET_STRATEGY},
44     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_BACKGROUND_POLICY_BY_UID), Permission::MANAGE_NET_STRATEGY},
45     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POWER_SAVE_TRUSTLIST), Permission::MANAGE_NET_STRATEGY},
46     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_POWER_SAVE_TRUSTLIST), Permission::MANAGE_NET_STRATEGY},
47     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POWER_SAVE_POLICY), Permission::MANAGE_NET_STRATEGY},
48     {static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_CHECK_PERMISSION), Permission::MANAGE_NET_STRATEGY},
49 };
50 } // namespace
51 
NetPolicyServiceStub()52 NetPolicyServiceStub::NetPolicyServiceStub()
53 {
54     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POLICY_BY_UID)] =
55         &NetPolicyServiceStub::OnSetPolicyByUid;
56     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_POLICY_BY_UID)] =
57         &NetPolicyServiceStub::OnGetPolicyByUid;
58     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_UIDS_BY_POLICY)] =
59         &NetPolicyServiceStub::OnGetUidsByPolicy;
60     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_IS_NET_ALLOWED_BY_METERED)] =
61         &NetPolicyServiceStub::OnIsUidNetAllowedMetered;
62     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_IS_NET_ALLOWED_BY_IFACE)] =
63         &NetPolicyServiceStub::OnIsUidNetAllowedIfaceName;
64     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_REGISTER_NET_POLICY_CALLBACK)] =
65         &NetPolicyServiceStub::OnRegisterNetPolicyCallback;
66     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_UNREGISTER_NET_POLICY_CALLBACK)] =
67         &NetPolicyServiceStub::OnUnregisterNetPolicyCallback;
68     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_NET_QUOTA_POLICIES)] =
69         &NetPolicyServiceStub::OnSetNetQuotaPolicies;
70     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_NET_QUOTA_POLICIES)] =
71         &NetPolicyServiceStub::OnGetNetQuotaPolicies;
72     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_RESET_POLICIES)] =
73         &NetPolicyServiceStub::OnResetPolicies;
74     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_UPDATE_REMIND_POLICY)] =
75         &NetPolicyServiceStub::OnSnoozePolicy;
76     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_IDLE_TRUSTLIST)] =
77         &NetPolicyServiceStub::OnSetDeviceIdleTrustlist;
78     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_IDLE_TRUSTLIST)] =
79         &NetPolicyServiceStub::OnGetDeviceIdleTrustlist;
80     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_DEVICE_IDLE_POLICY)] =
81         &NetPolicyServiceStub::OnSetDeviceIdlePolicy;
82     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_POWER_SAVE_TRUSTLIST)] =
83         &NetPolicyServiceStub::OnGetPowerSaveTrustlist;
84     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POWER_SAVE_TRUSTLIST)] =
85         &NetPolicyServiceStub::OnSetPowerSaveTrustlist;
86     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_BACKGROUND_POLICY)] =
87         &NetPolicyServiceStub::OnSetBackgroundPolicy;
88     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_BACKGROUND_POLICY)] =
89         &NetPolicyServiceStub::OnGetBackgroundPolicy;
90     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_GET_BACKGROUND_POLICY_BY_UID)] =
91         &NetPolicyServiceStub::OnGetBackgroundPolicyByUid;
92     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_SET_POWER_SAVE_POLICY)] =
93         &NetPolicyServiceStub::OnSetPowerSavePolicy;
94     memberFuncMap_[static_cast<uint32_t>(PolicyInterfaceCode::CMD_NPS_CHECK_PERMISSION)] =
95         &NetPolicyServiceStub::OnCheckPermission;
96     InitEventHandler();
97 }
98 
99 NetPolicyServiceStub::~NetPolicyServiceStub() = default;
100 
InitEventHandler()101 void NetPolicyServiceStub::InitEventHandler()
102 {
103     runner_ = AppExecFwk::EventRunner::Create(NET_POLICY_WORK_THREAD);
104     if (!runner_) {
105         NETMGR_LOG_E("Create net policy work event runner.");
106         return;
107     }
108     auto core = DelayedSingleton<NetPolicyCore>::GetInstance();
109     handler_ = std::make_shared<NetPolicyEventHandler>(runner_, core);
110     core->Init(handler_);
111 }
112 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)113 int32_t NetPolicyServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
114                                               MessageOption &option)
115 {
116     std::u16string myDescriptor = NetPolicyServiceStub::GetDescriptor();
117     std::u16string remoteDescriptor = data.ReadInterfaceToken();
118     if (myDescriptor != remoteDescriptor) {
119         NETMGR_LOG_E("descriptor checked fail");
120         return NETMANAGER_ERR_DESCRIPTOR_MISMATCH;
121     }
122 
123     if (handler_ == nullptr) {
124         NETMGR_LOG_E("Net policy handler is null, recreate handler.");
125         InitEventHandler();
126         if (handler_ == nullptr) {
127             NETMGR_LOG_E("recreate net policy handler failed.");
128             return NETMANAGER_ERR_INTERNAL;
129         }
130     }
131     auto itFunc = memberFuncMap_.find(code);
132     if (itFunc != memberFuncMap_.end()) {
133         int32_t checkPermissionResult = CheckPolicyPermission(code);
134         if (checkPermissionResult != NETMANAGER_SUCCESS) {
135             if (!reply.WriteInt32(checkPermissionResult)) {
136                 return IPC_STUB_WRITE_PARCEL_ERR;
137             }
138             return NETMANAGER_SUCCESS;
139         }
140         int32_t result = NETMANAGER_SUCCESS;
141         auto requestFunc = itFunc->second;
142         if (requestFunc != nullptr) {
143             handler_->PostSyncTask(
144                 [this, &data, &reply, &requestFunc, &result]() { result = (this->*requestFunc)(data, reply); },
145                 AppExecFwk::EventQueue::Priority::HIGH);
146             return result;
147         }
148     }
149     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
150 }
151 
SubCheckPermission(const std::string & permission,uint32_t funcCode)152 bool NetPolicyServiceStub::SubCheckPermission(const std::string &permission, uint32_t funcCode)
153 {
154     if (NetManagerPermission::CheckPermission(permission)) {
155         return true;
156     }
157     NETMGR_LOG_E("Permission denied funcCode: %{public}d permission: %{public}s", funcCode, permission.c_str());
158     return false;
159 }
160 
CheckPolicyPermission(uint32_t code)161 int32_t NetPolicyServiceStub::CheckPolicyPermission(uint32_t code)
162 {
163     bool result = NetManagerPermission::IsSystemCaller();
164     if (!result) {
165         return NETMANAGER_ERR_NOT_SYSTEM_CALL;
166     }
167     if (g_codeNPS.find(code) != g_codeNPS.end()) {
168         result = SubCheckPermission(g_codeNPS[code], code);
169         if (!result) {
170             return NETMANAGER_ERR_PERMISSION_DENIED;
171         }
172         return NETMANAGER_SUCCESS;
173     }
174     NETMGR_LOG_E("Error funcCode, need check");
175     return NETMANAGER_ERR_PERMISSION_DENIED;
176 }
177 
OnSetPolicyByUid(MessageParcel & data,MessageParcel & reply)178 int32_t NetPolicyServiceStub::OnSetPolicyByUid(MessageParcel &data, MessageParcel &reply)
179 {
180     uint32_t uid = 0;
181     if (!data.ReadUint32(uid)) {
182         NETMGR_LOG_E("Read Uint32 data failed.");
183         return NETMANAGER_ERR_READ_DATA_FAIL;
184     }
185 
186     uint32_t netPolicy = 0;
187     if (!data.ReadUint32(netPolicy)) {
188         NETMGR_LOG_E("Read Uint32 data failed.");
189         return NETMANAGER_ERR_READ_DATA_FAIL;
190     }
191 
192     int32_t result = SetPolicyByUid(uid, netPolicy);
193     if (!reply.WriteInt32(result)) {
194         NETMGR_LOG_E("Write int32 reply failed.");
195         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
196     }
197 
198     return NETMANAGER_SUCCESS;
199 }
200 
OnGetPolicyByUid(MessageParcel & data,MessageParcel & reply)201 int32_t NetPolicyServiceStub::OnGetPolicyByUid(MessageParcel &data, MessageParcel &reply)
202 {
203     uint32_t uid = 0;
204     if (!data.ReadUint32(uid)) {
205         return NETMANAGER_ERR_READ_DATA_FAIL;
206     }
207 
208     uint32_t policy = 0;
209     int32_t result = GetPolicyByUid(uid, policy);
210     if (!reply.WriteInt32(result)) {
211         NETMGR_LOG_E("Write int32 reply failed.");
212         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
213     }
214 
215     if (result == NETMANAGER_SUCCESS) {
216         if (!reply.WriteInt32(policy)) {
217             NETMGR_LOG_E("Write int32 reply failed.");
218             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
219         }
220     }
221 
222     return NETMANAGER_SUCCESS;
223 }
224 
OnGetUidsByPolicy(MessageParcel & data,MessageParcel & reply)225 int32_t NetPolicyServiceStub::OnGetUidsByPolicy(MessageParcel &data, MessageParcel &reply)
226 {
227     uint32_t policy = 0;
228     if (!data.ReadUint32(policy)) {
229         NETMGR_LOG_E("Read uint32 data failed");
230         return NETMANAGER_ERR_READ_DATA_FAIL;
231     }
232 
233     std::vector<uint32_t> uids;
234     int32_t result = GetUidsByPolicy(policy, uids);
235     if (!reply.WriteInt32(result)) {
236         NETMGR_LOG_E("Write int32 reply failed");
237         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
238     }
239 
240     if (result == NETMANAGER_SUCCESS) {
241         if (!reply.WriteUInt32Vector(uids)) {
242             NETMGR_LOG_E("Write uint32 vector reply failed");
243             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
244         }
245     }
246 
247     return NETMANAGER_SUCCESS;
248 }
249 
OnIsUidNetAllowedMetered(MessageParcel & data,MessageParcel & reply)250 int32_t NetPolicyServiceStub::OnIsUidNetAllowedMetered(MessageParcel &data, MessageParcel &reply)
251 {
252     uint32_t uid = 0;
253     bool metered = false;
254     if (!data.ReadUint32(uid)) {
255         NETMGR_LOG_E("Read uint32 data failed");
256         return NETMANAGER_ERR_READ_DATA_FAIL;
257     }
258 
259     if (!data.ReadBool(metered)) {
260         NETMGR_LOG_E("Read Bool data failed");
261         return NETMANAGER_ERR_READ_DATA_FAIL;
262     }
263 
264     bool isAllowed = false;
265     int32_t result = IsUidNetAllowed(uid, metered, isAllowed);
266     if (!reply.WriteInt32(result)) {
267         NETMGR_LOG_E("Write int32 reply failed");
268         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
269     }
270 
271     if (result == NETMANAGER_SUCCESS) {
272         if (!reply.WriteBool(isAllowed)) {
273             NETMGR_LOG_E("Write Bool reply failed");
274             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
275         }
276     }
277     return NETMANAGER_SUCCESS;
278 }
279 
OnIsUidNetAllowedIfaceName(MessageParcel & data,MessageParcel & reply)280 int32_t NetPolicyServiceStub::OnIsUidNetAllowedIfaceName(MessageParcel &data, MessageParcel &reply)
281 {
282     uint32_t uid = 0;
283     std::string ifaceName;
284     if (!data.ReadUint32(uid)) {
285         NETMGR_LOG_E("Read uint32 data failed");
286         return NETMANAGER_ERR_READ_DATA_FAIL;
287     }
288 
289     if (!data.ReadString(ifaceName)) {
290         NETMGR_LOG_E("Read String data failed");
291         return NETMANAGER_ERR_READ_DATA_FAIL;
292     }
293 
294     bool isAllowed = false;
295     int32_t result = IsUidNetAllowed(uid, ifaceName, isAllowed);
296     if (!reply.WriteInt32(result)) {
297         NETMGR_LOG_E("Write int32 reply failed");
298         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
299     }
300     if (result == NETMANAGER_SUCCESS) {
301         if (!reply.WriteBool(isAllowed)) {
302             NETMGR_LOG_E("Write Bool reply failed");
303             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
304         }
305     }
306     return NETMANAGER_SUCCESS;
307 }
308 
OnRegisterNetPolicyCallback(MessageParcel & data,MessageParcel & reply)309 int32_t NetPolicyServiceStub::OnRegisterNetPolicyCallback(MessageParcel &data, MessageParcel &reply)
310 {
311     sptr<IRemoteObject> remote = data.ReadRemoteObject();
312     if (remote == nullptr) {
313         NETMGR_LOG_E("Callback ptr is nullptr.");
314         reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL);
315         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
316     }
317 
318     sptr<INetPolicyCallback> callback = iface_cast<INetPolicyCallback>(remote);
319     int32_t result = RegisterNetPolicyCallback(callback);
320     if (!reply.WriteInt32(result)) {
321         NETMGR_LOG_E("Write int32 reply failed");
322         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
323     }
324 
325     return NETMANAGER_SUCCESS;
326 }
327 
OnUnregisterNetPolicyCallback(MessageParcel & data,MessageParcel & reply)328 int32_t NetPolicyServiceStub::OnUnregisterNetPolicyCallback(MessageParcel &data, MessageParcel &reply)
329 {
330     sptr<IRemoteObject> remote = data.ReadRemoteObject();
331     if (remote == nullptr) {
332         NETMGR_LOG_E("callback ptr is nullptr.");
333         reply.WriteInt32(NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL);
334         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
335     }
336     sptr<INetPolicyCallback> callback = iface_cast<INetPolicyCallback>(remote);
337     int32_t result = UnregisterNetPolicyCallback(callback);
338     if (!reply.WriteInt32(result)) {
339         NETMGR_LOG_E("Write int32 reply failed");
340         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
341     }
342 
343     return NETMANAGER_SUCCESS;
344 }
345 
OnSetNetQuotaPolicies(MessageParcel & data,MessageParcel & reply)346 int32_t NetPolicyServiceStub::OnSetNetQuotaPolicies(MessageParcel &data, MessageParcel &reply)
347 {
348     std::vector<NetQuotaPolicy> quotaPolicies;
349     if (!NetQuotaPolicy::Unmarshalling(data, quotaPolicies)) {
350         NETMGR_LOG_E("Unmarshalling failed.");
351         return NETMANAGER_ERR_READ_DATA_FAIL;
352     }
353 
354     int32_t result = SetNetQuotaPolicies(quotaPolicies);
355     if (!reply.WriteInt32(result)) {
356         NETMGR_LOG_E("Write int32 reply failed");
357         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
358     }
359 
360     return NETMANAGER_SUCCESS;
361 }
362 
OnGetNetQuotaPolicies(MessageParcel & data,MessageParcel & reply)363 int32_t NetPolicyServiceStub::OnGetNetQuotaPolicies(MessageParcel &data, MessageParcel &reply)
364 {
365     std::vector<NetQuotaPolicy> quotaPolicies;
366 
367     int32_t result = GetNetQuotaPolicies(quotaPolicies);
368     if (!reply.WriteInt32(result)) {
369         NETMGR_LOG_E("Write int32 reply failed");
370         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
371     }
372     if (result == NETMANAGER_SUCCESS) {
373         if (!NetQuotaPolicy::Marshalling(reply, quotaPolicies)) {
374             NETMGR_LOG_E("Marshalling failed");
375             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
376         }
377     }
378 
379     return NETMANAGER_SUCCESS;
380 }
381 
OnResetPolicies(MessageParcel & data,MessageParcel & reply)382 int32_t NetPolicyServiceStub::OnResetPolicies(MessageParcel &data, MessageParcel &reply)
383 {
384     std::string subscriberId;
385     if (!data.ReadString(subscriberId)) {
386         NETMGR_LOG_E("Read String data failed");
387         return NETMANAGER_ERR_READ_DATA_FAIL;
388     }
389 
390     int32_t result = ResetPolicies(subscriberId);
391     if (!reply.WriteInt32(result)) {
392         NETMGR_LOG_E("Write int32 reply failed");
393         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
394     }
395 
396     return NETMANAGER_SUCCESS;
397 }
398 
OnSetBackgroundPolicy(MessageParcel & data,MessageParcel & reply)399 int32_t NetPolicyServiceStub::OnSetBackgroundPolicy(MessageParcel &data, MessageParcel &reply)
400 {
401     bool isBackgroundPolicyAllow = false;
402     if (!data.ReadBool(isBackgroundPolicyAllow)) {
403         NETMGR_LOG_E("Read Bool data failed");
404         return NETMANAGER_ERR_READ_DATA_FAIL;
405     }
406 
407     int32_t result = SetBackgroundPolicy(isBackgroundPolicyAllow);
408     if (!reply.WriteInt32(result)) {
409         NETMGR_LOG_E("Write int32 reply failed");
410         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
411     }
412 
413     return NETMANAGER_SUCCESS;
414 }
415 
OnGetBackgroundPolicy(MessageParcel & data,MessageParcel & reply)416 int32_t NetPolicyServiceStub::OnGetBackgroundPolicy(MessageParcel &data, MessageParcel &reply)
417 {
418     bool backgroundPolicy = false;
419     int32_t result = GetBackgroundPolicy(backgroundPolicy);
420     if (!reply.WriteInt32(result)) {
421         NETMGR_LOG_E("Write int32 reply failed");
422         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
423     }
424     if (result == NETMANAGER_SUCCESS) {
425         if (!reply.WriteBool(backgroundPolicy)) {
426             NETMGR_LOG_E("Write Bool reply failed");
427             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
428         }
429     }
430     return NETMANAGER_SUCCESS;
431 }
432 
OnGetBackgroundPolicyByUid(MessageParcel & data,MessageParcel & reply)433 int32_t NetPolicyServiceStub::OnGetBackgroundPolicyByUid(MessageParcel &data, MessageParcel &reply)
434 {
435     uint32_t uid = 0;
436     if (!data.ReadUint32(uid)) {
437         NETMGR_LOG_E("Read uint32 data failed");
438         return NETMANAGER_ERR_READ_DATA_FAIL;
439     }
440 
441     uint32_t backgroundPolicyOfUid = 0;
442     int32_t result = GetBackgroundPolicyByUid(uid, backgroundPolicyOfUid);
443     if (!reply.WriteInt32(result)) {
444         NETMGR_LOG_E("Write int32 reply failed");
445         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
446     }
447     if (result == NETMANAGER_SUCCESS) {
448         if (!reply.WriteUint32(backgroundPolicyOfUid)) {
449             NETMGR_LOG_E("Write uint32 reply failed");
450             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
451         }
452     }
453 
454     return NETMANAGER_SUCCESS;
455 }
456 
OnSnoozePolicy(MessageParcel & data,MessageParcel & reply)457 int32_t NetPolicyServiceStub::OnSnoozePolicy(MessageParcel &data, MessageParcel &reply)
458 {
459     int32_t netType = 0;
460     if (!data.ReadInt32(netType)) {
461         NETMGR_LOG_E("Read int32 data failed");
462         return NETMANAGER_ERR_READ_DATA_FAIL;
463     }
464 
465     std::string simId;
466     if (!data.ReadString(simId)) {
467         NETMGR_LOG_E("Read String data failed");
468         return NETMANAGER_ERR_READ_DATA_FAIL;
469     }
470 
471     uint32_t remindType = 0;
472     if (!data.ReadUint32(remindType)) {
473         NETMGR_LOG_E("Read uint32 data failed");
474         return NETMANAGER_ERR_READ_DATA_FAIL;
475     }
476 
477     int32_t result = UpdateRemindPolicy(netType, simId, remindType);
478     if (!reply.WriteInt32(result)) {
479         NETMGR_LOG_E("Write int32 reply failed");
480         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
481     }
482 
483     return NETMANAGER_SUCCESS;
484 }
485 
OnSetDeviceIdleTrustlist(MessageParcel & data,MessageParcel & reply)486 int32_t NetPolicyServiceStub::OnSetDeviceIdleTrustlist(MessageParcel &data, MessageParcel &reply)
487 {
488     std::vector<uint32_t> uids;
489     if (!data.ReadUInt32Vector(&uids)) {
490         NETMGR_LOG_E("Read uint32 data failed");
491         return NETMANAGER_ERR_READ_DATA_FAIL;
492     }
493 
494     bool isAllowed = false;
495     if (!data.ReadBool(isAllowed)) {
496         NETMGR_LOG_E("Read Bool data failed");
497         return NETMANAGER_ERR_READ_DATA_FAIL;
498     }
499 
500     int32_t result = SetDeviceIdleTrustlist(uids, isAllowed);
501     if (!reply.WriteInt32(result)) {
502         NETMGR_LOG_E("Write int32 reply failed");
503         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
504     }
505 
506     return NETMANAGER_SUCCESS;
507 }
508 
OnGetDeviceIdleTrustlist(MessageParcel & data,MessageParcel & reply)509 int32_t NetPolicyServiceStub::OnGetDeviceIdleTrustlist(MessageParcel &data, MessageParcel &reply)
510 {
511     std::vector<uint32_t> uids;
512     int32_t result = GetDeviceIdleTrustlist(uids);
513     if (!reply.WriteInt32(result)) {
514         NETMGR_LOG_E("Write int32 reply failed");
515         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
516     }
517     if (result == NETMANAGER_SUCCESS) {
518         if (!reply.WriteUInt32Vector(uids)) {
519             NETMGR_LOG_E("Write uint32 vector reply failed");
520             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
521         }
522     }
523 
524     return NETMANAGER_SUCCESS;
525 }
526 
OnSetDeviceIdlePolicy(MessageParcel & data,MessageParcel & reply)527 int32_t NetPolicyServiceStub::OnSetDeviceIdlePolicy(MessageParcel &data, MessageParcel &reply)
528 {
529     bool isAllowed = false;
530     if (!data.ReadBool(isAllowed)) {
531         NETMGR_LOG_E("Read Bool data failed");
532         return NETMANAGER_ERR_READ_DATA_FAIL;
533     }
534 
535     int32_t result = SetDeviceIdlePolicy(isAllowed);
536     if (!reply.WriteInt32(result)) {
537         NETMGR_LOG_E("Write int32 reply failed");
538         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
539     }
540     return NETMANAGER_SUCCESS;
541 }
542 
OnGetPowerSaveTrustlist(MessageParcel & data,MessageParcel & reply)543 int32_t NetPolicyServiceStub::OnGetPowerSaveTrustlist(MessageParcel &data, MessageParcel &reply)
544 {
545     std::vector<uint32_t> uids;
546     int32_t result = GetPowerSaveTrustlist(uids);
547     if (!reply.WriteInt32(result)) {
548         NETMGR_LOG_E("Write int32 reply failed");
549         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
550     }
551     if (result == NETMANAGER_SUCCESS) {
552         if (!reply.WriteUInt32Vector(uids)) {
553             NETMGR_LOG_E("Write uint32 Vector reply failed");
554             return NETMANAGER_ERR_WRITE_REPLY_FAIL;
555         }
556     }
557 
558     return NETMANAGER_SUCCESS;
559 }
560 
OnSetPowerSaveTrustlist(MessageParcel & data,MessageParcel & reply)561 int32_t NetPolicyServiceStub::OnSetPowerSaveTrustlist(MessageParcel &data, MessageParcel &reply)
562 {
563     std::vector<uint32_t> uids;
564     if (!data.ReadUInt32Vector(&uids)) {
565         NETMGR_LOG_E("Read uint32 data failed");
566         return NETMANAGER_ERR_READ_DATA_FAIL;
567     }
568 
569     bool isAllowed = false;
570     if (!data.ReadBool(isAllowed)) {
571         NETMGR_LOG_E("Read Bool data failed");
572         return NETMANAGER_ERR_READ_DATA_FAIL;
573     }
574 
575     int32_t result = SetPowerSaveTrustlist(uids, isAllowed);
576     if (!reply.WriteInt32(result)) {
577         NETMGR_LOG_E("Write int32 reply failed");
578         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
579     }
580 
581     return NETMANAGER_SUCCESS;
582 }
583 
OnSetPowerSavePolicy(MessageParcel & data,MessageParcel & reply)584 int32_t NetPolicyServiceStub::OnSetPowerSavePolicy(MessageParcel &data, MessageParcel &reply)
585 {
586     bool isAllowed = false;
587     if (!data.ReadBool(isAllowed)) {
588         NETMGR_LOG_E("Read Bool data failed");
589         return NETMANAGER_ERR_READ_DATA_FAIL;
590     }
591 
592     int32_t result = SetPowerSavePolicy(isAllowed);
593     if (!reply.WriteInt32(result)) {
594         NETMGR_LOG_E("Write int32 reply failed");
595         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
596     }
597 
598     return NETMANAGER_SUCCESS;
599 }
600 
OnCheckPermission(MessageParcel & data,MessageParcel & reply)601 int32_t NetPolicyServiceStub::OnCheckPermission(MessageParcel &data, MessageParcel &reply)
602 {
603     if (!reply.WriteInt32(NETMANAGER_SUCCESS)) {
604         NETMGR_LOG_E("Write int32 reply failed");
605         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
606     }
607     return NETMANAGER_SUCCESS;
608 }
609 } // namespace NetManagerStandard
610 } // namespace OHOS
611