• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef NETWORKSHARE_SERVICE_H
17 #define NETWORKSHARE_SERVICE_H
18 
19 #include <memory>
20 #include "common_event.h"
21 #include "common_event_data.h"
22 #include "common_event_manager.h"
23 #include "common_event_support.h"
24 #include "singleton.h"
25 #include "system_ability.h"
26 #include "errorcode_convertor.h"
27 #include "network_share_service_stub.h"
28 #include "networkshare_tracker.h"
29 #include "ffrt.h"
30 
31 namespace OHOS {
32 namespace NetManagerStandard {
33 class NetworkShareService : public SystemAbility,
34                             public NetworkShareServiceStub,
35                             public std::enable_shared_from_this<NetworkShareService> {
36     DECLARE_DELAYED_SINGLETON(NetworkShareService)
37     DECLARE_SYSTEM_ABILITY(NetworkShareService)
38 
39     enum ServiceRunningState {
40         STATE_STOPPED = 0,
41         STATE_RUNNING,
42     };
43 
44     class CommonEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
45     public:
CommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)46         explicit CommonEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
47             : OHOS::EventFwk::CommonEventSubscriber(subscribeInfo) {};
48         virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override;
49     };
50 
51 #ifdef SHARE_NOTIFICATION_ENABLE
52     class WifiShareNtfSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
53     public:
WifiShareNtfSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)54         explicit WifiShareNtfSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
55             : OHOS::EventFwk::CommonEventSubscriber(subscribeInfo) {};
56         virtual void OnReceiveEvent(const EventFwk::CommonEventData &eventData) override;
57     };
58 #endif
59 public:
60     /**
61      * service start
62      */
63     void OnStart() override;
64 
65     /**
66      * service stop
67      */
68     void OnStop() override;
69 
70     /**
71      * is surpport share network
72      */
73     int32_t IsNetworkSharingSupported(int32_t &supported) override;
74 
75     /**
76      * has shared network
77      */
78     int32_t IsSharing(int32_t &sharingStatus) override;
79 
80     /**
81      * start network by type
82      */
83     int32_t StartNetworkSharing(int32_t type) override;
84 
85     /**
86      * stop network by type
87      */
88     int32_t StopNetworkSharing(int32_t type) override;
89 
90     /**
91      * get sharable regex
92      */
93     int32_t GetSharableRegexs(int32_t type, std::vector<std::string> &ifaceRegexs) override;
94 
95     /**
96      * get sharing type
97      */
98     int32_t GetSharingState(int32_t type, int32_t &state) override;
99 
100     /**
101      * get sharing ifaces
102      */
103     int32_t GetNetSharingIfaces(int32_t state, std::vector<std::string> &ifaces) override;
104 
105     /**
106      * register callback
107      */
108     int32_t RegisterSharingEvent(const sptr<ISharingEventCallback>& callback) override;
109 
110     /**
111      * unregister callback
112      */
113     int32_t UnregisterSharingEvent(const sptr<ISharingEventCallback>& callback) override;
114 
115     /**
116      * get downlink data bytes
117      */
118     int32_t GetStatsRxBytes(int32_t &bytes) override;
119 
120     /**
121      * get uplink data bytes
122      */
123     int32_t GetStatsTxBytes(int32_t &bytes) override;
124 
125     /**
126      * get total data bytes
127      */
128     int32_t GetStatsTotalBytes(int32_t &bytes) override;
129 
130     /**
131      * dump function
132      */
133     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
134 
135     /**
136      * set sysctl prop
137      */
138     int32_t SetConfigureForShare(bool enabled) override;
139 
140     int32_t GetBundleNameByUid(const int uid, std::string &bundleName);
141 
142 protected:
143     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
144     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
145 
146 private:
147     bool Init();
148     void GetDumpMessage(std::string &message);
149     void GetSharingType(const SharingIfaceType &type, const std::string &typeContent, std::string &sharingType);
150     void GetShareRegexsContent(const SharingIfaceType &type, std::string &shareRegexsContent);
151 
152     void OnNetSysRestart();
153     static void DisAllowNetworkShareEventCallback(const char *key, const char *value, void *context);
154     void SubscribeCommonEvent();
155 #ifdef SHARE_NOTIFICATION_ENABLE
156     void SubscribeWifiShareNtfEvent();
157 #endif
158 
159 private:
160     ServiceRunningState state_ = ServiceRunningState::STATE_STOPPED;
161     bool registerToService_ = false;
162     std::shared_ptr<CommonEventSubscriber> commonEventSubscriber_ = nullptr;
163 #ifdef SHARE_NOTIFICATION_ENABLE
164     std::shared_ptr<WifiShareNtfSubscriber> wifiShareNtfSubscriber_ = nullptr;
165 #endif
166     bool hasSARemoved_ = false;
167     int32_t setConfigTimes_ = 0;
168     ffrt::mutex setConfigureMutex_;
169     ffrt::mutex openFileMutex_;
170 };
171 } // namespace NetManagerStandard
172 } // namespace OHOS
173 #endif // NETWORKSHARE_SERVICE_H
174