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
|
// Copyright 2014 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.
/**
* @fileoverview Bridge that sends TTS messages from content scripts or
* other pages to the main background page.
*
*/
goog.provide('cvox.ChromeTts');
goog.require('cvox.ChromeTtsBase');
goog.require('cvox.HostFactory');
/**
* @constructor
* @extends {cvox.ChromeTtsBase}
*/
cvox.ChromeTts = function() {
goog.base(this);
this.addBridgeListener();
};
goog.inherits(cvox.ChromeTts, cvox.ChromeTtsBase);
/**
* Current call id, used for matching callback functions.
* @type {number}
*/
cvox.ChromeTts.callId = 1;
/**
* Maps call ids to callback functions.
* @type {Object.<number, Function>}
*/
cvox.ChromeTts.functionMap = new Object();
/** @override */
cvox.ChromeTts.prototype.speak = function(textString, queueMode, properties) {
if (!properties) {
properties = {};
}
goog.base(this, 'speak', textString, queueMode, properties);
cvox.ExtensionBridge.send(
this.createMessageForProperties_(textString, queueMode, properties));
};
/** @override */
cvox.ChromeTts.prototype.isSpeaking = function() {
cvox.ChromeTts.superClass_.isSpeaking.call(this);
return false;
};
/** @override */
cvox.ChromeTts.prototype.stop = function() {
cvox.ChromeTts.superClass_.stop.call(this);
cvox.ExtensionBridge.send(
{'target': 'TTS',
'action': 'stop'});
};
/** @override */
cvox.ChromeTts.prototype.increaseOrDecreaseProperty =
function(propertyName, increase) {
cvox.ExtensionBridge.send(
{'target': 'TTS',
'action': 'increaseOrDecrease',
'property': propertyName,
'increase': increase});
};
/**
* Increases a TTS speech property.
* @param {string} property_name The name of the property to increase.
* @param {boolean} announce Whether to announce that the property is
* changing.
*/
cvox.ChromeTts.prototype.increaseProperty = function(property_name, announce) {
goog.base(this, 'increaseProperty', property_name, announce);
cvox.ExtensionBridge.send(
{'target': 'TTS',
'action': 'increase' + property_name,
'announce': announce});
};
/**
* Listens for TTS_COMPLETED message and executes the callback function.
*/
cvox.ChromeTts.prototype.addBridgeListener = function() {
cvox.ExtensionBridge.addMessageListener(
function(msg, port) {
var message = msg['message'];
if (message == 'TTS_CALLBACK') {
var id = msg['id'];
var func = cvox.ChromeTts.functionMap[id];
if (func != undefined) {
func();
delete cvox.ChromeTts.functionMap[id];
}
}
});
};
/**
* Creates a message suitable for sending as a speak action to background tts.
* @param {string} textString The string of text to be spoken.
* @param {number=} queueMode The queue mode: cvox.AbstractTts.QUEUE_MODE_FLUSH,
* for flush, cvox.AbstractTts.QUEUE_MODE_QUEUE for adding to queue.
* @param {Object=} properties Speech properties to use for this utterance.
* @return {Object} A message.
* @private
*/
cvox.ChromeTts.prototype.createMessageForProperties_ =
function(textString, queueMode, properties) {
var message = {'target': 'TTS',
'action': 'speak',
'text': textString,
'queueMode': queueMode,
'properties': properties};
if (properties['startCallback'] != undefined) {
cvox.ChromeTts.functionMap[cvox.ChromeTts.callId] =
properties['startCallback'];
message['startCallbackId'] = cvox.ChromeTts.callId++;
}
if (properties['endCallback'] != undefined) {
cvox.ChromeTts.functionMap[cvox.ChromeTts.callId] =
properties['endCallback'];
message['endCallbackId'] = cvox.ChromeTts.callId++;
}
return message;
};
cvox.HostFactory.ttsConstructor = cvox.ChromeTts;
|