summaryrefslogtreecommitdiffstats
path: root/components/html_viewer/web_socket_handle_impl.cc
blob: 155dad2fbf56ceabd8bf9860f7a4d5c5b797f094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/html_viewer/web_socket_handle_impl.h"

#include <vector>

#include "base/bind.h"
#include "base/macros.h"
#include "components/html_viewer/blink_basic_type_converters.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/services/network/public/cpp/web_socket_read_queue.h"
#include "mojo/services/network/public/cpp/web_socket_write_queue.h"
#include "mojo/services/network/public/interfaces/web_socket_factory.mojom.h"
#include "third_party/WebKit/public/platform/WebSecurityOrigin.h"
#include "third_party/WebKit/public/platform/WebSocketHandleClient.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/platform/WebURL.h"
#include "third_party/WebKit/public/platform/WebVector.h"

using blink::WebSecurityOrigin;
using blink::WebSocketHandle;
using blink::WebSocketHandleClient;
using blink::WebString;
using blink::WebURL;
using blink::WebVector;

using mojo::ConvertTo;
using mojo::String;
using mojo::WebSocket;
using mojo::WebSocketReadQueue;

namespace mojo {

template<>
struct TypeConverter<WebSocket::MessageType, WebSocketHandle::MessageType> {
  static WebSocket::MessageType Convert(WebSocketHandle::MessageType type) {
    DCHECK(type == WebSocketHandle::MessageTypeContinuation ||
           type == WebSocketHandle::MessageTypeText ||
           type == WebSocketHandle::MessageTypeBinary);
    typedef WebSocket::MessageType MessageType;
    static_assert(
        static_cast<MessageType>(WebSocketHandle::MessageTypeContinuation) ==
            WebSocket::MESSAGE_TYPE_CONTINUATION,
        "WebSocket and WebSocketHandle enum values must match");
    static_assert(static_cast<MessageType>(WebSocketHandle::MessageTypeText) ==
                      WebSocket::MESSAGE_TYPE_TEXT,
                  "WebSocket and WebSocketHandle enum values must match");
    static_assert(
        static_cast<MessageType>(WebSocketHandle::MessageTypeBinary) ==
            WebSocket::MESSAGE_TYPE_BINARY,
        "WebSocket and WebSocketHandle enum values must match");
    return static_cast<WebSocket::MessageType>(type);
  }
};

template<>
struct TypeConverter<WebSocketHandle::MessageType, WebSocket::MessageType> {
  static WebSocketHandle::MessageType Convert(WebSocket::MessageType type) {
    DCHECK(type == WebSocket::MESSAGE_TYPE_CONTINUATION ||
           type == WebSocket::MESSAGE_TYPE_TEXT ||
           type == WebSocket::MESSAGE_TYPE_BINARY);
    return static_cast<WebSocketHandle::MessageType>(type);
  }
};

}  // namespace mojo

namespace html_viewer {

// This class forms a bridge from the mojo WebSocketClient interface and the
// Blink WebSocketHandleClient interface.
class WebSocketClientImpl : public mojo::WebSocketClient {
 public:
  WebSocketClientImpl(WebSocketHandleImpl* handle,
                      blink::WebSocketHandleClient* client,
                      mojo::InterfaceRequest<mojo::WebSocketClient> request)
      : handle_(handle), client_(client), binding_(this, request.Pass()) {}
  ~WebSocketClientImpl() override {}

 private:
  // WebSocketClient methods:
  void DidConnect(const String& selected_subprotocol,
                  const String& extensions,
                  mojo::ScopedDataPipeConsumerHandle receive_stream) override {
    blink::WebSocketHandleClient* client = client_;
    WebSocketHandleImpl* handle = handle_;
    receive_stream_ = receive_stream.Pass();
    read_queue_.reset(new WebSocketReadQueue(receive_stream_.get()));
    client->didConnect(handle,
                       selected_subprotocol.To<WebString>(),
                       extensions.To<WebString>());
    // |handle| can be deleted here.
  }

  void DidReceiveData(bool fin,
                      WebSocket::MessageType type,
                      uint32_t num_bytes) override {
    read_queue_->Read(num_bytes,
                      base::Bind(&WebSocketClientImpl::DidReadFromReceiveStream,
                                 base::Unretained(this),
                                 fin, type, num_bytes));
  }

