| OLD | NEW |
| 1 // Copyright (c) 2012 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 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 const char*, | 57 const char*, |
| 58 int) OVERRIDE; | 58 int) OVERRIDE; |
| 59 virtual void DidClose(WebSocketStreamHandle*) OVERRIDE; | 59 virtual void DidClose(WebSocketStreamHandle*) OVERRIDE; |
| 60 virtual void DidFail(WebSocketStreamHandle*, int, const string16&) OVERRIDE; | 60 virtual void DidFail(WebSocketStreamHandle*, int, const string16&) OVERRIDE; |
| 61 | 61 |
| 62 private: | 62 private: |
| 63 friend class base::RefCounted<Context>; | 63 friend class base::RefCounted<Context>; |
| 64 virtual ~Context() { | 64 virtual ~Context() { |
| 65 DCHECK(!handle_); | 65 DCHECK(!handle_); |
| 66 DCHECK(!client_); | 66 DCHECK(!client_); |
| 67 DCHECK(!bridge_); | 67 DCHECK(!bridge_.get()); |
| 68 } | 68 } |
| 69 | 69 |
| 70 WebSocketStreamHandleImpl* handle_; | 70 WebSocketStreamHandleImpl* handle_; |
| 71 WebSocketStreamHandleClient* client_; | 71 WebSocketStreamHandleClient* client_; |
| 72 // |bridge_| is alive from Connect to DidClose, so Context must be alive | 72 // |bridge_| is alive from Connect to DidClose, so Context must be alive |
| 73 // in the time period. | 73 // in the time period. |
| 74 scoped_refptr<WebSocketStreamHandleBridge> bridge_; | 74 scoped_refptr<WebSocketStreamHandleBridge> bridge_; |
| 75 | 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(Context); | 76 DISALLOW_COPY_AND_ASSIGN(Context); |
| 77 }; | 77 }; |
| 78 | 78 |
| 79 WebSocketStreamHandleImpl::Context::Context(WebSocketStreamHandleImpl* handle) | 79 WebSocketStreamHandleImpl::Context::Context(WebSocketStreamHandleImpl* handle) |
| 80 : handle_(handle), | 80 : handle_(handle), |
| 81 client_(NULL), | 81 client_(NULL), |
| 82 bridge_(NULL) { | 82 bridge_(NULL) { |
| 83 } | 83 } |
| 84 | 84 |
| 85 void WebSocketStreamHandleImpl::Context::Connect( | 85 void WebSocketStreamHandleImpl::Context::Connect( |
| 86 const WebURL& url, | 86 const WebURL& url, |
| 87 WebKitPlatformSupportImpl* platform) { | 87 WebKitPlatformSupportImpl* platform) { |
| 88 VLOG(1) << "Connect url=" << url; | 88 VLOG(1) << "Connect url=" << url; |
| 89 DCHECK(!bridge_); | 89 DCHECK(!bridge_.get()); |
| 90 bridge_ = platform->CreateWebSocketBridge(handle_, this); | 90 bridge_ = platform->CreateWebSocketBridge(handle_, this); |
| 91 AddRef(); // Will be released by DidClose(). | 91 AddRef(); // Will be released by DidClose(). |
| 92 bridge_->Connect(url); | 92 bridge_->Connect(url); |
| 93 } | 93 } |
| 94 | 94 |
| 95 bool WebSocketStreamHandleImpl::Context::Send(const WebData& data) { | 95 bool WebSocketStreamHandleImpl::Context::Send(const WebData& data) { |
| 96 VLOG(1) << "Send data.size=" << data.size(); | 96 VLOG(1) << "Send data.size=" << data.size(); |
| 97 DCHECK(bridge_); | 97 DCHECK(bridge_.get()); |
| 98 return bridge_->Send( | 98 return bridge_->Send( |
| 99 std::vector<char>(data.data(), data.data() + data.size())); | 99 std::vector<char>(data.data(), data.data() + data.size())); |
| 100 } | 100 } |
| 101 | 101 |
| 102 void WebSocketStreamHandleImpl::Context::Close() { | 102 void WebSocketStreamHandleImpl::Context::Close() { |
| 103 VLOG(1) << "Close"; | 103 VLOG(1) << "Close"; |
| 104 if (bridge_) | 104 if (bridge_.get()) |
| 105 bridge_->Close(); | 105 bridge_->Close(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void WebSocketStreamHandleImpl::Context::Detach() { | 108 void WebSocketStreamHandleImpl::Context::Detach() { |
| 109 handle_ = NULL; | 109 handle_ = NULL; |
| 110 client_ = NULL; | 110 client_ = NULL; |
| 111 // If Connect was called, |bridge_| is not NULL, so that this Context closes | 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 | 112 // the |bridge_| here. Then |bridge_| will call back DidClose, and will |
| 113 // be released by itself. | 113 // be released by itself. |
| 114 // Otherwise, |bridge_| is NULL. | 114 // Otherwise, |bridge_| is NULL. |
| 115 if (bridge_) | 115 if (bridge_.get()) |
| 116 bridge_->Close(); | 116 bridge_->Close(); |
| 117 } | 117 } |
| 118 | 118 |
| 119 void WebSocketStreamHandleImpl::Context::DidOpenStream( | 119 void WebSocketStreamHandleImpl::Context::DidOpenStream( |
| 120 WebSocketStreamHandle* web_handle, int max_amount_send_allowed) { | 120 WebSocketStreamHandle* web_handle, int max_amount_send_allowed) { |
| 121 VLOG(1) << "DidOpen"; | 121 VLOG(1) << "DidOpen"; |
| 122 if (client_) | 122 if (client_) |
| 123 client_->didOpenStream(handle_, max_amount_send_allowed); | 123 client_->didOpenStream(handle_, max_amount_send_allowed); |
| 124 } | 124 } |
| 125 | 125 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 | 187 |
| 188 bool WebSocketStreamHandleImpl::send(const WebData& data) { | 188 bool WebSocketStreamHandleImpl::send(const WebData& data) { |
| 189 return context_->Send(data); | 189 return context_->Send(data); |
| 190 } | 190 } |
| 191 | 191 |
| 192 void WebSocketStreamHandleImpl::close() { | 192 void WebSocketStreamHandleImpl::close() { |
| 193 context_->Close(); | 193 context_->Close(); |
| 194 } | 194 } |
| 195 | 195 |
| 196 } // namespace webkit_glue | 196 } // namespace webkit_glue |
| OLD | NEW |