Reenable NullOpenerRedirectForksProcess, as the offending patch has been reverted
[chromium-blink-merge.git] / chromeos / dbus / speech_synthesizer_client.cc
blob8326920b8cf0ff802cf414c731909e9f9f57b24a
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"
7 #include "base/bind.h"
8 #include "base/compiler_specific.h"
9 #include "dbus/bus.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"
15 namespace chromeos {
17 // TODO(chaitanyag): rename to "locale" after making equivalent change in
18 // Chrome OS code.
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 {
29 public:
30 explicit SpeechSynthesizerClientImpl(dbus::Bus* bus)
31 : proxy_(NULL),
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(),
65 callback));
68 private:
69 // Called when a response for Speak() is received
70 void OnSpeak(dbus::Response* response) {
71 if (!response) {
72 LOG(ERROR) << "Failed to speak.";
73 return;
75 VLOG(1) << "Spoke: " << response->ToString();
78 // Called when a response for StopSpeaking() is received
79 void OnStopSpeaking(dbus::Response* response) {
80 if (!response) {
81 LOG(ERROR) << "Failed to stop speaking.";
82 return;
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) {
90 bool value = false;
91 if (response) {
92 dbus::MessageReader reader(response);
93 reader.PopBool(&value);
94 } else {
95 LOG(ERROR) << "Failed to ask if it is speaking";
97 callback.Run(value);
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 {
110 public:
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 {
117 callback.Run(false);
120 private:
121 DISALLOW_COPY_AND_ASSIGN(SpeechSynthesizerClientStubImpl);
124 SpeechSynthesizerClient::SpeechSynthesizerClient() {
127 SpeechSynthesizerClient::~SpeechSynthesizerClient() {
130 // static
131 SpeechSynthesizerClient* SpeechSynthesizerClient::Create(
132 DBusClientImplementationType type,
133 dbus::Bus* bus) {
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