summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/network_list_resource.cc
blob: f4ec7ed4059f539a1cd166e42161b02d9cd079d0 (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
// 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/proxy/network_list_resource.h"

#include <stddef.h>

#include <algorithm>

#include "base/logging.h"
#include "ppapi/c/pp_errors.h"
#include "ppapi/shared_impl/array_writer.h"
#include "ppapi/shared_impl/var.h"
#include "ppapi/thunk/enter.h"

namespace ppapi {
namespace proxy {

NetworkListResource::NetworkListResource(PP_Instance instance,
                                         const SerializedNetworkList& list)
    : Resource(OBJECT_IS_PROXY, instance),
      list_(list) {
}

NetworkListResource::~NetworkListResource() {}

thunk::PPB_NetworkList_API* NetworkListResource::AsPPB_NetworkList_API() {
  return this;
}

uint32_t NetworkListResource::GetCount() {
  return static_cast<uint32_t>(list_.size());
}

PP_Var NetworkListResource::GetName(uint32_t index) {
  if (index >= list_.size())
    return PP_MakeUndefined();
  return StringVar::StringToPPVar(list_.at(index).name);
}

PP_NetworkList_Type NetworkListResource::GetType(uint32_t index) {
  if (index >= list_.size())
    return PP_NETWORKLIST_TYPE_UNKNOWN;
  return list_.at(index).type;
}

PP_NetworkList_State NetworkListResource::GetState(uint32_t index) {
  if (index >= list_.size())
    return PP_NETWORKLIST_STATE_DOWN;
  return list_.at(index).state;
}

int32_t NetworkListResource::GetIpAddresses(uint32_t index,
                                            const PP_ArrayOutput& output) {
  ArrayWriter writer(output);
  if (index >= list_.size() || !writer.is_valid())
    return PP_ERROR_BADARGUMENT;

  thunk::EnterResourceCreationNoLock enter(pp_instance());
  if (enter.failed())
    return PP_ERROR_FAILED;

  const std::vector<PP_NetAddress_Private>& addresses =
      list_.at(index).addresses;
  std::vector<PP_Resource> addr_resources;
  for (size_t i = 0; i < addresses.size(); ++i) {
    addr_resources.push_back(
        enter.functions()->CreateNetAddressFromNetAddressPrivate(
            pp_instance(), addresses[i]));
  }
  if (!writer.StoreResourceVector(addr_resources))
    return PP_ERROR_FAILED;

  return PP_OK;
}

PP_Var NetworkListResource::GetDisplayName(uint32_t index) {
  if (index >= list_.size())
    return PP_MakeUndefined();
  return StringVar::StringToPPVar(list_.at(index).display_name);
}

uint32_t NetworkListResource::GetMTU(uint32_t index) {
  if (index >= list_.size())
    return 0;
  return list_.at(index).mtu;
}

}  // namespace proxy
}  // namespace thunk