Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / p2p / ipc_network_manager.cc
blob0caa69c997ea032ff74f21fd3d013b632eaa1a28
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"
20 namespace content {
22 namespace {
24 rtc::AdapterType ConvertConnectionTypeToAdapterType(
25 net::NetworkChangeNotifier::ConnectionType type) {
26 switch (type) {
27 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN:
28 return rtc::ADAPTER_TYPE_UNKNOWN;
29 case net::NetworkChangeNotifier::CONNECTION_ETHERNET:
30 return rtc::ADAPTER_TYPE_ETHERNET;
31 case net::NetworkChangeNotifier::CONNECTION_WIFI:
32 return rtc::ADAPTER_TYPE_WIFI;
33 case net::NetworkChangeNotifier::CONNECTION_2G:
34 case net::NetworkChangeNotifier::CONNECTION_3G:
35 case net::NetworkChangeNotifier::CONNECTION_4G:
36 return rtc::ADAPTER_TYPE_CELLULAR;
37 default:
38 return rtc::ADAPTER_TYPE_UNKNOWN;
42 } // namespace
44 IpcNetworkManager::IpcNetworkManager(NetworkListManager* network_list_manager)
45 : network_list_manager_(network_list_manager),
46 start_count_(0),
47 network_list_received_(false),
48 weak_factory_(this) {
49 network_list_manager_->AddNetworkListObserver(this);
52 IpcNetworkManager::~IpcNetworkManager() {
53 DCHECK(!start_count_);
54 network_list_manager_->RemoveNetworkListObserver(this);
57 void IpcNetworkManager::StartUpdating() {
58 if (network_list_received_) {
59 // Post a task to avoid reentrancy.
60 base::ThreadTaskRunnerHandle::Get()->PostTask(
61 FROM_HERE, base::Bind(&IpcNetworkManager::SendNetworksChangedSignal,
62 weak_factory_.GetWeakPtr()));
64 ++start_count_;
67 void IpcNetworkManager::StopUpdating() {
68 DCHECK_GT(start_count_, 0);
69 --start_count_;
72 void IpcNetworkManager::OnNetworkListChanged(
73 const net::NetworkInterfaceList& list) {
75 // Update flag if network list received for the first time.
76 if (!network_list_received_)
77 network_list_received_ = true;
79 // rtc::Network uses these prefix_length to compare network
80 // interfaces discovered.
81 std::vector<rtc::Network*> networks;
82 for (net::NetworkInterfaceList::const_iterator it = list.begin();
83 it != list.end(); it++) {
84 if (it->address.size() == net::kIPv4AddressSize) {
85 uint32 address;
86 memcpy(&address, &it->address[0], sizeof(uint32));
87 address = rtc::NetworkToHost32(address);
88 rtc::IPAddress prefix =
89 rtc::TruncateIP(rtc::IPAddress(address), it->prefix_length);
90 rtc::Network* network =
91 new rtc::Network(it->name, it->name, prefix, it->prefix_length,
92 ConvertConnectionTypeToAdapterType(it->type));
93 network->AddIP(rtc::IPAddress(address));
94 networks.push_back(network);
95 } else if (it->address.size() == net::kIPv6AddressSize) {
96 in6_addr address;
97 memcpy(&address, &it->address[0], sizeof(in6_addr));
98 rtc::InterfaceAddress ip6_addr(address, it->ip_address_attributes);
100 // Only allow non-deprecated IPv6 addresses which don't contain MAC.
101 if (rtc::IPIsMacBased(ip6_addr) ||
102 (it->ip_address_attributes & net::IP_ADDRESS_ATTRIBUTE_DEPRECATED)) {
103 continue;
106 if (!rtc::IPIsPrivate(ip6_addr)) {
107 rtc::IPAddress prefix =
108 rtc::TruncateIP(rtc::IPAddress(ip6_addr), it->prefix_length);
109 rtc::Network* network =
110 new rtc::Network(it->name, it->name, prefix, it->prefix_length,
111 ConvertConnectionTypeToAdapterType(it->type));
112 network->AddIP(ip6_addr);
113 networks.push_back(network);
118 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
119 switches::kAllowLoopbackInPeerConnection)) {
120 std::string name_v4("loopback_ipv4");
121 rtc::IPAddress ip_address_v4(INADDR_LOOPBACK);
122 rtc::Network* network_v4 = new rtc::Network(
123 name_v4, name_v4, ip_address_v4, 32, rtc::ADAPTER_TYPE_UNKNOWN);
124 network_v4->AddIP(ip_address_v4);
125 networks.push_back(network_v4);
127 std::string name_v6("loopback_ipv6");
128 rtc::IPAddress ip_address_v6(in6addr_loopback);
129 rtc::Network* network_v6 = new rtc::Network(
130 name_v6, name_v6, ip_address_v6, 64, rtc::ADAPTER_TYPE_UNKNOWN);
131 network_v6->AddIP(ip_address_v6);
132 networks.push_back(network_v6);
135 bool changed = false;
136 NetworkManager::Stats stats;
137 MergeNetworkList(networks, &changed, &stats);
138 if (changed)
139 SignalNetworksChanged();
141 // Send interface counts to UMA.
142 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces",
143 stats.ipv4_network_count);
144 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6Interfaces",
145 stats.ipv6_network_count);
148 void IpcNetworkManager::SendNetworksChangedSignal() {
149 SignalNetworksChanged();
152 } // namespace content