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 virtual unsigned long bufferedAmount() override;
54 virtual bool sendStringData(const blink::WebString& data) override;
55 virtual bool sendRawData(const char* data, size_t length) override;
56 virtual void close() override;
58 const scoped_refptr<webrtc::DataChannelInterface>& channel() const;
61 void OnStateChange(webrtc::DataChannelInterface::DataState state);
62 void OnMessage(scoped_ptr<webrtc::DataBuffer> buffer);
63 void RecordMessageSent(size_t num_bytes);
65 class CONTENT_EXPORT Observer
66 : public NON_EXPORTED_BASE(
67 base::RefCountedThreadSafe<RtcDataChannelHandler::Observer>),
68 public NON_EXPORTED_BASE(webrtc::DataChannelObserver) {
70 Observer(RtcDataChannelHandler* handler,
71 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread,
72 webrtc::DataChannelInterface* channel);
74 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread() const;
75 const scoped_refptr<webrtc::DataChannelInterface>& channel() const;
80 friend class base::RefCountedThreadSafe<RtcDataChannelHandler::Observer>;
83 // webrtc::DataChannelObserver implementation.
84 void OnStateChange() override;
85 void OnMessage(const webrtc::DataBuffer& buffer) override;
87 void OnStateChangeImpl(webrtc::DataChannelInterface::DataState state);
88 void OnMessageImpl(scoped_ptr<webrtc::DataBuffer> buffer);
90 RtcDataChannelHandler* handler_;
91 const scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
92 const scoped_refptr<webrtc::DataChannelInterface> channel_;
95 scoped_refptr<Observer> observer_;
96 base::ThreadChecker thread_checker_;
98 blink::WebRTCDataChannelHandlerClient* webkit_client_;
101 } // namespace content
103 #endif // CONTENT_RENDERER_MEDIA_RTC_DATA_CHANNEL_HANDLER_H_