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

Unified Diff: chrome/browser/speech/chrome_speech_recognition_manager_delegate.cc

Issue 9972011: Speech refactoring: Reimplemented SpeechRecognitionManagerImpl as a FSM. (CL1.7) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed according to Satish review. Created 8 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/speech/chrome_speech_recognition_manager_delegate.cc
diff --git a/chrome/browser/speech/chrome_speech_recognition_manager_delegate.cc b/chrome/browser/speech/chrome_speech_recognition_manager_delegate.cc
index db4a23833dce03d8d49b50aa58c9b4a97d8d0ccb..a9de274b275c1c7ef39af60edcd9ed0b96e70c7f 100644
--- a/chrome/browser/speech/chrome_speech_recognition_manager_delegate.cc
+++ b/chrome/browser/speech/chrome_speech_recognition_manager_delegate.cc
@@ -10,16 +10,20 @@
#include "base/synchronization/lock.h"
#include "base/threading/thread_restrictions.h"
#include "base/utf_string_conversions.h"
+#include "base/synchronization/waitable_event.h"
Satish 2012/04/23 10:25:17 move up by 2 lines
Primiano Tucci (use gerrit) 2012/04/23 12:52:25 Done.
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/common/pref_names.h"
+#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/render_view_host_delegate.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/speech_recognition_manager.h"
#include "content/public/common/speech_recognition_error.h"
#include "content/public/common/speech_recognition_result.h"
+#include "content/public/browser/speech_recognition_session_context.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
@@ -27,8 +31,11 @@
#include "chrome/installer/util/wmi.h"
#endif
+using base::WaitableEvent;
using content::BrowserThread;
+using content::RenderViewHostImpl;
using content::SpeechRecognitionManager;
+using content::SpeechRecognitionSessionContext;
namespace speech {
@@ -105,17 +112,18 @@ ChromeSpeechRecognitionManagerDelegate::
}
void ChromeSpeechRecognitionManagerDelegate::ShowRecognitionRequested(
- int session_id,
- int render_process_id,
- int render_view_id,
- const gfx::Rect& element_rect) {
- bubble_controller_->CreateBubble(session_id, render_process_id,
- render_view_id, element_rect);
+ int session_id) {
+ const SpeechRecognitionSessionContext& context =
+ SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
+ bubble_controller_->CreateBubble(session_id,
+ context.render_process_id,
+ context.render_view_id,
+ context.element_rect);
}
-void ChromeSpeechRecognitionManagerDelegate::GetRequestInfo(
+void ChromeSpeechRecognitionManagerDelegate::GetDiagnosticInformation(
bool* can_report_metrics,
- std::string* request_info) {
+ std::string* hardware_info) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (!optional_request_info_.get()) {
optional_request_info_ = new OptionalRequestInfo();
@@ -129,7 +137,26 @@ void ChromeSpeechRecognitionManagerDelegate::GetRequestInfo(
optional_request_info_->Refresh();
}
*can_report_metrics = optional_request_info_->can_report_metrics();
- *request_info = optional_request_info_->value();
+ *hardware_info = optional_request_info_->value();
+}
+
+bool ChromeSpeechRecognitionManagerDelegate::IsRecognitionAllowed(
+ int session_id) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ const SpeechRecognitionSessionContext& context =
+ SpeechRecognitionManager::GetInstance()->GetSessionContext(session_id);
+ bool check_result = false;
+ WaitableEvent event(false /*manual_reset*/, false /*initially_signaled*/);
+
+ BrowserThread::PostTask(
+ BrowserThread::UI, FROM_HERE,
Satish 2012/04/23 10:25:17 indent by 4 spaces
Primiano Tucci (use gerrit) 2012/04/23 12:52:25 Done.
+ base::Bind(&CheckRenderViewType,
+ context.render_process_id,
+ context.render_view_id,
+ base::Unretained(&check_result),
+ base::Unretained(&event)));
+ event.Wait();
+ return check_result;
}
void ChromeSpeechRecognitionManagerDelegate::ShowWarmUp(int session_id) {
@@ -149,51 +176,38 @@ void ChromeSpeechRecognitionManagerDelegate::ShowInputVolume(
bubble_controller_->SetBubbleInputVolume(session_id, volume, noise_volume);
}
-void ChromeSpeechRecognitionManagerDelegate::ShowMicError(int session_id,
- MicError error) {
- switch (error) {
- case MIC_ERROR_NO_DEVICE_AVAILABLE:
- bubble_controller_->SetBubbleMessage(
- session_id, l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_NO_MIC));
+void ChromeSpeechRecognitionManagerDelegate::ShowError(
+ int session_id, const content::SpeechRecognitionError& error) {
+ int error_message_id = 0;
+ switch (error.code) {
+ case content::SPEECH_RECOGNITION_ERROR_AUDIO:
+ switch (error.details) {
+ case content::SPEECH_AUDIO_ERROR_DETAILS_NO_MIC:
+ error_message_id = IDS_SPEECH_INPUT_NO_MIC;
+ break;
+ case content::SPEECH_AUDIO_ERROR_DETAILS_IN_USE:
+ error_message_id = IDS_SPEECH_INPUT_MIC_IN_USE;
+ break;
+ default:
+ error_message_id = IDS_SPEECH_INPUT_MIC_ERROR;
+ break;
+ }
break;
-
- case MIC_ERROR_DEVICE_IN_USE:
- bubble_controller_->SetBubbleMessage(
- session_id, l10n_util::GetStringUTF16(IDS_SPEECH_INPUT_MIC_IN_USE));
+ case content::SPEECH_RECOGNITION_ERROR_NO_SPEECH:
+ error_message_id = IDS_SPEECH_INPUT_NO_SPEECH;
+ break;
+ case content::SPEECH_RECOGNITION_ERROR_NO_MATCH:
+ error_message_id = IDS_SPEECH_INPUT_NO_RESULTS;
+ break;
+ case content::SPEECH_RECOGNITION_ERROR_NETWORK:
+ error_message_id = IDS_SPEECH_INPUT_NET_ERROR;
break;
-
default:
- NOTREACHED();
- }
-}
-
-void ChromeSpeechRecognitionManagerDelegate::ShowRecognizerError(
- int session_id, content::SpeechRecognitionErrorCode error) {
- struct ErrorMessageMapEntry {
- content::SpeechRecognitionErrorCode error;
- int message_id;
- };
- ErrorMessageMapEntry error_message_map[] = {
- {
- content::SPEECH_RECOGNITION_ERROR_AUDIO, IDS_SPEECH_INPUT_MIC_ERROR
- }, {
- content::SPEECH_RECOGNITION_ERROR_NO_SPEECH, IDS_SPEECH_INPUT_NO_SPEECH
- }, {
- content::SPEECH_RECOGNITION_ERROR_NO_MATCH, IDS_SPEECH_INPUT_NO_RESULTS
- }, {
- content::SPEECH_RECOGNITION_ERROR_NETWORK, IDS_SPEECH_INPUT_NET_ERROR
- }
- };
- for (size_t i = 0; i < ARRAYSIZE_UNSAFE(error_message_map); ++i) {
- if (error_message_map[i].error == error) {
- bubble_controller_->SetBubbleMessage(
- session_id,
- l10n_util::GetStringUTF16(error_message_map[i].message_id));
+ NOTREACHED() << "unknown error " << error.code;
return;
- }
}
-
- NOTREACHED() << "unknown error " << error;
+ bubble_controller_->SetBubbleMessage(
+ session_id, l10n_util::GetStringUTF16(error_message_id));
}
void ChromeSpeechRecognitionManagerDelegate::DoClose(int session_id) {
@@ -205,18 +219,41 @@ void ChromeSpeechRecognitionManagerDelegate::InfoBubbleButtonClicked(
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (button == SpeechRecognitionBubble::BUTTON_CANCEL) {
- SpeechRecognitionManager::GetInstance()->CancelRecognitionForRequest(
- session_id);
+ SpeechRecognitionManager::GetInstance()->AbortSession(session_id);
} else if (button == SpeechRecognitionBubble::BUTTON_TRY_AGAIN) {
- SpeechRecognitionManager::GetInstance()->StartRecognitionForRequest(
- session_id);
+ SpeechRecognitionManager::GetInstance()->StartSession(session_id);
}
}
void ChromeSpeechRecognitionManagerDelegate::InfoBubbleFocusChanged(
int session_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
- SpeechRecognitionManager::GetInstance()->FocusLostForRequest(session_id);
+ SpeechRecognitionManager::GetInstance()->DetachSession(session_id);
+}
+
+void ChromeSpeechRecognitionManagerDelegate::CheckRenderViewType(
+ int render_process_id,
+ int render_view_id,
+ bool* result,
+ WaitableEvent* event) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ RenderViewHostImpl* render_view_host =
+ RenderViewHostImpl::FromID(render_process_id, render_view_id);
+ // For host delegates other than VIEW_TYPE_WEB_CONTENTS we can't reliably show
+ // a popup, including the speech input bubble. In these cases for privacy
+ // reasons we don't want to start recording if the user can't be properly
+ // notified. An example of this is trying to show the speech input bubble
+ // within an extension popup: http://crbug.com/92083. In these situations the
+ // speech input extension API should be used instead.
+ if (!render_view_host || !render_view_host->GetDelegate()) {
Satish 2012/04/23 10:25:17 could merge this and the first 'else if' clause
Primiano Tucci (use gerrit) 2012/04/23 12:52:25 Done.
+ *result = false;
+ } else if (render_view_host->GetDelegate()->GetRenderViewType() !=
+ content::VIEW_TYPE_WEB_CONTENTS) {
+ *result = false;
+ } else {
+ *result = true;
+ }
+ event->Signal();
}
} // namespace speech

Powered by Google App Engine
This is Rietveld 408576698