OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // An implementation of WebSocketStreamHandle. | 5 // An implementation of WebSocketStreamHandle. |
6 | 6 |
7 #include "webkit/glue/websocketstreamhandle_impl.h" | 7 #include "webkit/glue/websocketstreamhandle_impl.h" |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebData.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebData.h" |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSocketStreamHandle
Client.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSocketStreamHandle
Client.h" |
| 18 #include "webkit/glue/webkitplatformsupport_impl.h" |
18 #include "webkit/glue/websocketstreamhandle_bridge.h" | 19 #include "webkit/glue/websocketstreamhandle_bridge.h" |
19 #include "webkit/glue/websocketstreamhandle_delegate.h" | 20 #include "webkit/glue/websocketstreamhandle_delegate.h" |
20 | 21 |
21 namespace webkit_glue { | 22 namespace webkit_glue { |
22 | 23 |
23 // WebSocketStreamHandleImpl::Context ----------------------------------------- | 24 // WebSocketStreamHandleImpl::Context ----------------------------------------- |
24 | 25 |
25 class WebSocketStreamHandleImpl::Context | 26 class WebSocketStreamHandleImpl::Context |
26 : public base::RefCounted<Context>, | 27 : public base::RefCounted<Context>, |
27 public WebSocketStreamHandleDelegate { | 28 public WebSocketStreamHandleDelegate { |
28 public: | 29 public: |
29 explicit Context(WebSocketStreamHandleImpl* handle); | 30 explicit Context(WebSocketStreamHandleImpl* handle); |
30 | 31 |
31 WebKit::WebSocketStreamHandleClient* client() const { return client_; } | 32 WebKit::WebSocketStreamHandleClient* client() const { return client_; } |
32 void set_client(WebKit::WebSocketStreamHandleClient* client) { | 33 void set_client(WebKit::WebSocketStreamHandleClient* client) { |
33 client_ = client; | 34 client_ = client; |
34 } | 35 } |
35 | 36 |
36 void Connect(const WebKit::WebURL& url); | 37 void Connect(const WebKit::WebURL& url, WebKitPlatformSupportImpl* platform); |
37 bool Send(const WebKit::WebData& data); | 38 bool Send(const WebKit::WebData& data); |
38 void Close(); | 39 void Close(); |
39 | 40 |
40 // Must be called before |handle_| or |client_| is deleted. | 41 // Must be called before |handle_| or |client_| is deleted. |
41 // Once detached, it never calls |client_| back. | 42 // Once detached, it never calls |client_| back. |
42 void Detach(); | 43 void Detach(); |
43 | 44 |
44 // WebSocketStreamHandleDelegate methods: | 45 // WebSocketStreamHandleDelegate methods: |
45 virtual void DidOpenStream(WebKit::WebSocketStreamHandle*, int); | 46 virtual void DidOpenStream(WebKit::WebSocketStreamHandle*, int); |
46 virtual void DidSendData(WebKit::WebSocketStreamHandle*, int); | 47 virtual void DidSendData(WebKit::WebSocketStreamHandle*, int); |
(...skipping 17 matching lines...) Expand all Loading... |
64 | 65 |
65 DISALLOW_COPY_AND_ASSIGN(Context); | 66 DISALLOW_COPY_AND_ASSIGN(Context); |
66 }; | 67 }; |
67 | 68 |
68 WebSocketStreamHandleImpl::Context::Context(WebSocketStreamHandleImpl* handle) | 69 WebSocketStreamHandleImpl::Context::Context(WebSocketStreamHandleImpl* handle) |
69 : handle_(handle), | 70 : handle_(handle), |
70 client_(NULL), | 71 client_(NULL), |
71 bridge_(NULL) { | 72 bridge_(NULL) { |
72 } | 73 } |
73 | 74 |
74 void WebSocketStreamHandleImpl::Context::Connect(const WebKit::WebURL& url) { | 75 void WebSocketStreamHandleImpl::Context::Connect( |
| 76 const WebKit::WebURL& url, |
| 77 WebKitPlatformSupportImpl* platform) { |
75 VLOG(1) << "Connect url=" << url; | 78 VLOG(1) << "Connect url=" << url; |
76 DCHECK(!bridge_); | 79 DCHECK(!bridge_); |
77 bridge_ = WebSocketStreamHandleBridge::Create(handle_, this); | 80 bridge_ = platform->CreateWebSocketBridge(handle_, this); |
78 AddRef(); // Will be released by DidClose(). | 81 AddRef(); // Will be released by DidClose(). |
79 bridge_->Connect(url); | 82 bridge_->Connect(url); |
80 } | 83 } |
81 | 84 |
82 bool WebSocketStreamHandleImpl::Context::Send(const WebKit::WebData& data) { | 85 bool WebSocketStreamHandleImpl::Context::Send(const WebKit::WebData& data) { |
83 VLOG(1) << "Send data.size=" << data.size(); | 86 VLOG(1) << "Send data.size=" << data.size(); |
84 DCHECK(bridge_); | 87 DCHECK(bridge_); |
85 return bridge_->Send( | 88 return bridge_->Send( |
86 std::vector<char>(data.data(), data.data() + data.size())); | 89 std::vector<char>(data.data(), data.data() + data.size())); |
87 } | 90 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 if (client_) { | 134 if (client_) { |
132 WebKit::WebSocketStreamHandleClient* client = client_; | 135 WebKit::WebSocketStreamHandleClient* client = client_; |
133 client_ = NULL; | 136 client_ = NULL; |
134 client->didClose(handle); | 137 client->didClose(handle); |
135 } | 138 } |
136 Release(); | 139 Release(); |
137 } | 140 } |
138 | 141 |
139 // WebSocketStreamHandleImpl ------------------------------------------------ | 142 // WebSocketStreamHandleImpl ------------------------------------------------ |
140 | 143 |
141 WebSocketStreamHandleImpl::WebSocketStreamHandleImpl() | 144 WebSocketStreamHandleImpl::WebSocketStreamHandleImpl( |
142 : ALLOW_THIS_IN_INITIALIZER_LIST(context_(new Context(this))) { | 145 WebKitPlatformSupportImpl* platform) |
| 146 : ALLOW_THIS_IN_INITIALIZER_LIST(context_(new Context(this))), |
| 147 platform_(platform) { |
143 } | 148 } |
144 | 149 |
145 WebSocketStreamHandleImpl::~WebSocketStreamHandleImpl() { | 150 WebSocketStreamHandleImpl::~WebSocketStreamHandleImpl() { |
146 // We won't receive any events from |context_|. | 151 // We won't receive any events from |context_|. |
147 // |context_| is ref counted, and will be released when it received | 152 // |context_| is ref counted, and will be released when it received |
148 // DidClose. | 153 // DidClose. |
149 context_->Detach(); | 154 context_->Detach(); |
150 } | 155 } |
151 | 156 |
152 void WebSocketStreamHandleImpl::connect( | 157 void WebSocketStreamHandleImpl::connect( |
153 const WebKit::WebURL& url, WebKit::WebSocketStreamHandleClient* client) { | 158 const WebKit::WebURL& url, WebKit::WebSocketStreamHandleClient* client) { |
154 VLOG(1) << "connect url=" << url; | 159 VLOG(1) << "connect url=" << url; |
155 DCHECK(!context_->client()); | 160 DCHECK(!context_->client()); |
156 context_->set_client(client); | 161 context_->set_client(client); |
157 | 162 |
158 context_->Connect(url); | 163 context_->Connect(url, platform_); |
159 } | 164 } |
160 | 165 |
161 bool WebSocketStreamHandleImpl::send(const WebKit::WebData& data) { | 166 bool WebSocketStreamHandleImpl::send(const WebKit::WebData& data) { |
162 return context_->Send(data); | 167 return context_->Send(data); |
163 } | 168 } |
164 | 169 |
165 void WebSocketStreamHandleImpl::close() { | 170 void WebSocketStreamHandleImpl::close() { |
166 context_->Close(); | 171 context_->Close(); |
167 } | 172 } |
168 | 173 |
169 } // namespace webkit_glue | 174 } // namespace webkit_glue |
OLD | NEW |