• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "content/renderer/pepper/pepper_websocket_host.h"
6 
7 #include <string>
8 
9 #include "content/public/renderer/renderer_ppapi_host.h"
10 #include "net/base/net_util.h"
11 #include "ppapi/c/pp_errors.h"
12 #include "ppapi/c/ppb_websocket.h"
13 #include "ppapi/host/dispatch_host_message.h"
14 #include "ppapi/host/host_message_context.h"
15 #include "ppapi/host/ppapi_host.h"
16 #include "ppapi/proxy/ppapi_messages.h"
17 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
18 #include "third_party/WebKit/public/platform/WebString.h"
19 #include "third_party/WebKit/public/platform/WebURL.h"
20 #include "third_party/WebKit/public/web/WebDocument.h"
21 #include "third_party/WebKit/public/web/WebElement.h"
22 #include "third_party/WebKit/public/web/WebPluginContainer.h"
23 #include "third_party/WebKit/public/web/WebSocket.h"
24 
25 using blink::WebArrayBuffer;
26 using blink::WebDocument;
27 using blink::WebString;
28 using blink::WebSocket;
29 using blink::WebURL;
30 
31 namespace content {
32 
33 #define COMPILE_ASSERT_MATCHING_ENUM(webkit_name, np_name)                   \
34   COMPILE_ASSERT(                                                            \
35       static_cast<int>(WebSocket::webkit_name) == static_cast<int>(np_name), \
36       mismatching_enums)
37 
38 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeNormalClosure,
39                              PP_WEBSOCKETSTATUSCODE_NORMAL_CLOSURE);
40 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeGoingAway,
41                              PP_WEBSOCKETSTATUSCODE_GOING_AWAY);
42 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeProtocolError,
43                              PP_WEBSOCKETSTATUSCODE_PROTOCOL_ERROR);
44 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeUnsupportedData,
45                              PP_WEBSOCKETSTATUSCODE_UNSUPPORTED_DATA);
46 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeNoStatusRcvd,
47                              PP_WEBSOCKETSTATUSCODE_NO_STATUS_RECEIVED);
48 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeAbnormalClosure,
49                              PP_WEBSOCKETSTATUSCODE_ABNORMAL_CLOSURE);
50 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeInvalidFramePayloadData,
51                              PP_WEBSOCKETSTATUSCODE_INVALID_FRAME_PAYLOAD_DATA);
52 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodePolicyViolation,
53                              PP_WEBSOCKETSTATUSCODE_POLICY_VIOLATION);
54 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeMessageTooBig,
55                              PP_WEBSOCKETSTATUSCODE_MESSAGE_TOO_BIG);
56 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeMandatoryExt,
57                              PP_WEBSOCKETSTATUSCODE_MANDATORY_EXTENSION);
58 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeInternalError,
59                              PP_WEBSOCKETSTATUSCODE_INTERNAL_SERVER_ERROR);
60 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeTLSHandshake,
61                              PP_WEBSOCKETSTATUSCODE_TLS_HANDSHAKE);
62 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeMinimumUserDefined,
63                              PP_WEBSOCKETSTATUSCODE_USER_REGISTERED_MIN);
64 COMPILE_ASSERT_MATCHING_ENUM(CloseEventCodeMaximumUserDefined,
65                              PP_WEBSOCKETSTATUSCODE_USER_PRIVATE_MAX);
66 
PepperWebSocketHost(RendererPpapiHost * host,PP_Instance instance,PP_Resource resource)67 PepperWebSocketHost::PepperWebSocketHost(RendererPpapiHost* host,
68                                          PP_Instance instance,
69                                          PP_Resource resource)
70     : ResourceHost(host->GetPpapiHost(), instance, resource),
71       renderer_ppapi_host_(host),
72       connecting_(false),
73       initiating_close_(false),
74       accepting_close_(false),
75       error_was_received_(false) {}
76 
~PepperWebSocketHost()77 PepperWebSocketHost::~PepperWebSocketHost() {
78   if (websocket_)
79     websocket_->disconnect();
80 }
81 
OnResourceMessageReceived(const IPC::Message & msg,ppapi::host::HostMessageContext * context)82 int32_t PepperWebSocketHost::OnResourceMessageReceived(
83     const IPC::Message& msg,
84     ppapi::host::HostMessageContext* context) {
85   PPAPI_BEGIN_MESSAGE_MAP(PepperWebSocketHost, msg)
86     PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_Connect,
87                                       OnHostMsgConnect)
88     PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_Close,
89                                       OnHostMsgClose)
90     PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_SendText,
91                                       OnHostMsgSendText)
92     PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_SendBinary,
93                                       OnHostMsgSendBinary)
94     PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_Fail,
95                                       OnHostMsgFail)
96   PPAPI_END_MESSAGE_MAP()
97   return PP_ERROR_FAILED;
98 }
99 
didConnect()100 void PepperWebSocketHost::didConnect() {
101   std::string protocol;
102   if (websocket_)
103     protocol = websocket_->subprotocol().utf8();
104   connecting_ = false;
105   connect_reply_.params.set_result(PP_OK);
106   host()->SendReply(connect_reply_,
107                     PpapiPluginMsg_WebSocket_ConnectReply(url_, protocol));
108 }
109 
didReceiveMessage(const blink::WebString & message)110 void PepperWebSocketHost::didReceiveMessage(const blink::WebString& message) {
111   // Dispose packets after receiving an error.
112   if (error_was_received_)
113     return;
114 
115   // Send an IPC to transport received data.
116   std::string string_message = message.utf8();
117   host()->SendUnsolicitedReply(
118       pp_resource(), PpapiPluginMsg_WebSocket_ReceiveTextReply(string_message));
119 }
120 
didReceiveArrayBuffer(const blink::WebArrayBuffer & binaryData)121 void PepperWebSocketHost::didReceiveArrayBuffer(
122     const blink::WebArrayBuffer& binaryData) {
123   // Dispose packets after receiving an error.
124   if (error_was_received_)
125     return;
126 
127   // Send an IPC to transport received data.
128   uint8_t* data = static_cast<uint8_t*>(binaryData.data());
129   std::vector<uint8_t> array_message(data, data + binaryData.byteLength());
130   host()->SendUnsolicitedReply(
131       pp_resource(),
132       PpapiPluginMsg_WebSocket_ReceiveBinaryReply(array_message));
133 }
134 
didReceiveMessageError()135 void PepperWebSocketHost::didReceiveMessageError() {
136   // Records the error, then stops receiving any frames after this error.
137   // The error must be notified after all queued messages are read.
138   error_was_received_ = true;
139 
140   // Send an IPC to report the error. After this IPC, ReceiveTextReply and
141   // ReceiveBinaryReply IPC are not sent anymore because |error_was_received_|
142   // blocks.
143   host()->SendUnsolicitedReply(pp_resource(),
144                                PpapiPluginMsg_WebSocket_ErrorReply());
145 }
146 
didUpdateBufferedAmount(unsigned long buffered_amount)147 void PepperWebSocketHost::didUpdateBufferedAmount(
148     unsigned long buffered_amount) {
149   // Send an IPC to update buffered amount.
150   host()->SendUnsolicitedReply(
151       pp_resource(),
152       PpapiPluginMsg_WebSocket_BufferedAmountReply(buffered_amount));
153 }
154 
didStartClosingHandshake()155 void PepperWebSocketHost::didStartClosingHandshake() {
156   accepting_close_ = true;
157 
158   // Send an IPC to notice that server starts closing handshake.
159   host()->SendUnsolicitedReply(
160       pp_resource(),
161       PpapiPluginMsg_WebSocket_StateReply(PP_WEBSOCKETREADYSTATE_CLOSING));
162 }
163 
didClose(unsigned long unhandled_buffered_amount,ClosingHandshakeCompletionStatus status,unsigned short code,const blink::WebString & reason)164 void PepperWebSocketHost::didClose(unsigned long unhandled_buffered_amount,
165                                    ClosingHandshakeCompletionStatus status,
166                                    unsigned short code,
167                                    const blink::WebString& reason) {
168   if (connecting_) {
169     connecting_ = false;
170     connect_reply_.params.set_result(PP_ERROR_FAILED);
171     host()->SendReply(
172         connect_reply_,
173         PpapiPluginMsg_WebSocket_ConnectReply(url_, std::string()));
174   }
175 
176   // Set close_was_clean_.
177   bool was_clean = (initiating_close_ || accepting_close_) &&
178                    !unhandled_buffered_amount &&
179                    status == WebSocketClient::ClosingHandshakeComplete;
180 
181   if (initiating_close_) {
182     initiating_close_ = false;
183     close_reply_.params.set_result(PP_OK);
184     host()->SendReply(
185         close_reply_,
186         PpapiPluginMsg_WebSocket_CloseReply(
187             unhandled_buffered_amount, was_clean, code, reason.utf8()));
188   } else {
189     accepting_close_ = false;
190     host()->SendUnsolicitedReply(
191         pp_resource(),
192         PpapiPluginMsg_WebSocket_ClosedReply(
193             unhandled_buffered_amount, was_clean, code, reason.utf8()));
194   }
195 
196   // Disconnect.
197   if (websocket_)
198     websocket_->disconnect();
199 }
200 
OnHostMsgConnect(ppapi::host::HostMessageContext * context,const std::string & url,const std::vector<std::string> & protocols)201 int32_t PepperWebSocketHost::OnHostMsgConnect(
202     ppapi::host::HostMessageContext* context,
203     const std::string& url,
204     const std::vector<std::string>& protocols) {
205   // Validate url and convert it to WebURL.
206   GURL gurl(url);
207   url_ = gurl.spec();
208   if (!gurl.is_valid())
209     return PP_ERROR_BADARGUMENT;
210   if (!gurl.SchemeIs("ws") && !gurl.SchemeIs("wss"))
211     return PP_ERROR_BADARGUMENT;
212   if (gurl.has_ref())
213     return PP_ERROR_BADARGUMENT;
214   if (!net::IsPortAllowedByDefault(gurl.IntPort()))
215     return PP_ERROR_BADARGUMENT;
216   WebURL web_url(gurl);
217 
218   // Validate protocols.
219   std::string protocol_string;
220   for (std::vector<std::string>::const_iterator vector_it = protocols.begin();
221        vector_it != protocols.end();
222        ++vector_it) {
223 
224     // Check containing characters.
225     for (std::string::const_iterator string_it = vector_it->begin();
226          string_it != vector_it->end();
227          ++string_it) {
228       uint8_t character = *string_it;
229       // WebSocket specification says "(Subprotocol string must consist of)
230       // characters in the range U+0021 to U+007E not including separator
231       // characters as defined in [RFC2616]."
232       const uint8_t minimumProtocolCharacter = '!';  // U+0021.
233       const uint8_t maximumProtocolCharacter = '~';  // U+007E.
234       if (character < minimumProtocolCharacter ||
235           character > maximumProtocolCharacter || character == '"' ||
236           character == '(' || character == ')' || character == ',' ||
237           character == '/' ||
238           (character >= ':' && character <= '@') ||  // U+003A - U+0040
239           (character >= '[' && character <= ']') ||  // U+005B - u+005D
240           character == '{' ||
241           character == '}')
242         return PP_ERROR_BADARGUMENT;
243     }
244     // Join protocols with the comma separator.
245     if (vector_it != protocols.begin())
246       protocol_string.append(",");
247     protocol_string.append(*vector_it);
248   }
249 
250   // Convert protocols to WebString.
251   WebString web_protocols = WebString::fromUTF8(protocol_string);
252 
253   // Create blink::WebSocket object and connect.
254   blink::WebPluginContainer* container =
255       renderer_ppapi_host_->GetContainerForInstance(pp_instance());
256   if (!container)
257     return PP_ERROR_BADARGUMENT;
258   // TODO(toyoshim) Remove following WebDocument object copy.
259   WebDocument document = container->element().document();
260   websocket_.reset(WebSocket::create(document, this));
261   DCHECK(websocket_.get());
262   if (!websocket_)
263     return PP_ERROR_NOTSUPPORTED;
264 
265   // Set receiving binary object type.
266   websocket_->setBinaryType(WebSocket::BinaryTypeArrayBuffer);
267   websocket_->connect(web_url, web_protocols);
268 
269   connect_reply_ = context->MakeReplyMessageContext();
270   connecting_ = true;
271   return PP_OK_COMPLETIONPENDING;
272 }
273 
OnHostMsgClose(ppapi::host::HostMessageContext * context,int32_t code,const std::string & reason)274 int32_t PepperWebSocketHost::OnHostMsgClose(
275     ppapi::host::HostMessageContext* context,
276     int32_t code,
277     const std::string& reason) {
278   if (!websocket_)
279     return PP_ERROR_FAILED;
280   close_reply_ = context->MakeReplyMessageContext();
281   initiating_close_ = true;
282 
283   blink::WebSocket::CloseEventCode event_code =
284       static_cast<blink::WebSocket::CloseEventCode>(code);
285   if (code == PP_WEBSOCKETSTATUSCODE_NOT_SPECIFIED) {
286     // PP_WEBSOCKETSTATUSCODE_NOT_SPECIFIED and CloseEventCodeNotSpecified are
287     // assigned to different values. A conversion is needed if
288     // PP_WEBSOCKETSTATUSCODE_NOT_SPECIFIED is specified.
289     event_code = blink::WebSocket::CloseEventCodeNotSpecified;
290   }
291 
292   WebString web_reason = WebString::fromUTF8(reason);
293   websocket_->close(event_code, web_reason);
294   return PP_OK_COMPLETIONPENDING;
295 }
296 
OnHostMsgSendText(ppapi::host::HostMessageContext * context,const std::string & message)297 int32_t PepperWebSocketHost::OnHostMsgSendText(
298     ppapi::host::HostMessageContext* context,
299     const std::string& message) {
300   if (websocket_) {
301     WebString web_message = WebString::fromUTF8(message);
302     websocket_->sendText(web_message);
303   }
304   return PP_OK;
305 }
306 
OnHostMsgSendBinary(ppapi::host::HostMessageContext * context,const std::vector<uint8_t> & message)307 int32_t PepperWebSocketHost::OnHostMsgSendBinary(
308     ppapi::host::HostMessageContext* context,
309     const std::vector<uint8_t>& message) {
310   if (websocket_.get() && !message.empty()) {
311     WebArrayBuffer web_message = WebArrayBuffer::create(message.size(), 1);
312     memcpy(web_message.data(), &message.front(), message.size());
313     websocket_->sendArrayBuffer(web_message);
314   }
315   return PP_OK;
316 }
317 
OnHostMsgFail(ppapi::host::HostMessageContext * context,const std::string & message)318 int32_t PepperWebSocketHost::OnHostMsgFail(
319     ppapi::host::HostMessageContext* context,
320     const std::string& message) {
321   if (websocket_)
322     websocket_->fail(WebString::fromUTF8(message));
323   return PP_OK;
324 }
325 
326 }  // namespace content
327