17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file.
47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ppapi/proxy/udp_socket_resource.h"
67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
77d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ppapi/shared_impl/tracked_callback.h"
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ppapi/thunk/enter.h"
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ppapi/thunk/ppb_net_address_api.h"
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ppapi/thunk/resource_creation_api.h"
117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace ppapi {
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace proxy {
147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace {
167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)typedef thunk::EnterResourceNoLock<thunk::PPB_NetAddress_API>
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    EnterNetAddressNoLock;
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)UDPSocketResource::UDPSocketResource(Connection connection,
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                     PP_Instance instance)
24eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    : UDPSocketResourceBase(connection, instance, false) {
257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)UDPSocketResource::~UDPSocketResource() {
287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)thunk::PPB_UDPSocket_API* UDPSocketResource::AsPPB_UDPSocket_API() {
317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return this;
327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)int32_t UDPSocketResource::Bind(PP_Resource addr,
357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                scoped_refptr<TrackedCallback> callback) {
367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EnterNetAddressNoLock enter(addr, true);
377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (enter.failed())
387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return PP_ERROR_BADARGUMENT;
397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return BindImpl(&enter.object()->GetNetAddressPrivate(), callback);
417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)PP_Resource UDPSocketResource::GetBoundAddress() {
447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  PP_NetAddress_Private addr_private;
457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!GetBoundAddressImpl(&addr_private))
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return 0;
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  thunk::EnterResourceCreationNoLock enter(pp_instance());
497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (enter.failed())
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return 0;
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return enter.functions()->CreateNetAddressFromNetAddressPrivate(
527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      pp_instance(), addr_private);
537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)int32_t UDPSocketResource::RecvFrom(char* buffer,
567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                    int32_t num_bytes,
577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                    PP_Resource* addr,
587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                    scoped_refptr<TrackedCallback> callback) {
597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return RecvFromImpl(buffer, num_bytes, addr, callback);
607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)int32_t UDPSocketResource::SendTo(const char* buffer,
637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                  int32_t num_bytes,
647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                  PP_Resource addr,
657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                  scoped_refptr<TrackedCallback> callback) {
667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  EnterNetAddressNoLock enter(addr, true);
677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (enter.failed())
687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return PP_ERROR_BADARGUMENT;
697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return SendToImpl(buffer, num_bytes, &enter.object()->GetNetAddressPrivate(),
717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                    callback);
727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void UDPSocketResource::Close() {
757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  CloseImpl();
767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)int32_t UDPSocketResource::SetOption(
79eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    PP_UDPSocket_Option name,
807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    const PP_Var& value,
81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    scoped_refptr<TrackedCallback> callback) {
82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  return SetOptionImpl(name, value, callback);
837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace proxy
867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace ppapi
87