Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / p2p / ipc_network_manager.cc
blobe279f9830f9696dfde53e711f20f279de7dfd9c0
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"
6 #include <string>
7 #include "base/bind.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"
14 namespace content {
16 namespace {
18 rtc::AdapterType ConvertConnectionTypeToAdapterType(
19 net::NetworkChangeNotifier::ConnectionType type) {
20 switch (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;
31 default:
32 return rtc::ADAPTER_TYPE_UNKNOWN;
36 } // namespace
38 IpcNetworkManager::IpcNetworkManager(NetworkListManager* network_list_manager)
39 : network_list_manager_(network_list_manager),
40 start_count_(0),
41 network_list_received_(false),
42 weak_factory_(this) {
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(
55 FROM_HERE,
56 base::Bind(&IpcNetworkManager::SendNetworksChangedSignal,
57 weak_factory_.GetWeakPtr()));
59 ++start_count_;
62 void IpcNetworkManager::StopUpdating() {
63 DCHECK_GT(start_count_, 0);
64 --start_count_;
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) {
80 uint32 address;
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) {
91 in6_addr address;
92 memcpy(&address, &it->address[0], sizeof(in6_addr));
93 rtc::InterfaceAddress ip6_addr(address, it->ip_address_attributes);
95 // Only allow non-deprecated IPv6 addresses which don't contain MAC.
96 if (rtc::IPIsMacBased(ip6_addr) ||
97 (it->ip_address_attributes & net::IP_ADDRESS_ATTRIBUTE_DEPRECATED)) {
98 continue;
101 if (!rtc::IPIsPrivate(ip6_addr)) {
102 rtc::IPAddress prefix =
103 rtc::TruncateIP(rtc::IPAddress(ip6_addr), it->prefix_length);
104 rtc::Network* network =
105 new rtc::Network(it->name, it->name, prefix, it->prefix_length,
106 ConvertConnectionTypeToAdapterType(it->type));
107 network->AddIP(ip6_addr);
108 networks.push_back(network);
113 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
114 switches::kAllowLoopbackInPeerConnection)) {
115 std::string name_v4("loopback_ipv4");
116 rtc::IPAddress ip_address_v4(INADDR_LOOPBACK);
117 rtc::Network* network_v4 = new rtc::Network(
118 name_v4, name_v4, ip_address_v4, 32, rtc::ADAPTER_TYPE_UNKNOWN);
119 network_v4->AddIP(ip_address_v4);
120 networks.push_back(network_v4);
122 std::string name_v6("loopback_ipv6");
123 rtc::IPAddress ip_address_v6(in6addr_loopback);
124 rtc::Network* network_v6 = new rtc::Network(
125 name_v6, name_v6, ip_address_v6, 64, rtc::ADAPTER_TYPE_UNKNOWN);
126 network_v6->AddIP(ip_address_v6);
127 networks.push_back(network_v6);
130 bool changed = false;
131 NetworkManager::Stats stats;
132 MergeNetworkList(networks, &changed, &stats);
133 if (changed)
134 SignalNetworksChanged();
136 // Send interface counts to UMA.
137 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces",
138 stats.ipv4_network_count);
139 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6Interfaces",
140 stats.ipv6_network_count);
143 void IpcNetworkManager::SendNetworksChangedSignal() {
144 SignalNetworksChanged();
147 } // namespace content