summaryrefslogtreecommitdiffstats
path: root/ppapi/cpp/private/network_list_private.cc
blob: 35bb66aad63187bd8aff533da0d4b1463f437136 (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
// 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 "ppapi/cpp/private/network_list_private.h"

#include "ppapi/cpp/module_impl.h"
#include "ppapi/cpp/var.h"

namespace pp {

namespace {

template <> const char* interface_name<PPB_NetworkList_Private>() {
  return PPB_NETWORKLIST_PRIVATE_INTERFACE;
}

}  // namespace

NetworkListPrivate::NetworkListPrivate() {
}

NetworkListPrivate::NetworkListPrivate(PassRef, PP_Resource resource)
    : Resource(PASS_REF, resource) {
}

// static
bool NetworkListPrivate::IsAvailable() {
  return has_interface<PPB_NetworkList_Private>();
}

uint32_t NetworkListPrivate::GetCount() const {
  if (!has_interface<PPB_NetworkList_Private>())
    return 0;
  return get_interface<PPB_NetworkList_Private>()->GetCount(pp_resource());
}

std::string NetworkListPrivate::GetName(uint32_t index) const {
  if (!has_interface<PPB_NetworkList_Private>())
    return std::string();
  Var result(PASS_REF,
             get_interface<PPB_NetworkList_Private>()->GetName(
                 pp_resource(), index));
  return result.is_string() ? result.AsString() : std::string();
}

PP_NetworkListType_Private NetworkListPrivate::GetType(uint32_t index) const {
  if (!has_interface<PPB_NetworkList_Private>())
    return PP_NETWORKLIST_ETHERNET;
  return get_interface<PPB_NetworkList_Private>()->GetType(
      pp_resource(), index);
}

PP_NetworkListState_Private NetworkListPrivate::GetState(uint32_t index) const {
  if (!has_interface<PPB_NetworkList_Private>())
    return PP_NETWORKLIST_DOWN;
  return get_interface<PPB_NetworkList_Private>()->GetState(
      pp_resource(), index);
}

void NetworkListPrivate::GetIpAddresses(
    uint32_t index,
    std::vector<PP_NetAddress_Private>* addresses) const {
  if (!has_interface<PPB_NetworkList_Private>())
    return;

  // Most netword interfaces don't have more than 3 network
  // interfaces.
  addresses->resize(3);

  int32_t result = get_interface<PPB_NetworkList_Private>()->GetIpAddresses(
      pp_resource(), index, &addresses->front(), addresses->size());

  if (result < 0) {
    addresses->resize(0);
    return;
  }

  if (result <= static_cast<int32_t>(addresses->size())) {
    addresses->resize(result);
    return;
  }

  addresses->resize(result);
  result = get_interface<PPB_NetworkList_Private>()->GetIpAddresses(
      pp_resource(), index, &addresses->front(), addresses->size());
  if (result < 0) {
    addresses->resize(0);
  } else if (result < static_cast<int32_t>(addresses->size())) {
    addresses->resize(result);
  }
}

std::string NetworkListPrivate::GetDisplayName(uint32_t index) const {
  if (!has_interface<PPB_NetworkList_Private>())
    return std::string();
  Var result(PASS_REF,
             get_interface<PPB_NetworkList_Private>()->GetDisplayName(
                 pp_resource(), index));
  return result.is_string() ? result.AsString() : std::string();
}

uint32_t NetworkListPrivate::GetMTU(uint32_t index) const {
  if (!has_interface<PPB_NetworkList_Private>())
    return 0;
  return get_interface<PPB_NetworkList_Private>()->GetMTU(
      pp_resource(), index);
}

}  // namespace pp