Home | History | Annotate | Download | only in web
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef WebSocketChannelClientProxy_h
      6 #define WebSocketChannelClientProxy_h
      7 
      8 #include "modules/websockets/WebSocketChannelClient.h"
      9 #include "platform/heap/Handle.h"
     10 #include "web/WebSocketImpl.h"
     11 #include "wtf/PassOwnPtr.h"
     12 #include "wtf/Vector.h"
     13 #include "wtf/text/WTFString.h"
     14 
     15 namespace blink {
     16 
     17 // Ideally we want to simply make WebSocketImpl inherit from
     18 // WebSocketChannelClient, but we cannot do that because WebSocketChannelClient
     19 // needs to be on Oilpan's heap whereas WebSocketImpl cannot be on Oilpan's
     20 // heap. Thus we need to introduce a proxy class to decouple WebSocketImpl
     21 // from WebSocketChannelClient.
     22 class WebSocketChannelClientProxy FINAL : public GarbageCollectedFinalized<WebSocketChannelClientProxy>, public WebSocketChannelClient {
     23     USING_GARBAGE_COLLECTED_MIXIN(WebSocketChannelClientProxy)
     24 public:
     25     static WebSocketChannelClientProxy* create(WebSocketImpl* impl)
     26     {
     27         return new WebSocketChannelClientProxy(impl);
     28     }
     29 
     30     virtual void didConnect(const String& subprotocol, const String& extensions) OVERRIDE
     31     {
     32         m_impl->didConnect(subprotocol, extensions);
     33     }
     34     virtual void didReceiveMessage(const String& message) OVERRIDE
     35     {
     36         m_impl->didReceiveMessage(message);
     37     }
     38     virtual void didReceiveBinaryData(PassOwnPtr<Vector<char> > binaryData) OVERRIDE
     39     {
     40         m_impl->didReceiveBinaryData(binaryData);
     41     }
     42     virtual void didReceiveMessageError() OVERRIDE
     43     {
     44         m_impl->didReceiveMessageError();
     45     }
     46     virtual void didConsumeBufferedAmount(unsigned long consumed) OVERRIDE
     47     {
     48         m_impl->didConsumeBufferedAmount(consumed);
     49     }
     50     virtual void didStartClosingHandshake() OVERRIDE
     51     {
     52         m_impl->didStartClosingHandshake();
     53     }
     54     virtual void didClose(ClosingHandshakeCompletionStatus status, unsigned short code, const String& reason) OVERRIDE
     55     {
     56         WebSocketImpl* impl = m_impl;
     57         m_impl = nullptr;
     58         impl->didClose(status, code, reason);
     59     }
     60 
     61 private:
     62     explicit WebSocketChannelClientProxy(WebSocketImpl* impl)
     63         : m_impl(impl)
     64     {
     65     }
     66 
     67     WebSocketImpl* m_impl;
     68 };
     69 
     70 } // namespace blink
     71 
     72 #endif // WebSocketChannelClientProxy_h
     73