Chromium Code Reviews| Index: talk/app/webrtc/java/jni/androidnetworkmonitor_jni.cc |
| diff --git a/talk/app/webrtc/java/jni/androidnetworkmonitor_jni.cc b/talk/app/webrtc/java/jni/androidnetworkmonitor_jni.cc |
| index 1716c19cf8be84344a3ef1bff3f7bebf8a7d5647..edb565965083e97400be7ae540601f7b5670bbb9 100644 |
| --- a/talk/app/webrtc/java/jni/androidnetworkmonitor_jni.cc |
| +++ b/talk/app/webrtc/java/jni/androidnetworkmonitor_jni.cc |
| @@ -71,6 +71,29 @@ static NetworkType GetNetworkTypeFromJava(JNIEnv* jni, jobject j_network_type) { |
| return NetworkType::NETWORK_UNKNOWN; |
| } |
| +static rtc::AdapterType NetworkTypeToAdapterType(NetworkType network_type) { |
| + switch (network_type) { |
|
pthatcher1
2016/01/19 02:13:07
I'd prefer AdapterTypeFromNetworkType
honghaiz3
2016/01/21 21:38:36
Done.
|
| + case NETWORK_UNKNOWN: |
| + RTC_DCHECK(false) << "Unknown network type "; |
| + return rtc::ADAPTER_TYPE_UNKNOWN; |
| + case NETWORK_ETHERNET: |
| + return rtc::ADAPTER_TYPE_ETHERNET; |
| + case NETWORK_WIFI: |
| + return rtc::ADAPTER_TYPE_WIFI; |
| + case NETWORK_4G: |
| + case NETWORK_3G: |
| + case NETWORK_2G: |
| + return rtc::ADAPTER_TYPE_CELLULAR; |
| + case NETWORK_BLUETOOTH: |
| + // There is no corresponding mapping for bluetooth networks. |
| + // Map it to VPN for now. |
| + return rtc::ADAPTER_TYPE_VPN; |
|
pthatcher1
2016/01/19 02:13:07
Would UNKNOWN be a better map? What does bluetoot
honghaiz3
2016/01/21 21:38:35
It has nothing to do with VPN, except perhaps, VPN
|
| + default: |
| + RTC_DCHECK(false) << "Invalid network type " << network_type; |
| + return rtc::ADAPTER_TYPE_UNKNOWN; |
| + } |
| +} |
| + |
| static rtc::IPAddress GetIPAddressFromJava(JNIEnv* jni, jobject j_ip_address) { |
| jclass j_ip_address_class = GetObjectClass(jni, j_ip_address); |
| jfieldID j_address_id = GetFieldID(jni, j_ip_address_class, "address", "[B"); |
| @@ -186,7 +209,37 @@ void AndroidNetworkMonitor::Start() { |
| jmethodID m = |
| GetMethodID(jni(), *j_network_monitor_class_, "startMonitoring", "(J)V"); |
| jni()->CallVoidMethod(*j_network_monitor_, m, jlongFromPointer(this)); |
| - CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.startMonitoring"; |
| + CHECK_EXCEPTION(jni()) << "Error during CallVoidMethod"; |
| + |
| + UpdateNetworkInfos(); |
| +} |
| + |
| +void AndroidNetworkMonitor::UpdateNetworkInfos() { |
| + std::vector<NetworkInformation> network_infos; |
| + GetAllNetworkInfos(&network_infos); |
| + for (NetworkInformation& info : network_infos) { |
| + // This method is already on the worker thread. |
| + OnNetworkAvailable_w(info); |
|
Taylor Brandstetter
2016/01/20 01:51:57
This will add networks to the maps, but not remove
honghaiz3
2016/01/21 21:38:36
Partially it was because we do not have the signal
|
| + } |
| +} |
| + |
| +void AndroidNetworkMonitor::GetAllNetworkInfos( |
| + std::vector<NetworkInformation>* network_infos) { |
| + RTC_CHECK(thread_checker_.CalledOnValidThread()); |
| + network_infos->clear(); |
| + jmethodID m = GetMethodID( |
| + jni(), *j_network_monitor_class_, "getAllNetworkInfos", |
| + "()[Lorg/webrtc/NetworkMonitorAutoDetect$NetworkInformation;"); |
| + jobjectArray j_network_infos = static_cast<jobjectArray>( |
| + jni()->CallObjectMethod(*j_network_monitor_, m)); |
| + CHECK_EXCEPTION(jni()) << "Error during CallObjectMethod"; |
| + size_t num_infos = jni()->GetArrayLength(j_network_infos); |
| + for (size_t i = 0; i < num_infos; ++i) { |
| + jobject j_network_info = jni()->GetObjectArrayElement(j_network_infos, i); |
| + CHECK_EXCEPTION(jni()) << "Error during GetObjectArrayElement"; |
| + network_infos->push_back( |
| + GetNetworkInformationFromJava(jni(), j_network_info)); |
| + } |
| } |
| void AndroidNetworkMonitor::Stop() { |
| @@ -208,6 +261,7 @@ void AndroidNetworkMonitor::Stop() { |
| CHECK_EXCEPTION(jni()) << "Error during NetworkMonitor.stopMonitoring"; |
| network_info_by_address_.clear(); |
| + adapter_types_.clear(); |
| } |
| int AndroidNetworkMonitor::BindSocketToNetwork(int socket_fd, |
| @@ -266,11 +320,21 @@ void AndroidNetworkMonitor::OnNetworkAvailable( |
| void AndroidNetworkMonitor::OnNetworkAvailable_w( |
| const NetworkInformation& network_info) { |
| LOG(LS_INFO) << "Network available: " << network_info.ToString(); |
| + adapter_types_[network_info.interface_name] = |
| + NetworkTypeToAdapterType(network_info.type); |
| for (rtc::IPAddress address : network_info.ip_addresses) { |
| network_info_by_address_[address] = network_info; |
| } |
|
Taylor Brandstetter
2016/01/20 01:51:57
It seems redundant to have two data structures tha
honghaiz3
2016/01/21 21:38:36
I used two structures so speed up the lookup. But
Taylor Brandstetter
2016/01/26 02:35:09
Ah; so the second map exists so you can remember t
|
| } |
| +rtc::AdapterType AndroidNetworkMonitor::GetAdapterType( |
| + const std::string& if_name) { |
| + if (adapter_types_.find(if_name) == adapter_types_.end()) { |
| + UpdateNetworkInfos(); |
|
Taylor Brandstetter
2016/01/20 01:51:57
Why is UpdateNetworkInfos necessary here? Won't na
honghaiz3
2016/01/21 21:38:36
There might be some short-term inconsistency betwe
|
| + } |
| + return adapter_types_[if_name]; |
|
pthatcher1
2016/01/19 02:13:07
You can avoid too lookups by storing the result of
honghaiz3
2016/01/21 21:38:36
I am removing the update here.
It was a little co
|
| +} |
| + |
| rtc::NetworkMonitorInterface* |
| AndroidNetworkMonitorFactory::CreateNetworkMonitor() { |
| return new AndroidNetworkMonitor(); |