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