OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "remoting/protocol/pepper_network_manager.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/message_loop.h" | |
9 #include "ppapi/cpp/module.h" | |
10 #include "ppapi/cpp/private/network_list_private.h" | |
11 #include "ppapi/cpp/private/net_address_private.h" | |
12 | |
13 namespace remoting { | |
14 namespace protocol { | |
15 | |
16 PepperNetworkManager::PepperNetworkManager(const pp::InstanceHandle& instance) | |
17 : monitor_(instance, &PepperNetworkManager::OnNetworkListCallbackHandler, | |
18 this), | |
19 start_count_(0), | |
20 network_list_received_(false), | |
21 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | |
22 } | |
23 | |
24 PepperNetworkManager::~PepperNetworkManager() { | |
25 DCHECK(!start_count_); | |
26 } | |
27 | |
28 void PepperNetworkManager::StartUpdating() { | |
29 if (network_list_received_) { | |
30 // Post a task to avoid reentrancy. | |
31 MessageLoop::current()->PostTask( | |
32 FROM_HERE, base::Bind(&PepperNetworkManager::SendNetworksChangedSignal, | |
33 weak_factory_.GetWeakPtr())); | |
34 } | |
35 ++start_count_; | |
36 } | |
37 | |
38 void PepperNetworkManager::StopUpdating() { | |
39 DCHECK_GT(start_count_, 0); | |
40 --start_count_; | |
41 } | |
42 | |
43 // static | |
44 void PepperNetworkManager::OnNetworkListCallbackHandler( | |
45 void* user_data, | |
46 PP_Resource list_resource) { | |
47 PepperNetworkManager* object = static_cast<PepperNetworkManager*>(user_data); | |
48 pp::NetworkListPrivate list(list_resource); | |
49 object->OnNetworkList(list); | |
50 pp::Module::Get()->core()->ReleaseResource(list_resource); | |
51 } | |
52 | |
53 void PepperNetworkManager::OnNetworkList(const pp::NetworkListPrivate& list) { | |
54 network_list_received_ = true; | |
55 | |
56 std::vector<talk_base::Network*> networks; | |
57 size_t count = list.GetCount(); | |
58 for (size_t i = 0; i < count; i++) { | |
59 std::vector<PP_NetAddress_Private> addresses; | |
60 list.GetIpAddresses(i, &addresses); | |
61 | |
62 if (addresses.size() == 0) | |
63 continue; | |
64 | |
65 char address_bytes[sizeof(in6_addr)]; | |
66 if (!pp::NetAddressPrivate::GetAddress( | |
67 addresses[0], &address_bytes, sizeof(address_bytes))) { | |
68 LOG(ERROR) << "Failed to get address for network interface."; | |
69 continue; | |
70 } | |
71 | |
72 int prefix_length; | |
73 | |
74 // TODO(sergeyu): Copy all addresses, not only the first one. | |
75 talk_base::IPAddress address; | |
76 switch (pp::NetAddressPrivate::GetFamily(addresses[0])) { | |
77 case PP_NETADDRESSFAMILY_IPV4: { | |
78 in_addr* address_ipv4 = reinterpret_cast<in_addr*>(address_bytes); | |
79 address = talk_base::IPAddress(*address_ipv4); | |
80 prefix_length = sizeof(in_addr) * 8; | |
81 break; | |
82 } | |
83 | |
84 case PP_NETADDRESSFAMILY_IPV6: { | |
85 in6_addr* address_ipv6 = reinterpret_cast<in6_addr*>(address_bytes); | |
86 address = talk_base::IPAddress(*address_ipv6); | |
87 prefix_length = sizeof(in6_addr) * 8; | |
88 break; | |
89 } | |
90 | |
91 default: | |
92 LOG(WARNING) << "Skipping address with unknown family: " | |
93 << pp::NetAddressPrivate::GetFamily(addresses[0]); | |
94 continue; | |
95 } | |
96 | |
97 talk_base::Network* network = new talk_base::Network( | |
98 list.GetName(i), list.GetDisplayName(i), address, prefix_length); | |
99 network->AddIP(address); | |
100 networks.push_back(network); | |
101 } | |
102 | |
103 bool changed = false; | |
104 MergeNetworkList(networks, &changed); | |
105 if (changed) | |
106 SignalNetworksChanged(); | |
107 } | |
108 | |
109 void PepperNetworkManager::SendNetworksChangedSignal() { | |
110 SignalNetworksChanged(); | |
111 } | |
112 | |
113 } // namespace protocol | |
114 } // namespace remoting | |
OLD | NEW |