1 // Copyright (c) 2012 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 // Custom binding for the tts API.
7 var binding
= require('binding').Binding
.create('tts');
9 var idGenerator
= requireNative('id_generator');
10 var sendRequest
= require('sendRequest').sendRequest
;
11 var lazyBG
= requireNative('lazy_background_page');
13 binding
.registerCustomHook(function(api
) {
14 var apiFunctions
= api
.apiFunctions
;
15 var tts
= api
.compiledApi
;
18 function ttsEventListener(event
) {
19 var eventHandler
= handlers
[event
.srcId
];
23 charIndex
: event
.charIndex
,
24 errorMessage
: event
.errorMessage
26 if (event
.isFinalEvent
) {
27 delete handlers
[event
.srcId
];
28 // Balanced in 'speak' handler.
29 lazyBG
.DecrementKeepaliveCount();
34 // This file will get run if an extension needs the ttsEngine permission, but
35 // it doesn't necessarily have the tts permission. If it doesn't, trying to
36 // add a listener to chrome.tts.onEvent will fail.
37 // See http://crbug.com/122474.
39 tts
.onEvent
.addListener(ttsEventListener
);
42 apiFunctions
.setHandleRequest('speak', function() {
44 if (args
.length
> 1 && args
[1] && args
[1].onEvent
) {
45 var id
= idGenerator
.GetNextId();
47 handlers
[id
] = args
[1].onEvent
;
48 // Keep the page alive until the event finishes.
49 // Balanced in eventHandler.
50 lazyBG
.IncrementKeepaliveCount();
52 sendRequest(this.name
, args
, this.definition
.parameters
);
57 exports
.binding
= binding
.generate();