OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/services/html_viewer/websockethandle_impl.h" | 5 #include "mojo/services/html_viewer/websockethandle_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
| 9 #include "mojo/services/html_viewer/blink_basic_type_converters.h" |
9 #include "mojo/services/public/interfaces/network/network_service.mojom.h" | 10 #include "mojo/services/public/interfaces/network/network_service.mojom.h" |
10 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h" | 11 #include "third_party/WebKit/public/platform/WebSerializedOrigin.h" |
11 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h" | 12 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h" |
12 #include "third_party/WebKit/public/platform/WebString.h" | 13 #include "third_party/WebKit/public/platform/WebString.h" |
13 #include "third_party/WebKit/public/platform/WebURL.h" | 14 #include "third_party/WebKit/public/platform/WebURL.h" |
14 #include "third_party/WebKit/public/platform/WebVector.h" | 15 #include "third_party/WebKit/public/platform/WebVector.h" |
15 | 16 |
16 using blink::WebSerializedOrigin; | 17 using blink::WebSerializedOrigin; |
17 using blink::WebSocketHandle; | 18 using blink::WebSocketHandle; |
18 using blink::WebSocketHandleClient; | 19 using blink::WebSocketHandleClient; |
19 using blink::WebString; | 20 using blink::WebString; |
20 using blink::WebURL; | 21 using blink::WebURL; |
21 using blink::WebVector; | 22 using blink::WebVector; |
22 | 23 |
23 namespace mojo { | 24 namespace mojo { |
24 | 25 |
25 template<> | 26 template<> |
26 struct TypeConverter<String, WebString> { | |
27 static String Convert(const WebString& str) { | |
28 return String(str.utf8()); | |
29 } | |
30 }; | |
31 template<> | |
32 struct TypeConverter<WebString, String> { | |
33 static WebString Convert(const String& str) { | |
34 return WebString::fromUTF8(str.get()); | |
35 } | |
36 }; | |
37 | |
38 template<typename T, typename U> | |
39 struct TypeConverter<Array<T>, WebVector<U> > { | |
40 static Array<T> Convert(const WebVector<U>& vector) { | |
41 Array<T> array(vector.size()); | |
42 for (size_t i = 0; i < vector.size(); ++i) | |
43 array[i] = TypeConverter<T, U>::Convert(vector[i]); | |
44 return array.Pass(); | |
45 } | |
46 }; | |
47 | |
48 template<> | |
49 struct TypeConverter<WebSocket::MessageType, WebSocketHandle::MessageType> { | 27 struct TypeConverter<WebSocket::MessageType, WebSocketHandle::MessageType> { |
50 static WebSocket::MessageType Convert(WebSocketHandle::MessageType type) { | 28 static WebSocket::MessageType Convert(WebSocketHandle::MessageType type) { |
51 DCHECK(type == WebSocketHandle::MessageTypeContinuation || | 29 DCHECK(type == WebSocketHandle::MessageTypeContinuation || |
52 type == WebSocketHandle::MessageTypeText || | 30 type == WebSocketHandle::MessageTypeText || |
53 type == WebSocketHandle::MessageTypeBinary); | 31 type == WebSocketHandle::MessageTypeBinary); |
54 typedef WebSocket::MessageType MessageType; | 32 typedef WebSocket::MessageType MessageType; |
55 COMPILE_ASSERT( | 33 COMPILE_ASSERT( |
56 static_cast<MessageType>(WebSocketHandle::MessageTypeContinuation) == | 34 static_cast<MessageType>(WebSocketHandle::MessageTypeContinuation) == |
57 WebSocket::MESSAGE_TYPE_CONTINUATION, | 35 WebSocket::MESSAGE_TYPE_CONTINUATION, |
58 enum_values_must_match_for_message_type); | 36 enum_values_must_match_for_message_type); |
(...skipping 27 matching lines...) Expand all Loading... |
86 blink::WebSocketHandleClient* client) | 64 blink::WebSocketHandleClient* client) |
87 : handle_(handle), client_(client) {} | 65 : handle_(handle), client_(client) {} |
88 virtual ~WebSocketClientImpl() {} | 66 virtual ~WebSocketClientImpl() {} |
89 | 67 |
90 private: | 68 private: |
91 // WebSocketClient methods: | 69 // WebSocketClient methods: |
92 virtual void DidConnect( | 70 virtual void DidConnect( |
93 bool fail, | 71 bool fail, |
94 const String& selected_subprotocol, | 72 const String& selected_subprotocol, |
95 const String& extensions) OVERRIDE { | 73 const String& extensions) OVERRIDE { |
96 client_->didConnect(handle_, | 74 blink::WebSocketHandleClient* client = client_; |
97 fail, | 75 WebSocketHandleImpl* handle = handle_; |
98 selected_subprotocol.To<WebString>(), | 76 if (fail) |
99 extensions.To<WebString>()); | 77 handle->Disconnect(); // deletes |this| |
| 78 client->didConnect(handle, |
| 79 fail, |
| 80 selected_subprotocol.To<WebString>(), |
| 81 extensions.To<WebString>()); |
| 82 // |handle| can be deleted here. |
100 } | 83 } |
| 84 |
101 virtual void DidReceiveData(bool fin, | 85 virtual void DidReceiveData(bool fin, |
102 WebSocket::MessageType type, | 86 WebSocket::MessageType type, |
103 ScopedDataPipeConsumerHandle data_pipe) OVERRIDE { | 87 ScopedDataPipeConsumerHandle data_pipe) OVERRIDE { |
104 uint32_t num_bytes; | 88 uint32_t num_bytes; |
105 ReadDataRaw(data_pipe.get(), NULL, &num_bytes, MOJO_READ_DATA_FLAG_QUERY); | 89 ReadDataRaw(data_pipe.get(), NULL, &num_bytes, MOJO_READ_DATA_FLAG_QUERY); |
106 std::vector<char> data(num_bytes); | 90 std::vector<char> data(num_bytes); |
107 ReadDataRaw( | 91 ReadDataRaw( |
108 data_pipe.get(), &data[0], &num_bytes, MOJO_READ_DATA_FLAG_NONE); | 92 data_pipe.get(), &data[0], &num_bytes, MOJO_READ_DATA_FLAG_NONE); |
109 const char* data_ptr = data.empty() ? NULL : &data[0]; | 93 const char* data_ptr = data.empty() ? NULL : &data[0]; |
110 client_->didReceiveData(handle_, | 94 client_->didReceiveData(handle_, |
111 fin, | 95 fin, |
112 ConvertTo<WebSocketHandle::MessageType>(type), | 96 ConvertTo<WebSocketHandle::MessageType>(type), |
113 data_ptr, | 97 data_ptr, |
114 data.size()); | 98 data.size()); |
| 99 // |handle| can be deleted here. |
115 } | 100 } |
116 | 101 |
117 virtual void DidReceiveFlowControl(int64_t quota) OVERRIDE { | 102 virtual void DidReceiveFlowControl(int64_t quota) OVERRIDE { |
118 client_->didReceiveFlowControl(handle_, quota); | 103 client_->didReceiveFlowControl(handle_, quota); |
119 // |handle_| can be deleted here. | 104 // |handle| can be deleted here. |
| 105 } |
| 106 |
| 107 virtual void DidFail(const String& message) OVERRIDE { |
| 108 blink::WebSocketHandleClient* client = client_; |
| 109 WebSocketHandleImpl* handle = handle_; |
| 110 handle->Disconnect(); // deletes |this| |
| 111 client->didFail(handle, message.To<WebString>()); |
| 112 // |handle| can be deleted here. |
| 113 } |
| 114 |
| 115 virtual void DidClose(bool was_clean, |
| 116 uint16_t code, |
| 117 const String& reason) OVERRIDE { |
| 118 blink::WebSocketHandleClient* client = client_; |
| 119 WebSocketHandleImpl* handle = handle_; |
| 120 handle->Disconnect(); // deletes |this| |
| 121 client->didClose(handle, was_clean, code, reason.To<WebString>()); |
| 122 // |handle| can be deleted here. |
120 } | 123 } |
121 | 124 |
122 WebSocketHandleImpl* handle_; | 125 WebSocketHandleImpl* handle_; |
123 blink::WebSocketHandleClient* client_; | 126 blink::WebSocketHandleClient* client_; |
124 | 127 |
125 DISALLOW_COPY_AND_ASSIGN(WebSocketClientImpl); | 128 DISALLOW_COPY_AND_ASSIGN(WebSocketClientImpl); |
126 }; | 129 }; |
127 | 130 |
128 WebSocketHandleImpl::WebSocketHandleImpl(NetworkService* network_service) | 131 WebSocketHandleImpl::WebSocketHandleImpl(NetworkService* network_service) |
129 : did_close_(false) { | 132 : did_close_(false) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 } | 182 } |
180 | 183 |
181 void WebSocketHandleImpl::flowControl(int64_t quota) { | 184 void WebSocketHandleImpl::flowControl(int64_t quota) { |
182 if (!client_) | 185 if (!client_) |
183 return; | 186 return; |
184 | 187 |
185 web_socket_->FlowControl(quota); | 188 web_socket_->FlowControl(quota); |
186 } | 189 } |
187 | 190 |
188 void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) { | 191 void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) { |
189 did_close_ = true; | |
190 web_socket_->Close(code, reason.utf8()); | 192 web_socket_->Close(code, reason.utf8()); |
191 } | 193 } |
192 | 194 |
| 195 void WebSocketHandleImpl::Disconnect() { |
| 196 did_close_ = true; |
| 197 client_.reset(); |
| 198 } |
| 199 |
193 } // namespace mojo | 200 } // namespace mojo |
OLD | NEW |