diff options
author | dtseng@chromium.org <dtseng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-26 22:54:02 +0000 |
---|---|---|
committer | dtseng@chromium.org <dtseng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-26 22:54:02 +0000 |
commit | 30f0ead27c120f784cb0b81d7d6852e0bdcac421 (patch) | |
tree | ee35d6344284a084eeb7c788c6aee37d5bdb8736 | |
parent | 0406838d8bcaf6100c84761e24de94952d87beb5 (diff) | |
download | chromium_src-30f0ead27c120f784cb0b81d7d6852e0bdcac421.zip chromium_src-30f0ead27c120f784cb0b81d7d6852e0bdcac421.tar.gz chromium_src-30f0ead27c120f784cb0b81d7d6852e0bdcac421.tar.bz2 |
Initial TTS implementation on Mac and Windows.BUG=none.TEST=base extension test. Currently no mocking for the native TTS api's. Manually tested engines with ChromeVox (navigating around a page, expected interrupts, stopping speech, etc.).
Review URL: http://codereview.chromium.org/3149027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57595 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/extensions/extension_function_dispatcher.cc | 4 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_tts_api_gtk.cc | 17 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_tts_api_mac.mm | 39 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_tts_api_win.cc | 86 | ||||
-rw-r--r-- | chrome/browser/extensions/extension_tts_apitest.cc | 11 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 5 |
6 files changed, 149 insertions, 13 deletions
diff --git a/chrome/browser/extensions/extension_function_dispatcher.cc b/chrome/browser/extensions/extension_function_dispatcher.cc index 51cc8fe..ce0cc58 100644 --- a/chrome/browser/extensions/extension_function_dispatcher.cc +++ b/chrome/browser/extensions/extension_function_dispatcher.cc @@ -46,9 +46,7 @@ #include "chrome/browser/extensions/extension_sidebar_api.h" #include "chrome/browser/extensions/extension_tabs_module.h" #include "chrome/browser/extensions/extension_test_api.h" -#if defined(OS_CHROMEOS) #include "chrome/browser/extensions/extension_tts_api.h" -#endif #include "chrome/browser/extensions/extensions_quota_service.h" #include "chrome/browser/extensions/extensions_service.h" #include "chrome/browser/profile.h" @@ -234,12 +232,10 @@ void FactoryRegistry::ResetFunctions() { RegisterFunction<GetFocusedControlFunction>(); RegisterFunction<SetAccessibilityEnabledFunction>(); -#if defined(OS_CHROMEOS) // Text-to-speech. RegisterFunction<ExtensionTtsSpeakFunction>(); RegisterFunction<ExtensionTtsStopSpeakingFunction>(); RegisterFunction<ExtensionTtsIsSpeakingFunction>(); -#endif // Clipboard. RegisterFunction<ExecuteCopyClipboardFunction>(); diff --git a/chrome/browser/extensions/extension_tts_api_gtk.cc b/chrome/browser/extensions/extension_tts_api_gtk.cc new file mode 100644 index 0000000..8c7f48a --- /dev/null +++ b/chrome/browser/extensions/extension_tts_api_gtk.cc @@ -0,0 +1,17 @@ +// 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 "extension_tts_api.h" + +bool ExtensionTtsSpeakFunction::RunImpl() { + return false; +} + +bool ExtensionTtsStopSpeakingFunction::RunImpl() { + return false; +} + +bool ExtensionTtsIsSpeakingFunction::RunImpl() { + return false; +} diff --git a/chrome/browser/extensions/extension_tts_api_mac.mm b/chrome/browser/extensions/extension_tts_api_mac.mm new file mode 100644 index 0000000..4351068 --- /dev/null +++ b/chrome/browser/extensions/extension_tts_api_mac.mm @@ -0,0 +1,39 @@ +// 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 "extension_tts_api.h" + +#include <string> + +#include "base/values.h" +#include "chrome/browser/extensions/extension_function.h" + +#import <cocoa/cocoa.h> + +static NSSpeechSynthesizer* speech_synthesizer_; + +void InitializeSpeechSynthesizer() { + if (!speech_synthesizer_) + speech_synthesizer_ = [[NSSpeechSynthesizer alloc] init]; +} + +bool ExtensionTtsSpeakFunction::RunImpl() { + InitializeSpeechSynthesizer(); + std::string utterance; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &utterance)); + return + [speech_synthesizer_ startSpeakingString: + [NSString stringWithUTF8String: utterance.c_str()]]; +} + +bool ExtensionTtsStopSpeakingFunction::RunImpl() { + InitializeSpeechSynthesizer(); + [speech_synthesizer_ stopSpeaking]; + return true; +} + +bool ExtensionTtsIsSpeakingFunction::RunImpl() { + InitializeSpeechSynthesizer(); + return [speech_synthesizer_ isSpeaking]; +} diff --git a/chrome/browser/extensions/extension_tts_api_win.cc b/chrome/browser/extensions/extension_tts_api_win.cc new file mode 100644 index 0000000..cb4c927 --- /dev/null +++ b/chrome/browser/extensions/extension_tts_api_win.cc @@ -0,0 +1,86 @@ +// 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 "extension_tts_api.h" + +#include <atlbase.h> +#include <atlcom.h> +#include <sapi.h> + +#include "base/scoped_comptr_win.h" +#include "base/singleton.h" +#include "base/values.h" + +class SpeechSynthesizerWrapper { + public: + SpeechSynthesizerWrapper() : speech_synthesizer_(NULL), + paused_(false), + permanent_failure_(false) { + InitializeSpeechSynthesizer(); + } + + bool InitializeSpeechSynthesizer() { + if (!SUCCEEDED(CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_SERVER, + IID_ISpVoice, reinterpret_cast<void**>(&speech_synthesizer_)))) { + permanent_failure_ = true; + return false; + } + + if (paused_) + speech_synthesizer_->Resume(); + return true; + } + + ScopedComPtr<ISpVoice> speech_synthesizer() { + return speech_synthesizer_; + } + + bool paused() { + return paused_; + } + + void paused(bool state) { + paused_ = state; + } + + private: + ScopedComPtr<ISpVoice> speech_synthesizer_; + bool paused_; + // Indicates an error retrieving the SAPI COM interface. + bool permanent_failure_; +}; + +typedef Singleton<SpeechSynthesizerWrapper> SpeechSynthesizerSingleton; + +bool ExtensionTtsSpeakFunction::RunImpl() { + ScopedComPtr<ISpVoice> speech_synthesizer = + SpeechSynthesizerSingleton::get()->speech_synthesizer(); + if (speech_synthesizer) { + std::wstring utterance; + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &utterance)); + if (SpeechSynthesizerSingleton::get()->paused()) + speech_synthesizer->Resume(); + speech_synthesizer->Speak( + utterance.c_str(), SPF_ASYNC | SPF_PURGEBEFORESPEAK, NULL); + return true; + } + + return false; +} + +bool ExtensionTtsStopSpeakingFunction::RunImpl() { + // We need to keep track of the paused state since SAPI doesn't have a stop + // method. + ScopedComPtr<ISpVoice> speech_synthesizer = + SpeechSynthesizerSingleton::get()->speech_synthesizer(); + if (speech_synthesizer && !SpeechSynthesizerSingleton::get()->paused()) { + speech_synthesizer->Pause(); + SpeechSynthesizerSingleton::get()->paused(true); + } + return true; +} + +bool ExtensionTtsIsSpeakingFunction::RunImpl() { + return false; +} diff --git a/chrome/browser/extensions/extension_tts_apitest.cc b/chrome/browser/extensions/extension_tts_apitest.cc index 5acda10..b1a035b 100644 --- a/chrome/browser/extensions/extension_tts_apitest.cc +++ b/chrome/browser/extensions/extension_tts_apitest.cc @@ -7,20 +7,15 @@ #include "chrome/common/chrome_switches.h" #include "testing/gmock/include/gmock/gmock.h" -// This extension API is currently only supported on Chrome OS. -#if defined(OS_CHROMEOS) -#define MAYBE_Tts Tts -#else -#define MAYBE_Tts DISABLED_Tts -#endif - -IN_PROC_BROWSER_TEST_F(ExtensionApiTest, MAYBE_Tts) { +IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Tts) { CommandLine::ForCurrentProcess()->AppendSwitch( switches::kEnableExperimentalExtensionApis); + #if defined(OS_CHROMEOS) chromeos::CrosMock crosMock; crosMock.InitMockSpeechSynthesisLibrary(); crosMock.SetSpeechSynthesisLibraryExpectations(); + #endif ASSERT_TRUE(RunExtensionTest("tts")) << message_; } diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index e265b11..4ea29de 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1442,6 +1442,9 @@ 'browser/extensions/extension_test_api.h', 'browser/extensions/extension_tts_api.cc', 'browser/extensions/extension_tts_api.h', + 'browser/extensions/extension_tts_api_gtk.cc', + 'browser/extensions/extension_tts_api_mac.mm', + 'browser/extensions/extension_tts_api_win.cc', 'browser/extensions/extension_toolbar_model.cc', 'browser/extensions/extension_toolbar_model.h', 'browser/extensions/extension_updater.cc', @@ -3090,7 +3093,6 @@ ['exclude', 'browser/dom_ui/mediaplayer_ui.cc'], ['exclude', 'browser/dom_ui/slideshow_ui.cc'], ['exclude', 'browser/extensions/extension_tts_api.cc'], - ['exclude', 'browser/extensions/extension_tts_api.h'], ['exclude', 'browser/renderer_host/offline_resource_handler.cc'], ['exclude', 'browser/renderer_host/offline_resource_handler.h'], ], @@ -3810,6 +3812,7 @@ 'browser/bookmarks/bookmark_menu_controller.h', 'browser/browser_accessibility_win.cc', 'browser/browser_accessibility_manager_win.cc', + 'browser/extensions/extension_tts_api_win.cc', 'browser/google_update.cc', 'browser/history/history_indexer.idl', 'browser/history_tab_ui.cc', |