OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 |
| 33 #if ENABLE(WEB_SOCKETS) |
| 34 |
| 35 #include "WebSocket.h" |
| 36 |
| 37 #include "CString.h" |
| 38 #include "DOMWindow.h" |
| 39 #include "Event.h" |
| 40 #include "EventException.h" |
| 41 #include "EventListener.h" |
| 42 #include "EventNames.h" |
| 43 #include "Logging.h" |
| 44 #include "MessageEvent.h" |
| 45 #include "ScriptExecutionContext.h" |
| 46 #include "SecurityOrigin.h" |
| 47 #include "WebSocketChannel.h" |
| 48 #include "WebSocketRequest.h" |
| 49 #include <wtf/StdLibExtras.h> |
| 50 |
| 51 namespace { |
| 52 |
| 53 bool IsValidProtocolString(const WebCore::String& protocol) |
| 54 { |
| 55 const UChar* characters = protocol.characters(); |
| 56 for (size_t i = 0; i < protocol.length(); i++) { |
| 57 if (characters[i] < 0x21 || characters[i] > 0x7E) |
| 58 return false; |
| 59 } |
| 60 return true; |
| 61 } |
| 62 |
| 63 } // anonymous namespace |
| 64 |
| 65 namespace WebCore { |
| 66 |
| 67 WebSocket::WebSocket(ScriptExecutionContext* context) |
| 68 : ActiveDOMObject(context, this) |
| 69 , m_state(CONNECTING) |
| 70 { |
| 71 } |
| 72 |
| 73 WebSocket::~WebSocket() |
| 74 { |
| 75 close(); |
| 76 } |
| 77 |
| 78 void WebSocket::connect(const KURL& url, ExceptionCode& ec) |
| 79 { |
| 80 connect(url, "", ec); |
| 81 } |
| 82 |
| 83 void WebSocket::connect(const KURL& url, const String& protocol, ExceptionCode&
ec) |
| 84 { |
| 85 LOG(Network, "WebSocket %p connect to %s protocol=%s", this, url.string().ut
f8().data(), protocol.utf8().data()); |
| 86 m_url = url; |
| 87 m_protocol = protocol; |
| 88 |
| 89 if (!m_url.protocolIs("ws") && !m_url.protocolIs("wss")) { |
| 90 LOG_ERROR("Error: wrong url for WebSocket %s", url.string().utf8().data(
)); |
| 91 m_state = CLOSED; |
| 92 ec = SYNTAX_ERR; |
| 93 return; |
| 94 } |
| 95 if (!protocol.isEmpty() && !IsValidProtocolString(protocol)) { |
| 96 LOG_ERROR("Error: wrong protocol for WebSocket %s", protocol.utf8().data
()); |
| 97 m_state = CLOSED; |
| 98 ec = SYNTAX_ERR; |
| 99 return; |
| 100 } |
| 101 // TODO(ukai): if m_url.port() is blocking port, raise SECURITY_ERR. |
| 102 |
| 103 WebSocketRequest request(m_url, m_protocol, scriptExecutionContext()->securi
tyOrigin()->toString()); |
| 104 m_channel = WebSocketChannel::create(scriptExecutionContext(), this, request
); |
| 105 m_channel->connect(); |
| 106 } |
| 107 |
| 108 bool WebSocket::send(const String& message, ExceptionCode& ec) |
| 109 { |
| 110 LOG(Network, "WebSocket %p send %s", this, message.utf8().data()); |
| 111 if (m_state == CONNECTING) { |
| 112 ec = INVALID_STATE_ERR; |
| 113 return false; |
| 114 } |
| 115 // No exception is raised if the connection was once established but has sub
sequently been closed. |
| 116 if (m_state == CLOSED) |
| 117 return false; |
| 118 // FIXME: check message is valid |
| 119 return m_channel->send(message); |
| 120 } |
| 121 |
| 122 void WebSocket::close() |
| 123 { |
| 124 LOG(Network, "WebSocket %p close", this); |
| 125 if (m_state == CLOSED) |
| 126 return; |
| 127 m_state = CLOSED; |
| 128 m_channel->close(); |
| 129 } |
| 130 |
| 131 const KURL& WebSocket::url() const |
| 132 { |
| 133 return m_url; |
| 134 } |
| 135 |
| 136 WebSocket::State WebSocket::readyState() const |
| 137 { |
| 138 return m_state; |
| 139 } |
| 140 |
| 141 unsigned long WebSocket::bufferedAmount() const |
| 142 { |
| 143 if (m_state == OPEN) |
| 144 return m_channel->bufferedAmount(); |
| 145 return 0; |
| 146 } |
| 147 |
| 148 ScriptExecutionContext* WebSocket::scriptExecutionContext() const |
| 149 { |
| 150 return ActiveDOMObject::scriptExecutionContext(); |
| 151 } |
| 152 |
| 153 void WebSocket::addEventListener(const AtomicString& eventType, PassRefPtr<Event
Listener> listener, bool useCapture) |
| 154 { |
| 155 // FIXME: implement this. |
| 156 } |
| 157 |
| 158 void WebSocket::removeEventListener(const AtomicString& eventType, EventListener
* listener, bool useCapture) |
| 159 { |
| 160 // FIXME: implement this. |
| 161 } |
| 162 |
| 163 bool WebSocket::dispatchEvent(PassRefPtr<Event> event, ExceptionCode& ec) |
| 164 { |
| 165 // FIXME: implement this. |
| 166 return false; |
| 167 } |
| 168 |
| 169 void WebSocket::didConnect() |
| 170 { |
| 171 LOG(Network, "WebSocket %p didConnect", this); |
| 172 if (m_state != CONNECTING) { |
| 173 didClose(); |
| 174 return; |
| 175 } |
| 176 m_state = OPEN; |
| 177 dispatchOpenEvent(); |
| 178 } |
| 179 |
| 180 void WebSocket::didReceiveMessage(const String& msg) |
| 181 { |
| 182 LOG(Network, "WebSocket %p didReceiveMessage %s", this, msg.utf8().data()); |
| 183 if (m_state != OPEN) |
| 184 return; |
| 185 dispatchMessageEvent(msg); |
| 186 } |
| 187 |
| 188 void WebSocket::didClose() |
| 189 { |
| 190 LOG(Network, "WebSocket %p didClose", this); |
| 191 m_state = CLOSED; |
| 192 dispatchCloseEvent(); |
| 193 } |
| 194 |
| 195 void WebSocket::dispatchOpenEvent() |
| 196 { |
| 197 RefPtr<Event> evt = Event::create(eventNames().openEvent, false, false); |
| 198 if (m_onopen) { |
| 199 evt->setTarget(this); |
| 200 evt->setCurrentTarget(this); |
| 201 m_onopen->handleEvent(evt.get(), false); |
| 202 } |
| 203 // FIXME: need dispatchEvent? |
| 204 } |
| 205 |
| 206 void WebSocket::dispatchMessageEvent(const String& msg) |
| 207 { |
| 208 RefPtr<MessageEvent> evt = MessageEvent::create(); |
| 209 // FIXME: origin, lastEventId, source, messagePort. |
| 210 evt->initMessageEvent(eventNames().messageEvent, false, false, msg, "", "",
NULL, NULL); |
| 211 if (m_onmessage) { |
| 212 evt->setTarget(this); |
| 213 evt->setCurrentTarget(this); |
| 214 m_onmessage->handleEvent(evt.get(), false); |
| 215 } |
| 216 // FIXME: need dispatchEvent? |
| 217 } |
| 218 |
| 219 void WebSocket::dispatchCloseEvent() |
| 220 { |
| 221 RefPtr<Event> evt = Event::create(eventNames().closeEvent, false, false); |
| 222 if (m_onclose) { |
| 223 evt->setTarget(this); |
| 224 evt->setCurrentTarget(this); |
| 225 m_onclose->handleEvent(evt.get(), false); |
| 226 } |
| 227 // FIXME: need dispatchEvent? |
| 228 } |
| 229 |
| 230 } // namespace WebCore |
| 231 |
| 232 #endif |
OLD | NEW |