1// Copyright (c) 2012 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_RENDERER_PEPPER_PEPPER_WEBSOCKET_HOST_H_
6#define CONTENT_RENDERER_PEPPER_PEPPER_WEBSOCKET_HOST_H_
7
8#include <queue>
9
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
12#include "content/common/content_export.h"
13#include "ppapi/host/host_message_context.h"
14#include "ppapi/host/resource_host.h"
15#include "ppapi/proxy/resource_message_params.h"
16#include "third_party/WebKit/public/web/WebSocket.h"
17#include "third_party/WebKit/public/web/WebSocketClient.h"
18
19namespace ppapi {
20class StringVar;
21class Var;
22}  // namespace ppapi
23
24namespace content {
25
26class RendererPpapiHost;
27
28class CONTENT_EXPORT PepperWebSocketHost
29    : public ppapi::host::ResourceHost,
30      public NON_EXPORTED_BASE(::blink::WebSocketClient) {
31 public:
32  explicit PepperWebSocketHost(RendererPpapiHost* host,
33                               PP_Instance instance,
34                               PP_Resource resource);
35  virtual ~PepperWebSocketHost();
36
37  virtual int32_t OnResourceMessageReceived(
38      const IPC::Message& msg,
39      ppapi::host::HostMessageContext* context) OVERRIDE;
40
41  // WebSocketClient implementation.
42  virtual void didConnect();
43  virtual void didReceiveMessage(const blink::WebString& message);
44  virtual void didReceiveArrayBuffer(const blink::WebArrayBuffer& binaryData);
45  virtual void didReceiveMessageError();
46  virtual void didUpdateBufferedAmount(unsigned long buffered_amount);
47  virtual void didStartClosingHandshake();
48  virtual void didClose(unsigned long unhandled_buffered_amount,
49                        ClosingHandshakeCompletionStatus status,
50                        unsigned short code,
51                        const blink::WebString& reason);
52
53 private:
54  // IPC message handlers.
55  int32_t OnHostMsgConnect(ppapi::host::HostMessageContext* context,
56                           const std::string& url,
57                           const std::vector<std::string>& protocols);
58  int32_t OnHostMsgClose(ppapi::host::HostMessageContext* context,
59                         int32_t code,
60                         const std::string& reason);
61  int32_t OnHostMsgSendText(ppapi::host::HostMessageContext* context,
62                            const std::string& message);
63  int32_t OnHostMsgSendBinary(ppapi::host::HostMessageContext* context,
64                              const std::vector<uint8_t>& message);
65  int32_t OnHostMsgFail(ppapi::host::HostMessageContext* context,
66                        const std::string& message);
67
68  // Non-owning pointer.
69  RendererPpapiHost* renderer_ppapi_host_;
70
71  // IPC reply parameters.
72  ppapi::host::ReplyMessageContext connect_reply_;
73  ppapi::host::ReplyMessageContext close_reply_;
74
75  // The server URL to which this instance connects.
76  std::string url_;
77
78  // A flag to indicate if opening handshake is going on.
79  bool connecting_;
80
81  // A flag to indicate if client initiated closing handshake is performed.
82  bool initiating_close_;
83
84  // A flag to indicate if server initiated closing handshake is performed.
85  bool accepting_close_;
86
87  // Becomes true if any error is detected. Incoming data will be disposed
88  // if this variable is true.
89  bool error_was_received_;
90
91  // Keeps the WebKit side WebSocket object. This is used for calling WebKit
92  // side functions via WebKit API.
93  scoped_ptr<blink::WebSocket> websocket_;
94
95  DISALLOW_COPY_AND_ASSIGN(PepperWebSocketHost);
96};
97
98}  // namespace content
99
100#endif  // CONTENT_RENDERER_PEPPER_PEPPER_WEBSOCKET_HOST_H_
101