WebSocket: Add binaryType attribute
authoryutak@chromium.org <yutak@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 10 Aug 2011 09:19:51 +0000 (09:19 +0000)
committeryutak@chromium.org <yutak@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 10 Aug 2011 09:19:51 +0000 (09:19 +0000)
https://bugs.webkit.org/show_bug.cgi?id=65967

Reviewed by Kent Tamura.

Source/WebCore:

Add a new attribute "binaryType" to WebSocket. It controls the data type of MessageEvent
for WebSocket's binary frames.

Tests: http/tests/websocket/tests/hixie76/undefined-attributes.html
       http/tests/websocket/tests/hybi/binary-type.html

* websockets/WebSocket.cpp:
(WebCore::WebSocket::WebSocket):
(WebCore::WebSocket::binaryType):
(WebCore::WebSocket::setBinaryType):
* websockets/WebSocket.h:
* websockets/WebSocket.idl:

LayoutTests:

* http/tests/websocket/tests/hixie76/undefined-attributes-expected.txt: Added.
* http/tests/websocket/tests/hixie76/undefined-attributes.html: Added.
* http/tests/websocket/tests/hybi/binary-type-expected.txt: Added.
* http/tests/websocket/tests/hybi/binary-type.html: Added.

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

LayoutTests/ChangeLog
LayoutTests/http/tests/websocket/tests/hixie76/undefined-attributes-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/websocket/tests/hixie76/undefined-attributes.html [new file with mode: 0644]
LayoutTests/http/tests/websocket/tests/hybi/binary-type-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/websocket/tests/hybi/binary-type.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/websockets/WebSocket.cpp
Source/WebCore/websockets/WebSocket.h
Source/WebCore/websockets/WebSocket.idl

index f736095..f3d397a 100644 (file)
@@ -1,5 +1,17 @@
 2011-08-10  Yuta Kitamura  <yutak@chromium.org>
 
+        WebSocket: Add binaryType attribute
+        https://bugs.webkit.org/show_bug.cgi?id=65967
+
+        Reviewed by Kent Tamura.
+
+        * http/tests/websocket/tests/hixie76/undefined-attributes-expected.txt: Added.
+        * http/tests/websocket/tests/hixie76/undefined-attributes.html: Added.
+        * http/tests/websocket/tests/hybi/binary-type-expected.txt: Added.
+        * http/tests/websocket/tests/hybi/binary-type.html: Added.
+
+2011-08-10  Yuta Kitamura  <yutak@chromium.org>
+
         WebSocket: Accept multiple subprotocols
         https://bugs.webkit.org/show_bug.cgi?id=65247
 
diff --git a/LayoutTests/http/tests/websocket/tests/hixie76/undefined-attributes-expected.txt b/LayoutTests/http/tests/websocket/tests/hixie76/undefined-attributes-expected.txt
new file mode 100644 (file)
index 0000000..ec49f62
--- /dev/null
@@ -0,0 +1,9 @@
+Test WebSocket attributes which should not be defined when hixie-76 protocol is used.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+PASS ws.binaryType is undefined.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/http/tests/websocket/tests/hixie76/undefined-attributes.html b/LayoutTests/http/tests/websocket/tests/hixie76/undefined-attributes.html
new file mode 100644 (file)
index 0000000..46eaa0c
--- /dev/null
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+<head>
+<link rel="stylesheet" href="../../../../js-test-resources/js-test-style.css">
+<script src="../../../../js-test-resources/js-test-pre.js"></script>
+</head>
+<body>
+<div id="description"></div>
+<div id="console"></div>
+<script type="text/javascript">
+description("Test WebSocket attributes which should not be defined when hixie-76 protocol is used.");
+
+var ws = new WebSocket("ws://127.0.0.1:8880/websocket/tests/hixie76/simple");
+
+shouldBeUndefined("ws.binaryType");
+
+var successfullyParsed = true;
+</script>
+<script src="../../../../js-test-resources/js-test-post.js"></script>
+</body>
+</html>
diff --git a/LayoutTests/http/tests/websocket/tests/hybi/binary-type-expected.txt b/LayoutTests/http/tests/websocket/tests/hybi/binary-type-expected.txt
new file mode 100644 (file)
index 0000000..a3513d7
--- /dev/null
@@ -0,0 +1,14 @@
+Test WebSocket.binaryType attribute.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+PASS ws.binaryType is "blob"
+PASS ws.binaryType is "arraybuffer"
+PASS ws.binaryType is "blob"
+PASS ws.binaryType = 'Blob' threw exception Error: SYNTAX_ERR: DOM Exception 12.
+PASS ws.binaryType = 'ArrayBuffer' threw exception Error: SYNTAX_ERR: DOM Exception 12.
+PASS ws.binaryType = '' threw exception Error: SYNTAX_ERR: DOM Exception 12.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/http/tests/websocket/tests/hybi/binary-type.html b/LayoutTests/http/tests/websocket/tests/hybi/binary-type.html
new file mode 100644 (file)
index 0000000..f086356
--- /dev/null
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html>
+<head>
+<link rel="stylesheet" href="../../../../js-test-resources/js-test-style.css">
+<script src="../../../../js-test-resources/js-test-pre.js"></script>
+</head>
+<body>
+<div id="description"></div>
+<div id="console"></div>
+<script>
+description("Test WebSocket.binaryType attribute.");
+
+if (window.layoutTestController)
+    layoutTestController.overridePreference("WebKitHixie76WebSocketProtocolEnabled", 0);
+
+var ws = new WebSocket("ws://localhost:8880/websocket/tests/hybi/simple");
+shouldBeEqualToString("ws.binaryType", "blob");
+
+ws.binaryType = "arraybuffer";
+shouldBeEqualToString("ws.binaryType", "arraybuffer");
+
+ws.binaryType = "blob";
+shouldBeEqualToString("ws.binaryType", "blob");
+
+shouldThrow("ws.binaryType = 'Blob'", "'Error: SYNTAX_ERR: DOM Exception 12'");
+shouldThrow("ws.binaryType = 'ArrayBuffer'", "'Error: SYNTAX_ERR: DOM Exception 12'");
+shouldThrow("ws.binaryType = ''", "'Error: SYNTAX_ERR: DOM Exception 12'");
+
+var successfullyParsed = true;
+</script>
+<script src="../../../../js-test-resources/js-test-post.js"></script>
+</body>
+</html>
index 7194fec..5cc8c3d 100644 (file)
@@ -1,5 +1,25 @@
 2011-08-10  Yuta Kitamura  <yutak@chromium.org>
 
