Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: chrome/browser/chromeos/dbus/speech_synthesizer_client.cc

Issue 8334018: Move the code in libcros' chromeos_speech_synthesis.cc into Chrome (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed comments and copyright, cleaned up includes Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface,
41 "Speak");
satorux1 2011/10/18 20:49:25 Can you define a constant for "Speak" in service_c
hashimoto 2011/10/19 00:08:47 Done.
42 dbus::MessageWriter writer(&method_call);
43 writer.AppendString(text);
44 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
45 base::Bind(&SpeechSynthesizerClientImpl::OnSpeak,
46 weak_ptr_factory_.GetWeakPtr()));
47 }
48
49 virtual void SetSpeakProperties(const std::string& props) OVERRIDE {
50 dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface,
51 "SetProperties");
satorux1 2011/10/18 20:49:25 ditto.
52 dbus::MessageWriter writer(&method_call);
53 writer.AppendString(props);
54 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
55 base::Bind(
56 &SpeechSynthesizerClientImpl::OnSetSpeakProperties,
57 weak_ptr_factory_.GetWeakPtr()));
58 }
59
60 virtual void StopSpeaking() OVERRIDE {
61 dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface,
62 "Stop");
63 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
64 base::Bind(&SpeechSynthesizerClientImpl::OnStopSpeaking,
65 weak_ptr_factory_.GetWeakPtr()));
66 }
67
68 virtual void IsSpeaking(IsSpeakingCallback callback) OVERRIDE {
69 dbus::MethodCall method_call(speech_synthesis::kSpeechSynthesizerInterface,
70 "IsSpeaking");
71 proxy_->CallMethod(&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
72 base::Bind(&SpeechSynthesizerClientImpl::OnIsSpeaking,
73 weak_ptr_factory_.GetWeakPtr(),
74 callback));
75 }
76
77 private:
78 // Called when a response for Speak() is received
79 void OnSpeak(dbus::Response* response) {
80 if (!response) {
81 LOG(ERROR) << "Failed to speak.";
82 return;
83 }
84 VLOG(1) << "Spoke: " << response->ToString();
85 }
86
87 // Called when a response for SetSpeakProperties() is received
88 void OnSetSpeakProperties(dbus::Response* response) {
89 if (!response) {
90 LOG(ERROR) << "Failed to set speak properties.";
91 return;
92 }
93 VLOG(1) << "Set speak properties: " << response->ToString();
94 }
95
96 // Called when a response for StopSpeaking() is received
97 void OnStopSpeaking(dbus::Response* response) {
98 if (!response) {
99 LOG(ERROR) << "Failed to stop speaking.";
100 return;
101 }
102 VLOG(1) << "Stopped speaking: " << response->ToString();
103 }
104
105 // Called when a response for IsSpeaking() is received
106 void OnIsSpeaking(IsSpeakingCallback callback, dbus::Response* response) {
107 bool value = false;
108 if (response) {
109 dbus::MessageReader reader(response);
110 reader.PopBool(&value);
111 } else {
112 LOG(ERROR) << "Failed to ask if it is speaking";
113 }
114 callback.Run(value);
115 }
116
117 dbus::ObjectProxy* proxy_;
118 base::WeakPtrFactory<SpeechSynthesizerClientImpl> weak_ptr_factory_;
119
120 DISALLOW_COPY_AND_ASSIGN(SpeechSynthesizerClientImpl);
121 };
122
123 class SpeechSynthesizerClientStubImpl : public SpeechSynthesizerClient {
124 public:
125 SpeechSynthesizerClientStubImpl() {}
126 virtual ~SpeechSynthesizerClientStubImpl() {}
127 virtual void Speak(const std::string& text) OVERRIDE {}
128 virtual void SetSpeakProperties(const std::string& props) OVERRIDE {}
129 virtual void StopSpeaking() OVERRIDE {}
130 virtual void IsSpeaking(IsSpeakingCallback callback) OVERRIDE {
131 callback.Run(false);
132 }
133
134 private:
135 DISALLOW_COPY_AND_ASSIGN(SpeechSynthesizerClientStubImpl);
136 };
137
138 // static
139 SpeechSynthesizerClient* SpeechSynthesizerClient::Create(dbus::Bus* bus) {
140 if (system::runtime_environment::IsRunningOnChromeOS()) {
141 return new SpeechSynthesizerClientImpl(bus);
142 } else {
143 return new SpeechSynthesizerClientStubImpl();
144 }
145 }
146
147 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698