- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / platform_apps / web_view / speech_recognition_api / guest.js
1 // Copyright 2013 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 var LOG = function(msg) {
6   window.console.log(msg);
7 };
8
9 var embederWindowChannel = null;
10
11 var notifyEmbedder = function(msg_array) {
12   embederWindowChannel.postMessage(JSON.stringify(msg_array), '*');
13 };
14
15 var runSpeechRecognitionAPI = function() {
16   LOG('runSpeechRecognitionAPI');
17   var r = new webkitSpeechRecognition();
18   var succeeded = false;
19   r.onstart = function() {
20     succeeded = true;
21     LOG('r.onstart');
22     notifyEmbedder(['recognition', 'onstart', '']);
23     r.abort();
24   };
25   r.onerror = function() {
26     LOG('r.onerror');
27     if (succeeded) {
28       return;
29     }
30     notifyEmbedder(['recognition', 'onerror', '']);
31   };
32   r.onresult = function(e) {
33     if (!e.results || !e.results.length) {
34       notifyEmbedder(['recognition', 'unknown', '']);
35       return;
36     }
37     var transcript = e.results[0][0].transcript;
38     notifyEmbedder(['recognition', 'onresult', transcript]);
39   };
40   r.start();
41 };
42
43 var onPostMessageReceived = function(e) {
44   embederWindowChannel = e.source;
45   var data = JSON.parse(e.data);
46   if (data[0] == 'create-channel') {
47     runSpeechRecognitionAPI();
48   }
49 };
50
51 window.addEventListener('message', onPostMessageReceived);