| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/platform/WebData.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebData.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSocketStr
eamHandleClient.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSocketStr
eamHandleClient.h" |
| 18 #include "webkit/glue/webkitplatformsupport_impl.h" | 18 #include "webkit/glue/webkitplatformsupport_impl.h" |
| 19 #include "webkit/glue/websocketstreamhandle_bridge.h" | 19 #include "webkit/glue/websocketstreamhandle_bridge.h" |
| 20 #include "webkit/glue/websocketstreamhandle_delegate.h" | 20 #include "webkit/glue/websocketstreamhandle_delegate.h" |
| 21 | 21 |
| 22 using WebKit::WebData; |
| 23 using WebKit::WebSocketStreamHandle; |
| 24 using WebKit::WebSocketStreamHandleClient; |
| 25 using WebKit::WebURL; |
| 26 |
| 22 namespace webkit_glue { | 27 namespace webkit_glue { |
| 23 | 28 |
| 24 // WebSocketStreamHandleImpl::Context ----------------------------------------- | 29 // WebSocketStreamHandleImpl::Context ----------------------------------------- |
| 25 | 30 |
| 26 class WebSocketStreamHandleImpl::Context | 31 class WebSocketStreamHandleImpl::Context |
| 27 : public base::RefCounted<Context>, | 32 : public base::RefCounted<Context>, |
| 28 public WebSocketStreamHandleDelegate { | 33 public WebSocketStreamHandleDelegate { |
| 29 public: | 34 public: |
| 30 explicit Context(WebSocketStreamHandleImpl* handle); | 35 explicit Context(WebSocketStreamHandleImpl* handle); |
| 31 | 36 |
| 32 WebKit::WebSocketStreamHandleClient* client() const { return client_; } | 37 WebSocketStreamHandleClient* client() const { return client_; } |
| 33 void set_client(WebKit::WebSocketStreamHandleClient* client) { | 38 void set_client(WebSocketStreamHandleClient* client) { |
| 34 client_ = client; | 39 client_ = client; |
| 35 } | 40 } |
| 36 | 41 |
| 37 void Connect(const WebKit::WebURL& url, WebKitPlatformSupportImpl* platform); | 42 void Connect(const WebURL& url, WebKitPlatformSupportImpl* platform); |
| 38 bool Send(const WebKit::WebData& data); | 43 bool Send(const WebData& data); |
| 39 void Close(); | 44 void Close(); |
| 40 | 45 |
| 41 // Must be called before |handle_| or |client_| is deleted. | 46 // Must be called before |handle_| or |client_| is deleted. |
| 42 // Once detached, it never calls |client_| back. | 47 // Once detached, it never calls |client_| back. |
| 43 void Detach(); | 48 void Detach(); |
| 44 | 49 |
| 45 // WebSocketStreamHandleDelegate methods: | 50 // WebSocketStreamHandleDelegate methods: |
| 46 virtual void DidOpenStream(WebKit::WebSocketStreamHandle*, int); | 51 virtual void DidOpenStream(WebSocketStreamHandle*, int); |
| 47 virtual void DidSendData(WebKit::WebSocketStreamHandle*, int); | 52 virtual void DidSendData(WebSocketStreamHandle*, int); |
| 48 virtual void DidReceiveData( | 53 virtual void DidReceiveData(WebSocketStreamHandle*, const char*, int); |
| 49 WebKit::WebSocketStreamHandle*, const char*, int); | 54 virtual void DidClose(WebSocketStreamHandle*); |
| 50 virtual void DidClose(WebKit::WebSocketStreamHandle*); | |
| 51 | 55 |
| 52 private: | 56 private: |
| 53 friend class base::RefCounted<Context>; | 57 friend class base::RefCounted<Context>; |
| 54 ~Context() { | 58 ~Context() { |
| 55 DCHECK(!handle_); | 59 DCHECK(!handle_); |
| 56 DCHECK(!client_); | 60 DCHECK(!client_); |
| 57 DCHECK(!bridge_); | 61 DCHECK(!bridge_); |
| 58 } | 62 } |
| 59 | 63 |
| 60 WebSocketStreamHandleImpl* handle_; | 64 WebSocketStreamHandleImpl* handle_; |
| 61 WebKit::WebSocketStreamHandleClient* client_; | 65 WebSocketStreamHandleClient* client_; |
| 62 // |bridge_| is alive from Connect to DidClose, so Context must be alive | 66 // |bridge_| is alive from Connect to DidClose, so Context must be alive |
| 63 // in the time period. | 67 // in the time period. |
| 64 scoped_refptr<WebSocketStreamHandleBridge> bridge_; | 68 scoped_refptr<WebSocketStreamHandleBridge> bridge_; |
| 65 | 69 |
| 66 DISALLOW_COPY_AND_ASSIGN(Context); | 70 DISALLOW_COPY_AND_ASSIGN(Context); |
| 67 }; | 71 }; |
| 68 | 72 |
| 69 WebSocketStreamHandleImpl::Context::Context(WebSocketStreamHandleImpl* handle) | 73 WebSocketStreamHandleImpl::Context::Context(WebSocketStreamHandleImpl* handle) |
| 70 : handle_(handle), | 74 : handle_(handle), |
| 71 client_(NULL), | 75 client_(NULL), |
| 72 bridge_(NULL) { | 76 bridge_(NULL) { |
| 73 } | 77 } |
| 74 | 78 |
| 75 void WebSocketStreamHandleImpl::Context::Connect( | 79 void WebSocketStreamHandleImpl::Context::Connect( |
| 76 const WebKit::WebURL& url, | 80 const WebURL& url, |
| 77 WebKitPlatformSupportImpl* platform) { | 81 WebKitPlatformSupportImpl* platform) { |
| 78 VLOG(1) << "Connect url=" << url; | 82 VLOG(1) << "Connect url=" << url; |
| 79 DCHECK(!bridge_); | 83 DCHECK(!bridge_); |
| 80 bridge_ = platform->CreateWebSocketBridge(handle_, this); | 84 bridge_ = platform->CreateWebSocketBridge(handle_, this); |
| 81 AddRef(); // Will be released by DidClose(). | 85 AddRef(); // Will be released by DidClose(). |
| 82 bridge_->Connect(url); | 86 bridge_->Connect(url); |
| 83 } | 87 } |
| 84 | 88 |
| 85 bool WebSocketStreamHandleImpl::Context::Send(const WebKit::WebData& data) { | 89 bool WebSocketStreamHandleImpl::Context::Send(const WebData& data) { |
| 86 VLOG(1) << "Send data.size=" << data.size(); | 90 VLOG(1) << "Send data.size=" << data.size(); |
| 87 DCHECK(bridge_); | 91 DCHECK(bridge_); |
| 88 return bridge_->Send( | 92 return bridge_->Send( |
| 89 std::vector<char>(data.data(), data.data() + data.size())); | 93 std::vector<char>(data.data(), data.data() + data.size())); |
| 90 } | 94 } |
| 91 | 95 |
| 92 void WebSocketStreamHandleImpl::Context::Close() { | 96 void WebSocketStreamHandleImpl::Context::Close() { |
| 93 VLOG(1) << "Close"; | 97 VLOG(1) << "Close"; |
| 94 if (bridge_) | 98 if (bridge_) |
| 95 bridge_->Close(); | 99 bridge_->Close(); |
| 96 } | 100 } |
| 97 | 101 |
| 98 void WebSocketStreamHandleImpl::Context::Detach() { | 102 void WebSocketStreamHandleImpl::Context::Detach() { |
| 99 handle_ = NULL; | 103 handle_ = NULL; |
| 100 client_ = NULL; | 104 client_ = NULL; |
| 101 // If Connect was called, |bridge_| is not NULL, so that this Context closes | 105 // If Connect was called, |bridge_| is not NULL, so that this Context closes |
| 102 // the |bridge_| here. Then |bridge_| will call back DidClose, and will | 106 // the |bridge_| here. Then |bridge_| will call back DidClose, and will |
| 103 // be released by itself. | 107 // be released by itself. |
| 104 // Otherwise, |bridge_| is NULL. | 108 // Otherwise, |bridge_| is NULL. |
| 105 if (bridge_) | 109 if (bridge_) |
| 106 bridge_->Close(); | 110 bridge_->Close(); |
| 107 } | 111 } |
| 108 | 112 |
| 109 void WebSocketStreamHandleImpl::Context::DidOpenStream( | 113 void WebSocketStreamHandleImpl::Context::DidOpenStream( |
| 110 WebKit::WebSocketStreamHandle* web_handle, int max_amount_send_allowed) { | 114 WebSocketStreamHandle* web_handle, int max_amount_send_allowed) { |
| 111 VLOG(1) << "DidOpen"; | 115 VLOG(1) << "DidOpen"; |
| 112 if (client_) | 116 if (client_) |
| 113 client_->didOpenStream(handle_, max_amount_send_allowed); | 117 client_->didOpenStream(handle_, max_amount_send_allowed); |
| 114 } | 118 } |
| 115 | 119 |
| 116 void WebSocketStreamHandleImpl::Context::DidSendData( | 120 void WebSocketStreamHandleImpl::Context::DidSendData( |
| 117 WebKit::WebSocketStreamHandle* web_handle, int amount_sent) { | 121 WebSocketStreamHandle* web_handle, int amount_sent) { |
| 118 if (client_) | 122 if (client_) |
| 119 client_->didSendData(handle_, amount_sent); | 123 client_->didSendData(handle_, amount_sent); |
| 120 } | 124 } |
| 121 | 125 |
| 122 void WebSocketStreamHandleImpl::Context::DidReceiveData( | 126 void WebSocketStreamHandleImpl::Context::DidReceiveData( |
| 123 WebKit::WebSocketStreamHandle* web_handle, const char* data, int size) { | 127 WebSocketStreamHandle* web_handle, const char* data, int size) { |
| 124 if (client_) | 128 if (client_) |
| 125 client_->didReceiveData(handle_, WebKit::WebData(data, size)); | 129 client_->didReceiveData(handle_, WebData(data, size)); |
| 126 } | 130 } |
| 127 | 131 |
| 128 void WebSocketStreamHandleImpl::Context::DidClose( | 132 void WebSocketStreamHandleImpl::Context::DidClose( |
| 129 WebKit::WebSocketStreamHandle* web_handle) { | 133 WebSocketStreamHandle* web_handle) { |
| 130 VLOG(1) << "DidClose"; | 134 VLOG(1) << "DidClose"; |
| 131 bridge_ = NULL; | 135 bridge_ = NULL; |
| 132 WebSocketStreamHandleImpl* handle = handle_; | 136 WebSocketStreamHandleImpl* handle = handle_; |
| 133 handle_ = NULL; | 137 handle_ = NULL; |
| 134 if (client_) { | 138 if (client_) { |
| 135 WebKit::WebSocketStreamHandleClient* client = client_; | 139 WebSocketStreamHandleClient* client = client_; |
| 136 client_ = NULL; | 140 client_ = NULL; |
| 137 client->didClose(handle); | 141 client->didClose(handle); |
| 138 } | 142 } |
| 139 Release(); | 143 Release(); |
| 140 } | 144 } |
| 141 | 145 |
| 142 // WebSocketStreamHandleImpl ------------------------------------------------ | 146 // WebSocketStreamHandleImpl ------------------------------------------------ |
| 143 | 147 |
| 144 WebSocketStreamHandleImpl::WebSocketStreamHandleImpl( | 148 WebSocketStreamHandleImpl::WebSocketStreamHandleImpl( |
| 145 WebKitPlatformSupportImpl* platform) | 149 WebKitPlatformSupportImpl* platform) |
| 146 : ALLOW_THIS_IN_INITIALIZER_LIST(context_(new Context(this))), | 150 : ALLOW_THIS_IN_INITIALIZER_LIST(context_(new Context(this))), |
| 147 platform_(platform) { | 151 platform_(platform) { |
| 148 } | 152 } |
| 149 | 153 |
| 150 WebSocketStreamHandleImpl::~WebSocketStreamHandleImpl() { | 154 WebSocketStreamHandleImpl::~WebSocketStreamHandleImpl() { |
| 151 // We won't receive any events from |context_|. | 155 // We won't receive any events from |context_|. |
| 152 // |context_| is ref counted, and will be released when it received | 156 // |context_| is ref counted, and will be released when it received |
| 153 // DidClose. | 157 // DidClose. |
| 154 context_->Detach(); | 158 context_->Detach(); |
| 155 } | 159 } |
| 156 | 160 |
| 157 void WebSocketStreamHandleImpl::connect( | 161 void WebSocketStreamHandleImpl::connect( |
| 158 const WebKit::WebURL& url, WebKit::WebSocketStreamHandleClient* client) { | 162 const WebURL& url, WebSocketStreamHandleClient* client) { |
| 159 VLOG(1) << "connect url=" << url; | 163 VLOG(1) << "connect url=" << url; |
| 160 DCHECK(!context_->client()); | 164 DCHECK(!context_->client()); |
| 161 context_->set_client(client); | 165 context_->set_client(client); |
| 162 | 166 |
| 163 context_->Connect(url, platform_); | 167 context_->Connect(url, platform_); |
| 164 } | 168 } |
| 165 | 169 |
| 166 bool WebSocketStreamHandleImpl::send(const WebKit::WebData& data) { | 170 bool WebSocketStreamHandleImpl::send(const WebData& data) { |
| 167 return context_->Send(data); | 171 return context_->Send(data); |
| 168 } | 172 } |
| 169 | 173 |
| 170 void WebSocketStreamHandleImpl::close() { | 174 void WebSocketStreamHandleImpl::close() { |
| 171 context_->Close(); | 175 context_->Close(); |
| 172 } | 176 } |
| 173 | 177 |
| 174 } // namespace webkit_glue | 178 } // namespace webkit_glue |
| OLD | NEW |