| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "content/child/web_socket_stream_handle_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 "content/child/child_thread.h" | |
| 17 #include "content/child/socket_stream_dispatcher.h" | |
| 18 #include "content/child/web_socket_stream_handle_bridge.h" | |
| 19 #include "content/child/web_socket_stream_handle_delegate.h" | |
| 20 #include "third_party/WebKit/public/platform/WebData.h" | |
| 21 #include "third_party/WebKit/public/platform/WebSocketStreamError.h" | |
| 22 #include "third_party/WebKit/public/platform/WebSocketStreamHandleClient.h" | |
| 23 #include "third_party/WebKit/public/platform/WebURL.h" | |
| 24 | |
| 25 using blink::WebData; | |
| 26 using blink::WebSocketStreamError; | |
| 27 using blink::WebSocketStreamHandle; | |
| 28 using blink::WebSocketStreamHandleClient; | |
| 29 using blink::WebURL; | |
| 30 | |
| 31 namespace content { | |
| 32 | |
| 33 // WebSocketStreamHandleImpl::Context ----------------------------------------- | |
| 34 | |
| 35 class WebSocketStreamHandleImpl::Context | |
| 36 : public base::RefCounted<Context>, | |
| 37 public WebSocketStreamHandleDelegate { | |
| 38 public: | |
| 39 explicit Context(WebSocketStreamHandleImpl* handle); | |
| 40 | |
| 41 WebSocketStreamHandleClient* client() const { return client_; } | |
| 42 void set_client(WebSocketStreamHandleClient* client) { | |
| 43 client_ = client; | |
| 44 } | |
| 45 | |
| 46 void Connect(const WebURL& url); | |
| 47 bool Send(const WebData& data); | |
| 48 void Close(); | |
| 49 | |
| 50 // Must be called before |handle_| or |client_| is deleted. | |
| 51 // Once detached, it never calls |client_| back. | |
| 52 void Detach(); | |
| 53 | |
| 54 // WebSocketStreamHandleDelegate methods: | |
| 55 void DidOpenStream(WebSocketStreamHandle*, int) override; | |
| 56 void DidSendData(WebSocketStreamHandle*, int) override; | |
| 57 void DidReceiveData(WebSocketStreamHandle*, const char*, int) override; | |
| 58 void DidClose(WebSocketStreamHandle*) override; | |
| 59 void DidFail(WebSocketStreamHandle*, int, const base::string16&) override; | |
| 60 | |
| 61 private: | |
| 62 friend class base::RefCounted<Context>; | |
| 63 ~Context() override { | |
| 64 DCHECK(!handle_); | |
| 65 DCHECK(!client_); | |
| 66 DCHECK(!bridge_.get()); | |
| 67 } | |
| 68 | |
| 69 WebSocketStreamHandleImpl* handle_; | |
| 70 WebSocketStreamHandleClient* client_; | |
| 71 // |bridge_| is alive from Connect to DidClose, so Context must be alive | |
| 72 // in the time period. | |
| 73 scoped_refptr<WebSocketStreamHandleBridge> bridge_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(Context); | |
| 76 }; | |
| 77 | |
| 78 WebSocketStreamHandleImpl::Context::Context(WebSocketStreamHandleImpl* handle) | |
| 79 : handle_(handle), | |
| 80 client_(NULL) { | |
| 81 } | |
| 82 | |
| 83 void WebSocketStreamHandleImpl::Context::Connect(const WebURL& url) { | |
| 84 VLOG(1) << "Connect url=" << url; | |
| 85 DCHECK(!bridge_.get()); | |
| 86 | |
| 87 SocketStreamDispatcher* dispatcher = | |
| 88 ChildThread::current()->socket_stream_dispatcher(); | |
| 89 bridge_ = dispatcher->CreateBridge(handle_, this); | |
| 90 | |
| 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 base::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 : context_(new Context(this)) { | |
| 168 } | |
| 169 | |
| 170 WebSocketStreamHandleImpl::~WebSocketStreamHandleImpl() { | |
| 171 // We won't receive any events from |context_|. | |
| 172 // |context_| is ref counted, and will be released when it received | |
| 173 // DidClose. | |
| 174 context_->Detach(); | |
| 175 } | |
| 176 | |
| 177 void WebSocketStreamHandleImpl::connect( | |
| 178 const WebURL& url, WebSocketStreamHandleClient* client) { | |
| 179 VLOG(1) << "connect url=" << url; | |
| 180 DCHECK(!context_->client()); | |
| 181 context_->set_client(client); | |
| 182 | |
| 183 context_->Connect(url); | |
| 184 } | |
| 185 | |
| 186 bool WebSocketStreamHandleImpl::send(const WebData& data) { | |
| 187 return context_->Send(data); | |
| 188 } | |
| 189 | |
| 190 void WebSocketStreamHandleImpl::close() { | |
| 191 context_->Close(); | |
| 192 } | |
| 193 | |
| 194 } // namespace content | |
| OLD | NEW |