Index: webkit/child/websocket_handle_impl.cc |
diff --git a/webkit/child/websocket_handle_impl.cc b/webkit/child/websocket_handle_impl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1386d14f63c09979aaedc1fbe7d316af7da1101f |
--- /dev/null |
+++ b/webkit/child/websocket_handle_impl.cc |
@@ -0,0 +1,207 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "webkit/child/websocket_handle_impl.h" |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "base/compiler_specific.h" |
+#include "base/logging.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/strings/string16.h" |
+#include "third_party/WebKit/public/platform/WebData.h" |
+#include "third_party/WebKit/public/platform/WebSocketHandle.h" |
+#include "third_party/WebKit/public/platform/WebSocketHandleClient.h" |
+#include "third_party/WebKit/public/platform/WebString.h" |
+#include "third_party/WebKit/public/platform/WebURL.h" |
+#include "webkit/child/webkitplatformsupport_impl.h" |
+#include "webkit/child/websocket_handle_bridge.h" |
+#include "webkit/child/websocket_handle_delegate.h" |
+ |
+using WebKit::WebString; |
+using WebKit::WebURL; |
+using WebKit::WebVector; |
+using WebKit::WebSocketHandle; |
+using WebKit::WebSocketHandleClient; |
+ |
+namespace webkit_glue { |
+ |
+class WebSocketHandleImpl::Context |
+ : public WebSocketHandleDelegate { |
+ public: |
+ explicit Context(WebSocketHandle* handle) |
+ : handle_(handle), |
+ client_(NULL) {} |
+ virtual ~Context(); |
+ |
+ void Connect(const GURL& url, |
+ const std::vector<std::string>& protocols, |
+ const GURL& origin, |
+ WebSocketHandleClient* client, |
+ WebKitPlatformSupportImpl* platform); |
+ void Send(bool fin, |
+ WebSocketHandleBridge::MessageType type, |
+ const char* data, |
+ size_t size); |
+ void FlowControl(int64_t quota); |
+ void Close(unsigned short code, const std::string& reason); |
+ |
+ // WebSocketHandleDelegate functions. |
+ virtual void DidConnect(bool fail, |
+ const std::string& protocol, |
+ const std::string& extensions) OVERRIDE; |
+ virtual void DidReceiveData(bool fin, |
+ WebSocketHandleBridge::MessageType type, |
+ const char* data, |
+ size_t size) OVERRIDE; |
+ virtual void DidReceiveFlowControl(int64_t quota) OVERRIDE; |
+ virtual void DidClose(unsigned short code, |
+ const std::string& reason) OVERRIDE; |
+ |
+ private: |
+ WebSocketHandle* handle_; |
+ WebSocketHandleClient* client_; |
+ scoped_refptr<WebSocketHandleBridge> bridge_; |
+}; |
+ |
+void WebSocketHandleImpl::Context::Connect( |
+ const GURL& url, |
+ const std::vector<std::string>& protocols, |
+ const GURL& origin, |
+ WebSocketHandleClient* client, |
+ WebKitPlatformSupportImpl* platform) { |
+ DCHECK(!bridge_); |
+ DCHECK(client); |
+ client_ = client; |
+ bridge_ = platform->CreateWebSocketBridge(this); |
+ bridge_->Connect(url, protocols, origin, this); |
+} |
+ |
+WebSocketHandleImpl::Context::~Context() { |
+ if (bridge_) |
+ bridge_->Disconnect(); |
+} |
+ |
+void WebSocketHandleImpl::Context::Send(bool fin, |
+ WebSocketHandleBridge::MessageType type, |
+ const char* data, |
+ size_t size) { |
+ DCHECK(bridge_); |
+ bridge_->Send(fin, type, data, size); |
+} |
+ |
+void WebSocketHandleImpl::Context::FlowControl(int64_t quota) { |
+ DCHECK(bridge_); |
+ bridge_->FlowControl(quota); |
+} |
+ |
+void WebSocketHandleImpl::Context::Close(unsigned short code, |
+ const std::string& reason) { |
+ DCHECK(bridge_); |
+ bridge_->Close(code, reason); |
+} |
+ |
+void WebSocketHandleImpl::Context::DidConnect(bool fail, |
+ const std::string& protocol, |
+ const std::string& extensions) { |
+ DCHECK(client_); |
+ WebString protocol_to_pass = WebString::fromUTF8(protocol); |
+ WebString extensions_to_pass = WebString::fromUTF8(extensions); |
+ client_->didConnect(handle_, fail, protocol_to_pass, extensions_to_pass); |
+} |
+ |
+void WebSocketHandleImpl::Context::DidReceiveData( |
+ bool fin, |
+ WebSocketHandleBridge::MessageType type, |
+ const char* data, |
+ size_t size) { |
+ DCHECK(client_); |
+ WebSocketHandle::MessageType type_to_pass; |
+ switch (type) { |
+ case WebSocketHandleBridge::MESSAGE_TYPE_CONTINUATION: |
+ type_to_pass = WebSocketHandle::MessageTypeContinuation; |
+ break; |
+ case WebSocketHandleBridge::MESSAGE_TYPE_TEXT: |
+ type_to_pass = WebSocketHandle::MessageTypeText; |
+ break; |
+ case WebSocketHandleBridge::MESSAGE_TYPE_BINARY: |
+ type_to_pass = WebSocketHandle::MessageTypeBinary; |
+ break; |
+ default: |
+ NOTREACHED(); |
+ break; |
+ } |
+ |
+ client_->didReceiveData(handle_, fin, type_to_pass, data, size); |
+} |
+ |
+void WebSocketHandleImpl::Context::DidReceiveFlowControl(int64_t quota) { |
+ DCHECK(client_); |
+ client_->didReceiveFlowControl(handle_, quota); |
+} |
+ |
+void WebSocketHandleImpl::Context::DidClose(unsigned short code, |
+ const std::string& reason) { |
+ DCHECK(client_); |
+ WebString reason_to_pass = WebString::fromUTF8(reason); |
+ client_->didClose(handle_, code, reason_to_pass); |
+ if (bridge_) { |
+ bridge_->Disconnect(); |
+ client_ = NULL; |
+ bridge_ = NULL; |
+ } |
+} |
+ |
+WebSocketHandleImpl::WebSocketHandleImpl(WebKitPlatformSupportImpl* platform) |
+ : context_(new Context(this)), |
+ platform_(platform) { |
+} |
+ |
+WebSocketHandleImpl::~WebSocketHandleImpl() {} |
+ |
+void WebSocketHandleImpl::connect(const WebURL& url, |
+ const WebVector<WebString>& protocols, |
+ const WebString& origin, |
+ WebSocketHandleClient* client) { |
+ std::vector<std::string> protocols_to_pass; |
+ for (size_t i = 0; i < protocols.size(); ++i) |
+ protocols_to_pass.push_back(protocols[i].utf8()); |
+ GURL origin_to_pass(origin.utf8()); |
+ context_->Connect(url, protocols_to_pass, origin_to_pass, client, platform_); |
+} |
+ |
+void WebSocketHandleImpl::send(bool fin, |
+ WebSocketHandle::MessageType type, |
+ const char* data, |
+ size_t size) { |
+ WebSocketHandleBridge::MessageType type_to_pass; |
+ switch (type) { |
+ case WebSocketHandle::MessageTypeContinuation: |
+ type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_CONTINUATION; |
+ break; |
+ case WebSocketHandle::MessageTypeText: |
+ type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_TEXT; |
+ break; |
+ case WebSocketHandle::MessageTypeBinary: |
+ type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_BINARY; |
+ break; |
+ default: |
+ NOTREACHED(); |
+ break; |
+ } |
+ context_->Send(fin, type_to_pass, data, size); |
+} |
+ |
+void WebSocketHandleImpl::flowControl(int64_t quota) { |
+ context_->FlowControl(quota); |
+} |
+ |
+void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) { |
+ std::string reason_to_pass = reason.utf8(); |
+ context_->Close(code, reason_to_pass); |
+} |
+ |
+} // namespace webkit_glue |