WebSocketChannel minor refactoring for code manageability
authortoyoshim@chromium.org <toyoshim@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 15 Feb 2012 06:39:35 +0000 (06:39 +0000)
committertoyoshim@chromium.org <toyoshim@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 15 Feb 2012 06:39:35 +0000 (06:39 +0000)
https://bugs.webkit.org/show_bug.cgi?id=78576

Reviewed by Kent Tamura.

Change the first argument type of WebSocketChannel
from ScriptExecutionContext to Document.
WebSocketChannel always assume this ScriptExecutionContext must
inherit Document. Then, it results in many static cast.
It isn't readable and dangerous against future code changes.

* websockets/ThreadableWebSocketChannel.cpp: Pass the first argument for WebSocketChannel as Document.
(WebCore::ThreadableWebSocketChannel::create):
* websockets/WebSocketChannel.cpp: Replace all ScriptExecutionContext* m_context descriptions to Document* m_document.
(WebCore::WebSocketChannel::WebSocketChannel):
(WebCore::WebSocketChannel::connect):
(WebCore::WebSocketChannel::fail):
(WebCore::WebSocketChannel::disconnect):
(WebCore::WebSocketChannel::didOpenSocketStream):
(WebCore::WebSocketChannel::didCloseSocketStream):
(WebCore::WebSocketChannel::didReceiveSocketStreamData):
(WebCore::WebSocketChannel::didFailSocketStream):
(WebCore::WebSocketChannel::processBuffer):
(WebCore::WebSocketChannel::processOutgoingFrameQueue):
* websockets/WebSocketChannel.h: Change the first argument for construction to Document and hold it as Document m_document.
(WebCore):
(WebCore::WebSocketChannel::create):
(WebSocketChannel):
* websockets/WorkerThreadableWebSocketChannel.cpp: Pass the first argument for WebSocketChannel as Document.
(WebCore::WorkerThreadableWebSocketChannel::Peer::Peer):

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

Source/WebCore/ChangeLog
Source/WebCore/websockets/ThreadableWebSocketChannel.cpp
Source/WebCore/websockets/WebSocketChannel.cpp
Source/WebCore/websockets/WebSocketChannel.h
Source/WebCore/websockets/WorkerThreadableWebSocketChannel.cpp

index 4cd9a82..2181e57 100644 (file)
@@ -1,3 +1,36 @@
+2012-02-14  Takashi Toyoshima  <toyoshim@chromium.org>
+
+        WebSocketChannel minor refactoring for code manageability
+        https://bugs.webkit.org/show_bug.cgi?id=78576
+
+        Reviewed by Kent Tamura.
+
+        Change the first argument type of WebSocketChannel
+        from ScriptExecutionContext to Document.
+        WebSocketChannel always assume this ScriptExecutionContext must
+        inherit Document. Then, it results in many static cast.
+        It isn't readable and dangerous against future code changes.
+
+        * websockets/ThreadableWebSocketChannel.cpp: Pass the first argument for WebSocketChannel as Document.
+        (WebCore::ThreadableWebSocketChannel::create):
+        * websockets/WebSocketChannel.cpp: Replace all ScriptExecutionContext* m_context descriptions to Document* m_document.
+        (WebCore::WebSocketChannel::WebSocketChannel):
+        (WebCore::WebSocketChannel::connect):
+        (WebCore::WebSocketChannel::fail):
+        (WebCore::WebSocketChannel::disconnect):
+        (WebCore::WebSocketChannel::didOpenSocketStream):
+        (WebCore::WebSocketChannel::didCloseSocketStream):
+        (WebCore::WebSocketChannel::didReceiveSocketStreamData):
+        (WebCore::WebSocketChannel::didFailSocketStream):
+        (WebCore::WebSocketChannel::processBuffer):
+        (WebCore::WebSocketChannel::processOutgoingFrameQueue):
+        * websockets/WebSocketChannel.h: Change the first argument for construction to Document and hold it as Document m_document.
+        (WebCore):
+        (WebCore::WebSocketChannel::create):
+        (WebSocketChannel):
+        * websockets/WorkerThreadableWebSocketChannel.cpp: Pass the first argument for WebSocketChannel as Document.
+        (WebCore::WorkerThreadableWebSocketChannel::Peer::Peer):
+
 2012-02-14  Noel Gordon  <noel.gordon@gmail.com>
 
         Unreviewed, rolling out r107774.
