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 #include "chromeos/dbus/speech_synthesizer_client.h"
8 #include "base/compiler_specific.h"
10 #include "dbus/message.h"
11 #include "dbus/object_path.h"
12 #include "dbus/object_proxy.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
17 // TODO(chaitanyag): rename to "locale" after making equivalent change in
19 const char SpeechSynthesizerClient::kSpeechPropertyLocale
[] = "name";
21 const char SpeechSynthesizerClient::kSpeechPropertyGender
[] = "gender";
22 const char SpeechSynthesizerClient::kSpeechPropertyRate
[] = "rate";
23 const char SpeechSynthesizerClient::kSpeechPropertyPitch
[] = "pitch";
24 const char SpeechSynthesizerClient::kSpeechPropertyVolume
[] = "volume";
25 const char SpeechSynthesizerClient::kSpeechPropertyEquals
[] = "=";
26 const char SpeechSynthesizerClient::kSpeechPropertyDelimiter
[] = ";";
28 class SpeechSynthesizerClientImpl
: public SpeechSynthesizerClient
{
30 explicit SpeechSynthesizerClientImpl(dbus::Bus
* bus
)
32 weak_ptr_factory_(this) {
33 proxy_
= bus
->GetObjectProxy(
34 speech_synthesis::kSpeechSynthesizerServiceName
,
35 dbus::ObjectPath(speech_synthesis::kSpeechSynthesizerServicePath
));
37 virtual ~SpeechSynthesizerClientImpl() {}
39 virtual void Speak(const std::string
& text
,
40 const std::string
& properties
) OVERRIDE
{
41 dbus::MethodCall
method_call(speech_synthesis::kSpeechSynthesizerInterface
,
42 speech_synthesis::kSpeak
);
43 dbus::MessageWriter
writer(&method_call
);
44 writer
.AppendString(text
);
45 writer
.AppendString(properties
);
46 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
47 base::Bind(&SpeechSynthesizerClientImpl::OnSpeak
,
48 weak_ptr_factory_
.GetWeakPtr()));
51 virtual void StopSpeaking() OVERRIDE
{
52 dbus::MethodCall
method_call(speech_synthesis::kSpeechSynthesizerInterface
,
53 speech_synthesis::kStop
);
54 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
55 base::Bind(&SpeechSynthesizerClientImpl::OnStopSpeaking
,
56 weak_ptr_factory_
.GetWeakPtr()));
59 virtual void IsSpeaking(const IsSpeakingCallback
& callback
) OVERRIDE
{
60 dbus::MethodCall
method_call(speech_synthesis::kSpeechSynthesizerInterface
,
61 speech_synthesis::kIsSpeaking
);
62 proxy_
->CallMethod(&method_call
, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
63 base::Bind(&SpeechSynthesizerClientImpl::OnIsSpeaking
,
64 weak_ptr_factory_
.GetWeakPtr(),
69 // Called when a response for Speak() is received
70 void OnSpeak(dbus::Response
* response
) {
72 LOG(ERROR
) << "Failed to speak.";
75 VLOG(1) << "Spoke: " << response
->ToString();
78 // Called when a response for StopSpeaking() is received
79 void OnStopSpeaking(dbus::Response
* response
) {
81 LOG(ERROR
) << "Failed to stop speaking.";
84 VLOG(1) << "Stopped speaking: " << response
->ToString();
87 // Called when a response for IsSpeaking() is received
88 void OnIsSpeaking(const IsSpeakingCallback
& callback
,
89 dbus::Response
* response
) {
92 dbus::MessageReader
reader(response
);
93 reader
.PopBool(&value
);
95 LOG(ERROR
) << "Failed to ask if it is speaking";
100 dbus::ObjectProxy
* proxy_
;
102 // Note: This should remain the last member so it'll be destroyed and
103 // invalidate its weak pointers before any other members are destroyed.
104 base::WeakPtrFactory
<SpeechSynthesizerClientImpl
> weak_ptr_factory_
;
106 DISALLOW_COPY_AND_ASSIGN(SpeechSynthesizerClientImpl
);
109 class SpeechSynthesizerClientStubImpl
: public SpeechSynthesizerClient
{
111 SpeechSynthesizerClientStubImpl() {}
112 virtual ~SpeechSynthesizerClientStubImpl() {}
113 virtual void Speak(const std::string
& text
,
114 const std::string
& properties
) OVERRIDE
{}
115 virtual void StopSpeaking() OVERRIDE
{}
116 virtual void IsSpeaking(const IsSpeakingCallback
& callback
) OVERRIDE
{
121 DISALLOW_COPY_AND_ASSIGN(SpeechSynthesizerClientStubImpl
);
124 SpeechSynthesizerClient::SpeechSynthesizerClient() {
127 SpeechSynthesizerClient::~SpeechSynthesizerClient() {
131 SpeechSynthesizerClient
* SpeechSynthesizerClient::Create(
132 DBusClientImplementationType type
,
134 if (type
== REAL_DBUS_CLIENT_IMPLEMENTATION
)
135 return new SpeechSynthesizerClientImpl(bus
);
136 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION
, type
);
137 return new SpeechSynthesizerClientStubImpl();
140 } // namespace chromeos