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 #include "WebSocket.h" |
| 33 |
| 34 #include "V8Binding.h" |
| 35 #include "V8ObjectEventListener.h" |
| 36 #include "V8Proxy.h" |
| 37 #include "V8Utilities.h" |
| 38 |
| 39 namespace WebCore { |
| 40 |
| 41 ACCESSOR_GETTER(WebSocketOnopen) |
| 42 { |
| 43 INC_STATS("DOM.WebSocket.onopen._get"); |
| 44 WebSocket* websocket = V8Proxy::convertToNativeObject<WebSocket>(V8ClassInde
x::WEBSOCKET, info.Holder()); |
| 45 if (websocket->onopen()) { |
| 46 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(we
bsocket->onopen()); |
| 47 v8::Local<v8::Object> v8Listener = listener->getListenerObject(); |
| 48 return v8Listener; |
| 49 } |
| 50 return v8::Null(); |
| 51 } |
| 52 |
| 53 ACCESSOR_SETTER(WebSocketOnopen) |
| 54 { |
| 55 INC_STATS("DOM.WebSocket.onopen._set"); |
| 56 WebSocket* websocket = V8Proxy::convertToNativeObject<WebSocket>(V8ClassInde
x::WEBSOCKET, info.Holder()); |
| 57 if (value->IsNull()) { |
| 58 if (websocket->onopen()) { |
| 59 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*
>(websocket->onopen()); |
| 60 v8::Local<v8::Object> v8Listener = listener->getListenerObject(); |
| 61 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kWebSock
etCacheIndex); |
| 62 } |
| 63 // Clear the listener. |
| 64 websocket->setOnopen(0); |
| 65 } else { |
| 66 RefPtr<EventListener> listener = getEventListener(websocket, value, fals
e); |
| 67 if (listener) { |
| 68 websocket->setOnopen(listener); |
| 69 createHiddenDependency(info.Holder(), value, V8Custom::kWebSocketCac
heIndex); |
| 70 } |
| 71 } |
| 72 } |
| 73 |
| 74 ACCESSOR_GETTER(WebSocketOnmessage) |
| 75 { |
| 76 INC_STATS("DOM.WebSocket.onmessage._get"); |
| 77 WebSocket* websocket = V8Proxy::convertToNativeObject<WebSocket>(V8ClassInde
x::WEBSOCKET, info.Holder()); |
| 78 if (websocket->onmessage()) { |
| 79 RefPtr<V8ObjectEventListener> listener = static_cast<V8ObjectEventListen
er*>(websocket->onmessage()); |
| 80 v8::Local<v8::Object> v8Listener = listener->getListenerObject(); |
| 81 return v8Listener; |
| 82 } |
| 83 return v8::Null(); |
| 84 } |
| 85 |
| 86 ACCESSOR_SETTER(WebSocketOnmessage) |
| 87 { |
| 88 INC_STATS("DOM.WebSocket.onmessage._set"); |
| 89 WebSocket* websocket = V8Proxy::convertToNativeObject<WebSocket>(V8ClassInde
x::WEBSOCKET, info.Holder()); |
| 90 if (value->IsNull()) { |
| 91 if (websocket->onmessage()) { |
| 92 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*
>(websocket->onmessage()); |
| 93 v8::Local<v8::Object> v8Listener = listener->getListenerObject(); |
| 94 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kWebSock
etCacheIndex); |
| 95 } |
| 96 // Clear the listener. |
| 97 websocket->setOnmessage(0); |
| 98 } else { |
| 99 RefPtr<EventListener> listener = getEventListener(websocket, value, fals
e); |
| 100 if (listener) { |
| 101 websocket->setOnmessage(listener); |
| 102 createHiddenDependency(info.Holder(), value, V8Custom::kWebSocketCac
heIndex); |
| 103 } |
| 104 } |
| 105 } |
| 106 |
| 107 ACCESSOR_GETTER(WebSocketOnclose) |
| 108 { |
| 109 INC_STATS("DOM.WebSocket.onclose._get"); |
| 110 WebSocket* websocket = V8Proxy::convertToNativeObject<WebSocket>(V8ClassInde
x::WEBSOCKET, info.Holder()); |
| 111 if (websocket->onclose()) { |
| 112 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(we
bsocket->onclose()); |
| 113 v8::Local<v8::Object> v8Listener = listener->getListenerObject(); |
| 114 return v8Listener; |
| 115 } |
| 116 return v8::Null(); |
| 117 } |
| 118 |
| 119 ACCESSOR_SETTER(WebSocketOnclose) |
| 120 { |
| 121 INC_STATS("DOM.WebSocket.onclose._set"); |
| 122 WebSocket* websocket = V8Proxy::convertToNativeObject<WebSocket>(V8ClassInde
x::WEBSOCKET, info.Holder()); |
| 123 if (value->IsNull()) { |
| 124 if (websocket->onclose()) { |
| 125 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*
>(websocket->onclose()); |
| 126 v8::Local<v8::Object> v8Listener = listener->getListenerObject(); |
| 127 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kWebSock
etCacheIndex); |
| 128 } |
| 129 // Clear the listener. |
| 130 websocket->setOnclose(0); |
| 131 } else { |
| 132 RefPtr<EventListener> listener = getEventListener(websocket, value, fals
e); |
| 133 if (listener) { |
| 134 websocket->setOnclose(listener); |
| 135 createHiddenDependency(info.Holder(), value, V8Custom::kWebSocketCac
heIndex); |
| 136 } |
| 137 } |
| 138 } |
| 139 |
| 140 // ??? AddEventListener, RemoveEventListener |
| 141 |
| 142 CALLBACK_FUNC_DECL(WebSocketConstructor) |
| 143 { |
| 144 INC_STATS("DOM.WebSocket.Constructor"); |
| 145 |
| 146 if (!args.IsConstructCall()) |
| 147 return throwError("DOM object custructor cannot be called as a function.
"); |
| 148 if (args.Length() == 0) |
| 149 return throwError("Not enough arguments", V8Proxy::SyntaxError); |
| 150 |
| 151 v8::TryCatch tryCatch; |
| 152 v8::Handle<v8::String> urlstring = args[0]->ToString(); |
| 153 if (tryCatch.HasCaught()) |
| 154 return throwError(tryCatch.Exception()); |
| 155 if (urlstring.IsEmpty()) |
| 156 return throwError("Empty URL", V8Proxy::SyntaxError); |
| 157 |
| 158 // Get the script execution context. |
| 159 ScriptExecutionContext* context = 0; |
| 160 // TODO(ukai): worker? |
| 161 Frame* frame = V8Proxy::retrieveFrame(); |
| 162 if (!frame) |
| 163 return thowError("WebSocket constructor associated frame is unavailable"
, V8Proxy::ReferenceError); |
| 164 context = frame->document(); |
| 165 if (!context) |
| 166 return thowError("WebSocket constructor associated document is unavailab
le", V8Proxy::ReferenceError); |
| 167 |
| 168 const KURL& url = context->completeURL(toWebCoreString(urlstring)); |
| 169 |
| 170 RefPtr<WebSocket> websocket = WebSocket::create(context); |
| 171 |
| 172 if (args.Length() < 2) |
| 173 websocket->connect(url, ec); |
| 174 else { |
| 175 v8::TryCzatch tryCatchProtocol; |
| 176 v8::Handle<v8::String> protocol = args[1]->ToString(); |
| 177 if (tryCatchProtocol.HasCaught()) |
| 178 return throwError(tryCatchProtocol.Exception()); |
| 179 if (protocol.IsEmpty()) |
| 180 return throwError("protocol is empty", V8Proxy::SyntaxError); |
| 181 websocket->connect(url, toWebCoreString(protocol), ec); |
| 182 } |
| 183 if (ec) |
| 184 return throwError(ec); |
| 185 |
| 186 // Setup the standard wrapper object internal fields. |
| 187 v8::Handle<v8::Object> wrapperObject = args.Holder(); |
| 188 V8Proxy::setDOMWrapper(wrapperObject, V8ClassIndex::WEBSOCKET, websocket.get
()); |
| 189 |
| 190 websocket->ref(); |
| 191 V8Proxy::setJSWrapperForActiveDOMObject(websocket.get(), v8::Persistent<v8::
Object>::New(wrapperObject)); |
| 192 |
| 193 return wrapperObject; |
| 194 } |
| 195 |
| 196 CALLBACK_FUNC_DECL(WebSocketSend) |
| 197 { |
| 198 INC_STATS("DOM.WebSocket.send()"); |
| 199 WebSocket* websocket = V8Proxy::convertToNativeObject<WebSocket>(V8ClassInde
x::WEBSOCKET, args.Holder()); |
| 200 |
| 201 ExceptionCode ec = 0; |
| 202 bool ret = false; |
| 203 if (args.Length() < 1) |
| 204 return throwError("Not enough arguments", V8Proxy::SyntaxError); |
| 205 else { |
| 206 String msg = toWebCoreString(args[0]); |
| 207 ret = websocket->send(msg, ec); |
| 208 } |
| 209 if (ec) |
| 210 return throwError(ec); |
| 211 return v8Boolean(ret); |
| 212 } |
| 213 |
| 214 CALLBACK_FUNC_DEC(WebSocketClose) |
| 215 { |
| 216 INC_STATS("DOM.WebSocket.close()"); |
| 217 WebSocket* websocket = V8Proxy::convertToNativeObject<WebSocket>(V8ClassInde
x::WEBSOCKET, args.Holder()); |
| 218 |
| 219 websocket->close(); |
| 220 return v8::Undefined(); |
| 221 } |
| 222 |
| 223 } // namespace WebCore |
OLD | NEW |