1 /* 2 * Copyright (c) 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 NETWORK_VPN_SERVICE_H 17 #define NETWORK_VPN_SERVICE_H 18 19 #include "event_handler.h" 20 #include "i_vpn_conn_state_cb.h" 21 #include "net_vpn_impl.h" 22 #include "networkvpn_service_stub.h" 23 #include "os_account_manager.h" 24 #include "singleton.h" 25 #include "system_ability.h" 26 27 namespace OHOS { 28 namespace NetManagerStandard { 29 class NetworkVpnService : public SystemAbility, public NetworkVpnServiceStub { 30 DECLARE_SINGLETON(NetworkVpnService) 31 DECLARE_SYSTEM_ABILITY(NetworkVpnService) 32 33 enum ServiceRunningState { 34 STATE_STOPPED = 0, 35 STATE_RUNNING, 36 }; 37 38 class VpnConnStateCb : public IVpnConnStateCb { 39 public: VpnConnStateCb(const NetworkVpnService & vpnService)40 explicit VpnConnStateCb(const NetworkVpnService &vpnService) : vpnService_(vpnService){}; 41 virtual ~VpnConnStateCb() = default; 42 void OnVpnConnStateChanged(const VpnConnectState &state) override; 43 44 private: 45 const NetworkVpnService &vpnService_; 46 }; 47 48 public: 49 /** 50 * service start 51 */ 52 void OnStart() override; 53 54 /** 55 * service stop 56 */ 57 void OnStop() override; 58 59 /** 60 * check current whether has vpn is running 61 */ 62 int32_t Prepare(bool &isExistVpn, bool &isRun, std::string &pkg) override; 63 64 /** 65 * This function is called when the three-party vpn application negotiation ends 66 */ 67 int32_t SetUpVpn(const sptr<VpnConfig> &config) override; 68 69 /** 70 * protect vpn tunnel 71 */ 72 int32_t Protect() override; 73 74 /** 75 * stop the vpn connection 76 */ 77 int32_t DestroyVpn() override; 78 79 /** 80 * register callback 81 */ 82 int32_t RegisterVpnEvent(const sptr<IVpnEventCallback> callback) override; 83 84 /** 85 * unregister callback 86 */ 87 int32_t UnregisterVpnEvent(const sptr<IVpnEventCallback> callback) override; 88 89 /** 90 * create the vpn connection 91 */ 92 int32_t CreateVpnConnection() override; 93 94 /** 95 * dump function 96 */ 97 int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override; 98 99 private: 100 bool Init(); 101 void GetDumpMessage(std::string &message); 102 int32_t CheckCurrentAccountType(int32_t &userId); 103 104 void OnVpnMultiUserSetUp(); 105 int32_t SyncRegisterVpnEvent(const sptr<IVpnEventCallback> callback); 106 int32_t SyncUnregisterVpnEvent(const sptr<IVpnEventCallback> callback); 107 108 private: 109 ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED; 110 bool isServicePublished_ = false; 111 std::shared_ptr<IVpnConnStateCb> vpnConnCallback_; 112 std::shared_ptr<NetVpnImpl> vpnObj_; 113 114 std::vector<sptr<IVpnEventCallback>> vpnEventCallbacks_; 115 std::shared_ptr<AppExecFwk::EventRunner> policyCallRunner_; 116 std::shared_ptr<AppExecFwk::EventHandler> policyCallHandler_; 117 }; 118 } // namespace NetManagerStandard 119 } // namespace OHOS 120 #endif // NETWORK_VPN_SERVICE_H 121