WebCore/websockets/ThreadableWebSocketChannelClientWrapper.h
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 #ifndef ThreadableWebSocketChannelClientWrapper_h
       
    32 #define ThreadableWebSocketChannelClientWrapper_h
       
    33 
       
    34 #if ENABLE(WEB_SOCKETS)
       
    35 
       
    36 #include "PlatformString.h"
       
    37 #include "WebSocketChannelClient.h"
       
    38 #include <wtf/PassRefPtr.h>
       
    39 #include <wtf/Threading.h>
       
    40 #include <wtf/Vector.h>
       
    41 
       
    42 namespace WebCore {
       
    43 
       
    44 class String;
       
    45 
       
    46 class ThreadableWebSocketChannelClientWrapper : public ThreadSafeShared<ThreadableWebSocketChannelClientWrapper> {
       
    47 public:
       
    48     static PassRefPtr<ThreadableWebSocketChannelClientWrapper> create(WebSocketChannelClient* client)
       
    49     {
       
    50         return adoptRef(new ThreadableWebSocketChannelClientWrapper(client));
       
    51     }
       
    52 
       
    53     void clearSyncMethodDone()
       
    54     {
       
    55         m_syncMethodDone = false;
       
    56     }
       
    57     void setSyncMethodDone()
       
    58     {
       
    59         m_syncMethodDone = true;
       
    60     }
       
    61 
       
    62     bool syncMethodDone() const
       
    63     {
       
    64         return m_syncMethodDone;
       
    65     }
       
    66 
       
    67     bool sent() const
       
    68     {
       
    69         return m_sent;
       
    70     }
       
    71     void setSent(bool sent)
       
    72     {
       
    73         m_sent = sent;
       
    74         m_syncMethodDone = true;
       
    75     }
       
    76 
       
    77     unsigned long bufferedAmount() const
       
    78     {
       
    79         return m_bufferedAmount;
       
    80     }
       
    81     void setBufferedAmount(unsigned long bufferedAmount)
       
    82     {
       
    83         m_bufferedAmount = bufferedAmount;
       
    84         m_syncMethodDone = true;
       
    85     }
       
    86 
       
    87     void clearClient()
       
    88     {
       
    89         m_client = 0;
       
    90     }
       
    91 
       
    92     void didConnect()
       
    93     {
       
    94         m_pendingConnected = true;
       
    95         if (!m_suspended)
       
    96             processPendingEvents();
       
    97     }
       
    98 
       
    99     void didReceiveMessage(const String& msg)
       
   100     {
       
   101         m_pendingMessages.append(msg);
       
   102         if (!m_suspended)
       
   103             processPendingEvents();
       
   104     }
       
   105 
       
   106     void didClose(unsigned long unhandledBufferedAmount)
       
   107     {
       
   108         m_pendingClosed = true;
       
   109         m_bufferedAmount = unhandledBufferedAmount;
       
   110         if (!m_suspended)
       
   111             processPendingEvents();
       
   112     }
       
   113 
       
   114     void suspend()
       
   115     {
       
   116         m_suspended = true;
       
   117     }
       
   118 
       
   119     void resume()
       
   120     {
       
   121         m_suspended = false;
       
   122         processPendingEvents();
       
   123     }
       
   124 
       
   125 protected:
       
   126     ThreadableWebSocketChannelClientWrapper(WebSocketChannelClient* client)
       
   127         : m_client(client)
       
   128         , m_syncMethodDone(false)
       
   129         , m_sent(false)
       
   130         , m_bufferedAmount(0)
       
   131         , m_suspended(false)
       
   132         , m_pendingConnected(false)
       
   133         , m_pendingClosed(false)
       
   134     {
       
   135     }
       
   136 
       
   137     void processPendingEvents()
       
   138     {
       
   139         ASSERT(!m_suspended);
       
   140         if (m_pendingConnected) {
       
   141             m_pendingConnected = false;
       
   142             if (m_client)
       
   143                 m_client->didConnect();
       
   144         }
       
   145 
       
   146         Vector<String> messages;
       
   147         messages.swap(m_pendingMessages);
       
   148         for (Vector<String>::const_iterator iter = messages.begin(); iter != messages.end(); ++iter) {
       
   149             if (m_client)
       
   150                 m_client->didReceiveMessage(*iter);
       
   151         }
       
   152 
       
   153         if (m_pendingClosed) {
       
   154             m_pendingClosed = false;
       
   155             if (m_client)
       
   156                 m_client->didClose(m_bufferedAmount);
       
   157         }
       
   158     }
       
   159 
       
   160     WebSocketChannelClient* m_client;
       
   161     bool m_syncMethodDone;
       
   162     bool m_sent;
       
   163     unsigned long m_bufferedAmount;
       
   164     bool m_suspended;
       
   165     bool m_pendingConnected;
       
   166     Vector<String> m_pendingMessages;
       
   167     bool m_pendingClosed;
       
   168 };
       
   169 
       
   170 } // namespace WebCore
       
   171 
       
   172 #endif // ENABLE(WEB_SOCKETS)
       
   173 
       
   174 #endif // ThreadableWebSocketChannelClientWrapper_h