summaryrefslogtreecommitdiffstats
path: root/content/child/websocket_bridge.cc
blob: 2af6e8ece84e35f3451851b1b67aeb1e6e92be8d (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Copyright 2013 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 "content/child/websocket_bridge.h"

#include <stdint.h>
#include <string>
#include <utility>
#include <vector>

#include "base/logging.h"
#include "base/strings/string_util.h"
#include "content/child/child_thread_impl.h"
#include "content/child/websocket_dispatcher.h"
#include "content/common/websocket.h"
#include "content/common/websocket_messages.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_message_macros.h"
#include "third_party/WebKit/public/platform/WebSerializedOrigin.h"
#include "third_party/WebKit/public/platform/WebSocketHandle.h"
#include "third_party/WebKit/public/platform/WebSocketHandleClient.h"
#include "third_party/WebKit/public/platform/WebSocketHandshakeRequestInfo.h"
#include "third_party/WebKit/public/platform/WebSocketHandshakeResponseInfo.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"
#include "url/gurl.h"
#include "url/origin.h"

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

namespace content {

namespace {

const unsigned short kAbnormalShutdownOpCode = 1006;

}  // namespace

WebSocketBridge::WebSocketBridge()
    : channel_id_(kInvalidChannelId),
      render_frame_id_(MSG_ROUTING_NONE),
      client_(NULL) {}

WebSocketBridge::~WebSocketBridge() {
  if (channel_id_ != kInvalidChannelId) {
    // The connection is abruptly disconnected by the renderer without
    // closing handshake.
    ChildThreadImpl::current()->Send(
        new WebSocketMsg_DropChannel(channel_id_,
                                     false,
                                     kAbnormalShutdownOpCode,
                                     std::string()));
  }
  Disconnect();
}

bool WebSocketBridge::OnMessageReceived(const IPC::Message& msg) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(WebSocketBridge, msg)
    IPC_MESSAGE_HANDLER(WebSocketMsg_AddChannelResponse, DidConnect)
    IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyStartOpeningHandshake,
                        DidStartOpeningHandshake)
    IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyFinishOpeningHandshake,
                        DidFinishOpeningHandshake)
    IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyFailure, DidFail)
    IPC_MESSAGE_HANDLER(WebSocketMsg_SendFrame, DidReceiveData)
    IPC_MESSAGE_HANDLER(WebSocketMsg_FlowControl, DidReceiveFlowControl)
    IPC_MESSAGE_HANDLER(WebSocketMsg_DropChannel, DidClose)
    IPC_MESSAGE_HANDLER(WebSocketMsg_NotifyClosing,
                        DidStartClosingHandshake)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

void WebSocketBridge::DidConnect(const std::string& selected_protocol,
                                 const std::string& extensions) {
  WebSocketHandleClient* client = client_;
  DVLOG(1) << "WebSocketBridge::DidConnect("
           << selected_protocol << ", "
           << extensions << ")";
  if (!client)
    return;

  WebString protocol_to_pass = WebString::fromUTF8(selected_protocol);
  WebString extensions_to_pass = WebString::fromUTF8(extensions);
  client->didConnect(this, protocol_to_pass, extensions_to_pass);
  // |this| can be deleted here.
}

void WebSocketBridge::DidStartOpeningHandshake(
    const WebSocketHandshakeRequest& request) {
  DVLOG(1) << "WebSocketBridge::DidStartOpeningHandshake("
           << request.url << ")";
  // All strings are already encoded to ASCII in the browser.
  blink::WebSocketHandshakeRequestInfo request_to_pass;
  request_to_pass.setURL(WebURL(request.url));
  for (size_t i = 0; i < request.headers.size(); ++i) {
    const std::pair<std::string, std::string>& header = request.headers[i];
    request_to_pass.addHeaderField(WebString::fromLatin1(header.first),
                                   WebString::fromLatin1(header.second));
  }
  request_to_pass.setHeadersText(WebString::fromLatin1(request.headers_text));
  client_->didStartOpeningHandshake(this, request_to_pass);
}

void WebSocketBridge::DidFinishOpeningHandshake(
    const WebSocketHandshakeResponse& response) {
  DVLOG(1) << "WebSocketBridge::DidFinishOpeningHandshake("
           << response.url << ")";
  // All strings are already encoded to ASCII in the browser.
  blink::WebSocketHandshakeResponseInfo response_to_pass;
  response_to_pass.setStatusCode(response.status_code);
  response_to_pass.setStatusText(WebString::fromLatin1(response.status_text));
  for (size_t i = 0; i < response.headers.size(); ++i) {
    const std::pair<std::string, std::string>& header = response.headers[i];
    response_to_pass.addHeaderField(WebString::fromLatin1(header.first),
                                    WebString::fromLatin1(header.second));
  }
  response_to_pass.setHeadersText(WebString::fromLatin1(response.headers_text));
  client_->didFinishOpeningHandshake(this, response_to_pass);
}

void WebSocketBridge::DidFail(const std::string& message) {
  DVLOG(1) << "WebSocketBridge::DidFail(" << message << ")";
  WebSocketHandleClient* client = client_;
  Disconnect();
  if (!client)
    return;

  WebString message_to_pass = WebString::fromUTF8(message);
  client->didFail(this, message_to_pass);
  // |this| can be deleted here.
}

