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