OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "webkit/child/websocket_handle_impl.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/compiler_specific.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/strings/string16.h" |
| 15 #include "third_party/WebKit/public/platform/WebData.h" |
| 16 #include "third_party/WebKit/public/platform/WebSocketHandle.h" |
| 17 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h" |
| 18 #include "third_party/WebKit/public/platform/WebString.h" |
| 19 #include "third_party/WebKit/public/platform/WebURL.h" |
| 20 #include "webkit/child/webkitplatformsupport_impl.h" |
| 21 #include "webkit/child/websocket_handle_bridge.h" |
| 22 #include "webkit/child/websocket_handle_delegate.h" |
| 23 |
| 24 using WebKit::WebString; |
| 25 using WebKit::WebURL; |
| 26 using WebKit::WebVector; |
| 27 using WebKit::WebSocketHandle; |
| 28 using WebKit::WebSocketHandleClient; |
| 29 |
| 30 namespace webkit_glue { |
| 31 |
| 32 class WebSocketHandleImpl::Context |
| 33 : public WebSocketHandleDelegate { |
| 34 public: |
| 35 explicit Context(WebSocketHandle* handle) |
| 36 : handle_(handle), |
| 37 client_(NULL) {} |
| 38 virtual ~Context(); |
| 39 |
| 40 void Connect(const GURL& url, |
| 41 const std::vector<std::string>& protocols, |
| 42 const GURL& origin, |
| 43 WebSocketHandleClient* client, |
| 44 WebKitPlatformSupportImpl* platform); |
| 45 void Send(bool fin, |
| 46 WebSocketHandleBridge::MessageType type, |
| 47 const char* data, |
| 48 size_t size); |
| 49 void FlowControl(int64_t quota); |
| 50 void Close(unsigned short code, const std::string& reason); |
| 51 |
| 52 // WebSocketHandleDelegate functions. |
| 53 virtual void DidConnect(bool fail, |
| 54 const std::string& protocol, |
| 55 const std::string& extensions) OVERRIDE; |
| 56 virtual void DidReceiveData(bool fin, |
| 57 WebSocketHandleBridge::MessageType type, |
| 58 const char* data, |
| 59 size_t size) OVERRIDE; |
| 60 virtual void DidReceiveFlowControl(int64_t quota) OVERRIDE; |
| 61 virtual void DidClose(unsigned short code, |
| 62 const std::string& reason) OVERRIDE; |
| 63 |
| 64 private: |
| 65 WebSocketHandle* handle_; |
| 66 WebSocketHandleClient* client_; |
| 67 scoped_refptr<WebSocketHandleBridge> bridge_; |
| 68 }; |
| 69 |
| 70 void WebSocketHandleImpl::Context::Connect( |
| 71 const GURL& url, |
| 72 const std::vector<std::string>& protocols, |
| 73 const GURL& origin, |
| 74 WebSocketHandleClient* client, |
| 75 WebKitPlatformSupportImpl* platform) { |
| 76 DCHECK(!bridge_); |
| 77 DCHECK(client); |
| 78 client_ = client; |
| 79 bridge_ = platform->CreateWebSocketBridge(this); |
| 80 bridge_->Connect(url, protocols, origin, this); |
| 81 } |
| 82 |
| 83 WebSocketHandleImpl::Context::~Context() { |
| 84 if (bridge_) |
| 85 bridge_->Disconnect(); |
| 86 } |
| 87 |
| 88 void WebSocketHandleImpl::Context::Send(bool fin, |
| 89 WebSocketHandleBridge::MessageType type, |
| 90 const char* data, |
| 91 size_t size) { |
| 92 DCHECK(bridge_); |
| 93 bridge_->Send(fin, type, data, size); |
| 94 } |
| 95 |
| 96 void WebSocketHandleImpl::Context::FlowControl(int64_t quota) { |
| 97 DCHECK(bridge_); |
| 98 bridge_->FlowControl(quota); |
| 99 } |
| 100 |
| 101 void WebSocketHandleImpl::Context::Close(unsigned short code, |
| 102 const std::string& reason) { |
| 103 DCHECK(bridge_); |
| 104 bridge_->Close(code, reason); |
| 105 } |
| 106 |
| 107 void WebSocketHandleImpl::Context::DidConnect(bool fail, |
| 108 const std::string& protocol, |
| 109 const std::string& extensions) { |
| 110 DCHECK(client_); |
| 111 WebString protocol_to_pass = WebString::fromUTF8(protocol); |
| 112 WebString extensions_to_pass = WebString::fromUTF8(extensions); |
| 113 client_->didConnect(handle_, fail, protocol_to_pass, extensions_to_pass); |
| 114 } |
| 115 |
| 116 void WebSocketHandleImpl::Context::DidReceiveData( |
| 117 bool fin, |
| 118 WebSocketHandleBridge::MessageType type, |
| 119 const char* data, |
| 120 size_t size) { |
| 121 DCHECK(client_); |
| 122 WebSocketHandle::MessageType type_to_pass; |
| 123 switch (type) { |
| 124 case WebSocketHandleBridge::MESSAGE_TYPE_CONTINUATION: |
| 125 type_to_pass = WebSocketHandle::MessageTypeContinuation; |
| 126 break; |
| 127 case WebSocketHandleBridge::MESSAGE_TYPE_TEXT: |
| 128 type_to_pass = WebSocketHandle::MessageTypeText; |
| 129 break; |
| 130 case WebSocketHandleBridge::MESSAGE_TYPE_BINARY: |
| 131 type_to_pass = WebSocketHandle::MessageTypeBinary; |
| 132 break; |
| 133 default: |
| 134 NOTREACHED(); |
| 135 break; |
| 136 } |
| 137 |
| 138 client_->didReceiveData(handle_, fin, type_to_pass, data, size); |
| 139 } |
| 140 |
| 141 void WebSocketHandleImpl::Context::DidReceiveFlowControl(int64_t quota) { |
| 142 DCHECK(client_); |
| 143 client_->didReceiveFlowControl(handle_, quota); |
| 144 } |
| 145 |
| 146 void WebSocketHandleImpl::Context::DidClose(unsigned short code, |
| 147 const std::string& reason) { |
| 148 DCHECK(client_); |
| 149 WebString reason_to_pass = WebString::fromUTF8(reason); |
| 150 client_->didClose(handle_, code, reason_to_pass); |
| 151 if (bridge_) { |
| 152 bridge_->Disconnect(); |
| 153 client_ = NULL; |
| 154 bridge_ = NULL; |
| 155 } |
| 156 } |
| 157 |
| 158 WebSocketHandleImpl::WebSocketHandleImpl(WebKitPlatformSupportImpl* platform) |
| 159 : context_(new Context(this)), |
| 160 platform_(platform) { |
| 161 } |
| 162 |
| 163 WebSocketHandleImpl::~WebSocketHandleImpl() {} |
| 164 |
| 165 void WebSocketHandleImpl::connect(const WebURL& url, |
| 166 const WebVector<WebString>& protocols, |
| 167 const WebString& origin, |
| 168 WebSocketHandleClient* client) { |
| 169 std::vector<std::string> protocols_to_pass; |
| 170 for (size_t i = 0; i < protocols.size(); ++i) |
| 171 protocols_to_pass.push_back(protocols[i].utf8()); |
| 172 GURL origin_to_pass(origin.utf8()); |
| 173 context_->Connect(url, protocols_to_pass, origin_to_pass, client, platform_); |
| 174 } |
| 175 |
| 176 void WebSocketHandleImpl::send(bool fin, |
| 177 WebSocketHandle::MessageType type, |
| 178 const char* data, |
| 179 size_t size) { |
| 180 WebSocketHandleBridge::MessageType type_to_pass; |
| 181 switch (type) { |
| 182 case WebSocketHandle::MessageTypeContinuation: |
| 183 type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_CONTINUATION; |
| 184 break; |
| 185 case WebSocketHandle::MessageTypeText: |
| 186 type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_TEXT; |
| 187 break; |
| 188 case WebSocketHandle::MessageTypeBinary: |
| 189 type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_BINARY; |
| 190 break; |
| 191 default: |
| 192 NOTREACHED(); |
| 193 break; |
| 194 } |
| 195 context_->Send(fin, type_to_pass, data, size); |
| 196 } |
| 197 |
| 198 void WebSocketHandleImpl::flowControl(int64_t quota) { |
| 199 context_->FlowControl(quota); |
| 200 } |
| 201 |
| 202 void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) { |
| 203 std::string reason_to_pass = reason.utf8(); |
| 204 context_->Close(code, reason_to_pass); |
| 205 } |
| 206 |
| 207 } // namespace webkit_glue |
OLD | NEW |