summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/input_tag_speech_dispatcher.cc
blob: b9490ce76392be69360ffd772341f0f670743c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/input_tag_speech_dispatcher.h"

#include "base/strings/utf_string_conversions.h"
#include "content/common/speech_recognition_messages.h"
#include "content/renderer/render_view_impl.h"
#include "third_party/WebKit/public/platform/WebSize.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebInputElement.h"
#include "third_party/WebKit/public/web/WebNode.h"
#include "third_party/WebKit/public/web/WebSecurityOrigin.h"
#include "third_party/WebKit/public/web/WebSpeechInputListener.h"
#include "third_party/WebKit/public/web/WebView.h"

using blink::WebDocument;
using blink::WebElement;
using blink::WebFrame;
using blink::WebInputElement;
using blink::WebNode;
using blink::WebView;

namespace content {

InputTagSpeechDispatcher::InputTagSpeechDispatcher(
    RenderViewImpl* render_view,
    blink::WebSpeechInputListener* listener)
    : RenderViewObserver(render_view),
      listener_(listener) {
}

bool InputTagSpeechDispatcher::OnMessageReceived(
    const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(InputTagSpeechDispatcher, message)
    IPC_MESSAGE_HANDLER(InputTagSpeechMsg_SetRecognitionResults,
                        OnSpeechRecognitionResults)
    IPC_MESSAGE_HANDLER(InputTagSpeechMsg_RecordingComplete,
                        OnSpeechRecordingComplete)
    IPC_MESSAGE_HANDLER(InputTagSpeechMsg_RecognitionComplete,
                        OnSpeechRecognitionComplete)
    IPC_MESSAGE_HANDLER(InputTagSpeechMsg_ToggleSpeechInput,
                        OnSpeechRecognitionToggleSpeechInput)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()
  return handled;
}

bool InputTagSpeechDispatcher::startRecognition(
    int request_id,
    const blink::WebRect& element_rect,
    const blink::WebString& language,
    const blink::WebString& grammar,
    const blink::WebSecurityOrigin& origin) {
  DVLOG(1) << "InputTagSpeechDispatcher::startRecognition enter";

  InputTagSpeechHostMsg_StartRecognition_Params params;
  params.grammar = UTF16ToUTF8(grammar);
  params.language = UTF16ToUTF8(language);
  params.origin_url = UTF16ToUTF8(origin.toString());
  params.render_view_id = routing_id();
  params.request_id = request_id;
  params.element_rect = element_rect;

  Send(new InputTagSpeechHostMsg_StartRecognition(params));
  DVLOG(1) << "InputTagSpeechDispatcher::startRecognition exit";
  return true;
}

void InputTagSpeechDispatcher::cancelRecognition(int request_id) {
  DVLOG(1) << "InputTagSpeechDispatcher::cancelRecognition enter";
  Send(new InputTagSpeechHostMsg_CancelRecognition(routing_id(), request_id));
  DVLOG(1) << "InputTagSpeechDispatcher::cancelRecognition exit";
}

void InputTagSpeechDispatcher::stopRecording(int request_id) {
  DVLOG(1) << "InputTagSpeechDispatcher::stopRecording enter";
  Send(new InputTagSpeechHostMsg_StopRecording(routing_id(),
                                               request_id));
  DVLOG(1) << "InputTagSpeechDispatcher::stopRecording exit";
}

void InputTagSpeechDispatcher::OnSpeechRecognitionResults(
    int request_id,
    const SpeechRecognitionResults& results) {
  DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionResults enter";
  DCHECK_EQ(results.size(), 1U);

  const SpeechRecognitionResult& result = results[0];
  blink::WebSpeechInputResultArray webkit_result(result.hypotheses.size());
  for (size_t i = 0; i < result.hypotheses.size(); ++i) {
    webkit_result[i].assign(result.hypotheses[i].utterance,
        result.hypotheses[i].confidence);
  }
  listener_->setRecognitionResult(request_id, webkit_result);

  DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionResults exit";
}

void InputTagSpeechDispatcher::OnSpeechRecordingComplete(int request_id) {
  DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecordingComplete enter";
  listener_->didCompleteRecording(request_id);
  DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecordingComplete exit";
}

void InputTagSpeechDispatcher::OnSpeechRecognitionComplete(int request_id) {
  DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionComplete enter";
  listener_->didCompleteRecognition(request_id);
  DVLOG(1) << "InputTagSpeechDispatcher::OnSpeechRecognitionComplete exit";
}

void InputTagSpeechDispatcher::OnSpeechRecognitionToggleSpeechInput() {
  DVLOG(1) <<"InputTagSpeechDispatcher::OnSpeechRecognitionToggleSpeechInput";

  WebView* web_view = render_view()->GetWebView();

  WebFrame* frame = web_view->mainFrame();
  if (!frame)
    return;

  WebDocument document = frame->document();
  if (document.isNull())
    return;

  WebNode focused_node = document.focusedNode();
  if (focused_node.isNull() || !focused_node.isElementNode())
    return;

  blink::WebElement element = focused_node.to<blink::WebElement>();
  blink::WebInputElement* input_element = blink::toWebInputElement(&element);
  if (!input_element)
    return;
  if (!input_element->isSpeechInputEnabled())
    return;

  if (input_element->getSpeechInputState() == WebInputElement::Idle) {
    input_element->startSpeechInput();
  } else {
    input_element->stopSpeechInput();
  }
}

}  // namespace content