• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2015 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef SDK_ANDROID_SRC_JNI_ANDROID_NETWORK_MONITOR_H_
12 #define SDK_ANDROID_SRC_JNI_ANDROID_NETWORK_MONITOR_H_
13 
14 #include <stdint.h>
15 #include <map>
16 #include <string>
17 #include <vector>
18 
19 #include "absl/types/optional.h"
20 #include "rtc_base/network_monitor.h"
21 #include "rtc_base/thread_checker.h"
22 #include "sdk/android/src/jni/jni_helpers.h"
23 
24 namespace webrtc {
25 namespace jni {
26 
27 typedef int64_t NetworkHandle;
28 
29 // c++ equivalent of java NetworkMonitorAutoDetect.ConnectionType.
30 enum NetworkType {
31   NETWORK_UNKNOWN,
32   NETWORK_ETHERNET,
33   NETWORK_WIFI,
34   NETWORK_5G,
35   NETWORK_4G,
36   NETWORK_3G,
37   NETWORK_2G,
38   NETWORK_UNKNOWN_CELLULAR,
39   NETWORK_BLUETOOTH,
40   NETWORK_VPN,
41   NETWORK_NONE
42 };
43 
44 // The information is collected from Android OS so that the native code can get
45 // the network type and handle (Android network ID) for each interface.
46 struct NetworkInformation {
47   std::string interface_name;
48   NetworkHandle handle;
49   NetworkType type;
50   NetworkType underlying_type_for_vpn;
51   std::vector<rtc::IPAddress> ip_addresses;
52 
53   NetworkInformation();
54   NetworkInformation(const NetworkInformation&);
55   NetworkInformation(NetworkInformation&&);
56   ~NetworkInformation();
57   NetworkInformation& operator=(const NetworkInformation&);
58   NetworkInformation& operator=(NetworkInformation&&);
59 
60   std::string ToString() const;
61 };
62 
63 class AndroidNetworkMonitor : public rtc::NetworkMonitorBase,
64                               public rtc::NetworkBinderInterface {
65  public:
66   AndroidNetworkMonitor(JNIEnv* env,
67                         const JavaRef<jobject>& j_application_context);
68   ~AndroidNetworkMonitor() override;
69 
70   // TODO(sakal): Remove once down stream dependencies have been updated.
SetAndroidContext(JNIEnv * jni,jobject context)71   static void SetAndroidContext(JNIEnv* jni, jobject context) {}
72 
73   void Start() override;
74   void Stop() override;
75 
76   rtc::NetworkBindingResult BindSocketToNetwork(
77       int socket_fd,
78       const rtc::IPAddress& address) override;
79   rtc::AdapterType GetAdapterType(const std::string& if_name) override;
80   rtc::AdapterType GetVpnUnderlyingAdapterType(
81       const std::string& if_name) override;
82   void OnNetworkConnected(const NetworkInformation& network_info);
83   void OnNetworkDisconnected(NetworkHandle network_handle);
84   // Always expected to be called on the network thread.
85   void SetNetworkInfos(const std::vector<NetworkInformation>& network_infos);
86 
87   void NotifyConnectionTypeChanged(JNIEnv* env,
88                                    const JavaRef<jobject>& j_caller);
89   void NotifyOfNetworkConnect(JNIEnv* env,
90                               const JavaRef<jobject>& j_caller,
91                               const JavaRef<jobject>& j_network_info);
92   void NotifyOfNetworkDisconnect(JNIEnv* env,
93                                  const JavaRef<jobject>& j_caller,
94                                  jlong network_handle);
95   void NotifyOfActiveNetworkList(JNIEnv* env,
96                                  const JavaRef<jobject>& j_caller,
97                                  const JavaRef<jobjectArray>& j_network_infos);
98 
99   // Visible for testing.
100   absl::optional<NetworkHandle> FindNetworkHandleFromAddress(
101       const rtc::IPAddress& address) const;
102 
103  private:
104   void OnNetworkConnected_w(const NetworkInformation& network_info);
105   void OnNetworkDisconnected_w(NetworkHandle network_handle);
106 
107   const int android_sdk_int_;
108   ScopedJavaGlobalRef<jobject> j_application_context_;
109   ScopedJavaGlobalRef<jobject> j_network_monitor_;
110   rtc::ThreadChecker thread_checker_;
111   bool started_ = false;
112   std::map<std::string, rtc::AdapterType> adapter_type_by_name_;
113   std::map<std::string, rtc::AdapterType> vpn_underlying_adapter_type_by_name_;
114   std::map<rtc::IPAddress, NetworkHandle> network_handle_by_address_;
115   std::map<NetworkHandle, NetworkInformation> network_info_by_handle_;
116   bool find_network_handle_without_ipv6_temporary_part_;
117   bool surface_cellular_types_;
118 };
119 
120 class AndroidNetworkMonitorFactory : public rtc::NetworkMonitorFactory {
121  public:
122   // Deprecated. Pass in application context to this class.
123   AndroidNetworkMonitorFactory();
124 
125   AndroidNetworkMonitorFactory(JNIEnv* env,
126                                const JavaRef<jobject>& j_application_context);
127 
128   ~AndroidNetworkMonitorFactory() override;
129 
130   rtc::NetworkMonitorInterface* CreateNetworkMonitor() override;
131 
132  private:
133   ScopedJavaGlobalRef<jobject> j_application_context_;
134 };
135 
136 }  // namespace jni
137 }  // namespace webrtc
138 
139 // TODO(magjed): Remove once external clients are updated.
140 namespace webrtc_jni {
141 
142 using webrtc::jni::AndroidNetworkMonitor;
143 using webrtc::jni::AndroidNetworkMonitorFactory;
144 
145 }  // namespace webrtc_jni
146 
147 #endif  // SDK_ANDROID_SRC_JNI_ANDROID_NETWORK_MONITOR_H_
148