index 17f24fb..029e4ff 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 Google Inc.  All rights reserved.
+ * Copyright (C) 2009, 2012 Google Inc.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -34,6 +34,7 @@
 
 #include "ThreadableWebSocketChannel.h"
 
+#include "Document.h"
 #include "PlatformString.h"
 #include "ScriptExecutionContext.h"
 #include "ThreadableWebSocketChannelClientWrapper.h"
@@ -66,7 +67,7 @@ PassRefPtr<ThreadableWebSocketChannel> ThreadableWebSocketChannel::create(Script
 #endif // ENABLE(WORKERS)
 
     ASSERT(context->isDocument());
-    return WebSocketChannel::create(context, client);
+    return WebSocketChannel::create(static_cast<Document*>(context), client);
 }
 
 } // namespace WebCore
index 18f4351..8728e16 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Google Inc.  All rights reserved.
+ * Copyright (C) 2011, 2012 Google Inc.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -88,8 +88,8 @@ const WebSocketChannel::OpCode WebSocketChannel::OpCodeClose = 0x8;
 const WebSocketChannel::OpCode WebSocketChannel::OpCodePing = 0x9;
 const WebSocketChannel::OpCode WebSocketChannel::OpCodePong = 0xA;
 
-WebSocketChannel::WebSocketChannel(ScriptExecutionContext* context, WebSocketChannelClient* client)
-    : m_context(context)
+WebSocketChannel::WebSocketChannel(Document* document, WebSocketChannelClient* client)
+    : m_document(document)
     , m_client(client)
     , m_buffer(0)
     , m_bufferSize(0)
@@ -110,12 +110,10 @@ WebSocketChannel::WebSocketChannel(ScriptExecutionContext* context, WebSocketCha
     , m_blobLoaderStatus(BlobLoaderNotStarted)
 #endif
 {
-    ASSERT(m_context->isDocument());
-    Document* document = static_cast<Document*>(m_context);
-    if (Settings* settings = document->settings())
+    if (Settings* settings = m_document->settings())
         m_useHixie76Protocol = settings->useHixie76WebSocketProtocol();
 
-    if (Page* page = document->page())
+    if (Page* page = m_document->page())
         m_identifier = page->progress()->createUniqueIdentifier();
 }
 
@@ -134,10 +132,10 @@ void WebSocketChannel::connect(const KURL& url, const String& protocol)
     LOG(Network, "WebSocketChannel %p connect", this);
     ASSERT(!m_handle);
     ASSERT(!m_suspended);
-    m_handshake = adoptPtr(new WebSocketHandshake(url, protocol, m_context, m_useHixie76Protocol));
+    m_handshake = adoptPtr(new WebSocketHandshake(url, protocol, m_document, m_useHixie76Protocol));
     m_handshake->reset();
     if (m_identifier)
-        InspectorInstrumentation::didCreateWebSocket(m_context, m_identifier, url, m_context->url());
+        InspectorInstrumentation::didCreateWebSocket(m_document, m_identifier, url, m_document->url());
     ref();
     m_handle = SocketStreamHandle::create(m_handshake->url(), this);
 }
