OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/p2p/ipc_network_manager.h" | 5 #include "content/renderer/p2p/ipc_network_manager.h" |
6 | 6 #include <string> |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/sys_byteorder.h" | 10 #include "base/sys_byteorder.h" |
11 #include "content/public/common/content_switches.h" | 11 #include "content/public/common/content_switches.h" |
12 #include "net/base/net_util.h" | 12 #include "net/base/net_util.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 namespace { | 16 namespace { |
(...skipping 11 matching lines...) Expand all Loading... |
28 case net::NetworkChangeNotifier::CONNECTION_3G: | 28 case net::NetworkChangeNotifier::CONNECTION_3G: |
29 case net::NetworkChangeNotifier::CONNECTION_4G: | 29 case net::NetworkChangeNotifier::CONNECTION_4G: |
30 return rtc::ADAPTER_TYPE_CELLULAR; | 30 return rtc::ADAPTER_TYPE_CELLULAR; |
31 default: | 31 default: |
32 return rtc::ADAPTER_TYPE_UNKNOWN; | 32 return rtc::ADAPTER_TYPE_UNKNOWN; |
33 } | 33 } |
34 } | 34 } |
35 | 35 |
36 } // namespace | 36 } // namespace |
37 | 37 |
38 IpcNetworkManager::IpcNetworkManager(P2PSocketDispatcher* socket_dispatcher) | 38 IpcNetworkManager::IpcNetworkManager(NetworkListManager* network_list_manager) |
39 : socket_dispatcher_(socket_dispatcher), | 39 : network_list_manager_(network_list_manager), |
40 start_count_(0), | 40 start_count_(0), |
41 network_list_received_(false), | 41 network_list_received_(false), |
42 weak_factory_(this) { | 42 weak_factory_(this) { |
43 socket_dispatcher_->AddNetworkListObserver(this); | 43 network_list_manager_->AddNetworkListObserver(this); |
44 } | 44 } |
45 | 45 |
46 IpcNetworkManager::~IpcNetworkManager() { | 46 IpcNetworkManager::~IpcNetworkManager() { |
47 DCHECK(!start_count_); | 47 DCHECK(!start_count_); |
48 socket_dispatcher_->RemoveNetworkListObserver(this); | 48 network_list_manager_->RemoveNetworkListObserver(this); |
49 } | 49 } |
50 | 50 |
51 void IpcNetworkManager::StartUpdating() { | 51 void IpcNetworkManager::StartUpdating() { |
52 if (network_list_received_) { | 52 if (network_list_received_) { |
53 // Post a task to avoid reentrancy. | 53 // Post a task to avoid reentrancy. |
54 base::MessageLoop::current()->PostTask( | 54 base::MessageLoop::current()->PostTask( |
55 FROM_HERE, | 55 FROM_HERE, |
56 base::Bind(&IpcNetworkManager::SendNetworksChangedSignal, | 56 base::Bind(&IpcNetworkManager::SendNetworksChangedSignal, |
57 weak_factory_.GetWeakPtr())); | 57 weak_factory_.GetWeakPtr())); |
58 } | 58 } |
59 ++start_count_; | 59 ++start_count_; |
60 } | 60 } |
61 | 61 |
62 void IpcNetworkManager::StopUpdating() { | 62 void IpcNetworkManager::StopUpdating() { |
63 DCHECK_GT(start_count_, 0); | 63 DCHECK_GT(start_count_, 0); |
64 --start_count_; | 64 --start_count_; |
65 } | 65 } |
66 | 66 |
67 void IpcNetworkManager::OnNetworkListChanged( | 67 void IpcNetworkManager::OnNetworkListChanged( |
68 const net::NetworkInterfaceList& list) { | 68 const net::NetworkInterfaceList& list) { |
69 | 69 |
70 // Update flag if network list received for the first time. | 70 // Update flag if network list received for the first time. |
71 if (!network_list_received_) | 71 if (!network_list_received_) |
72 network_list_received_ = true; | 72 network_list_received_ = true; |
73 | 73 |
74 // Note: 32 and 64 are the arbitrary(kind of) prefix length used to | |
75 // differentiate IPv4 and IPv6 addresses. | |
76 // rtc::Network uses these prefix_length to compare network | 74 // rtc::Network uses these prefix_length to compare network |
77 // interfaces discovered. | 75 // interfaces discovered. |
78 std::vector<rtc::Network*> networks; | 76 std::vector<rtc::Network*> networks; |
79 int ipv4_interfaces = 0; | 77 int ipv4_interfaces = 0; |
80 int ipv6_interfaces = 0; | 78 int ipv6_interfaces = 0; |
81 for (net::NetworkInterfaceList::const_iterator it = list.begin(); | 79 for (net::NetworkInterfaceList::const_iterator it = list.begin(); |
82 it != list.end(); it++) { | 80 it != list.end(); it++) { |
83 if (it->address.size() == net::kIPv4AddressSize) { | 81 if (it->address.size() == net::kIPv4AddressSize) { |
84 uint32 address; | 82 uint32 address; |
85 memcpy(&address, &it->address[0], sizeof(uint32)); | 83 memcpy(&address, &it->address[0], sizeof(uint32)); |
86 address = rtc::NetworkToHost32(address); | 84 address = rtc::NetworkToHost32(address); |
87 rtc::Network* network = new rtc::Network( | 85 rtc::IPAddress prefix = |
88 it->name, it->name, rtc::IPAddress(address), 32, | 86 rtc::TruncateIP(rtc::IPAddress(address), it->network_prefix); |
89 ConvertConnectionTypeToAdapterType(it->type)); | 87 rtc::Network* network = |
| 88 new rtc::Network(it->name, |
| 89 it->name, |
| 90 prefix, |
| 91 it->network_prefix, |
| 92 ConvertConnectionTypeToAdapterType(it->type)); |
90 network->AddIP(rtc::IPAddress(address)); | 93 network->AddIP(rtc::IPAddress(address)); |
91 networks.push_back(network); | 94 networks.push_back(network); |
92 ++ipv4_interfaces; | 95 ++ipv4_interfaces; |
93 } else if (it->address.size() == net::kIPv6AddressSize) { | 96 } else if (it->address.size() == net::kIPv6AddressSize) { |
94 in6_addr address; | 97 in6_addr address; |
95 memcpy(&address, &it->address[0], sizeof(in6_addr)); | 98 memcpy(&address, &it->address[0], sizeof(in6_addr)); |
96 rtc::IPAddress ip6_addr(address); | 99 rtc::IPAddress ip6_addr(address); |
97 if (!rtc::IPIsPrivate(ip6_addr)) { | 100 if (!rtc::IPIsPrivate(ip6_addr)) { |
98 rtc::Network* network = new rtc::Network( | 101 rtc::IPAddress prefix = |
99 it->name, it->name, ip6_addr, 64, | 102 rtc::TruncateIP(rtc::IPAddress(ip6_addr), it->network_prefix); |
100 ConvertConnectionTypeToAdapterType(it->type)); | 103 rtc::Network* network = |
| 104 new rtc::Network(it->name, |
| 105 it->name, |
| 106 prefix, |
| 107 it->network_prefix, |
| 108 ConvertConnectionTypeToAdapterType(it->type)); |
101 network->AddIP(ip6_addr); | 109 network->AddIP(ip6_addr); |
102 networks.push_back(network); | 110 networks.push_back(network); |
103 ++ipv6_interfaces; | 111 ++ipv6_interfaces; |
104 } | 112 } |
105 } | 113 } |
106 } | 114 } |
107 | 115 |
108 | 116 |
109 // Send interface counts to UMA. | 117 // Send interface counts to UMA. |
110 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces", | 118 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces", |
(...skipping 22 matching lines...) Expand all Loading... |
133 MergeNetworkList(networks, &changed); | 141 MergeNetworkList(networks, &changed); |
134 if (changed) | 142 if (changed) |
135 SignalNetworksChanged(); | 143 SignalNetworksChanged(); |
136 } | 144 } |
137 | 145 |
138 void IpcNetworkManager::SendNetworksChangedSignal() { | 146 void IpcNetworkManager::SendNetworksChangedSignal() { |
139 SignalNetworksChanged(); | 147 SignalNetworksChanged(); |
140 } | 148 } |
141 | 149 |
142 } // namespace content | 150 } // namespace content |
OLD | NEW |