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