WebCore: JavaScript bindings of WebSocket addEventListener/removeEventListener
authorukai@chromium.org <ukai@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 13 Oct 2009 01:44:18 +0000 (01:44 +0000)
committerukai@chromium.org <ukai@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 13 Oct 2009 01:44:18 +0000 (01:44 +0000)
https://bugs.webkit.org/show_bug.cgi?id=29841

Patch by Fumitoshi Ukai <ukai@chromium.org> on 2009-10-06
Reviewed by NOBODY (OOPS!).

Test: fast/websockets/websocket-event-target.html

* bindings/js/JSWebSocketCustom.cpp:
(WebCore::JSWebSocket::addEventListener):
(WebCore::JSWebSocket::removeEventListener):
* bindings/v8/custom/V8CustomBinding.h:
* bindings/v8/custom/V8WebSocketCustom.cpp:
(WebCore::CALLBACK_FUNC_DECL):
* websockets/WebSocket.idl:

WebKitTools: Enable experimentalWebSocket in DumpRenderTree for LayoutTest.
https://bugs.webkit.org/show_bug.cgi?id=29841

Patch by Fumitoshi Ukai <ukai@chromium.org> on 2009-10-06
Reviewed by NOBODY (OOPS!).

* DumpRenderTree/mac/DumpRenderTree.mm:
(resetDefaultsToConsistentValues):
* DumpRenderTree/win/DumpRenderTree.cpp:
(resetDefaultsToConsistentValues):

LayoutTests: Add a test to verify WebSocket's EventTarget methods.
https://bugs.webkit.org/show_bug.cgi?id=29841

Patch by Fumitoshi Ukai <ukai@chromium.org> on 2009-10-06
Reviewed by NOBODY (OOPS!).

* fast/websockets/script-tests/TEMPLATE.html: Copied from LayoutTests/fast/canvas/script-tests/TEMPLATE.html.
* fast/websockets/script-tests/websocket-event-target.js: Added.
(openListener):
(messageListener):
(closeListener):
* fast/websockets/websocket-event-target-expected.txt: Added.
* fast/websockets/websocket-event-target.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@49488 268f45cc-cd09-0410-ab3c-d52691b4dbfc

13 files changed:
LayoutTests/ChangeLog
LayoutTests/fast/websockets/script-tests/TEMPLATE.html [new file with mode: 0644]
LayoutTests/fast/websockets/script-tests/websocket-event-target.js [new file with mode: 0644]
LayoutTests/fast/websockets/websocket-event-target-expected.txt [new file with mode: 0644]
LayoutTests/fast/websockets/websocket-event-target.html [new file with mode: 0644]
WebCore/ChangeLog
WebCore/bindings/js/JSWebSocketCustom.cpp
WebCore/bindings/v8/custom/V8CustomBinding.h
WebCore/bindings/v8/custom/V8WebSocketCustom.cpp
WebCore/websockets/WebSocket.idl
WebKitTools/ChangeLog
WebKitTools/DumpRenderTree/mac/DumpRenderTree.mm
WebKitTools/DumpRenderTree/win/DumpRenderTree.cpp

index c09a103..49e5aeb 100644 (file)
@@ -1,3 +1,18 @@
+2009-10-12  Fumitoshi Ukai  <ukai@chromium.org>
+
+        Reviewed by Sam Weinig.
+
+        Add a test to verify WebSocket's EventTarget methods.
+        https://bugs.webkit.org/show_bug.cgi?id=29841
+
+        * fast/websockets/script-tests/TEMPLATE.html: Copied from LayoutTests/fast/canvas/script-tests/TEMPLATE.html.
+        * fast/websockets/script-tests/websocket-event-target.js: Added.
+        (openListener):
+        (messageListener):
+        (closeListener):
+        * fast/websockets/websocket-event-target-expected.txt: Added.
+        * fast/websockets/websocket-event-target.html: Added.
+
 2009-10-12  Sam Weinig  <sam@webkit.org>
 
         Reviewed by Darin Adler.
