Disable TabDragController tests that fail with a real compositor.
[chromium-blink-merge.git] / chrome / browser / resources / app_list / plugin_manager.js
blobfb8a6044f8190488a46fa2c7b61f31ec2f6d6748
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.
5 /**
6 * @fileoverview The manager of offline hotword speech recognizer plugin.
7 */
9 cr.define('speech', function() {
10 'use strict';
12 /**
13 * The type of the plugin state.
14 ** @enum {number}
16 var PluginState = {
17 UNINITIALIZED: 0,
18 LOADED: 1,
19 SAMPLING_RATE_READY: 2,
20 READY: 3,
21 RECOGNIZING: 4
24 /**
25 * The command names of the plugin.
26 * @enum {string}
28 var pluginCommands = {
29 SET_SAMPLING_RATE: 'h',
30 SET_CONFIG: 'm',
31 START_RECOGNIZING: 'r',
32 STOP_RECOGNIZING: 's'
35 /**
36 * The regexp pattern of the hotword recognition result.
38 var recognitionPattern = /^HotwordFiredEvent: \[(.*)\] confidence: (.*)/;
40 /**
41 * Checks the availability of the plugin.
42 * @return {boolean} True only if the plugin is available.
44 function isPluginAvailable() {
45 return !!($('recognizer') && $('recognizer').postMessage);
48 /**
49 * @constructor
51 function PluginManager(onReady, onRecognized) {
52 this.state = PluginState.UNINITIALIZED;
53 this.onReady_ = onReady;
54 this.onRecognized_ = onRecognized;
55 this.samplingRate_ = null;
56 this.config_ = null;
57 if (isPluginAvailable()) {
58 $('recognizer').addEventListener('message', this.onMessage_.bind(this));
59 $('recognizer').addEventListener('load', this.onLoad_.bind(this));
63 /**
64 * The event handler of the plugin status.
66 * @param {Event} messageEvent the event object from the plugin.
67 * @private
69 PluginManager.prototype.onMessage_ = function(messageEvent) {
70 if (this.state == PluginState.LOADED) {
71 if (messageEvent.data == 'stopped')
72 this.state = PluginState.SAMPLING_RATE_READY;
73 return;
76 if (messageEvent.data == 'audio') {
77 if (this.state < PluginState.READY)
78 this.onReady_(this);
79 this.state = PluginState.RECOGNIZING;
80 } else if (messageEvent.data == 'stopped') {
81 this.state = PluginState.READY;
82 } else {
83 var matched = recognitionPattern.exec(messageEvent.data);
84 if (matched && matched[1] == 'hotword_ok_google')
85 this.onRecognized_(Number(matched[2]));
89 /**
90 * The event handler when the plugin is loaded.
92 * @private
94 PluginManager.prototype.onLoad_ = function() {
95 if (this.state == PluginState.UNINITIALIZED) {
96 this.state = PluginState.LOADED;
97 if (this.samplingRate_ && this.config_)
98 this.initialize_(this.samplingRate_, this.config_);
103 * Sends the initialization messages to the plugin. This method is private.
104 * The initialization will happen from onLoad_ or scheduleInitialize.
106 * @param {number} samplingRate the sampling rate the plugin accepts.
107 * @param {string} config the url of the config file.
108 * @private
110 PluginManager.prototype.initialize_ = function(samplingRate, config) {
111 $('recognizer').postMessage(
112 pluginCommands.SET_SAMPLING_RATE + samplingRate);
113 $('recognizer').postMessage(pluginCommands.SET_CONFIG + config);
117 * Initializes the plugin with the specified parameter, or schedules the
118 * initialization if the plugin is not ready.
120 * @param {number} samplingRate the sampling rate the plugin accepts.
121 * @param {string} config the url of the config file.
123 PluginManager.prototype.scheduleInitialize = function(samplingRate, config) {
124 if (this.state == PluginState.UNINITIALIZED) {
125 this.samplingRate_ = samplingRate;
126 this.config_ = config;
127 } else {
128 this.initialize_(samplingRate, config);
133 * Asks the plugin to start recognizing the hotword.
135 PluginManager.prototype.startRecognizer = function() {
136 if (this.state == PluginState.READY)
137 $('recognizer').postMessage(pluginCommands.START_RECOGNIZING);
141 * Asks the plugin to stop recognizing the hotword.
143 PluginManager.prototype.stopRecognizer = function() {
144 if (this.state == PluginState.RECOGNIZING)
145 $('recognizer').postMessage(pluginCommands.STOP_RECOGNIZING);
149 * Sends the actual audio wave data.
151 * @param {cr.event.Event} event The event for the audio data.
153 PluginManager.prototype.sendAudioData = function(event) {
154 if (this.state == PluginState.RECOGNIZING)
155 $('recognizer').postMessage(event.data.buffer);
158 return {
159 PluginManager: PluginManager,
160 PluginState: PluginState,
161 isPluginAvailable: isPluginAvailable