Commit 2cbe08da authored by mpcomplete's avatar mpcomplete Committed by Commit bot

Mojo: Implement the rest of the WebSocket interface.

Also moved some generic TypeConverters to a separate file.

BUG=403930

Review URL: https://codereview.chromium.org/540683002

Cr-Commit-Position: refs/heads/master@{#293801}
parent 885a59ee
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
'third_party/WebKit' 'third_party/WebKit'
], ],
'sources': [ 'sources': [
'services/html_viewer/blink_basic_type_converters.cc',
'services/html_viewer/blink_basic_type_converters.h',
'services/html_viewer/blink_input_events_type_converters.cc', 'services/html_viewer/blink_input_events_type_converters.cc',
'services/html_viewer/blink_input_events_type_converters.h', 'services/html_viewer/blink_input_events_type_converters.h',
'services/html_viewer/blink_platform_impl.cc', 'services/html_viewer/blink_platform_impl.cc',
......
// Copyright 2014 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 "mojo/services/html_viewer/blink_basic_type_converters.h"
#include "mojo/public/cpp/bindings/string.h"
#include "third_party/WebKit/public/platform/WebString.h"
using blink::WebString;
namespace mojo {
// static
String TypeConverter<String, WebString>::Convert(const WebString& str) {
return String(str.utf8());
}
// static
WebString TypeConverter<WebString, String>::Convert(const String& str) {
return WebString::fromUTF8(str.get());
}
} // namespace mojo
// Copyright 2014 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.
#ifndef MOJO_SERVICES_HTML_VIEWER_BLINK_BASIC_TYPE_CONVERTERS_H_
#define MOJO_SERVICES_HTML_VIEWER_BLINK_BASIC_TYPE_CONVERTERS_H_
#include "mojo/public/cpp/bindings/type_converter.h"
#include "mojo/public/cpp/bindings/array.h"
#include "third_party/WebKit/public/platform/WebVector.h"
namespace blink {
class WebString;
}
namespace mojo {
class String;
template<>
struct TypeConverter<String, blink::WebString> {
static String Convert(const blink::WebString& str);
};
template<>
struct TypeConverter<blink::WebString, String> {
static blink::WebString Convert(const String& str);
};
template<typename T, typename U>
struct TypeConverter<Array<T>, blink::WebVector<U> > {
static Array<T> Convert(const blink::WebVector<U>& vector) {
Array<T> array(vector.size());
for (size_t i = 0; i < vector.size(); ++i)
array[i] = TypeConverter<T, U>::Convert(vector[i]);
return array.Pass();
}
};
} // namespace mojo
#endif // MOJO_SERVICES_HTML_VIEWER_BLINK_BASIC_TYPE_CONVERTERS_H_
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <vector> #include <vector>
#include "mojo/services/html_viewer/blink_basic_type_converters.h"
#include "mojo/services/public/interfaces/network/network_service.mojom.h" #include "mojo/services/public/interfaces/network/network_service.mojom.h"
#include "third_party/WebKit/public/platform/WebSerializedOrigin.h" #include "third_party/WebKit/public/platform/WebSerializedOrigin.h"
#include "third_party/WebKit/public/platform/WebSocketHandleClient.h" #include "third_party/WebKit/public/platform/WebSocketHandleClient.h"
...@@ -22,29 +23,6 @@ using blink::WebVector; ...@@ -22,29 +23,6 @@ using blink::WebVector;
namespace mojo { namespace mojo {
template<>
struct TypeConverter<String, WebString> {
static String Convert(const WebString& str) {
return String(str.utf8());
}
};
template<>
struct TypeConverter<WebString, String> {
static WebString Convert(const String& str) {
return WebString::fromUTF8(str.get());
}
};
template<typename T, typename U>
struct TypeConverter<Array<T>, WebVector<U> > {
static Array<T> Convert(const WebVector<U>& vector) {
Array<T> array(vector.size());
for (size_t i = 0; i < vector.size(); ++i)
array[i] = TypeConverter<T, U>::Convert(vector[i]);
return array.Pass();
}
};
template<> template<>
struct TypeConverter<WebSocket::MessageType, WebSocketHandle::MessageType> { struct TypeConverter<WebSocket::MessageType, WebSocketHandle::MessageType> {
static WebSocket::MessageType Convert(WebSocketHandle::MessageType type) { static WebSocket::MessageType Convert(WebSocketHandle::MessageType type) {
...@@ -93,11 +71,17 @@ class WebSocketClientImpl : public InterfaceImpl<WebSocketClient> { ...@@ -93,11 +71,17 @@ class WebSocketClientImpl : public InterfaceImpl<WebSocketClient> {
bool fail, bool fail,
const String& selected_subprotocol, const String& selected_subprotocol,
const String& extensions) OVERRIDE { const String& extensions) OVERRIDE {
client_->didConnect(handle_, blink::WebSocketHandleClient* client = client_;
fail, WebSocketHandleImpl* handle = handle_;
selected_subprotocol.To<WebString>(), if (fail)
extensions.To<WebString>()); handle->Disconnect(); // deletes |this|
client->didConnect(handle,
fail,
selected_subprotocol.To<WebString>(),
extensions.To<WebString>());
// |handle| can be deleted here.
} }
virtual void DidReceiveData(bool fin, virtual void DidReceiveData(bool fin,
WebSocket::MessageType type, WebSocket::MessageType type,
ScopedDataPipeConsumerHandle data_pipe) OVERRIDE { ScopedDataPipeConsumerHandle data_pipe) OVERRIDE {
...@@ -112,11 +96,30 @@ class WebSocketClientImpl : public InterfaceImpl<WebSocketClient> { ...@@ -112,11 +96,30 @@ class WebSocketClientImpl : public InterfaceImpl<WebSocketClient> {
ConvertTo<WebSocketHandle::MessageType>(type), ConvertTo<WebSocketHandle::MessageType>(type),
data_ptr, data_ptr,
data.size()); data.size());
// |handle| can be deleted here.
} }
virtual void DidReceiveFlowControl(int64_t quota) OVERRIDE { virtual void DidReceiveFlowControl(int64_t quota) OVERRIDE {
client_->didReceiveFlowControl(handle_, quota); client_->didReceiveFlowControl(handle_, quota);
// |handle_| can be deleted here. // |handle| can be deleted here.
}
virtual void DidFail(const String& message) OVERRIDE {
blink::WebSocketHandleClient* client = client_;
WebSocketHandleImpl* handle = handle_;
handle->Disconnect(); // deletes |this|
client->didFail(handle, message.To<WebString>());
// |handle| can be deleted here.
}
virtual void DidClose(bool was_clean,
uint16_t code,
const String& reason) OVERRIDE {
blink::WebSocketHandleClient* client = client_;
WebSocketHandleImpl* handle = handle_;
handle->Disconnect(); // deletes |this|
client->didClose(handle, was_clean, code, reason.To<WebString>());
// |handle| can be deleted here.
} }
WebSocketHandleImpl* handle_; WebSocketHandleImpl* handle_;
...@@ -186,8 +189,12 @@ void WebSocketHandleImpl::flowControl(int64_t quota) { ...@@ -186,8 +189,12 @@ void WebSocketHandleImpl::flowControl(int64_t quota) {
} }
void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) { void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) {
did_close_ = true;
web_socket_->Close(code, reason.utf8()); web_socket_->Close(code, reason.utf8());
} }
void WebSocketHandleImpl::Disconnect() {
did_close_ = true;
client_.reset();
}
} // namespace mojo } // namespace mojo
...@@ -21,6 +21,8 @@ class WebSocketHandleImpl : public blink::WebSocketHandle { ...@@ -21,6 +21,8 @@ class WebSocketHandleImpl : public blink::WebSocketHandle {
explicit WebSocketHandleImpl(NetworkService* network_service); explicit WebSocketHandleImpl(NetworkService* network_service);
private: private:
friend class WebSocketClientImpl;
virtual ~WebSocketHandleImpl(); virtual ~WebSocketHandleImpl();
// blink::WebSocketHandle methods: // blink::WebSocketHandle methods:
...@@ -36,6 +38,9 @@ class WebSocketHandleImpl : public blink::WebSocketHandle { ...@@ -36,6 +38,9 @@ class WebSocketHandleImpl : public blink::WebSocketHandle {
virtual void close(unsigned short code, virtual void close(unsigned short code,
const blink::WebString& reason) OVERRIDE; const blink::WebString& reason) OVERRIDE;
// Called when the socket is closed.
void Disconnect();
WebSocketPtr web_socket_; WebSocketPtr web_socket_;
scoped_ptr<WebSocketClientImpl> client_; scoped_ptr<WebSocketClientImpl> client_;
// True if close() was called. // True if close() was called.
......
...@@ -98,6 +98,8 @@ ChannelState WebSocketEventHandler::OnAddChannelResponse( ...@@ -98,6 +98,8 @@ ChannelState WebSocketEventHandler::OnAddChannelResponse(
const std::string& selected_protocol, const std::string& selected_protocol,
const std::string& extensions) { const std::string& extensions) {
client_->DidConnect(fail, selected_protocol, extensions); client_->DidConnect(fail, selected_protocol, extensions);
if (fail)
return WebSocketEventInterface::CHANNEL_DELETED;
return WebSocketEventInterface::CHANNEL_ALIVE; return WebSocketEventInterface::CHANNEL_ALIVE;
} }
...@@ -132,13 +134,15 @@ ChannelState WebSocketEventHandler::OnFlowControl(int64 quota) { ...@@ -132,13 +134,15 @@ ChannelState WebSocketEventHandler::OnFlowControl(int64 quota) {
} }
ChannelState WebSocketEventHandler::OnDropChannel(bool was_clean, ChannelState WebSocketEventHandler::OnDropChannel(bool was_clean,
uint16 code, uint16 code,
const std::string& reason) { const std::string& reason) {
return WebSocketEventInterface::CHANNEL_ALIVE; client_->DidClose(was_clean, code, reason);
return WebSocketEventInterface::CHANNEL_DELETED;
} }
ChannelState WebSocketEventHandler::OnFailChannel(const std::string& message) { ChannelState WebSocketEventHandler::OnFailChannel(const std::string& message) {
return WebSocketEventInterface::CHANNEL_ALIVE; client_->DidFail(message);
return WebSocketEventInterface::CHANNEL_DELETED;
} }
ChannelState WebSocketEventHandler::OnStartOpeningHandshake( ChannelState WebSocketEventHandler::OnStartOpeningHandshake(
...@@ -156,8 +160,8 @@ ChannelState WebSocketEventHandler::OnSSLCertificateError( ...@@ -156,8 +160,8 @@ ChannelState WebSocketEventHandler::OnSSLCertificateError(
const GURL& url, const GURL& url,
const net::SSLInfo& ssl_info, const net::SSLInfo& ssl_info,
bool fatal) { bool fatal) {
// The above method is always asynchronous. client_->DidFail("SSL Error");
return WebSocketEventInterface::CHANNEL_ALIVE; return WebSocketEventInterface::CHANNEL_DELETED;
} }
} // namespace mojo } // namespace mojo
...@@ -199,10 +203,9 @@ void WebSocketImpl::FlowControl(int64_t quota) { ...@@ -199,10 +203,9 @@ void WebSocketImpl::FlowControl(int64_t quota) {
channel_->SendFlowControl(quota); channel_->SendFlowControl(quota);
} }
void WebSocketImpl::Close(int16_t code, const String& reason) { void WebSocketImpl::Close(uint16_t code, const String& reason) {
DCHECK(channel_); DCHECK(channel_);
channel_->StartClosingHandshake(code, reason); channel_->StartClosingHandshake(code, reason);
} }
} // namespace mojo } // namespace mojo
...@@ -37,7 +37,7 @@ class WebSocketImpl : public InterfaceImpl<WebSocket> { ...@@ -37,7 +37,7 @@ class WebSocketImpl : public InterfaceImpl<WebSocket> {
WebSocket::MessageType type, WebSocket::MessageType type,
ScopedDataPipeConsumerHandle data) OVERRIDE; ScopedDataPipeConsumerHandle data) OVERRIDE;
virtual void FlowControl(int64_t quota) OVERRIDE; virtual void FlowControl(int64_t quota) OVERRIDE;
virtual void Close(int16_t code, const String& reason) OVERRIDE; virtual void Close(uint16_t code, const String& reason) OVERRIDE;
// The channel we use to send events to the network. // The channel we use to send events to the network.
scoped_ptr<net::WebSocketChannel> channel_; scoped_ptr<net::WebSocketChannel> channel_;
......
...@@ -12,13 +12,13 @@ interface WebSocket { ...@@ -12,13 +12,13 @@ interface WebSocket {
TEXT, TEXT,
BINARY BINARY
}; };
const int16 kAbnormalCloseCode = 1006; // stolen from websocket_bridge const uint16 kAbnormalCloseCode = 1006; // stolen from websocket_bridge
Connect( Connect(
string url, string[] protocols, string origin, WebSocketClient client); string url, string[] protocols, string origin, WebSocketClient client);
Send(bool fin, MessageType type, handle<data_pipe_consumer> data); Send(bool fin, MessageType type, handle<data_pipe_consumer> data);
FlowControl(int64 quota); FlowControl(int64 quota);
Close(int16 code, string reason); Close(uint16 code, string reason);
}; };
interface WebSocketClient { interface WebSocketClient {
...@@ -26,21 +26,14 @@ interface WebSocketClient { ...@@ -26,21 +26,14 @@ interface WebSocketClient {
DidReceiveData( DidReceiveData(
bool fin, WebSocket.MessageType type, handle<data_pipe_consumer> data); bool fin, WebSocket.MessageType type, handle<data_pipe_consumer> data);
DidReceiveFlowControl(int64 quota); DidReceiveFlowControl(int64 quota);
DidFail(string message);
DidClose(bool was_clean, uint16 code, string reason);
// TODO(mpcomplete): add these methods from blink: // Blink has 3 extra methods that we don't implement, because they are used
// void didStartOpeningHandshake(WebSocketHandle*, const // for the inspector:
// WebSocketHandshakeRequestInfo&) = 0; // didStartOpeningHandshake
// // didFinishOpeningHandshake
// void didFinishOpeningHandshake(WebSocketHandle*, const // didStartClosingHandshake
// WebSocketHandshakeResponseInfo&) = 0;
//
// void didFail(WebSocketHandle* /* handle */, const WebString&
// message) = 0;
//
// void didClose(WebSocketHandle* /* handle */, bool wasClean,
// unsigned short code, const WebString& reason) = 0;
//
// void didStartClosingHandshake(WebSocketHandle*) = 0;
}; };
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment