Home | History | Annotate | Download | only in websockets
      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 "WebSocketChannelClient.h"
     37 #include <wtf/PassRefPtr.h>
     38 #include <wtf/Threading.h>
     39 
     40 namespace WebCore {
     41 
     42 class String;
     43 
     44 class ThreadableWebSocketChannelClientWrapper : public ThreadSafeShared<ThreadableWebSocketChannelClientWrapper> {
     45 public:
     46     static PassRefPtr<ThreadableWebSocketChannelClientWrapper> create(WebSocketChannelClient* client)
     47     {
     48         return adoptRef(new ThreadableWebSocketChannelClientWrapper(client));
     49     }
     50 
     51     void clearSyncMethodDone()
     52     {
     53         m_syncMethodDone = false;
     54     }
     55     void setSyncMethodDone()
     56     {
     57         m_syncMethodDone = true;
     58     }
     59 
     60     bool syncMethodDone() const
     61     {
     62         return m_syncMethodDone;
     63     }
     64 
     65     bool sent() const
     66     {
     67         return m_sent;
     68     }
     69     void setSent(bool sent)
     70     {
     71         m_sent = sent;
     72         m_syncMethodDone = true;
     73     }
     74 
     75     unsigned long bufferedAmount() const
     76     {
     77         return m_bufferedAmount;
     78     }
     79     void setBufferedAmount(unsigned long bufferedAmount)
     80     {
     81         m_bufferedAmount = bufferedAmount;
     82         m_syncMethodDone = true;
     83     }
     84 
     85     void clearClient()
     86     {
     87         m_client = 0;
     88     }
     89 
     90     void didConnect()
     91     {
     92         if (m_client)
     93             m_client->didConnect();
     94     }
     95 
     96     void didReceiveMessage(const String& msg)
     97     {
     98         if (m_client)
     99             m_client->didReceiveMessage(msg);
    100     }
    101 
    102     void didClose(unsigned long unhandledBufferedAmount)
    103     {
    104         if (m_client)
    105             m_client->didClose(unhandledBufferedAmount);
    106     }
    107 
    108 protected:
    109     ThreadableWebSocketChannelClientWrapper(WebSocketChannelClient* client)
    110         : m_client(client)
    111         , m_syncMethodDone(false)
    112         , m_sent(false)
    113         , m_bufferedAmount(0)
    114     {
    115     }
    116 
    117     WebSocketChannelClient* m_client;
    118     bool m_syncMethodDone;
    119     bool m_sent;
    120     unsigned long m_bufferedAmount;
    121 };
    122 
    123 } // namespace WebCore
    124 
    125 #endif // ENABLE(WEB_SOCKETS)
    126 
    127 #endif // ThreadableWebSocketChannelClientWrapper_h
    128