1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file.
4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/c/pp_errors.h"
6eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ppapi/c/ppb_net_address.h"
7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/shared_impl/tracked_callback.h"
8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/thunk/enter.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/thunk/ppb_instance_api.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/thunk/ppb_net_address_api.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/thunk/resource_creation_api.h"
12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "ppapi/thunk/thunk.h"
13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace ppapi {
15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace thunk {
16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace {
18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)PP_Resource CreateFromIPv4Address(
20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    PP_Instance instance,
21eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const struct PP_NetAddress_IPv4* ipv4_addr) {
22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  VLOG(4) << "PPB_NetAddress::CreateFromIPv4Address()";
23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EnterResourceCreation enter(instance);
24868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (enter.failed())
25868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return 0;
26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return enter.functions()->CreateNetAddressFromIPv4Address(instance,
27868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                            ipv4_addr);
28868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)PP_Resource CreateFromIPv6Address(
31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    PP_Instance instance,
32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const struct PP_NetAddress_IPv6* ipv6_addr) {
33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  VLOG(4) << "PPB_NetAddress::CreateFromIPv6Address()";
34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EnterResourceCreation enter(instance);
35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (enter.failed())
36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return 0;
37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return enter.functions()->CreateNetAddressFromIPv6Address(instance,
38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                                            ipv6_addr);
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochPP_Bool IsNetAddress(PP_Resource resource) {
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  VLOG(4) << "PPB_NetAddress::IsNetAddress()";
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  EnterResource<PPB_NetAddress_API> enter(resource, false);
44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return PP_FromBool(enter.succeeded());
45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochPP_NetAddress_Family GetFamily(PP_Resource addr) {
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  VLOG(4) << "PPB_NetAddress::GetFamily()";
49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EnterResource<PPB_NetAddress_API> enter(addr, true);
50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (enter.failed())
51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return PP_NETADDRESS_FAMILY_UNSPECIFIED;
52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return enter.object()->GetFamily();
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)struct PP_Var DescribeAsString(PP_Resource addr, PP_Bool include_port) {
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  VLOG(4) << "PPB_NetAddress::DescribeAsString()";
57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EnterResource<PPB_NetAddress_API> enter(addr, true);
58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (enter.failed())
59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return PP_MakeUndefined();
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return enter.object()->DescribeAsString(include_port);
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)PP_Bool DescribeAsIPv4Address(PP_Resource addr,
64eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                              struct PP_NetAddress_IPv4* ipv4_addr) {
65eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  VLOG(4) << "PPB_NetAddress::DescribeAsIPv4Address()";
66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EnterResource<PPB_NetAddress_API> enter(addr, true);
67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (enter.failed())
68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return PP_FALSE;
69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return enter.object()->DescribeAsIPv4Address(ipv4_addr);
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)PP_Bool DescribeAsIPv6Address(PP_Resource addr,
73eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                              struct PP_NetAddress_IPv6* ipv6_addr) {
74eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  VLOG(4) << "PPB_NetAddress::DescribeAsIPv6Address()";
75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  EnterResource<PPB_NetAddress_API> enter(addr, true);
76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (enter.failed())
77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return PP_FALSE;
78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  return enter.object()->DescribeAsIPv6Address(ipv6_addr);
79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochconst PPB_NetAddress_1_0 g_ppb_netaddress_thunk_1_0 = {
82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  &CreateFromIPv4Address,
83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  &CreateFromIPv6Address,
84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  &IsNetAddress,
85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  &GetFamily,
86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  &DescribeAsString,
87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  &DescribeAsIPv4Address,
88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  &DescribeAsIPv6Address
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)};
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
91868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
93eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochconst PPB_NetAddress_1_0* GetPPB_NetAddress_1_0_Thunk() {
94eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return &g_ppb_netaddress_thunk_1_0;
95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}
96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace thunk
98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)}  // namespace ppapi
99