diff --git a/LayoutTests/fast/websockets/script-tests/TEMPLATE.html b/LayoutTests/fast/websockets/script-tests/TEMPLATE.html
new file mode 100644 (file)
index 0000000..1936e0d
--- /dev/null
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<link rel="stylesheet" href="../js/resources/js-test-style.css">
+<script src="../js/resources/js-test-pre.js"></script>
+</head>
+<body>
+<p id="description"></p>
+<div id="console"></div>
+<script src="YOUR_JS_FILE_HERE"></script>
+<script src="../js/resources/js-test-post.js"></script>
+</body>
+</html>
diff --git a/LayoutTests/fast/websockets/script-tests/websocket-event-target.js b/LayoutTests/fast/websockets/script-tests/websocket-event-target.js
new file mode 100644 (file)
index 0000000..a6f25af
--- /dev/null
@@ -0,0 +1,54 @@
+description("Make sure WebSocket object acts as EventTarget.");
+
+var ws = new WebSocket("ws://localhost:8000");
+var open_event_handled = false;
+var message_event_handled = false;
+var close_event_handled = false;
+function openListener() {
+    open_event_handled = true;
+};
+function messageListener() {
+    message_event_handled = true;
+};
+function closeListener() {
+    close_event_handled = true;
+}
+ws.addEventListener('open', openListener, false);
+ws.addEventListener('message', messageListener, false);
+ws.addEventListener('close', closeListener, false);
+
+shouldBe("open_event_handled", "false");
+shouldBe("message_event_handled", "false");
+shouldBe("close_event_handled", "false");
+
+var evt = document.createEvent("Events");
+evt.initEvent("open", true, false);
+ws.dispatchEvent(evt);
+shouldBe("open_event_handled", "true");
+
+open_event_handled = false;
+ws.removeEventListener('open', openListener);
+ws.dispatchEvent(evt);
+shouldBe("open_event_handled", "false");
+
+evt = document.createEvent("MessageEvent");
+evt.initEvent("message", true, false);
+ws.dispatchEvent(evt);
+shouldBe("message_event_handled", "true");
+
+message_event_handled = false;
+ws.removeEventListener('message', messageListener);
+ws.dispatchEvent(evt);
+shouldBe("message_event_handled", "false");
+
+evt = document.createEvent("Events");
+evt.initEvent("close", true, false);
+ws.dispatchEvent(evt);
+shouldBe("close_event_handled", "true");
+
+close_event_handled = false;
+ws.removeEventListener('close', closeListener);
+ws.dispatchEvent(evt);
+shouldBe("close_event_handled", "false");
+
+var successfullyParsed = true;
diff --git a/LayoutTests/fast/websockets/websocket-event-target-expected.txt b/LayoutTests/fast/websockets/websocket-event-target-expected.txt
new file mode 100644 (file)
index 0000000..86f206e
--- /dev/null
@@ -0,0 +1,18 @@
+Make sure WebSocket object acts as EventTarget.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS open_event_handled is false
+PASS message_event_handled is false
+PASS close_event_handled is false
+PASS open_event_handled is true
+PASS open_event_handled is false
+PASS message_event_handled is true
+PASS message_event_handled is false
+PASS close_event_handled is true
+PASS close_event_handled is false
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/fast/websockets/websocket-event-target.html b/LayoutTests/fast/websockets/websocket-event-target.html
new file mode 100644 (file)
index 0000000..6b19ee0
--- /dev/null
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<link rel="stylesheet" href="../js/resources/js-test-style.css">
+<script src="../js/resources/js-test-pre.js"></script>
+</head>
+<body>
+<p id="description"></p>
+<div id="console"></div>
+<script src="script-tests/websocket-event-target.js"></script>
+<script src="../js/resources/js-test-post.js"></script>
+</body>
+</html>
index 27008d7..bffe3b4 100644 (file)
@@ -1,3 +1,20 @@
+2009-10-12  Fumitoshi Ukai  <ukai@chromium.org>
+
+        Reviewed by Sam Weinig.
+
+        JavaScript bindings of WebSocket addEventListener/removeEventListener
+        https://bugs.webkit.org/show_bug.cgi?id=29841
+
+        Test: fast/websockets/websocket-event-target.html
+
+        * bindings/js/JSWebSocketCustom.cpp:
+        (WebCore::JSWebSocket::addEventListener):
+        (WebCore::JSWebSocket::removeEventListener):
+        * bindings/v8/custom/V8CustomBinding.h:
+        * bindings/v8/custom/V8WebSocketCustom.cpp:
+        (WebCore::CALLBACK_FUNC_DECL):
+        * websockets/WebSocket.idl:
+
 2009-10-12  Sam Weinig  <sam@webkit.org>
 
         Reviewed by Darin Adler.
