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 base::RefCounted<Context>, |
| 34 public WebSocketHandleDelegate { |
| 35 public: |
| 36 explicit Context(WebSocketHandle* handle) |
| 37 : is_active_(false) |
| 38 , handle_(handle) |
| 39 , client_(NULL) {} |
| 40 |
| 41 void Connect(const GURL& url, |
| 42 const std::vector<std::string>& protocols, |
| 43 const GURL& origin, |
| 44 WebSocketHandleClient* client); |
| 45 void Send(WebSocketHandleBridge::MessageType type, |
| 46 const char* data, |
| 47 size_t size, |
| 48 bool fin); |
| 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 succeed, |
| 54 const std::string& protocol, |
| 55 const std::string& extensions) OVERRIDE; |
| 56 virtual void DidReceiveData(WebSocketHandleBridge::MessageType type, |
| 57 const char* data, |
| 58 size_t size, |
| 59 bool fin) 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 virtual ~Context(); |
| 66 friend class base::RefCounted<WebSocketHandleImpl::Context>; |
| 67 bool is_active_; |
| 68 WebSocketHandle* handle_; |
| 69 WebSocketHandleClient* client_; |
| 70 scoped_refptr<WebSocketHandleBridge> bridge_; |
| 71 }; |
| 72 |
| 73 void WebSocketHandleImpl::Context::Connect( |
| 74 const GURL& url, |
| 75 const std::vector<std::string>& protocols, |
| 76 const GURL& origin, |
| 77 WebSocketHandleClient* client) { |
| 78 DCHECK(!is_active_); |
| 79 is_active_ = true; |
| 80 client_ = client; |
| 81 bridge_->Connect(url, protocols, origin, this); |
| 82 } |
| 83 |
| 84 WebSocketHandleImpl::Context::~Context() { |
| 85 if (bridge_) { |
| 86 bridge_->Disconnect(); |
| 87 } |
| 88 } |
| 89 |
| 90 |
| 91 void WebSocketHandleImpl::Context::Send(WebSocketHandleBridge::MessageType type, |
| 92 const char* data, |
| 93 size_t size, |
| 94 bool fin) { |
| 95 if (!is_active_) { |
| 96 return; |
| 97 } |
| 98 bridge_->Send(type, data, size, fin); |
| 99 } |
| 100 |
| 101 void WebSocketHandleImpl::Context::FlowControl(int64_t quota) { |
| 102 if (!is_active_) { |
| 103 return; |
| 104 } |
| 105 bridge_->FlowControl(quota); |
| 106 } |
| 107 |
| 108 void WebSocketHandleImpl::Context::Close(unsigned short code, |
| 109 const std::string& reason) { |
| 110 if (!is_active_) { |
| 111 return; |
| 112 } |
| 113 bridge_->Close(code, reason); |
| 114 } |
| 115 |
| 116 void WebSocketHandleImpl::Context::DidConnect(bool succeed, |
| 117 const std::string& protocol, |
| 118 const std::string& extensions) { |
| 119 if (!is_active_) { |
| 120 return; |
| 121 } |
| 122 WebString protocol_to_pass = WebString::fromUTF8(protocol); |
| 123 WebString extensions_to_pass = WebString::fromUTF8(extensions); |
| 124 client_->didConnect(handle_, succeed, protocol_to_pass, extensions_to_pass); |
| 125 } |
| 126 |
| 127 void WebSocketHandleImpl::Context::DidReceiveData( |
| 128 WebSocketHandleBridge::MessageType type, |
| 129 const char* data, |
| 130 size_t size, |
| 131 bool fin) { |
| 132 if (!is_active_) { |
| 133 return; |
| 134 } |
| 135 WebSocketHandle::MessageType type_to_pass; |
| 136 switch (type) { |
| 137 case WebSocketHandleBridge::MESSAGE_TYPE_TEXT: |
| 138 type_to_pass = WebSocketHandle::MessageTypeText; |
| 139 break; |
| 140 case WebSocketHandleBridge::MESSAGE_TYPE_BINARY: |
| 141 type_to_pass = WebSocketHandle::MessageTypeBinary; |
| 142 break; |
| 143 case WebSocketHandleBridge::MESSAGE_TYPE_CONTINUATION: |
| 144 type_to_pass = WebSocketHandle::MessageTypeContinuation; |
| 145 break; |
| 146 default: |
| 147 NOTREACHED(); |
| 148 break; |
| 149 } |
| 150 |
| 151 client_->didReceiveData(handle_, type_to_pass, data, size, fin); |
| 152 } |
| 153 |
| 154 void WebSocketHandleImpl::Context::DidReceiveFlowControl(int64_t quota) { |
| 155 if (!is_active_) { |
| 156 return; |
| 157 } |
| 158 client_->didReceiveFlowControl(handle_, quota); |
| 159 } |
| 160 |
| 161 void WebSocketHandleImpl::Context::DidClose(unsigned short code, |
| 162 const std::string& reason) { |
| 163 if (!is_active_) { |
| 164 return; |
| 165 } |
| 166 WebString reason_to_pass = WebString::fromUTF8(reason); |
| 167 client_->didClose(handle_, code, reason_to_pass); |
| 168 } |
| 169 |
| 170 WebSocketHandleImpl::WebSocketHandleImpl(WebKitPlatformSupportImpl* platform) |
| 171 : context_(new Context(this)) |
| 172 , platform_(platform) { |
| 173 } |
| 174 |
| 175 WebSocketHandleImpl::~WebSocketHandleImpl() {} |
| 176 |
| 177 void WebSocketHandleImpl::connect(const WebURL& url, |
| 178 const WebVector<WebString>& protocols, |
| 179 const WebString& origin, |
| 180 WebSocketHandleClient* client) { |
| 181 std::vector<std::string> protocols_to_pass; |
| 182 for (size_t i = 0; i < protocols.size(); ++i) { |
| 183 protocols_to_pass.push_back(protocols[i].utf8()); |
| 184 } |
| 185 GURL origin_to_pass(origin.utf8()); |
| 186 context_->Connect(url, protocols_to_pass, origin_to_pass, client); |
| 187 } |
| 188 |
| 189 void WebSocketHandleImpl::send(WebSocketHandle::MessageType type, |
| 190 const char* data, |
| 191 size_t size, |
| 192 bool fin) { |
| 193 WebSocketHandleBridge::MessageType type_to_pass; |
| 194 switch (type) { |
| 195 case WebSocketHandle::MessageTypeText: |
| 196 type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_TEXT; |
| 197 break; |
| 198 case WebSocketHandle::MessageTypeBinary: |
| 199 type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_BINARY; |
| 200 break; |
| 201 case WebSocketHandle::MessageTypeContinuation: |
| 202 type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_CONTINUATION; |
| 203 break; |
| 204 default: |
| 205 NOTREACHED(); |
| 206 break; |
| 207 } |
| 208 context_->Send(type_to_pass, data, size, fin); |
| 209 } |
| 210 |
| 211 void WebSocketHandleImpl::flowControl(int64_t quota) { |
| 212 context_->FlowControl(quota); |
| 213 } |
| 214 |
| 215 void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) { |
| 216 std::string reason_to_pass = reason.utf8(); |
| 217 context_->Close(code, reason_to_pass); |
| 218 } |
| 219 |
| 220 } // namespace webkit_glue |
OLD | NEW |