Skip to content

Commit

Permalink
Merge pull request electron#685 from atom/speech
Browse files Browse the repository at this point in the history
Add support for speech synthesizer and recognizer
  • Loading branch information
zcbenz committed Oct 8, 2014
2 parents 8519ea3 + b560176 commit ddf4f14
Show file tree
Hide file tree
Showing 30 changed files with 3,062 additions and 7 deletions.
21 changes: 20 additions & 1 deletion atom.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
'atom/browser/atom_javascript_dialog_manager.h',
'atom/browser/atom_resource_dispatcher_host_delegate.cc',
'atom/browser/atom_resource_dispatcher_host_delegate.h',
'atom/browser/atom_speech_recognition_manager_delegate.cc',
'atom/browser/atom_speech_recognition_manager_delegate.h',
'atom/browser/browser.cc',
'atom/browser/browser.h',
'atom/browser/browser_linux.cc',
Expand Down Expand Up @@ -214,6 +216,7 @@
'atom/common/crash_reporter/win/crash_service_main.h',
'atom/common/draggable_region.cc',
'atom/common/draggable_region.h',
'atom/common/google_api_key.h',
'atom/common/linux/application_info.cc',
'atom/common/native_mate_converters/accelerator_converter.cc',
'atom/common/native_mate_converters/accelerator_converter.h',
Expand Down Expand Up @@ -279,6 +282,16 @@
'chromium_src/chrome/browser/printing/printing_message_filter.h',
'chromium_src/chrome/browser/printing/printing_ui_web_contents_observer.cc',
'chromium_src/chrome/browser/printing/printing_ui_web_contents_observer.h',
'chromium_src/chrome/browser/speech/tts_controller.h',
'chromium_src/chrome/browser/speech/tts_controller_impl.cc',
'chromium_src/chrome/browser/speech/tts_controller_impl.h',
'chromium_src/chrome/browser/speech/tts_linux.cc',
'chromium_src/chrome/browser/speech/tts_mac.mm',
'chromium_src/chrome/browser/speech/tts_message_filter.cc',
'chromium_src/chrome/browser/speech/tts_message_filter.h',
'chromium_src/chrome/browser/speech/tts_platform.cc',
'chromium_src/chrome/browser/speech/tts_platform.h',
'chromium_src/chrome/browser/speech/tts_win.cc',
'chromium_src/chrome/browser/ui/libgtk2ui/app_indicator_icon_menu.cc',
'chromium_src/chrome/browser/ui/libgtk2ui/app_indicator_icon_menu.h',
'chromium_src/chrome/browser/ui/libgtk2ui/gtk2_status_icon.cc',
Expand All @@ -289,11 +302,17 @@
'chromium_src/chrome/browser/ui/views/status_icons/status_tray_state_changer_win.h',
'chromium_src/chrome/common/print_messages.cc',
'chromium_src/chrome/common/print_messages.h',
'chromium_src/chrome/common/tts_messages.h',
'chromium_src/chrome/common/tts_utterance_request.cc',
'chromium_src/chrome/common/tts_utterance_request.h',
'chromium_src/chrome/renderer/printing/print_web_view_helper.cc',
'chromium_src/chrome/renderer/printing/print_web_view_helper_linux.cc',
'chromium_src/chrome/renderer/printing/print_web_view_helper_mac.mm',
'chromium_src/chrome/renderer/printing/print_web_view_helper_win.cc',
'chromium_src/chrome/renderer/printing/print_web_view_helper.h',
'chromium_src/chrome/renderer/tts_dispatcher.cc',
'chromium_src/chrome/renderer/tts_dispatcher.h',
'chromium_src/library_loaders/libspeechd_loader.cc',
'<@(native_mate_files)',
],
'framework_sources': [
Expand Down Expand Up @@ -549,7 +568,7 @@
'vendor/breakpad/src',
],
'cflags': [
'-Wno-deprecated-declarations',
'-Wno-deprecated-register',
'-Wno-empty-body',
],
'dependencies': [
Expand Down
12 changes: 12 additions & 0 deletions atom/app/atom_main_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

#include "atom/app/atom_content_client.h"
#include "atom/browser/atom_browser_client.h"
#include "atom/common/google_api_key.h"
#include "atom/renderer/atom_renderer_client.h"
#include "base/command_line.h"
#include "base/debug/stack_trace.h"
#include "base/environment.h"
#include "base/logging.h"
#include "content/public/common/content_switches.h"
#include "ui/base/resource/resource_bundle.h"
Expand Down Expand Up @@ -52,6 +54,11 @@ bool AtomMainDelegate::BasicStartupComplete(int* exit_code) {
void AtomMainDelegate::PreSandboxStartup() {
brightray::MainDelegate::PreSandboxStartup();

// Set google API key.
scoped_ptr<base::Environment> env(base::Environment::Create());
if (!env->HasVar("GOOGLE_API_KEY"))
env->SetVar("GOOGLE_API_KEY", GOOGLEAPIS_API_KEY);

CommandLine* command_line = CommandLine::ForCurrentProcess();
std::string process_type = command_line->GetSwitchValueASCII(
switches::kProcessType);
Expand All @@ -73,6 +80,11 @@ void AtomMainDelegate::PreSandboxStartup() {
// Disable renderer sandbox for most of node's functions.
command_line->AppendSwitch(switches::kNoSandbox);

#if defined(OS_MACOSX)
// Enable AVFoundation.
command_line->AppendSwitch("enable-avfoundation");
#endif

// Add a flag to mark the end of switches added by atom-shell.
command_line->AppendSwitch("atom-shell-switches-end");
}
Expand Down
5 changes: 1 addition & 4 deletions atom/browser/atom_access_token_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
#include <utility>

#include "atom/browser/atom_browser_context.h"

#ifndef GOOGLEAPIS_API_KEY
#define GOOGLEAPIS_API_KEY "AIzaSyAQfxPJiounkhOjODEO5ZieffeBv6yft2Q"
#endif
#include "atom/common/google_api_key.h"

namespace atom {

Expand Down
9 changes: 9 additions & 0 deletions atom/browser/atom_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_resource_dispatcher_host_delegate.h"
#include "atom/browser/atom_speech_recognition_manager_delegate.h"
#include "atom/browser/native_window.h"
#include "atom/browser/window_list.h"
#include "chrome/browser/printing/printing_message_filter.h"
#include "chrome/browser/speech/tts_message_filter.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/resource_dispatcher_host.h"
Expand Down Expand Up @@ -51,7 +53,9 @@ AtomBrowserClient::~AtomBrowserClient() {

void AtomBrowserClient::RenderProcessWillLaunch(
content::RenderProcessHost* host) {
int id = host->GetID();
host->AddFilter(new PrintingMessageFilter(host->GetID()));
host->AddFilter(new TtsMessageFilter(id, host->GetBrowserContext()));
}

void AtomBrowserClient::ResourceDispatcherHostCreated() {
Expand All @@ -60,6 +64,11 @@ void AtomBrowserClient::ResourceDispatcherHostCreated() {
resource_dispatcher_delegate_.get());
}

content::SpeechRecognitionManagerDelegate*
AtomBrowserClient::GetSpeechRecognitionManagerDelegate() {
return new AtomSpeechRecognitionManagerDelegate;
}

content::AccessTokenStore* AtomBrowserClient::CreateAccessTokenStore() {
return new AtomAccessTokenStore;
}
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/atom_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class AtomBrowserClient : public brightray::BrowserClient {
virtual void RenderProcessWillLaunch(
content::RenderProcessHost* host) OVERRIDE;
virtual void ResourceDispatcherHostCreated() OVERRIDE;
virtual content::SpeechRecognitionManagerDelegate*
GetSpeechRecognitionManagerDelegate() override;
virtual content::AccessTokenStore* CreateAccessTokenStore() OVERRIDE;
virtual void OverrideWebkitPrefs(content::RenderViewHost* render_view_host,
const GURL& url,
Expand Down
74 changes: 74 additions & 0 deletions atom/browser/atom_speech_recognition_manager_delegate.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/browser/atom_speech_recognition_manager_delegate.h"

#include <string>

#include "base/callback.h"

namespace atom {

AtomSpeechRecognitionManagerDelegate::AtomSpeechRecognitionManagerDelegate() {
}

AtomSpeechRecognitionManagerDelegate::~AtomSpeechRecognitionManagerDelegate() {
}

void AtomSpeechRecognitionManagerDelegate::OnRecognitionStart(int session_id) {
}

void AtomSpeechRecognitionManagerDelegate::OnAudioStart(int session_id) {
}

void AtomSpeechRecognitionManagerDelegate::OnEnvironmentEstimationComplete(
int session_id) {
}

void AtomSpeechRecognitionManagerDelegate::OnSoundStart(int session_id) {
}

void AtomSpeechRecognitionManagerDelegate::OnSoundEnd(int session_id) {
}

void AtomSpeechRecognitionManagerDelegate::OnAudioEnd(int session_id) {
}

void AtomSpeechRecognitionManagerDelegate::OnRecognitionEnd(int session_id) {
}

void AtomSpeechRecognitionManagerDelegate::OnRecognitionResults(
int session_id, const content::SpeechRecognitionResults& result) {
}

void AtomSpeechRecognitionManagerDelegate::OnRecognitionError(
int session_id, const content::SpeechRecognitionError& error) {
}

void AtomSpeechRecognitionManagerDelegate::OnAudioLevelsChange(
int session_id, float volume, float noise_volume) {
}

void AtomSpeechRecognitionManagerDelegate::GetDiagnosticInformation(
bool* can_report_metrics, std::string* hardware_info) {
*can_report_metrics = false;
}

void AtomSpeechRecognitionManagerDelegate::CheckRecognitionIsAllowed(
int session_id,
base::Callback<void(bool ask_user, bool is_allowed)> callback) {
callback.Run(true, true);
}

content::SpeechRecognitionEventListener*
AtomSpeechRecognitionManagerDelegate::GetEventListener() {
return this;
}

bool AtomSpeechRecognitionManagerDelegate::FilterProfanities(
int render_process_id) {
return false;
}

} // namespace atom
52 changes: 52 additions & 0 deletions atom/browser/atom_speech_recognition_manager_delegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#ifndef ATOM_BROWSER_ATOM_SPEECH_RECOGNITION_MANAGER_DELEGATE_H_
#define ATOM_BROWSER_ATOM_SPEECH_RECOGNITION_MANAGER_DELEGATE_H_

#include <string>

#include "content/public/browser/speech_recognition_event_listener.h"
#include "content/public/browser/speech_recognition_manager_delegate.h"

namespace atom {

class AtomSpeechRecognitionManagerDelegate
: public content::SpeechRecognitionManagerDelegate,
public content::SpeechRecognitionEventListener {
public:
AtomSpeechRecognitionManagerDelegate();
virtual ~AtomSpeechRecognitionManagerDelegate();

// content::SpeechRecognitionEventListener:
virtual void OnRecognitionStart(int session_id) override;
virtual void OnAudioStart(int session_id) override;
virtual void OnEnvironmentEstimationComplete(int session_id) override;
virtual void OnSoundStart(int session_id) override;
virtual void OnSoundEnd(int session_id) override;
virtual void OnAudioEnd(int session_id) override;
virtual void OnRecognitionEnd(int session_id) override;
virtual void OnRecognitionResults(
int session_id, const content::SpeechRecognitionResults& result) override;
virtual void OnRecognitionError(
int session_id, const content::SpeechRecognitionError& error) override;
virtual void OnAudioLevelsChange(int session_id, float volume,
float noise_volume) override;

// content::SpeechRecognitionManagerDelegate:
virtual void GetDiagnosticInformation(bool* can_report_metrics,
std::string* hardware_info) override;
virtual void CheckRecognitionIsAllowed(
int session_id,
base::Callback<void(bool ask_user, bool is_allowed)> callback) override;
virtual content::SpeechRecognitionEventListener* GetEventListener() override;
virtual bool FilterProfanities(int render_process_id) override;

private:
DISALLOW_COPY_AND_ASSIGN(AtomSpeechRecognitionManagerDelegate);
};

} // namespace atom

#endif // ATOM_BROWSER_ATOM_SPEECH_RECOGNITION_MANAGER_DELEGATE_H_
1 change: 1 addition & 0 deletions atom/common/common_message_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

#include "atom/common/api/api_messages.h"
#include "chrome/common/print_messages.h"
#include "chrome/common/tts_messages.h"
12 changes: 12 additions & 0 deletions atom/common/google_api_key.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#ifndef ATOM_COMMON_GOOGLE_API_KEY_H_
#define ATOM_COMMON_GOOGLE_API_KEY_H_

#ifndef GOOGLEAPIS_API_KEY
#define GOOGLEAPIS_API_KEY "AIzaSyAQfxPJiounkhOjODEO5ZieffeBv6yft2Q"
#endif

#endif // ATOM_COMMON_GOOGLE_API_KEY_H_
6 changes: 6 additions & 0 deletions atom/renderer/atom_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "atom/renderer/api/atom_renderer_bindings.h"
#include "atom/renderer/atom_render_view_observer.h"
#include "chrome/renderer/printing/print_web_view_helper.h"
#include "chrome/renderer/tts_dispatcher.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/renderer/render_thread.h"
Expand Down Expand Up @@ -128,6 +129,11 @@ void AtomRendererClient::RenderViewCreated(content::RenderView* render_view) {
new AtomRenderViewObserver(render_view, this);
}

blink::WebSpeechSynthesizer* AtomRendererClient::OverrideSpeechSynthesizer(
blink::WebSpeechSynthesizerClient* client) {
return new TtsDispatcher(client);
}

void AtomRendererClient::DidCreateScriptContext(blink::WebFrame* frame,
v8::Handle<v8::Context> context,
int extension_group,
Expand Down
2 changes: 2 additions & 0 deletions atom/renderer/atom_renderer_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class AtomRendererClient : public content::ContentRendererClient,
virtual void RenderThreadStarted() OVERRIDE;
virtual void RenderFrameCreated(content::RenderFrame* render_frame) OVERRIDE;
virtual void RenderViewCreated(content::RenderView*) OVERRIDE;
virtual blink::WebSpeechSynthesizer* OverrideSpeechSynthesizer(
blink::WebSpeechSynthesizerClient* client);
virtual void DidCreateScriptContext(blink::WebFrame* frame,
v8::Handle<v8::Context> context,
int extension_group,
Expand Down
Loading

0 comments on commit ddf4f14

Please sign in to comment.