1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/input_tag_speech_dispatcher.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/common/speech_recognition_messages.h"
9 #include "content/renderer/render_view_impl.h"
10 #include "third_party/WebKit/public/platform/WebSize.h"
11 #include "third_party/WebKit/public/platform/WebString.h"
12 #include "third_party/WebKit/public/web/WebDocument.h"
13 #include "third_party/WebKit/public/web/WebElement.h"
14 #include "third_party/WebKit/public/web/WebFrame.h"
15 #include "third_party/WebKit/public/web/WebInputElement.h"
16 #include "third_party/WebKit/public/web/WebNode.h"
17 #include "third_party/WebKit/public/web/WebSecurityOrigin.h"
18 #include "third_party/WebKit/public/web/WebSpeechInputListener.h"
19 #include "third_party/WebKit/public/web/WebView.h"
21 using WebKit::WebDocument;
22 using WebKit::WebElement;
23 using WebKit::WebFrame;
24 using WebKit::WebInputElement;
25 using WebKit::WebNode;
26 using WebKit::WebView;
30 InputTagSpeechDispatcher::InputTagSpeechDispatcher(
31 RenderViewImpl* render_view,
32 WebKit::WebSpeechInputListener* listener)
33 : RenderViewObserver(render_view),
37 bool InputTagSpeechDispatcher::OnMessageReceived(
38 const IPC::Message& message) {
40 IPC_BEGIN_MESSAGE_MAP(InputTagSpeechDispatcher, message)
41 IPC_MESSAGE_HANDLER(InputTagSpeechMsg_SetRecognitionResults,
42 OnSpeechRecognitionResults)
43 IPC_MESSAGE_HANDLER(InputTagSpeechMsg_RecordingComplete,
44 OnSpeechRecordingComplete)
45 IPC_MESSAGE_HANDLER(InputTagSpeechMsg_RecognitionComplete,
46 OnSpeechRecognitionComplete)
47 IPC_MESSAGE_HANDLER(InputTagSpeechMsg_ToggleSpeechInput,
48 OnSpeechRecognitionToggleSpeechInput)
49 IPC_MESSAGE_UNHANDLED(handled = false)
54 bool InputTagSpeechDispatcher::startRecognition(
56 const WebKit::WebRect& element_rect,
57 const WebKit::WebString& language,
58 const WebKit::WebString& grammar,
59 const WebKit::WebSecurityOrigin& origin) {
60 DVLOG(1) << "InputTagSpeechDispatcher::startRecognition enter";
62 InputTagSpeechHostMsg_StartRecognition_Params params;
63 params.grammar = UTF16ToUTF8(grammar);
64 params.language = UTF16ToUTF8(language);
65 params.origin_url = UTF16ToUTF8(origin.toString());
66 params.render_view_id = routing_id();
67 params.request_id = request_id;
68 params.element_rect = element_rect;
70 Send(new InputTagSpeechHostMsg_StartRecognition(params));
71 DVLOG(1) << "InputTagSpeechDispatcher::startRecognition exit";
75 void InputTagSpeechDispatcher::cancelRecognition(int request_id) {
76 DVLOG(1) << "InputTagSpeechDispatcher::cancelRecognition enter";
77 Send(new InputTagSpeechHostMsg_CancelRecognition(routing_id(), request_id));
78 DVLOG(1) << "InputTagSpeechDispatcher::cancelRecognition exit";
81 void InputTagSpeechDispatcher::stopRecording(int request_id) {
82 DVLOG(1) << "InputTagSpeechDispatcher::stopRecording enter";
83 Send(new InputTagSpeechHostMsg_StopRecording(routing_id(),
85 DVLOG(1) << "InputTagSpeechDispatcher::stopRecording exit";
88 void InputTagSpeechDispatcher::OnSpeechRecognitionResults(
90 const SpeechRecognitionResults& results) {
91 DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionResults enter";
92 DCHECK_EQ(results.size(), 1U);
94 const SpeechRecognitionResult& result = results[0];
95 WebKit::WebSpeechInputResultArray webkit_result(result.hypotheses.size());
96 for (size_t i = 0; i < result.hypotheses.size(); ++i) {
97 webkit_result[i].assign(result.hypotheses[i].utterance,
98 result.hypotheses[i].confidence);
100 listener_->setRecognitionResult(request_id, webkit_result);
102 DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionResults exit";
105 void InputTagSpeechDispatcher::OnSpeechRecordingComplete(int request_id) {
106 DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecordingComplete enter";
107 listener_->didCompleteRecording(request_id);
108 DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecordingComplete exit";
111 void InputTagSpeechDispatcher::OnSpeechRecognitionComplete(int request_id) {
112 DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionComplete enter";
113 listener_->didCompleteRecognition(request_id);
114 DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionComplete exit";
117 void InputTagSpeechDispatcher::OnSpeechRecognitionToggleSpeechInput() {
118 DVLOG(1) <<"InputTagSpeechDispatcher::OnSpeechRecognitionToggleSpeechInput";
120 WebView* web_view = render_view()->GetWebView();
122 WebFrame* frame = web_view->mainFrame();
126 WebDocument document = frame->document();
127 if (document.isNull())
130 WebNode focused_node = document.focusedNode();
131 if (focused_node.isNull() || !focused_node.isElementNode())
134 WebKit::WebElement element = focused_node.to<WebKit::WebElement>();
135 WebKit::WebInputElement* input_element = WebKit::toWebInputElement(&element);
138 if (!input_element->isSpeechInputEnabled())
141 if (input_element->getSpeechInputState() == WebInputElement::Idle) {
142 input_element->startSpeechInput();
144 input_element->stopSpeechInput();
148 } // namespace content