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

Side by Side Diff: chrome/browser/extensions/extension_tts_api_chromeos.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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h"
5 #include "base/memory/singleton.h" 6 #include "base/memory/singleton.h"
6 #include "base/message_loop.h" 7 #include "base/message_loop.h"
7 #include "base/string_number_conversions.h" 8 #include "base/string_number_conversions.h"
8 #include "base/task.h" 9 #include "base/task.h"
9 #include "chrome/browser/chromeos/cros/cros_library.h" 10 #include "chrome/browser/chromeos/dbus/dbus_thread_manager.h"
10 #include "chrome/browser/chromeos/cros/speech_synthesis_library.h" 11 #include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h"
11 #include "chrome/browser/extensions/extension_tts_api_controller.h" 12 #include "chrome/browser/extensions/extension_tts_api_controller.h"
12 #include "chrome/browser/extensions/extension_tts_api_platform.h" 13 #include "chrome/browser/extensions/extension_tts_api_platform.h"
13 14
14 using base::DoubleToString; 15 using base::DoubleToString;
15 16
16 namespace { 17 namespace {
17 const char kCrosLibraryNotLoadedError[] = "Cros shared library not loaded.";
18 const int kSpeechCheckDelayIntervalMs = 100; 18 const int kSpeechCheckDelayIntervalMs = 100;
19 }; 19 };
20 20
21 class ExtensionTtsPlatformImplChromeOs : public ExtensionTtsPlatformImpl { 21 class ExtensionTtsPlatformImplChromeOs : public ExtensionTtsPlatformImpl {
22 public: 22 public:
23 virtual bool PlatformImplAvailable() { 23 virtual bool PlatformImplAvailable() {
24 return true; 24 return true;
25 } 25 }
26 26
27 virtual bool Speak( 27 virtual bool Speak(
28 int utterance_id, 28 int utterance_id,
29 const std::string& utterance, 29 const std::string& utterance,
30 const std::string& lang, 30 const std::string& lang,
31 const UtteranceContinuousParameters& params); 31 const UtteranceContinuousParameters& params);
32 32
33 virtual bool StopSpeaking(); 33 virtual bool StopSpeaking();
34 34
35 virtual bool SendsEvent(TtsEventType event_type); 35 virtual bool SendsEvent(TtsEventType event_type);
36 36
37 // Get the single instance of this class. 37 // Get the single instance of this class.
38 static ExtensionTtsPlatformImplChromeOs* GetInstance(); 38 static ExtensionTtsPlatformImplChromeOs* GetInstance();
39 39
40 private: 40 private:
41 ExtensionTtsPlatformImplChromeOs() 41 ExtensionTtsPlatformImplChromeOs()
42 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} 42 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
43 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {}
43 virtual ~ExtensionTtsPlatformImplChromeOs() {} 44 virtual ~ExtensionTtsPlatformImplChromeOs() {}
44 45
45 void PollUntilSpeechFinishes(int utterance_id); 46 void PollUntilSpeechFinishes(int utterance_id);
47 void ContinuePollingIfSpeechIsNotFinished(int utterance_id, bool result);
46 48
47 void AppendSpeakOption(std::string key, 49 void AppendSpeakOption(std::string key,
48 std::string value, 50 std::string value,
49 std::string* options); 51 std::string* options);
50 52
51 int utterance_id_; 53 int utterance_id_;
52 int utterance_length_; 54 int utterance_length_;
53 ScopedRunnableMethodFactory<ExtensionTtsPlatformImplChromeOs> method_factory_; 55 ScopedRunnableMethodFactory<ExtensionTtsPlatformImplChromeOs> method_factory_;
56 base::WeakPtrFactory<ExtensionTtsPlatformImplChromeOs> weak_ptr_factory_;
54 57
55 friend struct DefaultSingletonTraits<ExtensionTtsPlatformImplChromeOs>; 58 friend struct DefaultSingletonTraits<ExtensionTtsPlatformImplChromeOs>;
56 59
57 DISALLOW_COPY_AND_ASSIGN(ExtensionTtsPlatformImplChromeOs); 60 DISALLOW_COPY_AND_ASSIGN(ExtensionTtsPlatformImplChromeOs);
58 }; 61 };
59 62
60 // static 63 // static
61 ExtensionTtsPlatformImpl* ExtensionTtsPlatformImpl::GetInstance() { 64 ExtensionTtsPlatformImpl* ExtensionTtsPlatformImpl::GetInstance() {
62 return ExtensionTtsPlatformImplChromeOs::GetInstance(); 65 return ExtensionTtsPlatformImplChromeOs::GetInstance();
63 } 66 }
64 67
65 bool ExtensionTtsPlatformImplChromeOs::Speak( 68 bool ExtensionTtsPlatformImplChromeOs::Speak(
66 int utterance_id, 69 int utterance_id,
67 const std::string& utterance, 70 const std::string& utterance,
68 const std::string& lang, 71 const std::string& lang,
69 const UtteranceContinuousParameters& params) { 72 const UtteranceContinuousParameters& params) {
70 chromeos::CrosLibrary* cros_library = chromeos::CrosLibrary::Get();
71 if (!cros_library->EnsureLoaded()) {
72 set_error(kCrosLibraryNotLoadedError);
73 return false;
74 }
75
76 utterance_id_ = utterance_id; 73 utterance_id_ = utterance_id;
77 utterance_length_ = utterance.size(); 74 utterance_length_ = utterance.size();
78 75
79 std::string options; 76 std::string options;
80 77
81 if (!lang.empty()) { 78 if (!lang.empty()) {
82 AppendSpeakOption( 79 AppendSpeakOption(
83 chromeos::SpeechSynthesisLibrary::kSpeechPropertyLocale, 80 chromeos::SpeechSynthesizerClient::kSpeechPropertyLocale,
84 lang, 81 lang,
85 &options); 82 &options);
86 } 83 }
87 84
88 if (params.rate >= 0.0) { 85 if (params.rate >= 0.0) {
89 AppendSpeakOption( 86 AppendSpeakOption(
90 chromeos::SpeechSynthesisLibrary::kSpeechPropertyRate, 87 chromeos::SpeechSynthesizerClient::kSpeechPropertyRate,
91 DoubleToString(params.rate), 88 DoubleToString(params.rate),
92 &options); 89 &options);
93 } 90 }
94 91
95 if (params.pitch >= 0.0) { 92 if (params.pitch >= 0.0) {
96 // The TTS service allows a range of 0 to 2 for speech pitch. 93 // The TTS service allows a range of 0 to 2 for speech pitch.
97 AppendSpeakOption( 94 AppendSpeakOption(
98 chromeos::SpeechSynthesisLibrary::kSpeechPropertyPitch, 95 chromeos::SpeechSynthesizerClient::kSpeechPropertyPitch,
99 DoubleToString(params.pitch), 96 DoubleToString(params.pitch),
100 &options); 97 &options);
101 } 98 }
102 99
103 if (params.volume >= 0.0) { 100 if (params.volume >= 0.0) {
104 // The Chrome OS TTS service allows a range of 0 to 5 for speech volume, 101 // The Chrome OS TTS service allows a range of 0 to 5 for speech volume,
105 // but 5 clips, so map to a range of 0...4. 102 // but 5 clips, so map to a range of 0...4.
106 AppendSpeakOption( 103 AppendSpeakOption(
107 chromeos::SpeechSynthesisLibrary::kSpeechPropertyVolume, 104 chromeos::SpeechSynthesizerClient::kSpeechPropertyVolume,
108 DoubleToString(params.volume * 4), 105 DoubleToString(params.volume * 4),
109 &options); 106 &options);
110 } 107 }
111 108
112 if (!options.empty()) { 109 chromeos::SpeechSynthesizerClient* speech_synthesizer_client =
113 cros_library->GetSpeechSynthesisLibrary()->SetSpeakProperties( 110 chromeos::DBusThreadManager::Get()->speech_synthesizer_client();
114 options.c_str());
115 }
116 111
117 bool result = 112 if (!options.empty())
118 cros_library->GetSpeechSynthesisLibrary()->Speak(utterance.c_str()); 113 speech_synthesizer_client->SetSpeakProperties(options);
119 114
120 if (result) { 115 speech_synthesizer_client->Speak(utterance);
satorux1 2011/10/18 20:49:25 I was a bit worried about the change here as Speak
hashimoto 2011/10/19 00:08:47 Yes, this change keeps the semantics, nothing is c
121 ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); 116 ExtensionTtsController* controller = ExtensionTtsController::GetInstance();
122 controller->OnTtsEvent(utterance_id_, TTS_EVENT_START, 0, std::string()); 117 controller->OnTtsEvent(utterance_id_, TTS_EVENT_START, 0, std::string());
123 PollUntilSpeechFinishes(utterance_id_); 118 PollUntilSpeechFinishes(utterance_id_);
124 }
125 119
126 return result; 120 return true;
127 } 121 }
128 122
129 bool ExtensionTtsPlatformImplChromeOs::StopSpeaking() { 123 bool ExtensionTtsPlatformImplChromeOs::StopSpeaking() {
130 if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { 124 chromeos::DBusThreadManager::Get()->speech_synthesizer_client()->
131 return chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> 125 StopSpeaking();
132 StopSpeaking(); 126 return true;
133 }
134
135 set_error(kCrosLibraryNotLoadedError);
136 return false;
137 } 127 }
138 128
139 bool ExtensionTtsPlatformImplChromeOs::SendsEvent(TtsEventType event_type) { 129 bool ExtensionTtsPlatformImplChromeOs::SendsEvent(TtsEventType event_type) {
140 return (event_type == TTS_EVENT_START || 130 return (event_type == TTS_EVENT_START ||
141 event_type == TTS_EVENT_END || 131 event_type == TTS_EVENT_END ||
142 event_type == TTS_EVENT_ERROR); 132 event_type == TTS_EVENT_ERROR);
143 } 133 }
144 134
145 void ExtensionTtsPlatformImplChromeOs::PollUntilSpeechFinishes( 135 void ExtensionTtsPlatformImplChromeOs::PollUntilSpeechFinishes(
146 int utterance_id) { 136 int utterance_id) {
147 if (utterance_id != utterance_id_) { 137 if (utterance_id != utterance_id_) {
148 // This utterance must have been interrupted or cancelled. 138 // This utterance must have been interrupted or cancelled.
149 return; 139 return;
150 } 140 }
141 chromeos::SpeechSynthesizerClient* speech_synthesizer_client =
142 chromeos::DBusThreadManager::Get()->speech_synthesizer_client();
143 speech_synthesizer_client->IsSpeaking(base::Bind(
144 &ExtensionTtsPlatformImplChromeOs::ContinuePollingIfSpeechIsNotFinished,
145 weak_ptr_factory_.GetWeakPtr(), utterance_id));
satorux1 2011/10/18 20:49:25 Nice! It's now asynchronous thus not blocking UI.
hashimoto 2011/10/19 00:08:47 I tried to make IsSpeaking synchronous, but gave u
146 }
151 147
152 chromeos::CrosLibrary* cros_library = chromeos::CrosLibrary::Get(); 148 void ExtensionTtsPlatformImplChromeOs::ContinuePollingIfSpeechIsNotFinished(
153 ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); 149 int utterance_id, bool is_speaking) {
154 150 if (utterance_id != utterance_id_) {
155 if (!cros_library->EnsureLoaded()) { 151 // This utterance must have been interrupted or cancelled.
156 controller->OnTtsEvent(
157 utterance_id_, TTS_EVENT_ERROR, 0, kCrosLibraryNotLoadedError);
158 return; 152 return;
159 } 153 }
160 154 if (!is_speaking) {
161 if (!cros_library->GetSpeechSynthesisLibrary()->IsSpeaking()) { 155 ExtensionTtsController* controller = ExtensionTtsController::GetInstance();
162 controller->OnTtsEvent( 156 controller->OnTtsEvent(
163 utterance_id_, TTS_EVENT_END, utterance_length_, std::string()); 157 utterance_id_, TTS_EVENT_END, utterance_length_, std::string());
164 return; 158 return;
165 } 159 }
166 160 // Contniue polling.
167 MessageLoop::current()->PostDelayedTask( 161 MessageLoop::current()->PostDelayedTask(
168 FROM_HERE, method_factory_.NewRunnableMethod( 162 FROM_HERE, method_factory_.NewRunnableMethod(
169 &ExtensionTtsPlatformImplChromeOs::PollUntilSpeechFinishes, 163 &ExtensionTtsPlatformImplChromeOs::PollUntilSpeechFinishes,
170 utterance_id), 164 utterance_id),
171 kSpeechCheckDelayIntervalMs); 165 kSpeechCheckDelayIntervalMs);
172 } 166 }
173 167
174 void ExtensionTtsPlatformImplChromeOs::AppendSpeakOption( 168 void ExtensionTtsPlatformImplChromeOs::AppendSpeakOption(
175 std::string key, 169 std::string key,
176 std::string value, 170 std::string value,
177 std::string* options) { 171 std::string* options) {
178 *options += 172 *options +=
179 key + 173 key +
180 chromeos::SpeechSynthesisLibrary::kSpeechPropertyEquals + 174 chromeos::SpeechSynthesizerClient::kSpeechPropertyEquals +
181 value + 175 value +
182 chromeos::SpeechSynthesisLibrary::kSpeechPropertyDelimiter; 176 chromeos::SpeechSynthesizerClient::kSpeechPropertyDelimiter;
183 } 177 }
184 178
185 // static 179 // static
186 ExtensionTtsPlatformImplChromeOs* 180 ExtensionTtsPlatformImplChromeOs*
187 ExtensionTtsPlatformImplChromeOs::GetInstance() { 181 ExtensionTtsPlatformImplChromeOs::GetInstance() {
188 return Singleton<ExtensionTtsPlatformImplChromeOs>::get(); 182 return Singleton<ExtensionTtsPlatformImplChromeOs>::get();
189 } 183 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698