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" | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
not used?
yhirano
2013/09/03 09:12:08
DCHECK is defined in the file.
| |
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(bool fin, | |
46 WebSocketHandleBridge::MessageType type, | |
47 const char* data, | |
48 size_t size); | |
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 fail, | |
54 const std::string& protocol, | |
55 const std::string& extensions) OVERRIDE; | |
56 virtual void DidReceiveData(bool fin, | |
57 WebSocketHandleBridge::MessageType type, | |
58 const char* data, | |
59 size_t size) 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); | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:58:10
create bridge_. (told offline)
yhirano
2013/09/03 09:12:08
Done.
| |
82 } | |
83 | |
84 WebSocketHandleImpl::Context::~Context() { | |
85 if (bridge_) { | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
Remove {}?
yhirano
2013/09/03 09:12:08
Done.
| |
86 bridge_->Disconnect(); | |
87 } | |
88 } | |
89 | |
90 void WebSocketHandleImpl::Context::Send(bool fin, | |
91 WebSocketHandleBridge::MessageType type, | |
92 const char* data, | |
93 size_t size) { | |
94 if (!is_active_) { | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
ditto
yhirano
2013/09/03 09:12:08
Done.
| |
95 return; | |
96 } | |
97 bridge_->Send(fin, type, data, size); | |
98 } | |
99 | |
100 void WebSocketHandleImpl::Context::FlowControl(int64_t quota) { | |
101 if (!is_active_) { | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
ditto
yhirano
2013/09/03 09:12:08
Done.
| |
102 return; | |
103 } | |
104 bridge_->FlowControl(quota); | |
105 } | |
106 | |
107 void WebSocketHandleImpl::Context::Close(unsigned short code, | |
108 const std::string& reason) { | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
indent
yhirano
2013/09/03 09:12:08
Done.
| |
109 if (!is_active_) { | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
ditto
yhirano
2013/09/03 09:12:08
Done.
| |
110 return; | |
111 } | |
112 bridge_->Close(code, reason); | |
113 } | |
114 | |
115 void WebSocketHandleImpl::Context::DidConnect(bool fail, | |
116 const std::string& protocol, | |
117 const std::string& extensions) { | |
118 if (!is_active_) { | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
Remove {}?
yhirano
2013/09/03 09:12:08
Done.
| |
119 return; | |
120 } | |
121 WebString protocol_to_pass = WebString::fromUTF8(protocol); | |
122 WebString extensions_to_pass = WebString::fromUTF8(extensions); | |
123 client_->didConnect(handle_, fail, protocol_to_pass, extensions_to_pass); | |
124 } | |
125 | |
126 void WebSocketHandleImpl::Context::DidReceiveData( | |
127 bool fin, | |
128 WebSocketHandleBridge::MessageType type, | |
129 const char* data, | |
130 size_t size) { | |
131 if (!is_active_) { | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
ditto
yhirano
2013/09/03 09:12:08
Done.
| |
132 return; | |
133 } | |
134 WebSocketHandle::MessageType type_to_pass; | |
135 switch (type) { | |
136 case WebSocketHandleBridge::MESSAGE_TYPE_CONTINUATION: | |
137 type_to_pass = WebSocketHandle::MessageTypeContinuation; | |
138 break; | |
139 case WebSocketHandleBridge::MESSAGE_TYPE_TEXT: | |
140 type_to_pass = WebSocketHandle::MessageTypeText; | |
141 break; | |
142 case WebSocketHandleBridge::MESSAGE_TYPE_BINARY: | |
143 type_to_pass = WebSocketHandle::MessageTypeBinary; | |
144 break; | |
145 default: | |
146 NOTREACHED(); | |
147 break; | |
148 } | |
149 | |
150 client_->didReceiveData(handle_, fin, type_to_pass, data, size); | |
151 } | |
152 | |
153 void WebSocketHandleImpl::Context::DidReceiveFlowControl(int64_t quota) { | |
154 if (!is_active_) { | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
ditto
yhirano
2013/09/03 09:12:08
Done.
| |
155 return; | |
156 } | |
157 client_->didReceiveFlowControl(handle_, quota); | |
158 } | |
159 | |
160 void WebSocketHandleImpl::Context::DidClose(unsigned short code, | |
161 const std::string& reason) { | |
162 if (!is_active_) { | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
ditto
tyoshino (SeeGerritForStatus)
2013/09/03 07:58:10
unset is_active_? (told offline)
yhirano
2013/09/03 09:12:08
is_active_ can be replaced by (bridge_ != NULL) an
| |
163 return; | |
164 } | |
165 WebString reason_to_pass = WebString::fromUTF8(reason); | |
166 client_->didClose(handle_, code, reason_to_pass); | |
167 } | |
168 | |
169 WebSocketHandleImpl::WebSocketHandleImpl(WebKitPlatformSupportImpl* platform) | |
170 : context_(new Context(this)), | |
171 platform_(platform) { | |
172 } | |
173 | |
174 WebSocketHandleImpl::~WebSocketHandleImpl() {} | |
175 | |
176 void WebSocketHandleImpl::connect(const WebURL& url, | |
177 const WebVector<WebString>& protocols, | |
178 const WebString& origin, | |
179 WebSocketHandleClient* client) { | |
180 std::vector<std::string> protocols_to_pass; | |
181 for (size_t i = 0; i < protocols.size(); ++i) { | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
ditto
yhirano
2013/09/03 09:12:08
Done.
| |
182 protocols_to_pass.push_back(protocols[i].utf8()); | |
183 } | |
184 GURL origin_to_pass(origin.utf8()); | |
tyoshino (SeeGerritForStatus)
2013/09/03 07:18:30
just use WebURL's operator GURL()?
tyoshino (SeeGerritForStatus)
2013/09/03 07:58:10
I was wrong. Never mind. (told offline)
| |
185 context_->Connect(url, protocols_to_pass, origin_to_pass, client); | |
186 } | |
187 | |
188 void WebSocketHandleImpl::send(bool fin, | |
189 WebSocketHandle::MessageType type, | |
190 const char* data, | |
191 size_t size) { | |
192 WebSocketHandleBridge::MessageType type_to_pass; | |
193 switch (type) { | |
194 case WebSocketHandle::MessageTypeContinuation: | |
195 type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_CONTINUATION; | |
196 break; | |
197 case WebSocketHandle::MessageTypeText: | |
198 type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_TEXT; | |
199 break; | |
200 case WebSocketHandle::MessageTypeBinary: | |
201 type_to_pass = WebSocketHandleBridge::MESSAGE_TYPE_BINARY; | |
202 break; | |
203 default: | |
204 NOTREACHED(); | |
205 break; | |
206 } | |
207 context_->Send(fin, type_to_pass, data, size); | |
208 } | |
209 | |
210 void WebSocketHandleImpl::flowControl(int64_t quota) { | |
211 context_->FlowControl(quota); | |
212 } | |
213 | |
214 void WebSocketHandleImpl::close(unsigned short code, const WebString& reason) { | |
215 std::string reason_to_pass = reason.utf8(); | |
216 context_->Close(code, reason_to_pass); | |
217 } | |
218 | |
219 } // namespace webkit_glue | |
OLD | NEW |