OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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. | |
4 | |
5 #include "content/renderer/speech_input_dispatcher.h" | |
6 | |
7 #include "base/utf_string_conversions.h" | |
8 #include "content/common/speech_input_messages.h" | |
9 #include "content/renderer/render_view_impl.h" | |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSpeechInputListene
r.h" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
20 | |
21 using WebKit::WebDocument; | |
22 using WebKit::WebElement; | |
23 using WebKit::WebFrame; | |
24 using WebKit::WebInputElement; | |
25 using WebKit::WebNode; | |
26 using WebKit::WebView; | |
27 | |
28 SpeechInputDispatcher::SpeechInputDispatcher( | |
29 RenderViewImpl* render_view, | |
30 WebKit::WebSpeechInputListener* listener) | |
31 : content::RenderViewObserver(render_view), | |
32 listener_(listener) { | |
33 } | |
34 | |
35 bool SpeechInputDispatcher::OnMessageReceived(const IPC::Message& message) { | |
36 bool handled = true; | |
37 IPC_BEGIN_MESSAGE_MAP(SpeechInputDispatcher, message) | |
38 IPC_MESSAGE_HANDLER(SpeechInputMsg_SetRecognitionResult, | |
39 OnSpeechRecognitionResult) | |
40 IPC_MESSAGE_HANDLER(SpeechInputMsg_RecordingComplete, | |
41 OnSpeechRecordingComplete) | |
42 IPC_MESSAGE_HANDLER(SpeechInputMsg_RecognitionComplete, | |
43 OnSpeechRecognitionComplete) | |
44 IPC_MESSAGE_HANDLER(SpeechInputMsg_ToggleSpeechInput, | |
45 OnSpeechRecognitionToggleSpeechInput) | |
46 IPC_MESSAGE_UNHANDLED(handled = false) | |
47 IPC_END_MESSAGE_MAP() | |
48 return handled; | |
49 } | |
50 | |
51 bool SpeechInputDispatcher::startRecognition( | |
52 int request_id, | |
53 const WebKit::WebRect& element_rect, | |
54 const WebKit::WebString& language, | |
55 const WebKit::WebString& grammar, | |
56 const WebKit::WebSecurityOrigin& origin) { | |
57 VLOG(1) << "SpeechInputDispatcher::startRecognition enter"; | |
58 | |
59 SpeechInputHostMsg_StartRecognition_Params params; | |
60 params.grammar = UTF16ToUTF8(grammar); | |
61 params.language = UTF16ToUTF8(language); | |
62 params.origin_url = UTF16ToUTF8(origin.toString()); | |
63 params.render_view_id = routing_id(); | |
64 params.request_id = request_id; | |
65 gfx::Size scroll = render_view()->GetWebView()->mainFrame()->scrollOffset(); | |
66 params.element_rect = element_rect; | |
67 params.element_rect.Offset(-scroll.width(), -scroll.height()); | |
68 | |
69 Send(new SpeechInputHostMsg_StartRecognition(params)); | |
70 VLOG(1) << "SpeechInputDispatcher::startRecognition exit"; | |
71 return true; | |
72 } | |
73 | |
74 void SpeechInputDispatcher::cancelRecognition(int request_id) { | |
75 VLOG(1) << "SpeechInputDispatcher::cancelRecognition enter"; | |
76 Send(new SpeechInputHostMsg_CancelRecognition(routing_id(), request_id)); | |
77 VLOG(1) << "SpeechInputDispatcher::cancelRecognition exit"; | |
78 } | |
79 | |
80 void SpeechInputDispatcher::stopRecording(int request_id) { | |
81 VLOG(1) << "SpeechInputDispatcher::stopRecording enter"; | |
82 Send(new SpeechInputHostMsg_StopRecording(routing_id(), request_id)); | |
83 VLOG(1) << "SpeechInputDispatcher::stopRecording exit"; | |
84 } | |
85 | |
86 void SpeechInputDispatcher::OnSpeechRecognitionResult( | |
87 int request_id, | |
88 const content::SpeechInputResult& result) { | |
89 VLOG(1) << "SpeechInputDispatcher::OnSpeechRecognitionResult enter"; | |
90 WebKit::WebSpeechInputResultArray webkit_result(result.hypotheses.size()); | |
91 for (size_t i = 0; i < result.hypotheses.size(); ++i) { | |
92 webkit_result[i].assign(result.hypotheses[i].utterance, | |
93 result.hypotheses[i].confidence); | |
94 } | |
95 listener_->setRecognitionResult(request_id, webkit_result); | |
96 VLOG(1) << "SpeechInputDispatcher::OnSpeechRecognitionResult exit"; | |
97 } | |
98 | |
99 void SpeechInputDispatcher::OnSpeechRecordingComplete(int request_id) { | |
100 VLOG(1) << "SpeechInputDispatcher::OnSpeechRecordingComplete enter"; | |
101 listener_->didCompleteRecording(request_id); | |
102 VLOG(1) << "SpeechInputDispatcher::OnSpeechRecordingComplete exit"; | |
103 } | |
104 | |
105 void SpeechInputDispatcher::OnSpeechRecognitionComplete(int request_id) { | |
106 VLOG(1) << "SpeechInputDispatcher::OnSpeechRecognitionComplete enter"; | |
107 listener_->didCompleteRecognition(request_id); | |
108 VLOG(1) << "SpeechInputDispatcher::OnSpeechRecognitionComplete exit"; | |
109 } | |
110 | |
111 void SpeechInputDispatcher::OnSpeechRecognitionToggleSpeechInput() { | |
112 VLOG(1) << "SpeechInputDispatcher::OnSpeechRecognitionToggleSpeechInput"; | |
113 | |
114 WebView* web_view = render_view()->GetWebView(); | |
115 | |
116 WebFrame* frame = web_view->mainFrame(); | |
117 if (!frame) | |
118 return; | |
119 | |
120 WebDocument document = frame->document(); | |
121 if (document.isNull()) | |
122 return; | |
123 | |
124 WebNode focused_node = document.focusedNode(); | |
125 if (focused_node.isNull() || !focused_node.isElementNode()) | |
126 return; | |
127 | |
128 WebKit::WebElement element = focused_node.to<WebKit::WebElement>(); | |
129 WebKit::WebInputElement* input_element = WebKit::toWebInputElement(&element); | |
130 if (!input_element) | |
131 return; | |
132 if (!input_element->isSpeechInputEnabled()) | |
133 return; | |
134 | |
135 if (input_element->getSpeechInputState() == WebInputElement::Idle) { | |
136 input_element->startSpeechInput(); | |
137 } else { | |
138 input_element->stopSpeechInput(); | |
139 } | |
140 } | |
OLD | NEW |