1 // Copyright (c) 2012 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.
5 #ifndef CONTENT_RENDERER_MEDIA_RTC_DATA_CHANNEL_HANDLER_H_
6 #define CONTENT_RENDERER_MEDIA_RTC_DATA_CHANNEL_HANDLER_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/threading/thread_checker.h"
12 #include "content/common/content_export.h"
13 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h"
14 #include "third_party/WebKit/public/platform/WebRTCDataChannelHandler.h"
15 #include "third_party/WebKit/public/platform/WebRTCDataChannelHandlerClient.h"
19 // RtcDataChannelHandler is a delegate for the RTC PeerConnection DataChannel
20 // API messages going between WebKit and native PeerConnection DataChannels in
21 // libjingle. Instances of this class are owned by WebKit.
22 // WebKit call all of these methods on the main render thread.
23 // Callbacks to the webrtc::DataChannelObserver implementation also occur on
24 // the main render thread.
25 class CONTENT_EXPORT RtcDataChannelHandler
26 : NON_EXPORTED_BASE(public blink::WebRTCDataChannelHandler
) {
28 // This object can* be constructed on libjingle's signaling thread and then
29 // ownership is passed to the UI thread where it's eventually given to WebKit.
30 // The reason we must construct and hook ourselves up as an observer on the
31 // signaling thread is to avoid missing out on any state changes or messages
32 // that may occur before we've fully connected with webkit.
33 // This period is basically between when the ctor is called and until
34 // setClient is called.
35 // * For local data channels, the object will be construced on the main thread
36 // and we don't have the issue described above.
37 RtcDataChannelHandler(
38 const scoped_refptr
<base::SingleThreadTaskRunner
>& main_thread
,
39 webrtc::DataChannelInterface
* channel
);
40 virtual ~RtcDataChannelHandler();
42 // blink::WebRTCDataChannelHandler implementation.
43 virtual void setClient(
44 blink::WebRTCDataChannelHandlerClient
* client
) override
;
45 virtual blink::WebString
label() override
;
46 virtual bool isReliable() override
;
47 virtual bool ordered() const override
;
48 virtual unsigned short maxRetransmitTime() const override
;
49 virtual unsigned short maxRetransmits() const override
;
50 virtual blink::WebString
protocol() const override
;
51 virtual bool negotiated() const override
;
52 virtual unsigned short id() const override
;
53 // TODO(bemasc): Mark |state()| as |override| once https://codereview.chromium.org/782843003/
54 // lands in Blink and rolls into Chromium.
55 virtual blink::WebRTCDataChannelHandlerClient::ReadyState
state() const;
56 virtual unsigned long bufferedAmount() override
;
57 virtual bool sendStringData(const blink::WebString
& data
) override
;
58 virtual bool sendRawData(const char* data
, size_t length
) override
;
59 virtual void close() override
;
61 const scoped_refptr
<webrtc::DataChannelInterface
>& channel() const;
64 void OnStateChange(webrtc::DataChannelInterface::DataState state
);
65 void OnMessage(scoped_ptr
<webrtc::DataBuffer
> buffer
);
66 void RecordMessageSent(size_t num_bytes
);
68 class CONTENT_EXPORT Observer
69 : public NON_EXPORTED_BASE(
70 base::RefCountedThreadSafe
<RtcDataChannelHandler::Observer
>),
71 public NON_EXPORTED_BASE(webrtc::DataChannelObserver
) {
73 Observer(RtcDataChannelHandler
* handler
,
74 const scoped_refptr
<base::SingleThreadTaskRunner
>& main_thread
,
75 webrtc::DataChannelInterface
* channel
);
77 const scoped_refptr
<base::SingleThreadTaskRunner
>& main_thread() const;
78 const scoped_refptr
<webrtc::DataChannelInterface
>& channel() const;
80 // Clears the internal |handler_| pointer so that no further callbacks
81 // will be attempted, disassociates this observer from the channel and
82 // releases the channel pointer. Must be called on the main thread.
86 friend class base::RefCountedThreadSafe
<RtcDataChannelHandler::Observer
>;
89 // webrtc::DataChannelObserver implementation.
90 void OnStateChange() override
;
91 void OnMessage(const webrtc::DataBuffer
& buffer
) override
;
93 void OnStateChangeImpl(webrtc::DataChannelInterface::DataState state
);
94 void OnMessageImpl(scoped_ptr
<webrtc::DataBuffer
> buffer
);
96 RtcDataChannelHandler
* handler_
;
97 const scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_
;
98 scoped_refptr
<webrtc::DataChannelInterface
> channel_
;
101 scoped_refptr
<Observer
> observer_
;
102 base::ThreadChecker thread_checker_
;
104 blink::WebRTCDataChannelHandlerClient
* webkit_client_
;
107 } // namespace content
109 #endif // CONTENT_RENDERER_MEDIA_RTC_DATA_CHANNEL_HANDLER_H_