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 ~RtcDataChannelHandler() override
;
42 // blink::WebRTCDataChannelHandler implementation.
44 blink::WebRTCDataChannelHandlerClient
* client
) override
;
45 blink::WebString
label() override
;
46 bool isReliable() override
;
47 bool ordered() const override
;
48 unsigned short maxRetransmitTime() const override
;
49 unsigned short maxRetransmits() const override
;
50 blink::WebString
protocol() const override
;
51 bool negotiated() const override
;
52 unsigned short id() const override
;
53 blink::WebRTCDataChannelHandlerClient::ReadyState
state() const override
;
54 unsigned long bufferedAmount() override
;
55 bool sendStringData(const blink::WebString
& data
) override
;
56 bool sendRawData(const char* data
, size_t length
) override
;
57 void close() override
;
59 const scoped_refptr
<webrtc::DataChannelInterface
>& channel() const;
62 void OnStateChange(webrtc::DataChannelInterface::DataState state
);
63 void OnBufferedAmountDecrease(unsigned previous_amount
);
64 void OnMessage(scoped_ptr
<webrtc::DataBuffer
> buffer
);
65 void RecordMessageSent(size_t num_bytes
);
67 class CONTENT_EXPORT Observer
68 : public NON_EXPORTED_BASE(
69 base::RefCountedThreadSafe
<RtcDataChannelHandler::Observer
>),
70 public NON_EXPORTED_BASE(webrtc::DataChannelObserver
) {
72 Observer(RtcDataChannelHandler
* handler
,
73 const scoped_refptr
<base::SingleThreadTaskRunner
>& main_thread
,
74 webrtc::DataChannelInterface
* channel
);
76 const scoped_refptr
<base::SingleThreadTaskRunner
>& main_thread() const;
77 const scoped_refptr
<webrtc::DataChannelInterface
>& channel() const;
79 // Clears the internal |handler_| pointer so that no further callbacks
80 // will be attempted, disassociates this observer from the channel and
81 // releases the channel pointer. Must be called on the main thread.
85 friend class base::RefCountedThreadSafe
<RtcDataChannelHandler::Observer
>;
88 // webrtc::DataChannelObserver implementation.
89 void OnStateChange() override
;
90 void OnBufferedAmountChange(uint64 previous_amount
) override
;
91 void OnMessage(const webrtc::DataBuffer
& buffer
) override
;
93 void OnStateChangeImpl(webrtc::DataChannelInterface::DataState state
);
94 void OnBufferedAmountDecreaseImpl(unsigned previous_amount
);
95 void OnMessageImpl(scoped_ptr
<webrtc::DataBuffer
> buffer
);
97 RtcDataChannelHandler
* handler_
;
98 const scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_
;
99 scoped_refptr
<webrtc::DataChannelInterface
> channel_
;
102 scoped_refptr
<Observer
> observer_
;
103 base::ThreadChecker thread_checker_
;
105 blink::WebRTCDataChannelHandlerClient
* webkit_client_
;
108 } // namespace content
110 #endif // CONTENT_RENDERER_MEDIA_RTC_DATA_CHANNEL_HANDLER_H_