1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/p2p/ipc_network_manager.h"
8 #include "base/command_line.h"
9 #include "base/metrics/histogram.h"
10 #include "base/sys_byteorder.h"
11 #include "content/public/common/content_switches.h"
12 #include "net/base/net_util.h"
18 rtc::AdapterType
ConvertConnectionTypeToAdapterType(
19 net::NetworkChangeNotifier::ConnectionType type
) {
21 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN
:
22 return rtc::ADAPTER_TYPE_UNKNOWN
;
23 case net::NetworkChangeNotifier::CONNECTION_ETHERNET
:
24 return rtc::ADAPTER_TYPE_ETHERNET
;
25 case net::NetworkChangeNotifier::CONNECTION_WIFI
:
26 return rtc::ADAPTER_TYPE_WIFI
;
27 case net::NetworkChangeNotifier::CONNECTION_2G
:
28 case net::NetworkChangeNotifier::CONNECTION_3G
:
29 case net::NetworkChangeNotifier::CONNECTION_4G
:
30 return rtc::ADAPTER_TYPE_CELLULAR
;
32 return rtc::ADAPTER_TYPE_UNKNOWN
;
38 IpcNetworkManager::IpcNetworkManager(NetworkListManager
* network_list_manager
)
39 : network_list_manager_(network_list_manager
),
41 network_list_received_(false),
43 network_list_manager_
->AddNetworkListObserver(this);
46 IpcNetworkManager::~IpcNetworkManager() {
47 DCHECK(!start_count_
);
48 network_list_manager_
->RemoveNetworkListObserver(this);
51 void IpcNetworkManager::StartUpdating() {
52 if (network_list_received_
) {
53 // Post a task to avoid reentrancy.
54 base::MessageLoop::current()->PostTask(
56 base::Bind(&IpcNetworkManager::SendNetworksChangedSignal
,
57 weak_factory_
.GetWeakPtr()));
62 void IpcNetworkManager::StopUpdating() {
63 DCHECK_GT(start_count_
, 0);
67 void IpcNetworkManager::OnNetworkListChanged(
68 const net::NetworkInterfaceList
& list
) {
70 // Update flag if network list received for the first time.
71 if (!network_list_received_
)
72 network_list_received_
= true;
74 // rtc::Network uses these prefix_length to compare network
75 // interfaces discovered.
76 std::vector
<rtc::Network
*> networks
;
77 for (net::NetworkInterfaceList::const_iterator it
= list
.begin();
78 it
!= list
.end(); it
++) {
79 if (it
->address
.size() == net::kIPv4AddressSize
) {
81 memcpy(&address
, &it
->address
[0], sizeof(uint32
));
82 address
= rtc::NetworkToHost32(address
);
83 rtc::IPAddress prefix
=
84 rtc::TruncateIP(rtc::IPAddress(address
), it
->prefix_length
);
85 rtc::Network
* network
=
86 new rtc::Network(it
->name
, it
->name
, prefix
, it
->prefix_length
,
87 ConvertConnectionTypeToAdapterType(it
->type
));
88 network
->AddIP(rtc::IPAddress(address
));
89 networks
.push_back(network
);
90 } else if (it
->address
.size() == net::kIPv6AddressSize
) {
92 memcpy(&address
, &it
->address
[0], sizeof(in6_addr
));
93 rtc::IPAddress
ip6_addr(address
);
94 if (!rtc::IPIsPrivate(ip6_addr
)) {
95 rtc::IPAddress prefix
=
96 rtc::TruncateIP(rtc::IPAddress(ip6_addr
), it
->prefix_length
);
97 rtc::Network
* network
=
98 new rtc::Network(it
->name
, it
->name
, prefix
, it
->prefix_length
,
99 ConvertConnectionTypeToAdapterType(it
->type
));
100 network
->AddIP(ip6_addr
);
101 networks
.push_back(network
);
106 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
107 switches::kAllowLoopbackInPeerConnection
)) {
108 std::string
name_v4("loopback_ipv4");
109 rtc::IPAddress
ip_address_v4(INADDR_LOOPBACK
);
110 rtc::Network
* network_v4
= new rtc::Network(
111 name_v4
, name_v4
, ip_address_v4
, 32, rtc::ADAPTER_TYPE_UNKNOWN
);
112 network_v4
->AddIP(ip_address_v4
);
113 networks
.push_back(network_v4
);
115 std::string
name_v6("loopback_ipv6");
116 rtc::IPAddress
ip_address_v6(in6addr_loopback
);
117 rtc::Network
* network_v6
= new rtc::Network(
118 name_v6
, name_v6
, ip_address_v6
, 64, rtc::ADAPTER_TYPE_UNKNOWN
);
119 network_v6
->AddIP(ip_address_v6
);
120 networks
.push_back(network_v6
);
123 bool changed
= false;
124 NetworkManager::Stats stats
;
125 MergeNetworkList(networks
, &changed
, &stats
);
127 SignalNetworksChanged();
129 // Send interface counts to UMA.
130 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces",
131 stats
.ipv4_network_count
);
132 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6Interfaces",
133 stats
.ipv6_network_count
);
136 void IpcNetworkManager::SendNetworksChangedSignal() {
137 SignalNetworksChanged();
140 } // namespace content