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 "content/child/websocket_bridge.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/strings/string_util.h" | |
12 #include "content/child/child_thread.h" | |
13 #include "content/child/websocket_dispatcher.h" | |
14 #include "content/common/websocket_messages.h" | |
15 #include "ipc/ipc_message.h" | |
16 #include "ipc/ipc_message_macros.h" | |
17 #include "url/gurl.h" | |
18 #include "third_party/WebKit/public/platform/WebSocketHandle.h" | |
19 #include "third_party/WebKit/public/platform/WebSocketHandleClient.h" | |
20 #include "third_party/WebKit/public/platform/WebString.h" | |
21 #include "third_party/WebKit/public/platform/WebURL.h" | |
22 #include "third_party/WebKit/public/platform/WebVector.h" | |
23 | |
24 using WebKit::WebSocketHandle; | |
25 using WebKit::WebSocketHandleClient; | |
26 using WebKit::WebString; | |
27 using WebKit::WebURL; | |
28 using WebKit::WebVector; | |
29 | |
30 namespace content { | |
31 | |
32 WebSocketBridge::WebSocketBridge() | |
33 : channel_id_(kInvalidChannelId), client_(NULL) {} | |
34 | |
35 WebSocketBridge::~WebSocketBridge() { | |
36 Disconnect(); | |
37 } | |
38 | |
39 void WebSocketBridge::DidConnect(bool fail, | |
40 const WebString& selected_protocol, | |
41 const WebString& extensions) { | |
42 if (client_) | |
43 client_->didConnect(this, fail, selected_protocol, extensions); | |
Adam Rice
2013/09/17 14:49:43
I think maybe you need to call Disconnect() if |fa
yhirano
2013/09/18 00:49:30
Done.
| |
44 } | |
45 | |
46 void WebSocketBridge::DidReceiveData( | |
47 bool fin, | |
48 WebSocketHandle::MessageType type, | |
49 const char* data, | |
50 size_t size) { | |
51 if (client_) | |
52 client_->didReceiveData(this, fin, type, data, size); | |
53 } | |
54 | |
55 void WebSocketBridge::DidReceiveFlowControl(int64_t quota) { | |
56 if (client_) | |
57 client_->didReceiveFlowControl(this, quota); | |
58 } | |
59 | |
60 void WebSocketBridge::DidClose(unsigned short code, | |
61 const WebString& reason) { | |
62 if (client_) | |
63 client_->didClose(this, code, reason); | |
64 Disconnect(); | |
65 } | |
66 | |
67 void WebSocketBridge::connect( | |
68 const WebURL& url, | |
69 const WebVector<WebString>& protocols, | |
70 const WebString& origin, | |
71 WebSocketHandleClient* client) { | |
72 | |
73 DCHECK_EQ(kInvalidChannelId, channel_id_); | |
74 WebSocketDispatcher* dispatcher = | |
75 ChildThread::current()->websocket_dispatcher(); | |
76 channel_id_ = dispatcher->AddBridge(this); | |
77 client_ = client; | |
78 | |
79 std::vector<std::string> protocols_to_pass; | |
80 for (size_t i = 0; i < protocols.size(); ++i) | |
81 protocols_to_pass.push_back(protocols[i].utf8()); | |
82 GURL origin_to_pass(origin.utf8()); | |
83 | |
84 DVLOG(1) << "Bridge#" << channel_id_ << " Connect(" | |
85 << url << ", (" << JoinString(protocols_to_pass, ", ") << "), " | |
86 << origin_to_pass << ")"; | |
87 | |
88 ChildThread::current()->Send( | |
89 new WebSocketHostMsg_AddChannelRequest(channel_id_, | |
90 url, | |
91 protocols_to_pass, | |
92 origin_to_pass)); | |
93 } | |
94 | |
95 void WebSocketBridge::send(bool fin, | |
96 WebSocketHandle::MessageType type, | |
97 const char* data, | |
98 size_t size) { | |
99 if (channel_id_ == kInvalidChannelId) | |
100 return; | |
101 | |
102 WebSocketMessageType type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; | |
103 switch (type) { | |
104 case WebSocketHandle::MessageTypeContinuation: | |
105 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_CONTINUATION; | |
106 break; | |
107 case WebSocketHandle::MessageTypeText: | |
108 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_TEXT; | |
109 break; | |
110 case WebSocketHandle::MessageTypeBinary: | |
111 type_to_pass = WEB_SOCKET_MESSAGE_TYPE_BINARY; | |
112 break; | |
113 } | |
114 | |
115 DVLOG(1) << "Bridge #" << channel_id_ << " Send(" | |
116 << fin << ", " << type_to_pass << ", " | |
117 << "(data size = " << size << "))"; | |
118 | |
119 ChildThread::current()->Send( | |
120 new WebSocketMsg_SendFrame(channel_id_, | |
121 fin, | |
122 type_to_pass, | |
123 std::vector<char>(&data[0], &data[size]))); | |
Adam Rice
2013/09/17 14:49:43
FYI: Personally I find vector(data, data + size) e
yhirano
2013/09/18 00:49:30
Done.
| |
124 } | |
125 | |
126 void WebSocketBridge::flowControl(int64_t quota) { | |
127 if (channel_id_ == kInvalidChannelId) | |
128 return; | |
129 | |
130 DVLOG(1) << "Bridge #" << channel_id_ << " FlowControl(" << quota << ")"; | |
131 | |
132 ChildThread::current()->Send( | |
133 new WebSocketMsg_FlowControl(channel_id_, quota)); | |
134 } | |
135 | |
136 void WebSocketBridge::close(unsigned short code, | |
137 const WebString& reason) { | |
138 if (channel_id_ == kInvalidChannelId) | |
139 return; | |
140 | |
141 std::string reason_to_pass = reason.utf8(); | |
142 DVLOG(1) << "Bridge #" << channel_id_ << " Close(" | |
143 << code << ", " << reason_to_pass << ")"; | |
144 ChildThread::current()->Send( | |
145 new WebSocketMsg_DropChannel(channel_id_, code, reason_to_pass)); | |
146 } | |
147 | |
148 void WebSocketBridge::Disconnect() { | |
149 if (channel_id_ == kInvalidChannelId) | |
150 return; | |
151 WebSocketDispatcher* dispatcher = | |
152 ChildThread::current()->websocket_dispatcher(); | |
153 dispatcher->RemoveBridge(channel_id_); | |
154 | |
155 channel_id_ = kInvalidChannelId; | |
156 client_ = NULL; | |
157 } | |
158 | |
159 } // namespace content | |
OLD | NEW |