Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / media / rtc_dtmf_sender_handler.cc
blob65ddd28961ac307a5b36fe880b78155e8f2ab009
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/single_thread_task_runner.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/threading/thread_checker.h"
17 using webrtc::DtmfSenderInterface;
19 namespace content {
21 class RtcDtmfSenderHandler::Observer :
22 public base::RefCountedThreadSafe<Observer>,
23 public webrtc::DtmfSenderObserverInterface {
24 public:
25 explicit Observer(const base::WeakPtr<RtcDtmfSenderHandler>& handler)
26 : main_thread_(base::ThreadTaskRunnerHandle::Get()), handler_(handler) {}
28 private:
29 friend class base::RefCountedThreadSafe<Observer>;
31 ~Observer() override {}
33 void OnToneChange(const std::string& tone) override {
34 main_thread_->PostTask(FROM_HERE,
35 base::Bind(&RtcDtmfSenderHandler::Observer::OnToneChangeOnMainThread,
36 this, tone));
39 void OnToneChangeOnMainThread(const std::string& tone) {
40 DCHECK(thread_checker_.CalledOnValidThread());
41 if (handler_)
42 handler_->OnToneChange(tone);
45 base::ThreadChecker thread_checker_;
46 const scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
47 base::WeakPtr<RtcDtmfSenderHandler> handler_;
50 RtcDtmfSenderHandler::RtcDtmfSenderHandler(DtmfSenderInterface* dtmf_sender)
51 : dtmf_sender_(dtmf_sender),
52 webkit_client_(NULL),
53 weak_factory_(this) {
54 DVLOG(1) << "::ctor";
55 observer_ = new Observer(weak_factory_.GetWeakPtr());
56 dtmf_sender_->RegisterObserver(observer_.get());
59 RtcDtmfSenderHandler::~RtcDtmfSenderHandler() {
60 DVLOG(1) << "::dtor";
61 dtmf_sender_->UnregisterObserver();
62 // Release |observer| before |weak_factory_| is destroyed.
63 observer_ = NULL;
66 void RtcDtmfSenderHandler::setClient(
67 blink::WebRTCDTMFSenderHandlerClient* client) {
68 webkit_client_ = client;
71 blink::WebString RtcDtmfSenderHandler::currentToneBuffer() {
72 return base::UTF8ToUTF16(dtmf_sender_->tones());
75 bool RtcDtmfSenderHandler::canInsertDTMF() {
76 return dtmf_sender_->CanInsertDtmf();
79 bool RtcDtmfSenderHandler::insertDTMF(const blink::WebString& tones,
80 long duration,
81 long interToneGap) {
82 std::string utf8_tones = base::UTF16ToUTF8(base::StringPiece16(tones));
83 return dtmf_sender_->InsertDtmf(utf8_tones, static_cast<int>(duration),
84 static_cast<int>(interToneGap));
87 void RtcDtmfSenderHandler::OnToneChange(const std::string& tone) {
88 if (!webkit_client_) {
89 LOG(ERROR) << "WebRTCDTMFSenderHandlerClient not set.";
90 return;
92 webkit_client_->didPlayTone(base::UTF8ToUTF16(tone));
95 } // namespace content