1// Copyright 2013 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.
4
5#ifndef CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_TCP_SERVER_SOCKET_MESSAGE_FILTER_H_
6#define CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_TCP_SERVER_SOCKET_MESSAGE_FILTER_H_
7
8#include "base/basictypes.h"
9#include "base/compiler_specific.h"
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
12#include "content/common/content_export.h"
13#include "net/base/ip_endpoint.h"
14#include "net/socket/tcp_socket.h"
15#include "ppapi/c/pp_instance.h"
16#include "ppapi/host/resource_message_filter.h"
17
18struct PP_NetAddress_Private;
19
20namespace ppapi {
21namespace host {
22class PpapiHost;
23}
24}
25
26namespace content {
27
28class BrowserPpapiHostImpl;
29class ContentBrowserPepperHostFactory;
30
31// TODO(yzshen): Remove this class entirely and let
32// TCPServerSocketPrivateResource inherit TCPSocketResourceBase.
33class CONTENT_EXPORT PepperTCPServerSocketMessageFilter
34    : public ppapi::host::ResourceMessageFilter {
35 public:
36  PepperTCPServerSocketMessageFilter(ContentBrowserPepperHostFactory* factory,
37                                     BrowserPpapiHostImpl* host,
38                                     PP_Instance instance,
39                                     bool private_api);
40
41  static size_t GetNumInstances();
42
43 protected:
44  virtual ~PepperTCPServerSocketMessageFilter();
45
46 private:
47  enum State {
48    STATE_BEFORE_LISTENING,
49    STATE_LISTEN_IN_PROGRESS,
50    STATE_LISTENING,
51    STATE_ACCEPT_IN_PROGRESS,
52    STATE_CLOSED
53  };
54
55  // ppapi::host::ResourceMessageFilter overrides.
56  virtual scoped_refptr<base::TaskRunner> OverrideTaskRunnerForMessage(
57      const IPC::Message& message) OVERRIDE;
58  virtual int32_t OnResourceMessageReceived(
59      const IPC::Message& msg,
60      ppapi::host::HostMessageContext* context) OVERRIDE;
61
62  int32_t OnMsgListen(const ppapi::host::HostMessageContext* context,
63                      const PP_NetAddress_Private& addr,
64                      int32_t backlog);
65  int32_t OnMsgAccept(const ppapi::host::HostMessageContext* context);
66  int32_t OnMsgStopListening(const ppapi::host::HostMessageContext* context);
67
68  void DoListen(const ppapi::host::ReplyMessageContext& context,
69                const PP_NetAddress_Private& addr,
70                int32_t backlog);
71
72  void OnListenCompleted(const ppapi::host::ReplyMessageContext& context,
73                         int net_result);
74  void OnAcceptCompleted(const ppapi::host::ReplyMessageContext& context,
75                         int net_result);
76
77  void SendListenReply(const ppapi::host::ReplyMessageContext& context,
78                       int32_t pp_result,
79                       const PP_NetAddress_Private& local_addr);
80  void SendListenError(const ppapi::host::ReplyMessageContext& context,
81                       int32_t pp_result);
82  void SendAcceptReply(const ppapi::host::ReplyMessageContext& context,
83                       int32_t pp_result,
84                       int pending_resource_id,
85                       const PP_NetAddress_Private& local_addr,
86                       const PP_NetAddress_Private& remote_addr);
87  void SendAcceptError(const ppapi::host::ReplyMessageContext& context,
88                       int32_t pp_result);
89
90  // Following fields are initialized and used only on the IO thread.
91  // Non-owning ptr.
92  ppapi::host::PpapiHost* ppapi_host_;
93  // Non-owning ptr.
94  ContentBrowserPepperHostFactory* factory_;
95  PP_Instance instance_;
96
97  State state_;
98  scoped_ptr<net::TCPSocket> socket_;
99  scoped_ptr<net::TCPSocket> accepted_socket_;
100  net::IPEndPoint accepted_address_;
101
102  // Following fields are initialized on the IO thread but used only
103  // on the UI thread.
104  const bool external_plugin_;
105  const bool private_api_;
106  int render_process_id_;
107  int render_frame_id_;
108
109  DISALLOW_COPY_AND_ASSIGN(PepperTCPServerSocketMessageFilter);
110};
111
112}  // namespace content
113
114#endif  // CONTENT_BROWSER_RENDERER_HOST_PEPPER_PEPPER_TCP_SERVER_SOCKET_MESSAGE_FILTER_H_
115