OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" |
| 9 #include "chrome/browser/chromeos/system/runtime_environment.h" |
| 10 #include "dbus/bus.h" |
| 11 #include "dbus/message.h" |
| 12 #include "dbus/object_proxy.h" |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 14 |
| 15 namespace chromeos { |
| 16 |
| 17 // TODO(chaitanyag): rename to "locale" after making equivalent change in |
| 18 // Chrome OS code. |
| 19 const char SpeechSynthesizerClient::kSpeechPropertyLocale[] = "name"; |
| 20 |
| 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[] = ";"; |
| 27 |
| 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 speech_synthesis::kSpeechSynthesizerServicePath); |
| 36 } |
| 37 virtual ~SpeechSynthesizerClientImpl() {} |
| 38 |
| 39 virtual void Speak(const std::string& text) OVERRIDE { |
| 40 // TODO(hashimoto): Define speech synthesizer method names as constants |
| 41 // in service_constants.h. http://crosbug.com/21824 |
| 42 dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface, |
| 43 "Speak"); |
| 44 dbus::MessageWriter writer(&method_call); |
| 45 writer.AppendString(text); |
| 46 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 47 base::Bind(&SpeechSynthesizerClientImpl::OnSpeak, |
| 48 weak_ptr_factory_.GetWeakPtr())); |
| 49 } |
| 50 |
| 51 virtual void SetSpeakProperties(const std::string& props) OVERRIDE { |
| 52 dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface, |
| 53 "SetProperties"); |
| 54 dbus::MessageWriter writer(&method_call); |
| 55 writer.AppendString(props); |
| 56 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 57 base::Bind( |
| 58 &SpeechSynthesizerClientImpl::OnSetSpeakProperties, |
| 59 weak_ptr_factory_.GetWeakPtr())); |
| 60 } |
| 61 |
| 62 virtual void StopSpeaking() OVERRIDE { |
| 63 dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface, |
| 64 "Stop"); |
| 65 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 66 base::Bind(&SpeechSynthesizerClientImpl::OnStopSpeaking, |
| 67 weak_ptr_factory_.GetWeakPtr())); |
| 68 } |
| 69 |
| 70 virtual void IsSpeaking(IsSpeakingCallback callback) OVERRIDE { |
| 71 dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface, |
| 72 "IsSpeaking"); |
| 73 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 74 base::Bind(&SpeechSynthesizerClientImpl::OnIsSpeaking, |
| 75 weak_ptr_factory_.GetWeakPtr(), |
| 76 callback)); |
| 77 } |
| 78 |
| 79 private: |
| 80 // Called when a response for Speak() is received |
| 81 void OnSpeak(dbus::Response* response) { |
| 82 if (!response) { |
| 83 LOG(ERROR) << "Failed to speak."; |
| 84 return; |
| 85 } |
| 86 VLOG(1) << "Spoke: " << response->ToString(); |
| 87 } |
| 88 |
| 89 // Called when a response for SetSpeakProperties() is received |
| 90 void OnSetSpeakProperties(dbus::Response* response) { |
| 91 if (!response) { |
| 92 LOG(ERROR) << "Failed to set speak properties."; |
| 93 return; |
| 94 } |
| 95 VLOG(1) << "Set speak properties: " << response->ToString(); |
| 96 } |
| 97 |
| 98 // Called when a response for StopSpeaking() is received |
| 99 void OnStopSpeaking(dbus::Response* response) { |
| 100 if (!response) { |
| 101 LOG(ERROR) << "Failed to stop speaking."; |
| 102 return; |
| 103 } |
| 104 VLOG(1) << "Stopped speaking: " << response->ToString(); |
| 105 } |
| 106 |
| 107 // Called when a response for IsSpeaking() is received |
| 108 void OnIsSpeaking(IsSpeakingCallback callback, dbus::Response* response) { |
| 109 bool value = false; |
| 110 if (response) { |
| 111 dbus::MessageReader reader(response); |
| 112 reader.PopBool(&value); |
| 113 } else { |
| 114 LOG(ERROR) << "Failed to ask if it is speaking"; |
| 115 } |
| 116 callback.Run(value); |
| 117 } |
| 118 |
| 119 dbus::ObjectProxy* proxy_; |
| 120 base::WeakPtrFactory<SpeechSynthesizerClientImpl> weak_ptr_factory_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(SpeechSynthesizerClientImpl); |
| 123 }; |
| 124 |
| 125 class SpeechSynthesizerClientStubImpl : public SpeechSynthesizerClient { |
| 126 public: |
| 127 SpeechSynthesizerClientStubImpl() {} |
| 128 virtual ~SpeechSynthesizerClientStubImpl() {} |
| 129 virtual void Speak(const std::string& text) OVERRIDE {} |
| 130 virtual void SetSpeakProperties(const std::string& props) OVERRIDE {} |
| 131 virtual void StopSpeaking() OVERRIDE {} |
| 132 virtual void IsSpeaking(IsSpeakingCallback callback) OVERRIDE { |
| 133 callback.Run(false); |
| 134 } |
| 135 |
| 136 private: |
| 137 DISALLOW_COPY_AND_ASSIGN(SpeechSynthesizerClientStubImpl); |
| 138 }; |
| 139 |
| 140 // static |
| 141 SpeechSynthesizerClient* SpeechSynthesizerClient::Create(dbus::Bus* bus) { |
| 142 if (system::runtime_environment::IsRunningOnChromeOS()) { |
| 143 return new SpeechSynthesizerClientImpl(bus); |
| 144 } else { |
| 145 return new SpeechSynthesizerClientStubImpl(); |
| 146 } |
| 147 } |
| 148 |
| 149 } // namespace chromeos |
OLD | NEW |