| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "SocketStreamHandle.h" | 32 #include "SocketStreamHandle.h" |
| 33 | 33 |
| 34 #if ENABLE(WEB_SOCKETS) | 34 #if ENABLE(WEB_SOCKETS) |
| 35 | 35 |
| 36 #include "Logging.h" | 36 #include "Logging.h" |
| 37 #include "NotImplemented.h" | 37 #include "NotImplemented.h" |
| 38 #include "SocketStreamHandleClient.h" | 38 #include "SocketStreamHandleClient.h" |
| 39 #include "WebData.h" | 39 #include "WebData.h" |
| 40 #include "WebKit.h" | 40 #include "WebKit.h" |
| 41 #include "WebKitClient.h" |
| 42 #include "WebSocketStreamHandle.h" |
| 43 #include "WebSocketStreamHandleClient.h" |
| 41 #include <wtf/PassOwnPtr.h> | 44 #include <wtf/PassOwnPtr.h> |
| 42 | 45 |
| 43 using namespace WebKit; | 46 using namespace WebKit; |
| 44 | 47 |
| 45 namespace WebCore { | 48 namespace WebCore { |
| 46 | 49 |
| 47 class SocketStreamHandleInternal { | 50 class SocketStreamHandleInternal : public WebSocketStreamHandleClient { |
| 48 public: | 51 public: |
| 49 static PassOwnPtr<SocketStreamHandleInternal> create(SocketStreamHandle* handle) | 52 static PassOwnPtr<SocketStreamHandleInternal> create(SocketStreamHandle* handle) |
| 50 { | 53 { |
| 51 return new SocketStreamHandleInternal(handle); | 54 return new SocketStreamHandleInternal(handle); |
| 52 } | 55 } |
| 53 virtual ~SocketStreamHandleInternal(); | 56 virtual ~SocketStreamHandleInternal(); |
| 54 | 57 |
| 55 // TODO(ukai): implement this. | 58 void connect(const KURL&); |
| 59 int send(const char*, int); |
| 60 void close(); |
| 61 |
| 62 virtual void willOpenStream(WebSocketStreamHandle*, const WebURL&); |
| 63 |
| 64 virtual void didOpenStream(WebSocketStreamHandle*, int); |
| 65 virtual void didSendData(WebSocketStreamHandle*, int); |
| 66 virtual void didReceiveData(WebSocketStreamHandle*, const WebData&); |
| 67 virtual void didClose(WebSocketStreamHandle*); |
| 68 virtual void didFail(WebSocketStreamHandle*, const WebSocketStreamError&); |
| 56 | 69 |
| 57 private: | 70 private: |
| 58 explicit SocketStreamHandleInternal(SocketStreamHandle*); | 71 explicit SocketStreamHandleInternal(SocketStreamHandle*); |
| 59 | 72 |
| 60 SocketStreamHandle* m_handle; | 73 SocketStreamHandle* m_handle; |
| 74 OwnPtr<WebSocketStreamHandle> m_socket; |
| 75 int m_maxPendingSendAllowed; |
| 76 int m_pendingAmountSent; |
| 61 }; | 77 }; |
| 62 | 78 |
| 63 SocketStreamHandleInternal::SocketStreamHandleInternal(SocketStreamHandle* handle) | 79 SocketStreamHandleInternal::SocketStreamHandleInternal( |
| 80 SocketStreamHandle *handle) |
| 64 : m_handle(handle) | 81 : m_handle(handle) |
| 82 , m_maxPendingSendAllowed(0) |
| 83 , m_pendingAmountSent(0) |
| 65 { | 84 { |
| 66 } | 85 } |
| 67 | 86 |
| 68 SocketStreamHandleInternal::~SocketStreamHandleInternal() | 87 SocketStreamHandleInternal::~SocketStreamHandleInternal() |
| 69 { | 88 { |
| 70 m_handle = 0; | 89 m_handle = 0; |
| 71 } | 90 } |
| 72 | 91 |
| 92 void SocketStreamHandleInternal::connect(const KURL& url) |
| 93 { |
| 94 m_socket.set(webKitClient()->createSocketStreamHandle()); |
| 95 LOG(Network, "connect"); |
| 96 ASSERT(m_socket.get()); |
| 97 m_socket->connect(url, this); |
| 98 } |
| 99 |
| 100 int SocketStreamHandleInternal::send(const char* data, int len) |
| 101 { |
| 102 LOG(Network, "send len=%d", len); |
| 103 ASSERT(m_socket.get()); |
| 104 if (m_pendingAmountSent + len >= m_maxPendingSendAllowed) |
| 105 len = m_maxPendingSendAllowed - m_pendingAmountSent - 1; |
| 106 |
| 107 if (len <= 0) |
| 108 return len; |
| 109 WebData webdata(data, len); |
| 110 if (m_socket->send(webdata)) { |
| 111 m_pendingAmountSent += len; |
| 112 LOG(Network, "sent"); |
| 113 if (m_handle && m_handle->m_client) |
| 114 m_handle->m_client->willSendData(m_handle, webdata.data(), webdata.size()); |
| 115 return len; |
| 116 } |
| 117 LOG(Network, "busy. buffering"); |
| 118 return 0; |
| 119 } |
| 120 |
| 121 void SocketStreamHandleInternal::close() |
| 122 { |
| 123 LOG(Network, "close"); |
| 124 m_socket->close(); |
| 125 } |
| 126 |
| 127 void SocketStreamHandleInternal::willOpenStream(WebSocketStreamHandle* socketHandle, const WebURL& url) |
| 128 { |
| 129 LOG(Network, "willOpenStream"); |
| 130 if (m_handle && m_socket.get()) { |
| 131 ASSERT(socketHandle == m_socket.get()); |
| 132 if (m_handle->m_client) |
| 133 m_handle->m_client->willOpenStream(m_handle, url); |
| 134 } |
| 135 } |
| 136 |
| 137 void SocketStreamHandleInternal::didOpenStream(WebSocketStreamHandle* socketHandle, int maxPendingSendAllowed) |
| 138 { |
| 139 LOG(Network, "SocketStreamHandleInternal::didOpen %d", |
| 140 maxPendingSendAllowed); |
| 141 ASSERT(maxPendingSendAllowed > 0); |
| 142 if (m_handle && m_socket.get()) { |
| 143 ASSERT(socketHandle == m_socket.get()); |
| 144 m_maxPendingSendAllowed = maxPendingSendAllowed; |
| 145 m_handle->m_state = SocketStreamHandleBase::Open; |
| 146 if (m_handle->m_client) { |
| 147 m_handle->m_client->didOpen(m_handle); |
| 148 return; |
| 149 } |
| 150 } |
| 151 LOG(Network, "no m_handle or m_socket?"); |
| 152 } |
| 153 |
| 154 void SocketStreamHandleInternal::didSendData(WebSocketStreamHandle* socketHandle, int amountSent) |
| 155 { |
| 156 LOG(Network, "SocketStreamHandleInternal::didSendData %d", amountSent); |
| 157 ASSERT(amountSent > 0); |
| 158 if (m_handle && m_socket.get()) { |
| 159 ASSERT(socketHandle == m_socket.get()); |
| 160 m_pendingAmountSent -= amountSent; |
| 161 ASSERT(m_pendingAmountSent >= 0); |
| 162 m_handle->sendPendingData(); |
| 163 } |
| 164 } |
| 165 |
| 166 void SocketStreamHandleInternal::didReceiveData(WebSocketStreamHandle* socketHandle, const WebData& data) |
| 167 { |
| 168 LOG(Network, "didReceiveData"); |
| 169 if (m_handle && m_socket.get()) { |
| 170 ASSERT(socketHandle == m_socket.get()); |
| 171 if (m_handle->m_client) |
| 172 m_handle->m_client->didReceiveData(m_handle, data.data(), data.size()); |
| 173 } |
| 174 } |
| 175 |
| 176 void SocketStreamHandleInternal::didClose(WebSocketStreamHandle* socketHandle) |
| 177 { |
| 178 LOG(Network, "didClose"); |
| 179 if (m_handle && m_socket.get()) { |
| 180 ASSERT(socketHandle == m_socket.get()); |
| 181 m_socket.clear(); |
| 182 SocketStreamHandle* h = m_handle; |
| 183 m_handle = NULL; |
| 184 if (h->m_client) |
| 185 h->m_client->didClose(h); |
| 186 } |
| 187 } |
| 188 |
| 189 void SocketStreamHandleInternal::didFail(WebSocketStreamHandle* socketHandle, const WebSocketStreamError& err) |
| 190 { |
| 191 LOG(Network, "didFail"); |
| 192 if (m_handle && m_socket.get()) { |
| 193 ASSERT(socketHandle == m_socket.get()); |
| 194 m_socket.clear(); |
| 195 SocketStreamHandle* h = m_handle; |
| 196 m_handle = NULL; |
| 197 if (h->m_client) |
| 198 h->m_client->didClose(h); // didFail(h, err); |
| 199 } |
| 200 } |
| 201 |
| 202 // FIXME: auth |
| 203 |
| 73 // SocketStreamHandle ---------------------------------------------------------- | 204 // SocketStreamHandle ---------------------------------------------------------- |
| 74 | 205 |
| 75 SocketStreamHandle::SocketStreamHandle(const KURL& url, SocketStreamHandleClient* client) | 206 SocketStreamHandle::SocketStreamHandle(const KURL& url, SocketStreamHandleClient* client) |
| 76 : SocketStreamHandleBase(url, client) | 207 : SocketStreamHandleBase(url, client) |
| 77 { | 208 { |
| 78 m_internal = SocketStreamHandleInternal::create(this); | 209 m_internal = SocketStreamHandleInternal::create(this); |
| 79 notImplemented(); | 210 m_internal->connect(m_url); |
| 80 } | 211 } |
| 81 | 212 |
| 82 SocketStreamHandle::~SocketStreamHandle() | 213 SocketStreamHandle::~SocketStreamHandle() |
| 83 { | 214 { |
| 84 setClient(0); | 215 setClient(0); |
| 85 m_internal.clear(); | 216 m_internal.clear(); |
| 86 } | 217 } |
| 87 | 218 |
| 88 int SocketStreamHandle::platformSend(const char* buf, int len) | 219 int SocketStreamHandle::platformSend(const char* buf, int len) |
| 89 { | 220 { |
| 90 notImplemented(); | 221 if (!m_internal.get()) |
| 91 return 0; | 222 return 0; |
| 223 return m_internal->send(buf, len); |
| 92 } | 224 } |
| 93 | 225 |
| 94 void SocketStreamHandle::platformClose() | 226 void SocketStreamHandle::platformClose() |
| 95 { | 227 { |
| 96 notImplemented(); | 228 if (m_internal.get()) |
| 229 m_internal->close(); |
| 97 } | 230 } |
| 98 | 231 |
| 99 void SocketStreamHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge) | 232 void SocketStreamHandle::didReceiveAuthenticationChallenge(const AuthenticationChallenge& challenge) |
| 100 { | 233 { |
| 101 notImplemented(); | 234 if (m_client) |
| 235 m_client->didReceiveAuthenticationChallenge(this, challenge); |
| 102 } | 236 } |
| 103 | 237 |
| 104 void SocketStreamHandle::receivedCredential(const AuthenticationChallenge& challenge, const Credential& credential) | 238 void SocketStreamHandle::receivedCredential(const AuthenticationChallenge& challenge, const Credential& credential) |
| 105 { | 239 { |
| 106 notImplemented(); | 240 notImplemented(); |
| 107 } | 241 } |
| 108 | 242 |
| 109 void SocketStreamHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& challenge) | 243 void SocketStreamHandle::receivedRequestToContinueWithoutCredential(const AuthenticationChallenge& challenge) |
| 110 { | 244 { |
| 111 notImplemented(); | 245 notImplemented(); |
| 112 } | 246 } |
| 113 | 247 |
| 114 void SocketStreamHandle::receivedCancellation(const AuthenticationChallenge& challenge) | 248 void SocketStreamHandle::receivedCancellation(const AuthenticationChallenge& challenge) |
| 115 { | 249 { |
| 116 notImplemented(); | 250 if (m_client) |
| 251 m_client->receivedCancellation(this, challenge); |
| 117 } | 252 } |
| 118 | 253 |
| 119 } // namespace WebCore | 254 } // namespace WebCore |
| 120 | 255 |
| 121 #endif // ENABLE(WEB_SOCKETS) | 256 #endif // ENABLE(WEB_SOCKETS) |
| OLD | NEW |