| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/renderer/pepper/pepper_websocket_host.h" | 5 #include "content/renderer/pepper/pepper_websocket_host.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "content/public/renderer/renderer_ppapi_host.h" | 9 #include "content/public/renderer/renderer_ppapi_host.h" |
| 10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 PP_Resource resource) | 36 PP_Resource resource) |
| 37 : ResourceHost(host->GetPpapiHost(), instance, resource), | 37 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 38 renderer_ppapi_host_(host), | 38 renderer_ppapi_host_(host), |
| 39 connecting_(false), | 39 connecting_(false), |
| 40 initiating_close_(false), | 40 initiating_close_(false), |
| 41 accepting_close_(false), | 41 accepting_close_(false), |
| 42 error_was_received_(false) { | 42 error_was_received_(false) { |
| 43 } | 43 } |
| 44 | 44 |
| 45 PepperWebSocketHost::~PepperWebSocketHost() { | 45 PepperWebSocketHost::~PepperWebSocketHost() { |
| 46 if (websocket_.get()) | 46 if (websocket_) |
| 47 websocket_->disconnect(); | 47 websocket_->disconnect(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 int32_t PepperWebSocketHost::OnResourceMessageReceived( | 50 int32_t PepperWebSocketHost::OnResourceMessageReceived( |
| 51 const IPC::Message& msg, | 51 const IPC::Message& msg, |
| 52 ppapi::host::HostMessageContext* context) { | 52 ppapi::host::HostMessageContext* context) { |
| 53 IPC_BEGIN_MESSAGE_MAP(PepperWebSocketHost, msg) | 53 IPC_BEGIN_MESSAGE_MAP(PepperWebSocketHost, msg) |
| 54 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_Connect, | 54 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_Connect, |
| 55 OnHostMsgConnect) | 55 OnHostMsgConnect) |
| 56 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_Close, | 56 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_Close, |
| 57 OnHostMsgClose) | 57 OnHostMsgClose) |
| 58 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_SendText, | 58 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_SendText, |
| 59 OnHostMsgSendText) | 59 OnHostMsgSendText) |
| 60 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_SendBinary, | 60 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_SendBinary, |
| 61 OnHostMsgSendBinary) | 61 OnHostMsgSendBinary) |
| 62 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_Fail, | 62 PPAPI_DISPATCH_HOST_RESOURCE_CALL(PpapiHostMsg_WebSocket_Fail, |
| 63 OnHostMsgFail) | 63 OnHostMsgFail) |
| 64 IPC_END_MESSAGE_MAP() | 64 IPC_END_MESSAGE_MAP() |
| 65 return PP_ERROR_FAILED; | 65 return PP_ERROR_FAILED; |
| 66 } | 66 } |
| 67 | 67 |
| 68 void PepperWebSocketHost::didConnect() { | 68 void PepperWebSocketHost::didConnect() { |
| 69 std::string protocol; | 69 std::string protocol; |
| 70 if (websocket_.get()) | 70 if (websocket_) |
| 71 protocol = websocket_->subprotocol().utf8(); | 71 protocol = websocket_->subprotocol().utf8(); |
| 72 connecting_ = false; | 72 connecting_ = false; |
| 73 connect_reply_.params.set_result(PP_OK); | 73 connect_reply_.params.set_result(PP_OK); |
| 74 host()->SendReply(connect_reply_, | 74 host()->SendReply(connect_reply_, |
| 75 PpapiPluginMsg_WebSocket_ConnectReply( | 75 PpapiPluginMsg_WebSocket_ConnectReply( |
| 76 url_, | 76 url_, |
| 77 protocol)); | 77 protocol)); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void PepperWebSocketHost::didReceiveMessage(const WebKit::WebString& message) { | 80 void PepperWebSocketHost::didReceiveMessage(const WebKit::WebString& message) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 accepting_close_ = false; | 162 accepting_close_ = false; |
| 163 host()->SendUnsolicitedReply(pp_resource(), | 163 host()->SendUnsolicitedReply(pp_resource(), |
| 164 PpapiPluginMsg_WebSocket_ClosedReply( | 164 PpapiPluginMsg_WebSocket_ClosedReply( |
| 165 unhandled_buffered_amount, | 165 unhandled_buffered_amount, |
| 166 was_clean, | 166 was_clean, |
| 167 code, | 167 code, |
| 168 reason.utf8())); | 168 reason.utf8())); |
| 169 } | 169 } |
| 170 | 170 |
| 171 // Disconnect. | 171 // Disconnect. |
| 172 if (websocket_.get()) | 172 if (websocket_) |
| 173 websocket_->disconnect(); | 173 websocket_->disconnect(); |
| 174 } | 174 } |
| 175 | 175 |
| 176 int32_t PepperWebSocketHost::OnHostMsgConnect( | 176 int32_t PepperWebSocketHost::OnHostMsgConnect( |
| 177 ppapi::host::HostMessageContext* context, | 177 ppapi::host::HostMessageContext* context, |
| 178 const std::string& url, | 178 const std::string& url, |
| 179 const std::vector<std::string>& protocols) { | 179 const std::vector<std::string>& protocols) { |
| 180 // Validate url and convert it to WebURL. | 180 // Validate url and convert it to WebURL. |
| 181 GURL gurl(url); | 181 GURL gurl(url); |
| 182 url_ = gurl.spec(); | 182 url_ = gurl.spec(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 226 |
| 227 // Create WebKit::WebSocket object and connect. | 227 // Create WebKit::WebSocket object and connect. |
| 228 WebKit::WebPluginContainer* container = | 228 WebKit::WebPluginContainer* container = |
| 229 renderer_ppapi_host_->GetContainerForInstance(pp_instance()); | 229 renderer_ppapi_host_->GetContainerForInstance(pp_instance()); |
| 230 if (!container) | 230 if (!container) |
| 231 return PP_ERROR_BADARGUMENT; | 231 return PP_ERROR_BADARGUMENT; |
| 232 // TODO(toyoshim) Remove following WebDocument object copy. | 232 // TODO(toyoshim) Remove following WebDocument object copy. |
| 233 WebDocument document = container->element().document(); | 233 WebDocument document = container->element().document(); |
| 234 websocket_.reset(WebSocket::create(document, this)); | 234 websocket_.reset(WebSocket::create(document, this)); |
| 235 DCHECK(websocket_.get()); | 235 DCHECK(websocket_.get()); |
| 236 if (!websocket_.get()) | 236 if (!websocket_) |
| 237 return PP_ERROR_NOTSUPPORTED; | 237 return PP_ERROR_NOTSUPPORTED; |
| 238 | 238 |
| 239 // Set receiving binary object type. | 239 // Set receiving binary object type. |
| 240 websocket_->setBinaryType(WebSocket::BinaryTypeArrayBuffer); | 240 websocket_->setBinaryType(WebSocket::BinaryTypeArrayBuffer); |
| 241 websocket_->connect(web_url, web_protocols); | 241 websocket_->connect(web_url, web_protocols); |
| 242 | 242 |
| 243 connect_reply_ = context->MakeReplyMessageContext(); | 243 connect_reply_ = context->MakeReplyMessageContext(); |
| 244 connecting_ = true; | 244 connecting_ = true; |
| 245 return PP_OK_COMPLETIONPENDING; | 245 return PP_OK_COMPLETIONPENDING; |
| 246 } | 246 } |
| 247 | 247 |
| 248 int32_t PepperWebSocketHost::OnHostMsgClose( | 248 int32_t PepperWebSocketHost::OnHostMsgClose( |
| 249 ppapi::host::HostMessageContext* context, | 249 ppapi::host::HostMessageContext* context, |
| 250 int32_t code, | 250 int32_t code, |
| 251 const std::string& reason) { | 251 const std::string& reason) { |
| 252 if (!websocket_.get()) | 252 if (!websocket_) |
| 253 return PP_ERROR_FAILED; | 253 return PP_ERROR_FAILED; |
| 254 close_reply_ = context->MakeReplyMessageContext(); | 254 close_reply_ = context->MakeReplyMessageContext(); |
| 255 initiating_close_ = true; | 255 initiating_close_ = true; |
| 256 WebString web_reason = WebString::fromUTF8(reason); | 256 WebString web_reason = WebString::fromUTF8(reason); |
| 257 websocket_->close(code, web_reason); | 257 websocket_->close(code, web_reason); |
| 258 return PP_OK_COMPLETIONPENDING; | 258 return PP_OK_COMPLETIONPENDING; |
| 259 } | 259 } |
| 260 | 260 |
| 261 int32_t PepperWebSocketHost::OnHostMsgSendText( | 261 int32_t PepperWebSocketHost::OnHostMsgSendText( |
| 262 ppapi::host::HostMessageContext* context, | 262 ppapi::host::HostMessageContext* context, |
| 263 const std::string& message) { | 263 const std::string& message) { |
| 264 if (websocket_.get()) { | 264 if (websocket_) { |
| 265 WebString web_message = WebString::fromUTF8(message); | 265 WebString web_message = WebString::fromUTF8(message); |
| 266 websocket_->sendText(web_message); | 266 websocket_->sendText(web_message); |
| 267 } | 267 } |
| 268 return PP_OK; | 268 return PP_OK; |
| 269 } | 269 } |
| 270 | 270 |
| 271 int32_t PepperWebSocketHost::OnHostMsgSendBinary( | 271 int32_t PepperWebSocketHost::OnHostMsgSendBinary( |
| 272 ppapi::host::HostMessageContext* context, | 272 ppapi::host::HostMessageContext* context, |
| 273 const std::vector<uint8_t>& message) { | 273 const std::vector<uint8_t>& message) { |
| 274 if (websocket_.get() && !message.empty()) { | 274 if (websocket_.get() && !message.empty()) { |
| 275 WebArrayBuffer web_message = WebArrayBuffer::create(message.size(), 1); | 275 WebArrayBuffer web_message = WebArrayBuffer::create(message.size(), 1); |
| 276 memcpy(web_message.data(), &message.front(), message.size()); | 276 memcpy(web_message.data(), &message.front(), message.size()); |
| 277 websocket_->sendArrayBuffer(web_message); | 277 websocket_->sendArrayBuffer(web_message); |
| 278 } | 278 } |
| 279 return PP_OK; | 279 return PP_OK; |
| 280 } | 280 } |
| 281 | 281 |
| 282 int32_t PepperWebSocketHost::OnHostMsgFail( | 282 int32_t PepperWebSocketHost::OnHostMsgFail( |
| 283 ppapi::host::HostMessageContext* context, | 283 ppapi::host::HostMessageContext* context, |
| 284 const std::string& message) { | 284 const std::string& message) { |
| 285 if (websocket_.get()) | 285 if (websocket_) |
| 286 websocket_->fail(WebString::fromUTF8(message)); | 286 websocket_->fail(WebString::fromUTF8(message)); |
| 287 return PP_OK; | 287 return PP_OK; |
| 288 } | 288 } |
| 289 | 289 |
| 290 } // namespace content | 290 } // namespace content |
| OLD | NEW |