index d305502..e434cb0 100644 (file)
@@ -36,6 +36,7 @@
 #include "JSWebSocket.h"
 
 #include "KURL.h"
+#include "JSEventListener.h"
 #include "WebSocket.h"
 #include "NotImplemented.h"
 #include <runtime/Error.h>
@@ -59,7 +60,33 @@ JSValue JSWebSocket::send(ExecState* exec, const ArgList& args)
     return ret;
 }
 
-// FIXME: implement addEventListener/removeEventListener.
+JSValue JSWebSocket::addEventListener(ExecState* exec, const ArgList& args)
+{
+    JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(impl()->scriptExecutionContext());
+    if (!globalObject)
+        return jsUndefined();
+
+    JSValue listener = args.at(1);
+    if (!listener.isObject())
+        return jsUndefined();
+
+    impl()->addEventListener(args.at(0).toString(exec), JSEventListener::create(asObject(listener), false), args.at(2).toBoolean(exec));
+    return jsUndefined();
+}
+
+JSValue JSWebSocket::removeEventListener(ExecState* exec, const ArgList& args)
+{
+    JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(impl()->scriptExecutionContext());
+    if (!globalObject)
+        return jsUndefined();
+
+    JSValue listener = args.at(1);
+    if (!listener.isObject())
+        return jsUndefined();
+
+    impl()->removeEventListener(args.at(0).toString(exec), JSEventListener::create(asObject(listener), false).get(), args.at(2).toBoolean(exec));
+    return jsUndefined();
+}
 
 }  // namespace WebCore
 
