OLD | NEW |
| (Empty) |
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. | |
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 "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" | |
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 dbus::ObjectPath(speech_synthesis::kSpeechSynthesizerServicePath)); | |
36 } | |
37 virtual ~SpeechSynthesizerClientImpl() {} | |
38 | |
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())); | |
49 } | |
50 | |
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())); | |
57 } | |
58 | |
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)); | |
66 } | |
67 | |
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; | |
74 } | |
75 VLOG(1) << "Spoke: " << response->ToString(); | |
76 } | |
77 | |
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; | |
83 } | |
84 VLOG(1) << "Stopped speaking: " << response->ToString(); | |
85 } | |
86 | |
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"; | |
96 } | |
97 callback.Run(value); | |
98 } | |
99 | |
100 dbus::ObjectProxy* proxy_; | |
101 base::WeakPtrFactory<SpeechSynthesizerClientImpl> weak_ptr_factory_; | |
102 | |
103 DISALLOW_COPY_AND_ASSIGN(SpeechSynthesizerClientImpl); | |
104 }; | |
105 | |
106 class SpeechSynthesizerClientStubImpl : public SpeechSynthesizerClient { | |
107 public: | |
108 SpeechSynthesizerClientStubImpl() {} | |
109 virtual ~SpeechSynthesizerClientStubImpl() {} | |
110 virtual void Speak(const std::string& text, | |
111 const std::string& properties) OVERRIDE {} | |
112 virtual void StopSpeaking() OVERRIDE {} | |
113 virtual void IsSpeaking(const IsSpeakingCallback& callback) OVERRIDE { | |
114 callback.Run(false); | |
115 } | |
116 | |
117 private: | |
118 DISALLOW_COPY_AND_ASSIGN(SpeechSynthesizerClientStubImpl); | |
119 }; | |
120 | |
121 SpeechSynthesizerClient::SpeechSynthesizerClient() { | |
122 } | |
123 | |
124 SpeechSynthesizerClient::~SpeechSynthesizerClient() { | |
125 } | |
126 | |
127 // static | |
128 SpeechSynthesizerClient* SpeechSynthesizerClient::Create( | |
129 DBusClientImplementationType type, | |
130 dbus::Bus* bus) { | |
131 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
132 return new SpeechSynthesizerClientImpl(bus); | |
133 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
134 return new SpeechSynthesizerClientStubImpl(); | |
135 } | |
136 | |
137 } // namespace chromeos | |
OLD | NEW |