Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / content / renderer / p2p / ipc_network_manager.cc
blob2ddc397aef425a335f1c2b735e5887811d39b298
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 "base/bind.h"
8 #include "base/sys_byteorder.h"
9 #include "net/base/net_util.h"
11 namespace content {
13 IpcNetworkManager::IpcNetworkManager(P2PSocketDispatcher* socket_dispatcher)
14 : socket_dispatcher_(socket_dispatcher),
15 start_count_(0),
16 network_list_received_(false),
17 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
18 socket_dispatcher_->AddNetworkListObserver(this);
21 IpcNetworkManager::~IpcNetworkManager() {
22 DCHECK(!start_count_);
23 socket_dispatcher_->RemoveNetworkListObserver(this);
26 void IpcNetworkManager::StartUpdating() {
27 if (network_list_received_) {
28 // Post a task to avoid reentrancy.
29 MessageLoop::current()->PostTask(
30 FROM_HERE, base::Bind(&IpcNetworkManager::SendNetworksChangedSignal,
31 weak_factory_.GetWeakPtr()));
33 ++start_count_;
36 void IpcNetworkManager::StopUpdating() {
37 DCHECK_GT(start_count_, 0);
38 --start_count_;
41 void IpcNetworkManager::OnNetworkListChanged(
42 const net::NetworkInterfaceList& list) {
44 // Update flag if network list received for the first time.
45 if (!network_list_received_)
46 network_list_received_ = true;
48 std::vector<talk_base::Network*> networks;
49 for (net::NetworkInterfaceList::const_iterator it = list.begin();
50 it != list.end(); it++) {
51 uint32 address;
52 if (it->address.size() != net::kIPv4AddressSize)
53 continue;
54 memcpy(&address, &it->address[0], sizeof(uint32));
55 address = talk_base::NetworkToHost32(address);
56 talk_base::Network* network = new talk_base::Network(
57 it->name, it->name, talk_base::IPAddress(address), 32);
58 network->AddIP(talk_base::IPAddress(address));
59 networks.push_back(network);
62 bool changed = false;
63 MergeNetworkList(networks, &changed);
64 if (changed)
65 SignalNetworksChanged();
68 void IpcNetworkManager::SendNetworksChangedSignal() {
69 SignalNetworksChanged();
72 } // namespace content