Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ppapi / tests / extensions / socket_permissions / test_socket_permissions.cc
blobe01a8d683023f9f8143516481863585307348191
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ppapi/cpp/completion_callback.h"
6 #include "ppapi/cpp/instance.h"
7 #include "ppapi/cpp/module.h"
8 #include "ppapi/cpp/tcp_socket.h"
9 #include "ppapi/cpp/udp_socket.h"
10 #include "ppapi/utility/completion_callback_factory.h"
12 namespace {
14 class MyInstance : public pp::Instance {
15 public:
16 explicit MyInstance(PP_Instance instance)
17 : pp::Instance(instance),
18 tcp_socket_(this),
19 udp_socket_(this),
20 udp_multicast_socket_(this),
21 factory_(this) {}
22 virtual ~MyInstance() { }
24 void DidBindTCPSocket(int32_t result) {
25 // We should be able to bind the TCP socket with permission.
26 if (result != PP_OK) {
27 PostMessage(result);
28 return;
30 PP_NetAddress_IPv4 ipv4_address = {80, {127, 0, 0, 1}};
31 pp::NetAddress address(this, ipv4_address);
32 udp_socket_.Bind(address,
33 factory_.NewCallback(&MyInstance::DidBindUDPSocket));
36 void DidBindUDPSocket(int32_t result) {
37 // We should be able to bind the UDP socket with permission.
38 if (result != PP_OK) {
39 PostMessage(result);
40 return;
42 // Close the socket by releasing it.
43 udp_socket_ = pp::UDPSocket();
44 udp_multicast_socket_.SetOption(
45 PP_UDPSOCKET_OPTION_MULTICAST_LOOP, pp::Var(true),
46 factory_.NewCallback(&MyInstance::DidSetOptionMulticastUDPSocket));
49 void DidSetOptionMulticastUDPSocket(int32_t result) {
50 // We should be able to set the multicast option, even without permission.
51 if (result != PP_OK) {
52 PostMessage(result);
53 return;
55 PP_NetAddress_IPv4 ipv4_address = {80, {127, 0, 0, 1}};
56 pp::NetAddress address(this, ipv4_address);
57 udp_multicast_socket_.Bind(
58 address, factory_.NewCallback(&MyInstance::DidBindMulticastUDPSocket));
61 void DidBindMulticastUDPSocket(int32_t result) {
62 // We should NOT be able to do UDP multicast without permission.
63 if (result == PP_OK)
64 PostMessage("FAIL");
65 else
66 PostMessage("PASS");
69 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
70 PP_NetAddress_IPv4 ipv4_address = {80, {127, 0, 0, 1} };
71 pp::NetAddress address(this, ipv4_address);
72 tcp_socket_.Bind(address,
73 factory_.NewCallback(&MyInstance::DidBindTCPSocket));
74 return true;
77 private:
78 pp::TCPSocket tcp_socket_;
79 pp::UDPSocket udp_socket_;
80 pp::UDPSocket udp_multicast_socket_;
81 pp::CompletionCallbackFactory<MyInstance> factory_;
84 class MyModule : public pp::Module {
85 public:
86 MyModule() : pp::Module() { }
87 virtual ~MyModule() { }
89 virtual pp::Instance* CreateInstance(PP_Instance instance) {
90 return new MyInstance(instance);
94 } // namespace
96 namespace pp {
98 Module* CreateModule() {
99 return new MyModule();
102 } // namespace pp