index 2ff4ea9..d8cc354 100644 (file)
@@ -627,6 +627,8 @@ namespace WebCore {
         DECLARE_PROPERTY_ACCESSOR(WebSocketOnmessage);
         DECLARE_PROPERTY_ACCESSOR(WebSocketOnclose);
         DECLARE_CALLBACK(WebSocketConstructor);
+        DECLARE_CALLBACK(WebSocketAddEventListener);
+        DECLARE_CALLBACK(WebSocketRemoveEventListener);
         DECLARE_CALLBACK(WebSocketSend);
         DECLARE_CALLBACK(WebSocketClose);
 #endif
index f498e4f..b20635b 100644 (file)
 
 namespace WebCore {
 
-// ??? AddEventListener, RemoveEventListener
+CALLBACK_FUNC_DECL(WebSocketAddEventListener)
+{
+    INC_STATS("DOM.WebSocket.addEventListener()");
+    WebSocket* webSocket = V8DOMWrapper::convertToNativeObject<WebSocket>(V8ClassIndex::WEBSOCKET, args.Holder());
+
+    RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(webSocket, args[1], false, ListenerFindOrCreate);
+    if (listener) {
+        String type = toWebCoreString(args[0]);
+        bool useCapture = args[2]->BooleanValue();
+        webSocket->addEventListener(type, listener, useCapture);
+
+        createHiddenDependency(args.Holder(), args[1], V8Custom::kWebSocketCacheIndex);
+    }
+    return v8::Undefined();
+}
+
+CALLBACK_FUNC_DECL(WebSocketRemoveEventListener)
+{
+    INC_STATS("DOM.WebSocket.removeEventListener()");
+    WebSocket* webSocket = V8DOMWrapper::convertToNativeObject<WebSocket>(V8ClassIndex::WEBSOCKET, args.Holder());
+
+    RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(webSocket, args[1], false, ListenerFindOnly);
+    if (listener) {
+        String type = toWebCoreString(args[0]);
+        bool useCapture = args[2]->BooleanValue();
+        webSocket->removeEventListener(type, listener.get(), useCapture);
+        removeHiddenDependency(args.Holder(), args[1], V8Custom::kWebSocketCacheIndex);
+    }
+    return v8::Undefined();
+}
 
 CALLBACK_FUNC_DECL(WebSocketConstructor)
 {
index 04606fe..c662940 100644 (file)
@@ -55,13 +55,13 @@ module websockets {
         void close();
 
         // EventTarget interface
-        // [Custom] void addEventListener(in DOMString type,
-        //   in EventListener listener,
-        //   in boolean useCapture);
-        // [Custom] void removeEventListener(in DOMString type,
-        //   in EventListener listener,
-        //   in boolean useCapture);
-        // boolean dispatchEvent(in Event evt)
-        //   raises(EventException);
+        [Custom] void addEventListener(in DOMString type,
+                                       in EventListener listener,
+                                       in boolean useCapture);
+        [Custom] void removeEventListener(in DOMString type,
+                                          in EventListener listener,
+                                          in boolean useCapture);
+        boolean dispatchEvent(in Event evt)
+            raises(EventException);
     };
 }
index 381460f..e8296aa 100644 (file)
@@ -1,3 +1,15 @@
+2009-10-12  Fumitoshi Ukai  <ukai@chromium.org>
+
+        Reviewed by Sam Weinig.
+
+        Enable experimentalWebSocket in DumpRenderTree for LayoutTest.
+        https://bugs.webkit.org/show_bug.cgi?id=29841
+
+        * DumpRenderTree/mac/DumpRenderTree.mm:
+        (resetDefaultsToConsistentValues):
+        * DumpRenderTree/win/DumpRenderTree.cpp:
+        (resetDefaultsToConsistentValues):
+
 2009-10-12  Yaar Schnitman  <yaar@chromium.org>
 
         Reviewed by Dimitri Glazkov.
index df37eea..11348d7 100644 (file)
@@ -409,7 +409,7 @@ static void resetDefaultsToConsistentValues()
     [preferences setCacheModel:WebCacheModelDocumentBrowser];
     [preferences setXSSAuditorEnabled:NO];
     [preferences setExperimentalNotificationsEnabled:NO];
-    [preferences setExperimentalWebSocketsEnabled:NO];
+    [preferences setExperimentalWebSocketsEnabled:YES];
     [preferences setPluginAllowedRunTime:1];
 
     [preferences setPrivateBrowsingEnabled:NO];
index 7e013a7..1ad4f4f 100644 (file)
@@ -737,7 +737,7 @@ static void resetDefaultsToConsistentValues(IWebPreferences* preferences)
         prefsPrivate->setAuthorAndUserStylesEnabled(TRUE);
         prefsPrivate->setDeveloperExtrasEnabled(FALSE);
         prefsPrivate->setExperimentalNotificationsEnabled(TRUE);
-        prefsPrivate->setExperimentalWebSocketsEnabled(FALSE);
+        prefsPrivate->setExperimentalWebSocketsEnabled(TRUE);
         prefsPrivate->setShouldPaintNativeControls(FALSE); // FIXME - need to make DRT pass with Windows native controls <http://bugs.webkit.org/show_bug.cgi?id=25592>
         prefsPrivate->setXSSAuditorEnabled(FALSE);
         prefsPrivate->setOfflineWebApplicationCacheEnabled(TRUE);