summaryrefslogtreecommitdiffstats
path: root/remoting/client/plugin/pepper_network_manager.cc
blob: 1eaabc02907786a9347580da0029a28d9b1f4298 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/client/plugin/pepper_network_manager.h"

#include "base/bind.h"
#include "base/location.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/private/network_list_private.h"
#include "ppapi/cpp/private/net_address_private.h"

namespace remoting {

PepperNetworkManager::PepperNetworkManager(const pp::InstanceHandle& instance)
    : monitor_(instance, &PepperNetworkManager::OnNetworkListCallbackHandler,
               this),
      start_count_(0),
      network_list_received_(false),
      ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
}

PepperNetworkManager::~PepperNetworkManager() {
  DCHECK(!start_count_);
}

void PepperNetworkManager::StartUpdating() {
  if (network_list_received_) {
    // Post a task to avoid reentrancy.
    base::ThreadTaskRunnerHandle::Get()->PostTask(
        FROM_HERE, base::Bind(&PepperNetworkManager::SendNetworksChangedSignal,
                              weak_factory_.GetWeakPtr()));
  }
  ++start_count_;
}

void PepperNetworkManager::StopUpdating() {
  DCHECK_GT(start_count_, 0);
  --start_count_;
}

// static
void PepperNetworkManager::OnNetworkListCallbackHandler(
    void* user_data,
    PP_Resource list_resource) {
  PepperNetworkManager* object = static_cast<PepperNetworkManager*>(user_data);
  pp::NetworkListPrivate list(pp::PASS_REF, list_resource);
  object->OnNetworkList(list);
}

void PepperNetworkManager::OnNetworkList(const pp::NetworkListPrivate& list) {
  network_list_received_ = true;

  std::vector<talk_base::Network*> networks;
  size_t count = list.GetCount();
  for (size_t i = 0; i < count; i++) {
    std::vector<PP_NetAddress_Private> addresses;
    list.GetIpAddresses(i, &addresses);

    if (addresses.size() == 0)
      continue;

    char address_bytes[sizeof(in6_addr)];
    if (!pp::NetAddressPrivate::GetAddress(
            addresses[0], &address_bytes, sizeof(address_bytes))) {
      LOG(ERROR) << "Failed to get address for network interface.";
      continue;
    }

    int prefix_length;

    // TODO(sergeyu): Copy all addresses, not only the first one.
    talk_base::IPAddress address;
    switch (pp::NetAddressPrivate::GetFamily(addresses[0])) {
      case PP_NETADDRESSFAMILY_IPV4: {
        in_addr* address_ipv4 = reinterpret_cast<in_addr*>(address_bytes);
        address = talk_base::IPAddress(*address_ipv4);
        prefix_length = sizeof(in_addr) * 8;
        break;
      }

      case PP_NETADDRESSFAMILY_IPV6: {
        in6_addr* address_ipv6 = reinterpret_cast<in6_addr*>(address_bytes);
        address = talk_base::IPAddress(*address_ipv6);
        prefix_length = sizeof(in6_addr) * 8;
        break;
      }

      default:
        LOG(WARNING) << "Skipping address with unknown family: "
                     << pp::NetAddressPrivate::GetFamily(addresses[0]);
        continue;
    }

    talk_base::Network* network = new talk_base::Network(
        list.GetName(i), list.GetDisplayName(i), address, prefix_length);
    network->AddIP(address);
    networks.push_back(network);
  }

  bool changed = false;
  MergeNetworkList(networks, &changed);
  if (changed)
    SignalNetworksChanged();
}

void PepperNetworkManager::SendNetworksChangedSignal() {
  SignalNetworksChanged();
}

}  // namespace remoting