1// Copyright 2014 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/** 6 * @fileoverview Bridge that sends TTS messages from content scripts or 7 * other pages to the main background page. 8 * 9 */ 10 11goog.provide('cvox.ChromeTts'); 12 13goog.require('cvox.ChromeTtsBase'); 14goog.require('cvox.HostFactory'); 15 16/** 17 * @constructor 18 * @extends {cvox.ChromeTtsBase} 19 */ 20cvox.ChromeTts = function() { 21 goog.base(this); 22 23 this.addBridgeListener(); 24}; 25goog.inherits(cvox.ChromeTts, cvox.ChromeTtsBase); 26 27/** 28 * Current call id, used for matching callback functions. 29 * @type {number} 30 */ 31cvox.ChromeTts.callId = 1; 32 33/** 34 * Maps call ids to callback functions. 35 * @type {Object.<number, Function>} 36 */ 37cvox.ChromeTts.functionMap = new Object(); 38 39/** @override */ 40cvox.ChromeTts.prototype.speak = function(textString, queueMode, properties) { 41 if (!properties) { 42 properties = {}; 43 } 44 45 goog.base(this, 'speak', textString, queueMode, properties); 46 47 cvox.ExtensionBridge.send( 48 this.createMessageForProperties_(textString, queueMode, properties)); 49}; 50 51/** @override */ 52cvox.ChromeTts.prototype.isSpeaking = function() { 53 cvox.ChromeTts.superClass_.isSpeaking.call(this); 54 return false; 55}; 56 57/** @override */ 58cvox.ChromeTts.prototype.stop = function() { 59 cvox.ChromeTts.superClass_.stop.call(this); 60 cvox.ExtensionBridge.send( 61 {'target': 'TTS', 62 'action': 'stop'}); 63}; 64 65/** @override */ 66cvox.ChromeTts.prototype.increaseOrDecreaseProperty = 67 function(propertyName, increase) { 68 cvox.ExtensionBridge.send( 69 {'target': 'TTS', 70 'action': 'increaseOrDecrease', 71 'property': propertyName, 72 'increase': increase}); 73}; 74 75/** 76 * Increases a TTS speech property. 77 * @param {string} property_name The name of the property to increase. 78 * @param {boolean} announce Whether to announce that the property is 79 * changing. 80 */ 81cvox.ChromeTts.prototype.increaseProperty = function(property_name, announce) { 82 goog.base(this, 'increaseProperty', property_name, announce); 83 cvox.ExtensionBridge.send( 84 {'target': 'TTS', 85 'action': 'increase' + property_name, 86 'announce': announce}); 87}; 88 89/** 90 * Listens for TTS_COMPLETED message and executes the callback function. 91 */ 92cvox.ChromeTts.prototype.addBridgeListener = function() { 93 cvox.ExtensionBridge.addMessageListener( 94 function(msg, port) { 95 var message = msg['message']; 96 if (message == 'TTS_CALLBACK') { 97 var id = msg['id']; 98 var func = cvox.ChromeTts.functionMap[id]; 99 if (func != undefined) { 100 func(); 101 delete cvox.ChromeTts.functionMap[id]; 102 } 103 } 104 }); 105}; 106 107/** 108 * Creates a message suitable for sending as a speak action to background tts. 109 * @param {string} textString The string of text to be spoken. 110 * @param {number=} queueMode The queue mode: cvox.AbstractTts.QUEUE_MODE_FLUSH, 111 * for flush, cvox.AbstractTts.QUEUE_MODE_QUEUE for adding to queue. 112 * @param {Object=} properties Speech properties to use for this utterance. 113 * @return {Object} A message. 114 * @private 115 */ 116cvox.ChromeTts.prototype.createMessageForProperties_ = 117 function(textString, queueMode, properties) { 118 var message = {'target': 'TTS', 119 'action': 'speak', 120 'text': textString, 121 'queueMode': queueMode, 122 'properties': properties}; 123 124 if (properties['startCallback'] != undefined) { 125 cvox.ChromeTts.functionMap[cvox.ChromeTts.callId] = 126 properties['startCallback']; 127 message['startCallbackId'] = cvox.ChromeTts.callId++; 128 } 129 if (properties['endCallback'] != undefined) { 130 cvox.ChromeTts.functionMap[cvox.ChromeTts.callId] = 131 properties['endCallback']; 132 message['endCallbackId'] = cvox.ChromeTts.callId++; 133 } 134 return message; 135 }; 136 137cvox.HostFactory.ttsConstructor = cvox.ChromeTts; 138