Change DtmfSenderHandler to handle events on the signaling thread.
[chromium-blink-merge.git] / content / renderer / media / rtc_dtmf_sender_handler.cc
blob3aea3d15fe0544e435915f10f8f626e51777f246
1 // Copyright (c) 2013 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 #include "content/renderer/media/rtc_dtmf_sender_handler.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/threading/thread_checker.h"
16 using webrtc::DtmfSenderInterface;
18 namespace content {
20 class RtcDtmfSenderHandler::Observer :
21 public base::RefCountedThreadSafe<Observer>,
22 public webrtc::DtmfSenderObserverInterface {
23 public:
24 explicit Observer(const base::WeakPtr<RtcDtmfSenderHandler>& handler)
25 : main_thread_(base::MessageLoopProxy::current()), handler_(handler) {}
27 private:
28 friend class base::RefCountedThreadSafe<Observer>;
30 ~Observer() override {}
32 void OnToneChange(const std::string& tone) override {
33 main_thread_->PostTask(FROM_HERE,
34 base::Bind(&RtcDtmfSenderHandler::Observer::OnToneChangeOnMainThread,
35 this, tone));
38 void OnToneChangeOnMainThread(const std::string& tone) {
39 DCHECK(thread_checker_.CalledOnValidThread());
40 if (handler_)
41 handler_->OnToneChange(tone);
44 base::ThreadChecker thread_checker_;
45 const scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
46 base::WeakPtr<RtcDtmfSenderHandler> handler_;
49 RtcDtmfSenderHandler::RtcDtmfSenderHandler(DtmfSenderInterface* dtmf_sender)
50 : dtmf_sender_(dtmf_sender),
51 webkit_client_(NULL),
52 weak_factory_(this),
53 observer_(new Observer(weak_factory_.GetWeakPtr())) {
54 DVLOG(1) << "::ctor";
55 dtmf_sender_->RegisterObserver(observer_.get());
58 RtcDtmfSenderHandler::~RtcDtmfSenderHandler() {
59 DVLOG(1) << "::dtor";
60 dtmf_sender_->UnregisterObserver();
63 void RtcDtmfSenderHandler::setClient(
64 blink::WebRTCDTMFSenderHandlerClient* client) {
65 webkit_client_ = client;
68 blink::WebString RtcDtmfSenderHandler::currentToneBuffer() {
69 return base::UTF8ToUTF16(dtmf_sender_->tones());
72 bool RtcDtmfSenderHandler::canInsertDTMF() {
73 return dtmf_sender_->CanInsertDtmf();
76 bool RtcDtmfSenderHandler::insertDTMF(const blink::WebString& tones,
77 long duration,
78 long interToneGap) {
79 std::string utf8_tones = base::UTF16ToUTF8(tones);
80 return dtmf_sender_->InsertDtmf(utf8_tones, static_cast<int>(duration),
81 static_cast<int>(interToneGap));
84 void RtcDtmfSenderHandler::OnToneChange(const std::string& tone) {
85 if (!webkit_client_) {
86 LOG(ERROR) << "WebRTCDTMFSenderHandlerClient not set.";
87 return;
89 webkit_client_->didPlayTone(base::UTF8ToUTF16(tone));
92 } // namespace content