| Index: chrome/browser/chromeos/dbus/speech_synthesizer_client.cc
|
| diff --git a/chrome/browser/chromeos/dbus/speech_synthesizer_client.cc b/chrome/browser/chromeos/dbus/speech_synthesizer_client.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..aef2c1ecdf26705eef300a9dd806df98f2fb29f2
|
| --- /dev/null
|
| +++ b/chrome/browser/chromeos/dbus/speech_synthesizer_client.cc
|
| @@ -0,0 +1,149 @@
|
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/compiler_specific.h"
|
| +#include "chrome/browser/chromeos/system/runtime_environment.h"
|
| +#include "dbus/bus.h"
|
| +#include "dbus/message.h"
|
| +#include "dbus/object_proxy.h"
|
| +#include "third_party/cros_system_api/dbus/service_constants.h"
|
| +
|
| +namespace chromeos {
|
| +
|
| +// TODO(chaitanyag): rename to "locale" after making equivalent change in
|
| +// Chrome OS code.
|
| +const char SpeechSynthesizerClient::kSpeechPropertyLocale[] = "name";
|
| +
|
| +const char SpeechSynthesizerClient::kSpeechPropertyGender[] = "gender";
|
| +const char SpeechSynthesizerClient::kSpeechPropertyRate[] = "rate";
|
| +const char SpeechSynthesizerClient::kSpeechPropertyPitch[] = "pitch";
|
| +const char SpeechSynthesizerClient::kSpeechPropertyVolume[] = "volume";
|
| +const char SpeechSynthesizerClient::kSpeechPropertyEquals[] = "=";
|
| +const char SpeechSynthesizerClient::kSpeechPropertyDelimiter[] = ";";
|
| +
|
| +class SpeechSynthesizerClientImpl : public SpeechSynthesizerClient {
|
| + public:
|
| + explicit SpeechSynthesizerClientImpl(dbus::Bus* bus)
|
| + : proxy_(NULL),
|
| + weak_ptr_factory_(this) {
|
| + proxy_ = bus->GetObjectProxy(
|
| + speech_synthesis::kSpeechSynthesizerServiceName,
|
| + speech_synthesis::kSpeechSynthesizerServicePath);
|
| + }
|
| + virtual ~SpeechSynthesizerClientImpl() {}
|
| +
|
| + virtual void Speak(const std::string& text) OVERRIDE {
|
| + // TODO(hashimoto): Define speech synthesizer method names as constants
|
| + // in service_constants.h. http://crosbug.com/21824
|
| + dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface,
|
| + "Speak");
|
| + dbus::MessageWriter writer(&method_call);
|
| + writer.AppendString(text);
|
| + proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
|
| + base::Bind(&SpeechSynthesizerClientImpl::OnSpeak,
|
| + weak_ptr_factory_.GetWeakPtr()));
|
| + }
|
| +
|
| + virtual void SetSpeakProperties(const std::string& props) OVERRIDE {
|
| + dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface,
|
| + "SetProperties");
|
| + dbus::MessageWriter writer(&method_call);
|
| + writer.AppendString(props);
|
| + proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
|
| + base::Bind(
|
| + &SpeechSynthesizerClientImpl::OnSetSpeakProperties,
|
| + weak_ptr_factory_.GetWeakPtr()));
|
| + }
|
| +
|
| + virtual void StopSpeaking() OVERRIDE {
|
| + dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface,
|
| + "Stop");
|
| + proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
|
| + base::Bind(&SpeechSynthesizerClientImpl::OnStopSpeaking,
|
| + weak_ptr_factory_.GetWeakPtr()));
|
| + }
|
| +
|
| + virtual void IsSpeaking(IsSpeakingCallback callback) OVERRIDE {
|
| + dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface,
|
| + "IsSpeaking");
|
| + proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
|
| + base::Bind(&SpeechSynthesizerClientImpl::OnIsSpeaking,
|
| + weak_ptr_factory_.GetWeakPtr(),
|
| + callback));
|
| + }
|
| +
|
| + private:
|
| + // Called when a response for Speak() is received
|
| + void OnSpeak(dbus::Response* response) {
|
| + if (!response) {
|
| + LOG(ERROR) << "Failed to speak.";
|
| + return;
|
| + }
|
| + VLOG(1) << "Spoke: " << response->ToString();
|
| + }
|
| +
|
| + // Called when a response for SetSpeakProperties() is received
|
| + void OnSetSpeakProperties(dbus::Response* response) {
|
| + if (!response) {
|
| + LOG(ERROR) << "Failed to set speak properties.";
|
| + return;
|
| + }
|
| + VLOG(1) << "Set speak properties: " << response->ToString();
|
| + }
|
| +
|
| + // Called when a response for StopSpeaking() is received
|
| + void OnStopSpeaking(dbus::Response* response) {
|
| + if (!response) {
|
| + LOG(ERROR) << "Failed to stop speaking.";
|
| + return;
|
| + }
|
| + VLOG(1) << "Stopped speaking: " << response->ToString();
|
| + }
|
| +
|
| + // Called when a response for IsSpeaking() is received
|
| + void OnIsSpeaking(IsSpeakingCallback callback, dbus::Response* response) {
|
| + bool value = false;
|
| + if (response) {
|
| + dbus::MessageReader reader(response);
|
| + reader.PopBool(&value);
|
| + } else {
|
| + LOG(ERROR) << "Failed to ask if it is speaking";
|
| + }
|
| + callback.Run(value);
|
| + }
|
| +
|
| + dbus::ObjectProxy* proxy_;
|
| + base::WeakPtrFactory<SpeechSynthesizerClientImpl> weak_ptr_factory_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(SpeechSynthesizerClientImpl);
|
| +};
|
| +
|
| +class SpeechSynthesizerClientStubImpl : public SpeechSynthesizerClient {
|
| + public:
|
| + SpeechSynthesizerClientStubImpl() {}
|
| + virtual ~SpeechSynthesizerClientStubImpl() {}
|
| + virtual void Speak(const std::string& text) OVERRIDE {}
|
| + virtual void SetSpeakProperties(const std::string& props) OVERRIDE {}
|
| + virtual void StopSpeaking() OVERRIDE {}
|
| + virtual void IsSpeaking(IsSpeakingCallback callback) OVERRIDE {
|
| + callback.Run(false);
|
| + }
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(SpeechSynthesizerClientStubImpl);
|
| +};
|
| +
|
| +// static
|
| +SpeechSynthesizerClient* SpeechSynthesizerClient::Create(dbus::Bus* bus) {
|
| + if (system::runtime_environment::IsRunningOnChromeOS()) {
|
| + return new SpeechSynthesizerClientImpl(bus);
|
| + } else {
|
| + return new SpeechSynthesizerClientStubImpl();
|
| + }
|
| +}
|
| +
|
| +} // namespace chromeos
|
|
|