void WebSocketBridge::DidReceiveData(bool fin,
                                     WebSocketMessageType type,
                                     const std::vector<char>& data) {
  DVLOG(1) << "WebSocketBridge::DidReceiveData("
           << fin << ", "
           << type << ", "
           << "(data size = " << data.size() << "))";
  if (!client_)
    return;

  WebSocketHandle::MessageType type_to_pass =
      WebSocketHandle::MessageTypeContinuation;
  switch (type) {
    case WEB_SOCKET_MESSAGE_TYPE_CONTINUATION:
      type_to_pass = WebSocketHandle::MessageTypeContinuation;
      break;
    case WEB_SOCKET_MESSAGE_TYPE_TEXT:
      type_to_pass = WebSocketHandle::MessageTypeText;
      break;
    case WEB_SOCKET_MESSAGE_TYPE_BINARY:
      type_to_pass = WebSocketHandle::MessageTypeBinary;
      break;
  }
  const char* data_to_pass = data.empty() ? NULL : &data[0];
  client_->didReceiveData(this, fin, type_to_pass, data_to_pass, data.size());
  // |this| can be deleted here.
}

void WebSocketBridge::DidReceiveFlowControl(int64_t quota) {
  DVLOG(1) << "WebSocketBridge::DidReceiveFlowControl(" << quota << ")";
  if (!client_)
    return;

  client_->didReceiveFlowControl(this, quota);
  // |this| can be deleted here.
}

void WebSocketBridge::DidClose(bool was_clean,
                               unsigned short code,
                               const std::string& reason) {
  DVLOG(1) << "WebSocketBridge::DidClose("
           << was_clean << ", "
           << code << ", "
           << reason << ")";
  WebSocketHandleClient* client = client_;
  Disconnect();
  if (!client)
    return;

  WebString reason_to_pass = WebString::fromUTF8(reason);
  client->didClose(this, was_clean, code, reason_to_pass);
  // |this| can be deleted here.
}

void WebSocketBridge::DidStartClosingHandshake() {
  DVLOG(1) << "WebSocketBridge::DidStartClosingHandshake()";
  if (!client_)
    return;

  client_->didStartClosingHandshake(this);
  // |this| can be deleted here.
}

void WebSocketBridge::connect(
    const WebURL& url,
    const WebVector<WebString>& protocols,
    const WebSerializedOrigin& origin,
    WebSocketHandleClient* client) {
  DCHECK_EQ(kInvalidChannelId, channel_id_);
  WebSocketDispatcher* dispatcher =
      ChildThreadImpl::current()->websocket_dispatcher();
  channel_id_ = dispatcher->AddBridge(this);
  client_ = client;

  std::vector<std::string> protocols_to_pass;
  for (size_t i = 0; i < protocols.size(); ++i)
    protocols_to_pass.push_back(protocols[i].utf8());
  url::Origin origin_to_pass(origin);

  DVLOG(1) << "Bridge#" << channel_id_ << " Connect(" << url << ", ("
           << JoinString(protocols_to_pass, ", ") << "), "
           << origin_to_pass.string() << ")";

  ChildThreadImpl::current()->Send(new WebSocketHostMsg_AddChannelRequest(
      channel_id_, url, protocols_to_pass, origin_to_pass, render_frame_id_));
}

void WebSocketBridge::send(bool fin,
                           WebSocketHandle::MessageType type,
                           const char* data,
                           size_t size) {
  if (channel_id_ == kInvalidChannelId)
    return;

  WebSocketMessageType type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION;
  switch (type) {
    case WebSocketHandle::MessageTypeContinuation:
      type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION;
      break;
    case WebSocketHandle::MessageTypeText:
      type_to_pass = WEB_SOCKET_MESSAGE_TYPE_TEXT;
      break;
    case WebSocketHandle::MessageTypeBinary:
      type_to_pass = WEB_SOCKET_MESSAGE_TYPE_BINARY;
      break;
  }

  DVLOG(1) << "Bridge #" << channel_id_ << " Send("
           << fin << ", " << type_to_pass << ", "
           << "(data size = "  << size << "))";

  ChildThreadImpl::current()->Send(
      new WebSocketMsg_SendFrame(channel_id_,
                                 fin,
                                 type_to_pass,
                                 std::vector<char>(data, data + size)));
}

void WebSocketBridge::flowControl(int64_t quota) {
  if (channel_id_ == kInvalidChannelId)
    return;

  DVLOG(1) << "Bridge #" << channel_id_ << " FlowControl(" << quota << ")";

  ChildThreadImpl::current()->Send(
      new WebSocketMsg_FlowControl(channel_id_, quota));
}

void WebSocketBridge::close(unsigned short code,
                            const WebString& reason) {
  if (channel_id_ == kInvalidChannelId)
    return;

  std::string reason_to_pass = reason.utf8();
  DVLOG(1) << "Bridge #" << channel_id_ << " Close("
           << code << ", " << reason_to_pass << ")";
  // This method is for closing handshake and hence |was_clean| shall be true.
  ChildThreadImpl::current()->Send(
      new WebSocketMsg_DropChannel(channel_id_, true, code, reason_to_pass));
}

void WebSocketBridge::Disconnect() {
  if (channel_id_ == kInvalidChannelId)
    return;
  WebSocketDispatcher* dispatcher =
      ChildThreadImpl::current()->websocket_dispatcher();
  dispatcher->RemoveBridge(channel_id_);

  channel_id_ = kInvalidChannelId;
  client_ = NULL;
}

}  // namespace content