Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / renderer / p2p / ipc_network_manager.cc
blobd1ea5ca5d26560fda2b0802e6b689c77c168d0bd
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"
7 #include <string>
8 #include "base/bind.h"
9 #include "base/command_line.h"
10 #include "base/location.h"
11 #include "base/metrics/histogram.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/sys_byteorder.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "content/public/common/content_switches.h"
16 #include "net/base/ip_address_number.h"
17 #include "net/base/net_util.h"
18 #include "net/base/network_change_notifier.h"
19 #include "net/base/network_interfaces.h"
21 namespace content {
23 namespace {
25 rtc::AdapterType ConvertConnectionTypeToAdapterType(
26 net::NetworkChangeNotifier::ConnectionType type) {
27 switch (type) {
28 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN:
29 return rtc::ADAPTER_TYPE_UNKNOWN;
30 case net::NetworkChangeNotifier::CONNECTION_ETHERNET:
31 return rtc::ADAPTER_TYPE_ETHERNET;
32 case net::NetworkChangeNotifier::CONNECTION_WIFI:
33 return rtc::ADAPTER_TYPE_WIFI;
34 case net::NetworkChangeNotifier::CONNECTION_2G:
35 case net::NetworkChangeNotifier::CONNECTION_3G:
36 case net::NetworkChangeNotifier::CONNECTION_4G:
37 return rtc::ADAPTER_TYPE_CELLULAR;
38 default:
39 return rtc::ADAPTER_TYPE_UNKNOWN;
43 } // namespace
45 IpcNetworkManager::IpcNetworkManager(NetworkListManager* network_list_manager)
46 : network_list_manager_(network_list_manager),
47 start_count_(0),
48 network_list_received_(false),
49 weak_factory_(this) {
50 network_list_manager_->AddNetworkListObserver(this);
53 IpcNetworkManager::~IpcNetworkManager() {
54 DCHECK(!start_count_);
55 network_list_manager_->RemoveNetworkListObserver(this);
58 void IpcNetworkManager::StartUpdating() {
59 if (network_list_received_) {
60 // Post a task to avoid reentrancy.
61 base::ThreadTaskRunnerHandle::Get()->PostTask(
62 FROM_HERE, base::Bind(&IpcNetworkManager::SendNetworksChangedSignal,
63 weak_factory_.GetWeakPtr()));
65 ++start_count_;
68 void IpcNetworkManager::StopUpdating() {
69 DCHECK_GT(start_count_, 0);
70 --start_count_;
73 void IpcNetworkManager::OnNetworkListChanged(
74 const net::NetworkInterfaceList& list) {
76 // Update flag if network list received for the first time.
77 if (!network_list_received_)
78 network_list_received_ = true;
80 // rtc::Network uses these prefix_length to compare network
81 // interfaces discovered.
82 std::vector<rtc::Network*> networks;
83 for (net::NetworkInterfaceList::const_iterator it = list.begin();
84 it != list.end(); it++) {
85 if (it->address.size() == net::kIPv4AddressSize) {
86 uint32 address;
87 memcpy(&address, &it->address[0], sizeof(uint32));
88 address = rtc::NetworkToHost32(address);
89 rtc::IPAddress prefix =
90 rtc::TruncateIP(rtc::IPAddress(address), it->prefix_length);
91 rtc::Network* network =
92 new rtc::Network(it->name, it->name, prefix, it->prefix_length,
93 ConvertConnectionTypeToAdapterType(it->type));
94 network->AddIP(rtc::IPAddress(address));
95 networks.push_back(network);
96 } else if (it->address.size() == net::kIPv6AddressSize) {
97 in6_addr address;
98 memcpy(&address, &it->address[0], sizeof(in6_addr));
99 rtc::InterfaceAddress ip6_addr(address, it->ip_address_attributes);
101 // Only allow non-deprecated IPv6 addresses which don't contain MAC.
102 if (rtc::IPIsMacBased(ip6_addr) ||
103 (it->ip_address_attributes & net::IP_ADDRESS_ATTRIBUTE_DEPRECATED)) {
104 continue;
107 if (!rtc::IPIsPrivate(ip6_addr)) {
108 rtc::IPAddress prefix =
109 rtc::TruncateIP(rtc::IPAddress(ip6_addr), it->prefix_length);
110 rtc::Network* network =
111 new rtc::Network(it->name, it->name, prefix, it->prefix_length,
112 ConvertConnectionTypeToAdapterType(it->type));
113 network->AddIP(ip6_addr);
114 networks.push_back(network);
119 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
120 switches::kAllowLoopbackInPeerConnection)) {
121 std::string name_v4("loopback_ipv4");
122 rtc::IPAddress ip_address_v4(INADDR_LOOPBACK);
123 rtc::Network* network_v4 = new rtc::Network(
124 name_v4, name_v4, ip_address_v4, 32, rtc::ADAPTER_TYPE_UNKNOWN);
125 network_v4->AddIP(ip_address_v4);
126 networks.push_back(network_v4);
128 std::string name_v6("loopback_ipv6");
129 rtc::IPAddress ip_address_v6(in6addr_loopback);
130 rtc::Network* network_v6 = new rtc::Network(
131 name_v6, name_v6, ip_address_v6, 64, rtc::ADAPTER_TYPE_UNKNOWN);
132 network_v6->AddIP(ip_address_v6);
133 networks.push_back(network_v6);
136 bool changed = false;
137 NetworkManager::Stats stats;
138 MergeNetworkList(networks, &changed, &stats);
139 if (changed)
140 SignalNetworksChanged();
142 // Send interface counts to UMA.
143 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces",
144 stats.ipv4_network_count);
145 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6Interfaces",
146 stats.ipv6_network_count);
149 void IpcNetworkManager::SendNetworksChangedSignal() {
150 SignalNetworksChanged();
153 } // namespace content