  void DidReceiveFlowControl(int64_t quota) override {
    client_->didReceiveFlowControl(handle_, quota);
    // |handle| can be deleted here.
  }

  void DidFail(const String& message) override {
    blink::WebSocketHandleClient* client = client_;
    WebSocketHandleImpl* handle = handle_;
    handle->Disconnect();  // deletes |this|
    client->didFail(handle, message.To<WebString>());
    // |handle| can be deleted here.
  }

  void DidClose(bool was_clean, uint16_t code, const String& reason) override {
    blink::WebSocketHandleClient* client = client_;
    WebSocketHandleImpl* handle = handle_;
    handle->Disconnect();  // deletes |this|
    client->didClose(handle, was_clean, code, reason.To<WebString>());
    // |handle| can be deleted here.
  }

  void DidReadFromReceiveStream(bool fin,
                                WebSocket::MessageType type,
                                uint32_t num_bytes,
                                const char* data) {
    client_->didReceiveData(handle_,
                            fin,
                            ConvertTo<WebSocketHandle::MessageType>(type),
                            data,
                            num_bytes);
    // |handle_| can be deleted here.
  }

  // |handle_| owns this object, so it is guaranteed to outlive us.
  WebSocketHandleImpl* handle_;
  blink::WebSocketHandleClient* client_;
  mojo::ScopedDataPipeConsumerHandle receive_stream_;
  scoped_ptr<WebSocketReadQueue> read_queue_;
  mojo::Binding<mojo::WebSocketClient> binding_;

  DISALLOW_COPY_AND_ASSIGN(WebSocketClientImpl);
};

WebSocketHandleImpl::WebSocketHandleImpl(mojo::WebSocketFactory* factory)
    : did_close_(false) {
  factory->CreateWebSocket(GetProxy(&web_socket_));
}

WebSocketHandleImpl::~WebSocketHandleImpl() {
  if (!did_close_) {
    // The connection is abruptly disconnected by the renderer without
    // closing handshake.
    web_socket_->Close(WebSocket::kAbnormalCloseCode, String());
  }
}

void WebSocketHandleImpl::connect(const WebURL& url,
                                  const WebVector<WebString>& protocols,
                                  const WebSecurityOrigin& origin,
                                  WebSocketHandleClient* client) {
  // TODO(mpcomplete): Is this the right ownership model? Or should mojo own
  // |client_|?
  mojo::WebSocketClientPtr client_ptr;
  mojo::MessagePipe pipe;
  client_ptr.Bind(
      mojo::InterfacePtrInfo<mojo::WebSocketClient>(pipe.handle0.Pass(), 0u));
  mojo::InterfaceRequest<mojo::WebSocketClient> request;
  request.Bind(pipe.handle1.Pass());
  client_.reset(new WebSocketClientImpl(this, client, request.Pass()));

  mojo::DataPipe data_pipe;
  send_stream_ = data_pipe.producer_handle.Pass();
  write_queue_.reset(new mojo::WebSocketWriteQueue(send_stream_.get()));
  web_socket_->Connect(url.string().utf8(),
                       mojo::Array<String>::From(protocols),
                       origin.toString().utf8(),
                       data_pipe.consumer_handle.Pass(), client_ptr.Pass());
}

void WebSocketHandleImpl::send(bool fin,
                               WebSocketHandle::MessageType type,
                               const char* data,
                               size_t size) {
  if (!client_)
    return;

  uint32_t size32 = static_cast<uint32_t>(size);
  write_queue_->Write(
      data, size32,
      base::Bind(&WebSocketHandleImpl::DidWriteToSendStream,
                 base::Unretained(this),
                 fin, type, size32));
}

void WebSocketHandleImpl::flowControl(int64_t quota) {
  if (!client_)
    return;

  web_socket_->FlowControl(quota);
}

void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) {
  web_socket_->Close(code, reason.utf8());
}

void WebSocketHandleImpl::DidWriteToSendStream(
    bool fin,
    WebSocketHandle::MessageType type,
    uint32_t num_bytes,
    const char* data) {
  web_socket_->Send(fin, ConvertTo<WebSocket::MessageType>(type), num_bytes);
}

void WebSocketHandleImpl::Disconnect() {
  did_close_ = true;
  client_.reset();
}

}  // namespace html_viewer