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 #ifndef NET_POLICY_SERVICE_STUB_H 17 #define NET_POLICY_SERVICE_STUB_H 18 19 #include <map> 20 #include <mutex> 21 22 #include "ffrt.h" 23 #include "iremote_stub.h" 24 #include "i_net_policy_service.h" 25 #include "net_policy_event_handler.h" 26 27 namespace OHOS { 28 namespace NetManagerStandard { 29 constexpr const char *NET_POLICY_STUB_QUEUE = "NET_POLICY_STUB_QUEUE"; 30 constexpr const int32_t NETWORK_POLICY_REPORT_DELAY = 10 * 1000 * 1000; 31 constexpr const int32_t HIVIEW_UID = 1201; 32 constexpr const char *NETWORK_POLICY_CHANGED_EVENT = "custom.event.NETWORK_POLICY_CHANGED"; 33 constexpr const char *NETWORK_POLICY_INFO_KEY = "POLICY_INFO"; 34 constexpr const uint32_t NET_POLICY_WIFI_ALLOW = (1 << 0); 35 constexpr const uint32_t NET_POLICY_CELLULAR_ALLOW = (1 << 1); 36 37 class NetPolicyServiceStub : public IRemoteStub<INetPolicyService> { 38 public: 39 NetPolicyServiceStub(); 40 ~NetPolicyServiceStub(); 41 42 int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 43 44 protected: 45 bool SubCheckPermission(const std::string &permission, uint32_t funcCode); 46 int32_t CheckPolicyPermission(uint32_t funcCode); 47 48 ffrt::queue ffrtQueue_; 49 std::shared_ptr<NetPolicyEventHandler> handler_; 50 51 private: 52 using NetPolicyServiceFunc = int32_t (NetPolicyServiceStub::*)(MessageParcel &, MessageParcel &); 53 54 private: 55 void InitEventHandler(); 56 void ExtraNetPolicyServiceStub(); 57 int32_t OnSetPolicyByUid(MessageParcel &data, MessageParcel &reply); 58 int32_t OnGetPolicyByUid(MessageParcel &data, MessageParcel &reply); 59 int32_t OnGetUidsByPolicy(MessageParcel &data, MessageParcel &reply); 60 int32_t OnIsUidNetAllowedMetered(MessageParcel &data, MessageParcel &reply); 61 int32_t OnIsUidNetAllowedIfaceName(MessageParcel &data, MessageParcel &reply); 62 int32_t OnRegisterNetPolicyCallback(MessageParcel &data, MessageParcel &reply); 63 int32_t OnUnregisterNetPolicyCallback(MessageParcel &data, MessageParcel &reply); 64 int32_t OnSetNetQuotaPolicies(MessageParcel &data, MessageParcel &reply); 65 int32_t OnGetNetQuotaPolicies(MessageParcel &data, MessageParcel &reply); 66 int32_t OnResetPolicies(MessageParcel &data, MessageParcel &reply); 67 int32_t OnSetBackgroundPolicy(MessageParcel &data, MessageParcel &reply); 68 int32_t OnGetBackgroundPolicy(MessageParcel &data, MessageParcel &reply); 69 int32_t OnGetBackgroundPolicyByUid(MessageParcel &data, MessageParcel &reply); 70 int32_t OnSnoozePolicy(MessageParcel &data, MessageParcel &reply); 71 int32_t OnSetDeviceIdleTrustlist(MessageParcel &data, MessageParcel &reply); 72 int32_t OnGetDeviceIdleTrustlist(MessageParcel &data, MessageParcel &reply); 73 int32_t OnSetDeviceIdlePolicy(MessageParcel &data, MessageParcel &reply); 74 int32_t OnGetPowerSaveTrustlist(MessageParcel &data, MessageParcel &reply); 75 int32_t OnSetPowerSaveTrustlist(MessageParcel &data, MessageParcel &reply); 76 int32_t OnSetPowerSavePolicy(MessageParcel &data, MessageParcel &reply); 77 int32_t OnCheckPermission(MessageParcel &data, MessageParcel &reply); 78 int32_t OnFactoryResetPolicies(MessageParcel &data, MessageParcel &reply); 79 int32_t OnSetNetworkAccessPolicy(MessageParcel &data, MessageParcel &reply); 80 int32_t OnGetNetworkAccessPolicy(MessageParcel &data, MessageParcel &reply); 81 int32_t OnNotifyNetAccessPolicyDiag(MessageParcel &data, MessageParcel &reply); 82 int32_t OnSetNicTrafficAllowed(MessageParcel &data, MessageParcel &reply); 83 84 private: 85 void HandleStoreNetworkPolicy(uint32_t uid, NetworkAccessPolicy &policy, 86 uint32_t callingUid); 87 void HandleReportNetworkPolicy(); 88 89 private: 90 std::map<uint32_t, NetPolicyServiceFunc> memberFuncMap_; 91 std::once_flag onceFlag; 92 std::mutex setNetworkPolicyMutex_; 93 bool isPostDelaySetNetworkPolicy_ = false; 94 std::map<uint32_t, std::map<uint32_t, uint32_t>> appNetworkPolicyMap_; 95 }; 96 } // namespace NetManagerStandard 97 } // namespace OHOS 98 #endif // NET_POLICY_SERVICE_STUB_H 99