diff options
author | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-19 08:19:44 +0000 |
---|---|---|
committer | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-19 08:19:44 +0000 |
commit | 67340cae779526ebd94b30eff03d3a72186ab821 (patch) | |
tree | 15521794c505493fed3fb9c54d946883f0dd3c87 /chrome | |
parent | 20f00091826b936b317cf1268aac0cd763b3cfd4 (diff) | |
download | chromium_src-67340cae779526ebd94b30eff03d3a72186ab821.zip chromium_src-67340cae779526ebd94b30eff03d3a72186ab821.tar.gz chromium_src-67340cae779526ebd94b30eff03d3a72186ab821.tar.bz2 |
Move the code in libcros' chromeos_speech_synthesis.cc into Chrome
Add SpeechSynthesizerClient which consists the code copied from chromeos_speech_synthesis.cc
Remove the test ExtensionApiTest.TtsChromeOs from extension_tts_apitest.cc
BUG=chromium-os:16560
TEST=Enable accessibility feature by pressing Ctrl+Alt+Z on the login screen, ensure that it is speaking as before.
Review URL: http://codereview.chromium.org/8334018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@106246 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
17 files changed, 274 insertions, 313 deletions
diff --git a/chrome/browser/chromeos/accessibility_util.cc b/chrome/browser/chromeos/accessibility_util.cc index b487e78..dce20f9 100644 --- a/chrome/browser/chromeos/accessibility_util.cc +++ b/chrome/browser/chromeos/accessibility_util.cc @@ -7,8 +7,8 @@ #include "base/callback.h" #include "base/logging.h" #include "chrome/browser/browser_process.h" -#include "chrome/browser/chromeos/cros/cros_library.h" -#include "chrome/browser/chromeos/cros/speech_synthesis_library.h" +#include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" +#include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h" #include "chrome/browser/extensions/extension_accessibility_api.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/file_reader.h" @@ -155,19 +155,17 @@ void ToggleAccessibility(WebUI* login_web_ui) { }; void Speak(const char* speak_str, bool queue, bool interruptible) { - if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { - if (queue || !interruptible) { - std::string props = ""; - props.append("enqueue="); - props.append(queue ? "1;" : "0;"); - props.append("interruptible="); - props.append(interruptible ? "1;" : "0;"); - chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> - SetSpeakProperties(props.c_str()); - } - chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> - Speak(speak_str); + if (queue || !interruptible) { + std::string props = ""; + props.append("enqueue="); + props.append(queue ? "1;" : "0;"); + props.append("interruptible="); + props.append(interruptible ? "1;" : "0;"); + chromeos::DBusThreadManager::Get()->speech_synthesizer_client()-> + SetSpeakProperties(props); } + chromeos::DBusThreadManager::Get()->speech_synthesizer_client()-> + Speak(speak_str); } diff --git a/chrome/browser/chromeos/cros/cros_library.cc b/chrome/browser/chromeos/cros/cros_library.cc index fd54bb4..a6c642b 100644 --- a/chrome/browser/chromeos/cros/cros_library.cc +++ b/chrome/browser/chromeos/cros/cros_library.cc @@ -12,7 +12,6 @@ #include "chrome/browser/chromeos/cros/network_library.h" #include "chrome/browser/chromeos/cros/power_library.h" #include "chrome/browser/chromeos/cros/screen_lock_library.h" -#include "chrome/browser/chromeos/cros/speech_synthesis_library.h" #include "chrome/browser/chromeos/cros/update_library.h" #include "third_party/cros/chromeos_cros_api.h" @@ -87,7 +86,6 @@ DEFINE_GET_LIBRARY_METHOD(Mount, mount); DEFINE_GET_LIBRARY_METHOD(Network, network); DEFINE_GET_LIBRARY_METHOD(Power, power); DEFINE_GET_LIBRARY_METHOD(ScreenLock, screen_lock); -DEFINE_GET_LIBRARY_METHOD(SpeechSynthesis, speech_synthesis); DEFINE_GET_LIBRARY_METHOD(Update, update); bool CrosLibrary::LoadLibcros() { @@ -134,7 +132,6 @@ DEFINE_SET_LIBRARY_METHOD(Mount, mount); DEFINE_SET_LIBRARY_METHOD(Network, network); DEFINE_SET_LIBRARY_METHOD(Power, power); DEFINE_SET_LIBRARY_METHOD(ScreenLock, screen_lock); -DEFINE_SET_LIBRARY_METHOD(SpeechSynthesis, speech_synthesis); DEFINE_SET_LIBRARY_METHOD(Update, update); } // namespace chromeos diff --git a/chrome/browser/chromeos/cros/cros_library.h b/chrome/browser/chromeos/cros/cros_library.h index 708dbf9..2a67941 100644 --- a/chrome/browser/chromeos/cros/cros_library.h +++ b/chrome/browser/chromeos/cros/cros_library.h @@ -24,7 +24,6 @@ class MountLibrary; class NetworkLibrary; class PowerLibrary; class ScreenLockLibrary; -class SpeechSynthesisLibrary; class UpdateLibrary; // This class handles access to sub-parts of ChromeOS library. it provides @@ -52,7 +51,6 @@ class CrosLibrary { void SetNetworkLibrary(NetworkLibrary* library, bool own); void SetPowerLibrary(PowerLibrary* library, bool own); void SetScreenLockLibrary(ScreenLockLibrary* library, bool own); - void SetSpeechSynthesisLibrary(SpeechSynthesisLibrary* library, bool own); void SetUpdateLibrary(UpdateLibrary* library, bool own); private: @@ -79,7 +77,6 @@ class CrosLibrary { NetworkLibrary* GetNetworkLibrary(); PowerLibrary* GetPowerLibrary(); ScreenLockLibrary* GetScreenLockLibrary(); - SpeechSynthesisLibrary* GetSpeechSynthesisLibrary(); UpdateLibrary* GetUpdateLibrary(); // Getter for Test API that gives access to internal members of this class. @@ -151,7 +148,6 @@ class CrosLibrary { Library<NetworkLibrary> network_lib_; Library<PowerLibrary> power_lib_; Library<ScreenLockLibrary> screen_lock_lib_; - Library<SpeechSynthesisLibrary> speech_synthesis_lib_; Library<UpdateLibrary> update_lib_; // Stub implementations of the libraries should be used. diff --git a/chrome/browser/chromeos/cros/cros_mock.cc b/chrome/browser/chromeos/cros/cros_mock.cc index f592555..95ce0ec 100644 --- a/chrome/browser/chromeos/cros/cros_mock.cc +++ b/chrome/browser/chromeos/cros/cros_mock.cc @@ -12,7 +12,6 @@ #include "chrome/browser/chromeos/cros/mock_network_library.h" #include "chrome/browser/chromeos/cros/mock_power_library.h" #include "chrome/browser/chromeos/cros/mock_screen_lock_library.h" -#include "chrome/browser/chromeos/cros/mock_speech_synthesis_library.h" #include "chrome/browser/chromeos/login/wizard_controller.h" #include "chrome/browser/chromeos/login/wizard_screen.h" #include "chrome/test/base/in_process_browser_test.h" @@ -36,8 +35,7 @@ CrosMock::CrosMock() mock_cryptohome_library_(NULL), mock_network_library_(NULL), mock_power_library_(NULL), - mock_screen_lock_library_(NULL), - mock_speech_synthesis_library_(NULL) { + mock_screen_lock_library_(NULL) { } CrosMock::~CrosMock() { @@ -94,15 +92,6 @@ void CrosMock::InitMockScreenLockLibrary() { test_api()->SetScreenLockLibrary(mock_screen_lock_library_, true); } -void CrosMock::InitMockSpeechSynthesisLibrary() { - InitMockLibraryLoader(); - if (mock_speech_synthesis_library_) - return; - mock_speech_synthesis_library_ = - new StrictMock<MockSpeechSynthesisLibrary>(); - test_api()->SetSpeechSynthesisLibrary(mock_speech_synthesis_library_, true); -} - // Initialization of mocks. MockCryptohomeLibrary* CrosMock::mock_cryptohome_library() { return mock_cryptohome_library_; @@ -120,10 +109,6 @@ MockScreenLockLibrary* CrosMock::mock_screen_lock_library() { return mock_screen_lock_library_; } -MockSpeechSynthesisLibrary* CrosMock::mock_speech_synthesis_library() { - return mock_speech_synthesis_library_; -} - void CrosMock::SetStatusAreaMocksExpectations() { SetNetworkLibraryStatusAreaExpectations(); SetPowerLibraryStatusAreaExpectations(); @@ -275,36 +260,6 @@ void CrosMock::SetPowerLibraryExpectations() { .Times(AnyNumber()); } -void CrosMock::SetSpeechSynthesisLibraryExpectations() { - InSequence s; - EXPECT_CALL(*mock_speech_synthesis_library_, StopSpeaking()) - .WillOnce(Return(true)) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_speech_synthesis_library_, SetSpeakProperties(_)) - .WillOnce(Return(true)) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_speech_synthesis_library_, Speak(_)) - .WillOnce(Return(true)) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_speech_synthesis_library_, IsSpeaking()) - .WillOnce(Return(true)) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_speech_synthesis_library_, StopSpeaking()) - .WillOnce(Return(true)) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_speech_synthesis_library_, SetSpeakProperties(_)) - .WillOnce(Return(true)) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_speech_synthesis_library_, Speak(_)) - .WillOnce(Return(true)) - .RetiresOnSaturation(); - EXPECT_CALL(*mock_speech_synthesis_library_, IsSpeaking()) - .WillOnce(Return(true)) - .WillOnce(Return(true)) - .WillOnce(Return(false)) - .RetiresOnSaturation(); -} - void CrosMock::TearDownMocks() { // Prevent bogus gMock leak check from firing. if (loader_) @@ -317,8 +272,6 @@ void CrosMock::TearDownMocks() { test_api()->SetPowerLibrary(NULL, false); if (mock_screen_lock_library_) test_api()->SetScreenLockLibrary(NULL, false); - if (mock_speech_synthesis_library_) - test_api()->SetSpeechSynthesisLibrary(NULL, false); } } // namespace chromeos diff --git a/chrome/browser/chromeos/cros/cros_mock.h b/chrome/browser/chromeos/cros/cros_mock.h index 31c8a64..5435777 100644 --- a/chrome/browser/chromeos/cros/cros_mock.h +++ b/chrome/browser/chromeos/cros/cros_mock.h @@ -17,7 +17,6 @@ class MockLibraryLoader; class MockNetworkLibrary; class MockPowerLibrary; class MockScreenLockLibrary; -class MockSpeechSynthesisLibrary; // Class for initializing mocks for some parts of CrosLibrary. Once you mock // part of CrosLibrary it will be considered as successfully loaded and @@ -45,7 +44,6 @@ class CrosMock { void InitMockNetworkLibrary(); void InitMockPowerLibrary(); void InitMockScreenLockLibrary(); - void InitMockSpeechSynthesisLibrary(); // Get mocks. MockCryptohomeLibrary* mock_cryptohome_library(); @@ -53,7 +51,6 @@ class CrosMock { MockNetworkLibrary* mock_network_library(); MockPowerLibrary* mock_power_library(); MockScreenLockLibrary* mock_screen_lock_library(); - MockSpeechSynthesisLibrary* mock_speech_synthesis_library(); // This method sets up corresponding expectations for basic mocks that // are used by status area items. @@ -68,7 +65,6 @@ class CrosMock { void SetNetworkLibraryStatusAreaExpectations(); void SetPowerLibraryStatusAreaExpectations(); void SetPowerLibraryExpectations(); - void SetSpeechSynthesisLibraryExpectations(); void TearDownMocks(); @@ -83,7 +79,6 @@ class CrosMock { MockNetworkLibrary* mock_network_library_; MockPowerLibrary* mock_power_library_; MockScreenLockLibrary* mock_screen_lock_library_; - MockSpeechSynthesisLibrary* mock_speech_synthesis_library_; WifiNetworkVector wifi_networks_; CellularNetworkVector cellular_networks_; diff --git a/chrome/browser/chromeos/cros/mock_speech_synthesis_library.cc b/chrome/browser/chromeos/cros/mock_speech_synthesis_library.cc deleted file mode 100644 index 5a1d9d7..0000000 --- a/chrome/browser/chromeos/cros/mock_speech_synthesis_library.cc +++ /dev/null @@ -1,13 +0,0 @@ -// 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/cros/mock_speech_synthesis_library.h" - -namespace chromeos { - -MockSpeechSynthesisLibrary::MockSpeechSynthesisLibrary() {} - -MockSpeechSynthesisLibrary::~MockSpeechSynthesisLibrary() {} - -} // namespace chromeos diff --git a/chrome/browser/chromeos/cros/mock_speech_synthesis_library.h b/chrome/browser/chromeos/cros/mock_speech_synthesis_library.h deleted file mode 100644 index 6b0d79e..0000000 --- a/chrome/browser/chromeos/cros/mock_speech_synthesis_library.h +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2010 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. - -#ifndef CHROME_BROWSER_CHROMEOS_CROS_MOCK_SPEECH_SYNTHESIS_LIBRARY_H_ -#define CHROME_BROWSER_CHROMEOS_CROS_MOCK_SPEECH_SYNTHESIS_LIBRARY_H_ -#pragma once - -#include "chrome/browser/chromeos/cros/speech_synthesis_library.h" -#include "testing/gmock/include/gmock/gmock.h" - -namespace chromeos { - -class MockSpeechSynthesisLibrary : public SpeechSynthesisLibrary { - public: - MockSpeechSynthesisLibrary(); - virtual ~MockSpeechSynthesisLibrary(); - - MOCK_METHOD1(Speak, bool(const char*)); - MOCK_METHOD1(SetSpeakProperties, bool(const char*)); - MOCK_METHOD0(StopSpeaking, bool(void)); - MOCK_METHOD0(IsSpeaking, bool(void)); - MOCK_METHOD1(InitTts, void(InitStatusCallback)); -}; - -} // namespace chromeos - -#endif // CHROME_BROWSER_CHROMEOS_CROS_MOCK_SPEECH_SYNTHESIS_LIBRARY_H_ diff --git a/chrome/browser/chromeos/cros/speech_synthesis_library.cc b/chrome/browser/chromeos/cros/speech_synthesis_library.cc deleted file mode 100644 index 1102f94..0000000 --- a/chrome/browser/chromeos/cros/speech_synthesis_library.cc +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright (c) 2010 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/cros/speech_synthesis_library.h" - -#include "base/message_loop.h" -#include "chrome/browser/chromeos/cros/cros_library.h" -#include "third_party/cros/chromeos_speech_synthesis.h" - -namespace chromeos { - -// TODO(chaitanyag): rename to "locale" after making equivalent change in -// Chrome OS code. -const char SpeechSynthesisLibrary::kSpeechPropertyLocale[] = "name"; - -const char SpeechSynthesisLibrary::kSpeechPropertyGender[] = "gender"; -const char SpeechSynthesisLibrary::kSpeechPropertyRate[] = "rate"; -const char SpeechSynthesisLibrary::kSpeechPropertyPitch[] = "pitch"; -const char SpeechSynthesisLibrary::kSpeechPropertyVolume[] = "volume"; -const char SpeechSynthesisLibrary::kSpeechPropertyEquals[] = "="; -const char SpeechSynthesisLibrary::kSpeechPropertyDelimiter[] = ";"; - -class SpeechSynthesisLibraryImpl : public SpeechSynthesisLibrary { - public: - SpeechSynthesisLibraryImpl() {} - virtual ~SpeechSynthesisLibraryImpl() {} - - bool Speak(const char* text) { - return chromeos::Speak(text); - } - - bool SetSpeakProperties(const char* props) { - return chromeos::SetSpeakProperties(props); - } - - bool StopSpeaking() { - return chromeos::StopSpeaking(); - } - - bool IsSpeaking() { - return chromeos::IsSpeaking(); - } - - void InitTts(InitStatusCallback callback) { - chromeos::InitTts(callback); - } - - private: - DISALLOW_COPY_AND_ASSIGN(SpeechSynthesisLibraryImpl); -}; - -class SpeechSynthesisLibraryStubImpl : public SpeechSynthesisLibrary { - public: - SpeechSynthesisLibraryStubImpl() {} - virtual ~SpeechSynthesisLibraryStubImpl() {} - bool Speak(const char* text) { return true; } - bool SetSpeakProperties(const char* props) { return true; } - bool StopSpeaking() { return true; } - bool IsSpeaking() { return false; } - void InitTts(InitStatusCallback callback) {} - - private: - DISALLOW_COPY_AND_ASSIGN(SpeechSynthesisLibraryStubImpl); -}; - -// static -SpeechSynthesisLibrary* SpeechSynthesisLibrary::GetImpl(bool stub) { - SpeechSynthesisLibrary* impl; - if (stub) - impl = new SpeechSynthesisLibraryStubImpl(); - else - impl = new SpeechSynthesisLibraryImpl(); - return impl; -} - -} // namespace chromeos diff --git a/chrome/browser/chromeos/cros/speech_synthesis_library.h b/chrome/browser/chromeos/cros/speech_synthesis_library.h deleted file mode 100644 index e2fed5a..0000000 --- a/chrome/browser/chromeos/cros/speech_synthesis_library.h +++ /dev/null @@ -1,53 +0,0 @@ -// 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. - -#ifndef CHROME_BROWSER_CHROMEOS_CROS_SPEECH_SYNTHESIS_LIBRARY_H_ -#define CHROME_BROWSER_CHROMEOS_CROS_SPEECH_SYNTHESIS_LIBRARY_H_ -#pragma once - -#include "base/memory/singleton.h" - -namespace chromeos { - -// This interface defines the interaction with the ChromeOS login library APIs. -class SpeechSynthesisLibrary { - public: - typedef void(*InitStatusCallback)(bool success); - - virtual ~SpeechSynthesisLibrary() {} - - // Speaks the specified text. - virtual bool Speak(const char* text) = 0; - - // Sets options for the subsequent speech synthesis requests. - // Use the constants below. - virtual bool SetSpeakProperties(const char* props) = 0; - - // Stops speaking the current utterance. - virtual bool StopSpeaking() = 0; - - // Checks if the engine is currently speaking. - virtual bool IsSpeaking() = 0; - - // Starts the speech synthesis service and indicates through a callback if - // it started successfully. - virtual void InitTts(InitStatusCallback) = 0; - - // Factory function, creates a new instance and returns ownership. - // For normal usage, access the singleton via CrosLibrary::Get(). - static SpeechSynthesisLibrary* GetImpl(bool stub); - - // Constants to be used with SetSpeakProperties. - static const char kSpeechPropertyLocale[]; - static const char kSpeechPropertyGender[]; - static const char kSpeechPropertyRate[]; - static const char kSpeechPropertyPitch[]; - static const char kSpeechPropertyVolume[]; - static const char kSpeechPropertyEquals[]; - static const char kSpeechPropertyDelimiter[]; -}; - -} // namespace chromeos - -#endif // CHROME_BROWSER_CHROMEOS_CROS_SPEECH_SYNTHESIS_LIBRARY_H_ diff --git a/chrome/browser/chromeos/dbus/dbus_thread_manager.cc b/chrome/browser/chromeos/dbus/dbus_thread_manager.cc index 4087273..30486c9 100644 --- a/chrome/browser/chromeos/dbus/dbus_thread_manager.cc +++ b/chrome/browser/chromeos/dbus/dbus_thread_manager.cc @@ -10,6 +10,7 @@ #include "chrome/browser/chromeos/dbus/session_manager_client.h" #include "chrome/browser/chromeos/dbus/power_manager_client.h" #include "chrome/browser/chromeos/dbus/sensors_source.h" +#include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h" #include "chrome/common/chrome_switches.h" #include "dbus/bus.h" @@ -48,6 +49,9 @@ DBusThreadManager::DBusThreadManager() { // Create the session manager client. session_manager_client_.reset( SessionManagerClient::Create(system_bus_.get())); + // Create the speech synthesizer client. + speech_synthesizer_client_.reset( + SpeechSynthesizerClient::Create(system_bus_.get())); } DBusThreadManager::~DBusThreadManager() { diff --git a/chrome/browser/chromeos/dbus/dbus_thread_manager.h b/chrome/browser/chromeos/dbus/dbus_thread_manager.h index ffdeb7cc..3225403 100644 --- a/chrome/browser/chromeos/dbus/dbus_thread_manager.h +++ b/chrome/browser/chromeos/dbus/dbus_thread_manager.h @@ -23,6 +23,7 @@ class CrosDBusService; class PowerManagerClient; class SessionManagerClient; class SensorsSource; +class SpeechSynthesizerClient; // DBusThreadManager manages the D-Bus thread, the thread dedicated to // handling asynchronous D-Bus operations. @@ -73,6 +74,13 @@ class DBusThreadManager { void set_session_manager_client_for_testing( SessionManagerClient* session_manager_client); + // Returns the speech synthesizer client, owned by DBusThreadManager. + // Do not cache this pointer and use it after DBusThreadManager is shut + // down. + SpeechSynthesizerClient* speech_synthesizer_client() { + return speech_synthesizer_client_.get(); + } + private: DBusThreadManager(); virtual ~DBusThreadManager(); @@ -83,6 +91,7 @@ class DBusThreadManager { scoped_ptr<SensorsSource> sensors_source_; scoped_ptr<PowerManagerClient> power_manager_client_; scoped_ptr<SessionManagerClient> session_manager_client_; + scoped_ptr<SpeechSynthesizerClient> speech_synthesizer_client_; DISALLOW_COPY_AND_ASSIGN(DBusThreadManager); }; 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 0000000..aef2c1e --- /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 diff --git a/chrome/browser/chromeos/dbus/speech_synthesizer_client.h b/chrome/browser/chromeos/dbus/speech_synthesizer_client.h new file mode 100644 index 0000000..731e8dd --- /dev/null +++ b/chrome/browser/chromeos/dbus/speech_synthesizer_client.h @@ -0,0 +1,60 @@ +// 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. + +#ifndef CHROME_BROWSER_CHROMEOS_DBUS_SPEECH_SYNTHESIZER_CLIENT_H_ +#define CHROME_BROWSER_CHROMEOS_DBUS_SPEECH_SYNTHESIZER_CLIENT_H_ +#pragma once + +#include <string> + +#include "base/callback.h" + +namespace dbus { +class Bus; +} + +namespace chromeos { + +// SpeechSynthesizerClient is used to communicate with the speech synthesizer. +// All method should be called from the origin thread (UI thread) which +// initializes the DBusThreadManager instance. +class SpeechSynthesizerClient { + public: + // A callback function called when the result of IsSpeaking is ready. + // The argument indicates if the speech synthesizer is speaking or not. + typedef base::Callback<void(bool)> IsSpeakingCallback; + + virtual ~SpeechSynthesizerClient() {} + + // Speaks the specified text. + virtual void Speak(const std::string& text) = 0; + + // Sets options for the subsequent speech synthesis requests. + // Use the constants below. + virtual void SetSpeakProperties(const std::string& props) = 0; + + // Stops speaking the current utterance. + virtual void StopSpeaking() = 0; + + // Checks if the engine is currently speaking. + // |callback| will be called on the origin thread later with the result. + virtual void IsSpeaking(IsSpeakingCallback callback) = 0; + + // Factory function, creates a new instance and returns ownership. + // For normal usage, access the singleton via DBusThreadManager::Get(). + static SpeechSynthesizerClient* Create(dbus::Bus* bus); + + // Constants to be used with SetSpeakProperties. + static const char kSpeechPropertyLocale[]; + static const char kSpeechPropertyGender[]; + static const char kSpeechPropertyRate[]; + static const char kSpeechPropertyPitch[]; + static const char kSpeechPropertyVolume[]; + static const char kSpeechPropertyEquals[]; + static const char kSpeechPropertyDelimiter[]; +}; + +} // namespace chromeos + +#endif // CHROME_BROWSER_CHROMEOS_DBUS_SPEECH_SYNTHESIZER_CLIENT_H_ diff --git a/chrome/browser/extensions/extension_tts_api_chromeos.cc b/chrome/browser/extensions/extension_tts_api_chromeos.cc index cf58b3d..b35d460 100644 --- a/chrome/browser/extensions/extension_tts_api_chromeos.cc +++ b/chrome/browser/extensions/extension_tts_api_chromeos.cc @@ -7,15 +7,14 @@ #include "base/memory/weak_ptr.h" #include "base/message_loop.h" #include "base/string_number_conversions.h" -#include "chrome/browser/chromeos/cros/cros_library.h" -#include "chrome/browser/chromeos/cros/speech_synthesis_library.h" +#include "chrome/browser/chromeos/dbus/dbus_thread_manager.h" +#include "chrome/browser/chromeos/dbus/speech_synthesizer_client.h" #include "chrome/browser/extensions/extension_tts_api_controller.h" #include "chrome/browser/extensions/extension_tts_api_platform.h" using base::DoubleToString; namespace { -const char kCrosLibraryNotLoadedError[] = "Cros shared library not loaded."; const int kSpeechCheckDelayIntervalMs = 100; }; @@ -40,10 +39,11 @@ class ExtensionTtsPlatformImplChromeOs : public ExtensionTtsPlatformImpl { private: ExtensionTtsPlatformImplChromeOs() - : ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) {} + : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {} virtual ~ExtensionTtsPlatformImplChromeOs() {} void PollUntilSpeechFinishes(int utterance_id); + void ContinuePollingIfSpeechIsNotFinished(int utterance_id, bool result); void AppendSpeakOption(std::string key, std::string value, @@ -51,7 +51,7 @@ class ExtensionTtsPlatformImplChromeOs : public ExtensionTtsPlatformImpl { int utterance_id_; int utterance_length_; - base::WeakPtrFactory<ExtensionTtsPlatformImplChromeOs> ptr_factory_; + base::WeakPtrFactory<ExtensionTtsPlatformImplChromeOs> weak_ptr_factory_; friend struct DefaultSingletonTraits<ExtensionTtsPlatformImplChromeOs>; @@ -68,12 +68,6 @@ bool ExtensionTtsPlatformImplChromeOs::Speak( const std::string& utterance, const std::string& lang, const UtteranceContinuousParameters& params) { - chromeos::CrosLibrary* cros_library = chromeos::CrosLibrary::Get(); - if (!cros_library->EnsureLoaded()) { - set_error(kCrosLibraryNotLoadedError); - return false; - } - utterance_id_ = utterance_id; utterance_length_ = utterance.size(); @@ -81,14 +75,14 @@ bool ExtensionTtsPlatformImplChromeOs::Speak( if (!lang.empty()) { AppendSpeakOption( - chromeos::SpeechSynthesisLibrary::kSpeechPropertyLocale, + chromeos::SpeechSynthesizerClient::kSpeechPropertyLocale, lang, &options); } if (params.rate >= 0.0) { AppendSpeakOption( - chromeos::SpeechSynthesisLibrary::kSpeechPropertyRate, + chromeos::SpeechSynthesizerClient::kSpeechPropertyRate, DoubleToString(params.rate), &options); } @@ -96,7 +90,7 @@ bool ExtensionTtsPlatformImplChromeOs::Speak( if (params.pitch >= 0.0) { // The TTS service allows a range of 0 to 2 for speech pitch. AppendSpeakOption( - chromeos::SpeechSynthesisLibrary::kSpeechPropertyPitch, + chromeos::SpeechSynthesizerClient::kSpeechPropertyPitch, DoubleToString(params.pitch), &options); } @@ -105,36 +99,29 @@ bool ExtensionTtsPlatformImplChromeOs::Speak( // The Chrome OS TTS service allows a range of 0 to 5 for speech volume, // but 5 clips, so map to a range of 0...4. AppendSpeakOption( - chromeos::SpeechSynthesisLibrary::kSpeechPropertyVolume, + chromeos::SpeechSynthesizerClient::kSpeechPropertyVolume, DoubleToString(params.volume * 4), &options); } - if (!options.empty()) { - cros_library->GetSpeechSynthesisLibrary()->SetSpeakProperties( - options.c_str()); - } + chromeos::SpeechSynthesizerClient* speech_synthesizer_client = + chromeos::DBusThreadManager::Get()->speech_synthesizer_client(); - bool result = - cros_library->GetSpeechSynthesisLibrary()->Speak(utterance.c_str()); + if (!options.empty()) + speech_synthesizer_client->SetSpeakProperties(options); - if (result) { - ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); - controller->OnTtsEvent(utterance_id_, TTS_EVENT_START, 0, std::string()); - PollUntilSpeechFinishes(utterance_id_); - } + speech_synthesizer_client->Speak(utterance); + ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); + controller->OnTtsEvent(utterance_id_, TTS_EVENT_START, 0, std::string()); + PollUntilSpeechFinishes(utterance_id_); - return result; + return true; } bool ExtensionTtsPlatformImplChromeOs::StopSpeaking() { - if (chromeos::CrosLibrary::Get()->EnsureLoaded()) { - return chromeos::CrosLibrary::Get()->GetSpeechSynthesisLibrary()-> - StopSpeaking(); - } - - set_error(kCrosLibraryNotLoadedError); - return false; + chromeos::DBusThreadManager::Get()->speech_synthesizer_client()-> + StopSpeaking(); + return true; } bool ExtensionTtsPlatformImplChromeOs::SendsEvent(TtsEventType event_type) { @@ -149,26 +136,30 @@ void ExtensionTtsPlatformImplChromeOs::PollUntilSpeechFinishes( // This utterance must have been interrupted or cancelled. return; } + chromeos::SpeechSynthesizerClient* speech_synthesizer_client = + chromeos::DBusThreadManager::Get()->speech_synthesizer_client(); + speech_synthesizer_client->IsSpeaking(base::Bind( + &ExtensionTtsPlatformImplChromeOs::ContinuePollingIfSpeechIsNotFinished, + weak_ptr_factory_.GetWeakPtr(), utterance_id)); +} - chromeos::CrosLibrary* cros_library = chromeos::CrosLibrary::Get(); - ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); - - if (!cros_library->EnsureLoaded()) { - controller->OnTtsEvent( - utterance_id_, TTS_EVENT_ERROR, 0, kCrosLibraryNotLoadedError); +void ExtensionTtsPlatformImplChromeOs::ContinuePollingIfSpeechIsNotFinished( + int utterance_id, bool is_speaking) { + if (utterance_id != utterance_id_) { + // This utterance must have been interrupted or cancelled. return; } - - if (!cros_library->GetSpeechSynthesisLibrary()->IsSpeaking()) { + if (!is_speaking) { + ExtensionTtsController* controller = ExtensionTtsController::GetInstance(); controller->OnTtsEvent( utterance_id_, TTS_EVENT_END, utterance_length_, std::string()); return; } - + // Continue polling. MessageLoop::current()->PostDelayedTask( FROM_HERE, base::Bind( &ExtensionTtsPlatformImplChromeOs::PollUntilSpeechFinishes, - ptr_factory_.GetWeakPtr(), + weak_ptr_factory_.GetWeakPtr(), utterance_id), kSpeechCheckDelayIntervalMs); } @@ -179,9 +170,9 @@ void ExtensionTtsPlatformImplChromeOs::AppendSpeakOption( std::string* options) { *options += key + - chromeos::SpeechSynthesisLibrary::kSpeechPropertyEquals + + chromeos::SpeechSynthesizerClient::kSpeechPropertyEquals + value + - chromeos::SpeechSynthesisLibrary::kSpeechPropertyDelimiter; + chromeos::SpeechSynthesizerClient::kSpeechPropertyDelimiter; } // static diff --git a/chrome/browser/extensions/extension_tts_apitest.cc b/chrome/browser/extensions/extension_tts_apitest.cc index 71f2634..4c5b935 100644 --- a/chrome/browser/extensions/extension_tts_apitest.cc +++ b/chrome/browser/extensions/extension_tts_apitest.cc @@ -17,10 +17,6 @@ #define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING #include "testing/gmock_mutant.h" -#if defined(OS_CHROMEOS) -#include "chrome/browser/chromeos/cros/cros_mock.h" -#endif - using ::testing::AnyNumber; using ::testing::CreateFunctor; using ::testing::DoAll; @@ -305,17 +301,3 @@ IN_PROC_BROWSER_TEST_F(TtsApiTest, EngineWordCallbacks) { ASSERT_TRUE(RunExtensionTest("tts_engine/engine_word_callbacks")) << message_; } - -#if defined(OS_CHROMEOS) -// Fails since v8 roll at r96374: http://crbug.com/92482 -IN_PROC_BROWSER_TEST_F(ExtensionApiTest, FAILS_TtsChromeOs) { - CommandLine::ForCurrentProcess()->AppendSwitch( - switches::kEnableExperimentalExtensionApis); - - chromeos::CrosMock crosMock; - crosMock.InitMockSpeechSynthesisLibrary(); - crosMock.SetSpeechSynthesisLibraryExpectations(); - - ASSERT_TRUE(RunExtensionTest("tts/chromeos")) << message_; -} -#endif diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 602a56c..17a9b93 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -397,8 +397,6 @@ 'browser/chromeos/cros/power_library.h', 'browser/chromeos/cros/screen_lock_library.cc', 'browser/chromeos/cros/screen_lock_library.h', - 'browser/chromeos/cros/speech_synthesis_library.cc', - 'browser/chromeos/cros/speech_synthesis_library.h', 'browser/chromeos/cros/update_library.cc', 'browser/chromeos/cros/update_library.h', 'browser/chromeos/cros_settings.cc', @@ -422,6 +420,8 @@ 'browser/chromeos/dbus/proxy_resolution_service_provider.h', 'browser/chromeos/dbus/sensors_source.cc', 'browser/chromeos/dbus/sensors_source.h', + 'browser/chromeos/dbus/speech_synthesizer_client.cc', + 'browser/chromeos/dbus/speech_synthesizer_client.h', 'browser/chromeos/drop_shadow_label.cc', 'browser/chromeos/enterprise_extension_observer.cc', 'browser/chromeos/enterprise_extension_observer.h', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index df56ec8..9e4b1f5 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -2288,8 +2288,6 @@ 'browser/chromeos/cros/mock_power_library.h', 'browser/chromeos/cros/mock_screen_lock_library.cc', 'browser/chromeos/cros/mock_screen_lock_library.h', - 'browser/chromeos/cros/mock_speech_synthesis_library.cc', - 'browser/chromeos/cros/mock_speech_synthesis_library.h', 'browser/chromeos/cros/mock_update_library.cc', 'browser/chromeos/cros/mock_update_library.h', 'browser/chromeos/extensions/file_browser_notifications_browsertest.cc', |