+        WebSocket: Add binaryType attribute
+        https://bugs.webkit.org/show_bug.cgi?id=65967
+
+        Reviewed by Kent Tamura.
+
+        Add a new attribute "binaryType" to WebSocket. It controls the data type of MessageEvent
+        for WebSocket's binary frames.
+
+        Tests: http/tests/websocket/tests/hixie76/undefined-attributes.html
+               http/tests/websocket/tests/hybi/binary-type.html
+
+        * websockets/WebSocket.cpp:
+        (WebCore::WebSocket::WebSocket):
+        (WebCore::WebSocket::binaryType):
+        (WebCore::WebSocket::setBinaryType):
+        * websockets/WebSocket.h:
+        * websockets/WebSocket.idl:
+
+2011-08-10  Yuta Kitamura  <yutak@chromium.org>
+
         WebSocket: Accept multiple subprotocols
         https://bugs.webkit.org/show_bug.cgi?id=65247
 
index 2e0721a..a5b8949 100644 (file)
@@ -135,6 +135,7 @@ WebSocket::WebSocket(ScriptExecutionContext* context)
     : ActiveDOMObject(context, this)
     , m_state(CONNECTING)
     , m_bufferedAmountAfterClose(0)
+    , m_binaryType(BinaryTypeBlob)
 {
 }
 
@@ -291,6 +292,36 @@ unsigned long WebSocket::bufferedAmount() const
     return m_bufferedAmountAfterClose;
 }
 
+String WebSocket::binaryType() const
+{
+    if (m_channel->useHixie76Protocol())
+        return String();
+    switch (m_binaryType) {
+    case BinaryTypeBlob:
+        return "blob";
+    case BinaryTypeArrayBuffer:
+        return "arraybuffer";
+    }
+    ASSERT_NOT_REACHED();
+    return String();
+}
+
+void WebSocket::setBinaryType(const String& binaryType, ExceptionCode& ec)
+{
+    if (m_channel->useHixie76Protocol())
+        return;
+    if (binaryType == "blob") {
+        m_binaryType = BinaryTypeBlob;
+        return;
+    }
+    if (binaryType == "arraybuffer") {
+        m_binaryType = BinaryTypeArrayBuffer;
+        return;
+    }
+    ec = SYNTAX_ERR;
+    return;
+}
+
 ScriptExecutionContext* WebSocket::scriptExecutionContext() const
 {
     return ActiveDOMObject::scriptExecutionContext();
index 1fab346..e15c1e1 100644 (file)
@@ -74,6 +74,9 @@ namespace WebCore {
         State readyState() const;
         unsigned long bufferedAmount() const;
 
+        String binaryType() const;
+        void setBinaryType(const String& binaryType, ExceptionCode&);
+
         DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
         DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
         DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
@@ -107,12 +110,18 @@ namespace WebCore {
         virtual EventTargetData* eventTargetData();
         virtual EventTargetData* ensureEventTargetData();
 
+        enum BinaryType {
+            BinaryTypeBlob,
+            BinaryTypeArrayBuffer
+        };
+
         RefPtr<ThreadableWebSocketChannel> m_channel;
 
         State m_state;
         KURL m_url;
         EventTargetData m_eventTargetData;
         unsigned long m_bufferedAmountAfterClose;
+        BinaryType m_binaryType;
     };
 
 } // namespace WebCore
index 4428cc1..752efae 100644 (file)
@@ -58,6 +58,9 @@ module websockets {
         attribute EventListener onerror;
         attribute EventListener onclose;
 
+        attribute [ConvertNullStringTo=Undefined] DOMString binaryType
+            setter raises(DOMException);
+
         [RequiresAllArguments] boolean send(in DOMString data) raises(DOMException);
         void close();