Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Side by Side Diff: mojo/services/html_viewer/websockethandle_impl.cc

Issue 515923003: Mojo: Add a WebSocket/Client interface and hook it up to the HTML viewer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/services/html_viewer/websockethandle_impl.h ('k') | mojo/services/network/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "mojo/services/html_viewer/websockethandle_impl.h"
6
7 #include <vector>
8
9 #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/WebSocketHandleClient.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
13 #include "third_party/WebKit/public/platform/WebURL.h"
14 #include "third_party/WebKit/public/platform/WebVector.h"
15
16 using blink::WebSerializedOrigin;
17 using blink::WebSocketHandle;
18 using blink::WebSocketHandleClient;
19 using blink::WebString;
20 using blink::WebURL;
21 using blink::WebVector;
22
23 namespace mojo {
24
25 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> {
50 static WebSocket::MessageType Convert(WebSocketHandle::MessageType type) {
51 DCHECK(type == WebSocketHandle::MessageTypeContinuation ||
52 type == WebSocketHandle::MessageTypeText ||
53 type == WebSocketHandle::MessageTypeBinary);
54 typedef WebSocket::MessageType MessageType;
55 COMPILE_ASSERT(
56 static_cast<MessageType>(WebSocketHandle::MessageTypeContinuation) ==
57 WebSocket::MESSAGE_TYPE_CONTINUATION,
58 enum_values_must_match_for_message_type);
59 COMPILE_ASSERT(
60 static_cast<MessageType>(WebSocketHandle::MessageTypeText) ==
61 WebSocket::MESSAGE_TYPE_TEXT,
62 enum_values_must_match_for_message_type);
63 COMPILE_ASSERT(
64 static_cast<MessageType>(WebSocketHandle::MessageTypeBinary) ==
65 WebSocket::MESSAGE_TYPE_BINARY,
66 enum_values_must_match_for_message_type);
67 return static_cast<WebSocket::MessageType>(type);
68 }
69 };
70
71 template<>
72 struct TypeConverter<WebSocketHandle::MessageType, WebSocket::MessageType> {
73 static WebSocketHandle::MessageType Convert(WebSocket::MessageType type) {
74 DCHECK(type == WebSocket::MESSAGE_TYPE_CONTINUATION ||
75 type == WebSocket::MESSAGE_TYPE_TEXT ||
76 type == WebSocket::MESSAGE_TYPE_BINARY);
77 return static_cast<WebSocketHandle::MessageType>(type);
78 }
79 };
80
81 // This class forms a bridge from the mojo WebSocketClient interface and the
82 // Blink WebSocketHandleClient interface.
83 class WebSocketClientImpl : public InterfaceImpl<WebSocketClient> {
84 public:
85 explicit WebSocketClientImpl(WebSocketHandleImpl* handle,
86 blink::WebSocketHandleClient* client)
87 : handle_(handle), client_(client) {}
88 virtual ~WebSocketClientImpl() {}
89
90 private:
91 // WebSocketClient methods:
92 virtual void DidConnect(
93 bool fail,
94 const String& selected_subprotocol,
95 const String& extensions) OVERRIDE {
96 client_->didConnect(handle_,
97 fail,
98 selected_subprotocol.To<WebString>(),
99 extensions.To<WebString>());
100 }
101 virtual void DidReceiveData(bool fin,
102 WebSocket::MessageType type,
103 ScopedDataPipeConsumerHandle data_pipe) OVERRIDE {
104 uint32_t num_bytes;
105 ReadDataRaw(data_pipe.get(), NULL, &num_bytes, MOJO_READ_DATA_FLAG_QUERY);
106 std::vector<char> data(num_bytes);
107 ReadDataRaw(
108 data_pipe.get(), &data[0], &num_bytes, MOJO_READ_DATA_FLAG_NONE);
109 const char* data_ptr = data.empty() ? NULL : &data[0];
110 client_->didReceiveData(handle_,
111 fin,
112 ConvertTo<WebSocketHandle::MessageType>(type),
113 data_ptr,
114 data.size());
115 }
116
117 virtual void DidReceiveFlowControl(int64_t quota) OVERRIDE {
118 client_->didReceiveFlowControl(handle_, quota);
119 // |handle_| can be deleted here.
120 }
121
122 WebSocketHandleImpl* handle_;
123 blink::WebSocketHandleClient* client_;
124
125 DISALLOW_COPY_AND_ASSIGN(WebSocketClientImpl);
126 };
127
128 WebSocketHandleImpl::WebSocketHandleImpl(NetworkService* network_service)
129 : did_close_(false) {
130 network_service->CreateWebSocket(Get(&web_socket_));
131 }
132
133 WebSocketHandleImpl::~WebSocketHandleImpl() {
134 if (!did_close_) {
135 // The connection is abruptly disconnected by the renderer without
136 // closing handshake.
137 web_socket_->Close(WebSocket::kAbnormalCloseCode, String());
138 }
139 }
140
141 void WebSocketHandleImpl::connect(const WebURL& url,
142 const WebVector<WebString>& protocols,
143 const WebSerializedOrigin& origin,
144 WebSocketHandleClient* client) {
145 client_.reset(new WebSocketClientImpl(this, client));
146 WebSocketClientPtr client_ptr;
147 // TODO(mpcomplete): Is this the right ownership model? Or should mojo own
148 // |client_|?
149 WeakBindToProxy(client_.get(), &client_ptr);
150 web_socket_->Connect(url.string().utf8(),
151 Array<String>::From(protocols),
152 origin.string().utf8(),
153 client_ptr.Pass());
154 }
155
156 void WebSocketHandleImpl::send(bool fin,
157 WebSocketHandle::MessageType type,
158 const char* data,
159 size_t size) {
160 if (!client_)
161 return;
162
163 // TODO(mpcomplete): reuse the data pipe for subsequent sends.
164 uint32_t num_bytes = static_cast<uint32_t>(size);
165 MojoCreateDataPipeOptions options;
166 options.struct_size = sizeof(MojoCreateDataPipeOptions);
167 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
168 options.element_num_bytes = 1;
169 options.capacity_num_bytes = num_bytes;
170 DataPipe data_pipe(options);
171 WriteDataRaw(data_pipe.producer_handle.get(),
172 data,
173 &num_bytes,
174 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
175 web_socket_->Send(
176 fin,
177 ConvertTo<WebSocket::MessageType>(type),
178 data_pipe.consumer_handle.Pass());
179 }
180
181 void WebSocketHandleImpl::flowControl(int64_t quota) {
182 if (!client_)
183 return;
184
185 web_socket_->FlowControl(quota);
186 }
187
188 void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) {
189 did_close_ = true;
190 web_socket_->Close(code, reason.utf8());
191 }
192
193 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/services/html_viewer/websockethandle_impl.h ('k') | mojo/services/network/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698