@@ -228,8 +226,8 @@ void WebSocketChannel::fail(const String& reason)
 {
     LOG(Network, "WebSocketChannel %p fail: %s", this, reason.utf8().data());
     ASSERT(!m_suspended);
-    if (m_context)
-        m_context->addConsoleMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, reason, m_handshake->clientOrigin());
+    if (m_document)
+        m_document->addConsoleMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, reason, m_handshake->clientOrigin());
     if (!m_useHixie76Protocol) {
         // Hybi-10 specification explicitly states we must not continue to handle incoming data
         // once the WebSocket connection is failed (section 7.1.7).
@@ -247,12 +245,12 @@ void WebSocketChannel::fail(const String& reason)
 void WebSocketChannel::disconnect()
 {
     LOG(Network, "WebSocketChannel %p disconnect", this);
-    if (m_identifier && m_context)
-        InspectorInstrumentation::didCloseWebSocket(m_context, m_identifier);
+    if (m_identifier && m_document)
+        InspectorInstrumentation::didCloseWebSocket(m_document, m_identifier);
     if (m_handshake)
         m_handshake->clearScriptExecutionContext();
     m_client = 0;
-    m_context = 0;
+    m_document = 0;
     if (m_handle)
         m_handle->disconnect();
 }
@@ -273,10 +271,10 @@ void WebSocketChannel::didOpenSocketStream(SocketStreamHandle* handle)
 {
     LOG(Network, "WebSocketChannel %p didOpenSocketStream", this);
     ASSERT(handle == m_handle);
-    if (!m_context)
+    if (!m_document)
         return;
     if (m_identifier)
-        InspectorInstrumentation::willSendWebSocketHandshakeRequest(m_context, m_identifier, m_handshake->clientHandshakeRequest());
+        InspectorInstrumentation::willSendWebSocketHandshakeRequest(m_document, m_identifier, m_handshake->clientHandshakeRequest());
     CString handshakeMessage = m_handshake->clientHandshakeMessage();
     if (!handle->send(handshakeMessage.data(), handshakeMessage.length()))
         fail("Failed to send WebSocket handshake.");
@@ -285,8 +283,8 @@ void WebSocketChannel::didOpenSocketStream(SocketStreamHandle* handle)
 void WebSocketChannel::didCloseSocketStream(SocketStreamHandle* handle)
 {
     LOG(Network, "WebSocketChannel %p didCloseSocketStream", this);
-    if (m_identifier && m_context)
-        InspectorInstrumentation::didCloseWebSocket(m_context, m_identifier);
+    if (m_identifier && m_document)
+        InspectorInstrumentation::didCloseWebSocket(m_document, m_identifier);
     ASSERT_UNUSED(handle, handle == m_handle || !m_handle);
     m_closed = true;
     if (m_closingTimer.isActive())
@@ -299,7 +297,7 @@ void WebSocketChannel::didCloseSocketStream(SocketStreamHandle* handle)
             return;
         WebSocketChannelClient* client = m_client;
         m_client = 0;
-        m_context = 0;
+        m_document = 0;
         m_handle = 0;
         if (client)
             client->didClose(m_unhandledBufferedAmount, m_receivedClosingHandshake ? WebSocketChannelClient::ClosingHandshakeComplete : WebSocketChannelClient::ClosingHandshakeIncomplete, m_closeEventCode, m_closeEventReason);
@@ -312,7 +310,7 @@ void WebSocketChannel::didReceiveSocketStreamData(SocketStreamHandle* handle, co
     LOG(Network, "WebSocketChannel %p didReceiveSocketStreamData %d", this, len);
     RefPtr<WebSocketChannel> protect(this); // The client can close the channel, potentially removing the last reference.
     ASSERT(handle == m_handle);
-    if (!m_context) {
+    if (!m_document) {
         return;
     }
     if (len <= 0) {
@@ -346,7 +344,7 @@ void WebSocketChannel::didFailSocketStream(SocketStreamHandle* handle, const Soc
 {
     LOG(Network, "WebSocketChannel %p didFailSocketStream", this);
     ASSERT(handle == m_handle || !m_handle);
-    if (m_context) {
+    if (m_document) {
         String message;
         if (error.isNull())
             message = "WebSocket network error";
@@ -358,7 +356,7 @@ void WebSocketChannel::didFailSocketStream(SocketStreamHandle* handle, const Soc
         ASSERT(failingURL.isNull() || m_handshake->url().string() == failingURL);
         if (failingURL.isNull())
             failingURL = m_handshake->url().string();
-        m_context->addConsoleMessage(NetworkMessageSource, LogMessageType, ErrorMessageLevel, message, failingURL);
+        m_document->addConsoleMessage(NetworkMessageSource, LogMessageType, ErrorMessageLevel, message, failingURL);
     }
     m_shouldDiscardReceivedData = true;
     handle->disconnect();
@@ -464,14 +462,11 @@ bool WebSocketChannel::processBuffer()
             return false;
         if (m_handshake->mode() == WebSocketHandshake::Connected) {
             if (m_identifier)
-                InspectorInstrumentation::didReceiveWebSocketHandshakeResponse(m_context, m_identifier, m_handshake->serverHandshakeResponse());
+                InspectorInstrumentation::didReceiveWebSocketHandshakeResponse(m_document, m_identifier, m_handshake->serverHandshakeResponse());
             if (!m_handshake->serverSetCookie().isEmpty()) {
-                if (m_context->isDocument()) {
-                    Document* document = static_cast<Document*>(m_context);
-                    if (cookiesEnabled(document)) {
-                        ExceptionCode ec; // Exception (for sandboxed documents) ignored.
-                        document->setCookie(m_handshake->serverSetCookie(), ec);
-                    }
+                if (cookiesEnabled(m_document)) {
+                    ExceptionCode ec; // Exception (for sandboxed documents) ignored.
+                    m_document->setCookie(m_handshake->serverSetCookie(), ec);
                 }
             }
             // FIXME: handle set-cookie2.
@@ -922,7 +917,7 @@ void WebSocketChannel::processOutgoingFrameQueue()
                 ASSERT(!m_blobLoader);
                 m_blobLoader = adoptPtr(new FileReaderLoader(FileReaderLoader::ReadAsArrayBuffer, this));
                 m_blobLoaderStatus = BlobLoaderStarted;
-                m_blobLoader->start(m_context, frame->blobData.get());
+                m_blobLoader->start(m_document, frame->blobData.get());
                 m_outgoingFrameQueue.prepend(frame.release());
                 return;
 
index 8bdb47f..73fc52d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Google Inc.  All rights reserved.
+ * Copyright (C) 2011, 2012 Google Inc.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -46,8 +46,8 @@
 namespace WebCore {
 
 class Blob;
+class Document;
 class FileReaderLoader;
-class ScriptExecutionContext;
 class SocketStreamHandle;
 class SocketStreamError;
 class WebSocketChannelClient;
@@ -59,7 +59,7 @@ class WebSocketChannel : public RefCounted<WebSocketChannel>, public SocketStrea
 {
     WTF_MAKE_FAST_ALLOCATED;
 public:
-    static PassRefPtr<WebSocketChannel> create(ScriptExecutionContext* context, WebSocketChannelClient* client) { return adoptRef(new WebSocketChannel(context, client)); }
+    static PassRefPtr<WebSocketChannel> create(Document* document, WebSocketChannelClient* client) { return adoptRef(new WebSocketChannel(document, client)); }
     virtual ~WebSocketChannel();
 
     bool send(const char* data, int length);
@@ -119,7 +119,7 @@ protected:
     virtual void derefThreadableWebSocketChannel() { deref(); }
 
 private:
-    WebSocketChannel(ScriptExecutionContext*, WebSocketChannelClient*);
+    WebSocketChannel(Document*, WebSocketChannelClient*);
 
     bool appendToBuffer(const char* data, size_t len);
     void skipBuffer(size_t len);
@@ -220,7 +220,7 @@ private:
     };
 #endif
 
-    ScriptExecutionContext* m_context;
+    Document* m_document;
     WebSocketChannelClient* m_client;
     OwnPtr<WebSocketHandshake> m_handshake;
     RefPtr<SocketStreamHandle> m_handle;
index af03a4e..b77ea88 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Google Inc.  All rights reserved.
+ * Copyright (C) 2011, 2012 Google Inc.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -36,6 +36,7 @@
 
 #include "Blob.h"
 #include "CrossThreadTask.h"
+#include "Document.h"
 #include "PlatformString.h"
 #include "ScriptExecutionContext.h"
 #include "ThreadableWebSocketChannelClientWrapper.h"
@@ -151,7 +152,7 @@ void WorkerThreadableWebSocketChannel::resume()
 WorkerThreadableWebSocketChannel::Peer::Peer(PassRefPtr<ThreadableWebSocketChannelClientWrapper> clientWrapper, WorkerLoaderProxy& loaderProxy, ScriptExecutionContext* context, const String& taskMode)
     : m_workerClientWrapper(clientWrapper)
     , m_loaderProxy(loaderProxy)
-    , m_mainWebSocketChannel(WebSocketChannel::create(context, this))
+    , m_mainWebSocketChannel(WebSocketChannel::create(static_cast<Document*>(context), this))
     , m_taskMode(taskMode)
 {
     ASSERT(isMainThread());