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 "V8WebSocket.h"
36
37 #include "Frame.h"
38 #include "Settings.h"
39 #include "V8Binding.h"
40 #include "V8Proxy.h"
41 #include "V8Utilities.h"
42 #include "WebSocket.h"
43 #include "WorkerContext.h"
44 #include "WorkerContextExecutionProxy.h"
45
46 namespace WebCore {
47
addEventListenerCallback(const v8::Arguments & args)48 v8::Handle<v8::Value> V8WebSocket::addEventListenerCallback(const v8::Arguments& args)
49 {
50 INC_STATS("DOM.WebSocket.addEventListener()");
51 WebSocket* webSocket = V8WebSocket::toNative(args.Holder());
52
53 RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(webSocket, args[1], false, ListenerFindOrCreate);
54 if (listener) {
55 String type = toWebCoreString(args[0]);
56 bool useCapture = args[2]->BooleanValue();
57 webSocket->addEventListener(type, listener, useCapture);
58
59 createHiddenDependency(args.Holder(), args[1], cacheIndex);
60 }
61 return v8::Undefined();
62 }
63
removeEventListenerCallback(const v8::Arguments & args)64 v8::Handle<v8::Value> V8WebSocket::removeEventListenerCallback(const v8::Arguments& args)
65 {
66 INC_STATS("DOM.WebSocket.removeEventListener()");
67 WebSocket* webSocket = V8WebSocket::toNative(args.Holder());
68
69 RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(webSocket, args[1], false, ListenerFindOnly);
70 if (listener) {
71 String type = toWebCoreString(args[0]);
72 bool useCapture = args[2]->BooleanValue();
73 webSocket->removeEventListener(type, listener.get(), useCapture);
74 removeHiddenDependency(args.Holder(), args[1], cacheIndex);
75 }
76 return v8::Undefined();
77 }
78
constructorCallback(const v8::Arguments & args)79 v8::Handle<v8::Value> V8WebSocket::constructorCallback(const v8::Arguments& args)
80 {
81 INC_STATS("DOM.WebSocket.Constructor");
82
83 if (!args.IsConstructCall())
84 return throwError("DOM object custructor cannot be called as a function.");
85 if (args.Length() == 0)
86 return throwError("Not enough arguments", V8Proxy::SyntaxError);
87
88 v8::TryCatch tryCatch;
89 v8::Handle<v8::String> urlstring = args[0]->ToString();
90 if (tryCatch.HasCaught())
91 return throwError(tryCatch.Exception());
92 if (urlstring.IsEmpty())
93 return throwError("Empty URL", V8Proxy::SyntaxError);
94
95 // Get the script execution context.
96 ScriptExecutionContext* context = getScriptExecutionContext();
97 if (!context)
98 return throwError("WebSocket constructor's associated frame is not available", V8Proxy::ReferenceError);
99
100 const KURL& url = context->completeURL(toWebCoreString(urlstring));
101
102 RefPtr<WebSocket> webSocket = WebSocket::create(context);
103 ExceptionCode ec = 0;
104
105 if (args.Length() < 2)
106 webSocket->connect(url, ec);
107 else {
108 v8::TryCatch tryCatchProtocol;
109 v8::Handle<v8::String> protocol = args[1]->ToString();
110 if (tryCatchProtocol.HasCaught())
111 return throwError(tryCatchProtocol.Exception());
112 webSocket->connect(url, toWebCoreString(protocol), ec);
113 }
114 if (ec)
115 return throwError(ec);
116
117 // Setup the standard wrapper object internal fields.
118 V8DOMWrapper::setDOMWrapper(args.Holder(), V8ClassIndex::ToInt(V8ClassIndex::WEBSOCKET), webSocket.get());
119
120 // Add object to the wrapper map.
121 webSocket->ref();
122 V8DOMWrapper::setJSWrapperForActiveDOMObject(webSocket.get(), v8::Persistent<v8::Object>::New(args.Holder()));
123
124 return args.Holder();
125 }
126
sendCallback(const v8::Arguments & args)127 v8::Handle<v8::Value> V8WebSocket::sendCallback(const v8::Arguments& args)
128 {
129 INC_STATS("DOM.WebSocket.send()");
130 WebSocket* webSocket = V8WebSocket::toNative(args.Holder());
131
132 ExceptionCode ec = 0;
133 bool ret = false;
134 if (args.Length() < 1)
135 return throwError("Not enough arguments", V8Proxy::SyntaxError);
136 else {
137 String msg = toWebCoreString(args[0]);
138 ret = webSocket->send(msg, ec);
139 }
140 if (ec)
141 return throwError(ec);
142 return v8Boolean(ret);
143 }
144
145 } // namespace WebCore
146
147 #endif // ENABLE(WEB_SOCKETS)
148